public SourceSnippet createFieldInjection ( final FieldLiteral < ? > field , final String injecteeName , NameGenerator nameGenerator , List < InjectorMethod > methodsOutput ) throws NoSourceNameException { final boolean hasInjectee = injecteeName != null ; final boolean useNativeMethod = field . isPrivate ( ) || ReflectUtil . isPrivate ( field . getDeclaringType ( ) ) || field . isLegacyFinalField ( ) ; // Determine method signature parts. final String injecteeTypeName = ReflectUtil . getSourceName ( field . getRawDeclaringType ( ) ) ; String fieldTypeName = ReflectUtil . getSourceName ( field . getFieldType ( ) ) ; String methodBaseName = nameGenerator . convertToValidMemberName ( injecteeTypeName + "_" + field . getName ( ) + "_fieldInjection" ) ; final String methodName = nameGenerator . createMethodName ( methodBaseName ) ; // Field injections are performed in the package of the class declaring the // field. Any private types referenced by the injection must be visible // from there. final String packageName = ReflectUtil . getUserPackageName ( field . getDeclaringType ( ) ) ; String signatureParams = fieldTypeName + " value" ; boolean isLongAcccess = field . getFieldType ( ) . getRawType ( ) . equals ( Long . TYPE ) ; if ( hasInjectee ) { signatureParams = injecteeTypeName + " injectee, " + signatureParams ; } // Compose method implementation and invocation. String annotation ; if ( isLongAcccess ) { annotation = "@com.google.gwt.core.client.UnsafeNativeLong " ; } else { annotation = "" ; } String header = useNativeMethod ? "public native " : "public " ; String signature = annotation + header + "void " + methodName + "(" + signatureParams + ")" ; InjectorMethod injectionMethod = new AbstractInjectorMethod ( useNativeMethod , signature , packageName ) { public String getMethodBody ( InjectorWriteContext writeContext ) throws NoSourceNameException { if ( ! useNativeMethod ) { return ( hasInjectee ? "injectee." : injecteeTypeName + "." ) + field . getName ( ) + " = value;" ; } else { return ( hasInjectee ? "injectee." : "" ) + getJsniSignature ( field ) + " = value;" ; } } } ; methodsOutput . add ( injectionMethod ) ; return new SourceSnippet ( ) { public String getSource ( InjectorWriteContext writeContext ) { List < String > callParams = new ArrayList < String > ( ) ; if ( hasInjectee ) { callParams . add ( injecteeName ) ; } callParams . add ( writeContext . callGetter ( guiceUtil . getKey ( field ) ) ) ; return writeContext . callMethod ( methodName , packageName , callParams ) + ";\n" ; } } ; }
Creates a field injecting method and returns a string that invokes the written method .