Search in sources :

Example 1 with FieldLiteral

use of com.google.gwt.inject.rebind.reflect.FieldLiteral in project google-gin by gwtplus.

the class GinjectorBindingsOutputterTest method testOutputStaticInjections.

// Verify that outputting static injections creates and dispatches to the
// correct fragment classes.
public void testOutputStaticInjections() throws Exception {
    PrintWriter printWriter = new PrintWriter(new ByteArrayOutputStream());
    GeneratorContext ctx = createMock(GeneratorContext.class, "ctx");
    expect(ctx.tryCreate((TreeLogger) anyObject(), (String) anyObject(), (String) anyObject())).andStubReturn(printWriter);
    Capture<FieldLiteral<SuperClass>> fieldCapture = new Capture<FieldLiteral<SuperClass>>();
    Capture<MethodLiteral<SuperClass, Method>> methodCapture = new Capture<MethodLiteral<SuperClass, Method>>();
    NameGenerator nameGenerator = createMock(NameGenerator.class, "nameGenerator");
    expect(nameGenerator.convertToValidMemberName("injectStatic_com.google.gwt.inject.rebind.output." + "GinjectorBindingsOutputterTest$SubClass")).andStubReturn("test_injectSubClass");
    expect(nameGenerator.convertToValidMemberName("injectStatic_com.google.gwt.inject.rebind.output.subpackage." + "SubPackageClass")).andStubReturn("test_injectSubPackageClass");
    SourceWriteUtil sourceWriteUtil = createMock(SourceWriteUtil.class, "sourceWriteUtil");
    expect(sourceWriteUtil.createFieldInjection(capture(fieldCapture), (String) anyObject(), (NameGenerator) anyObject(), (List<InjectorMethod>) anyObject())).andReturn(SourceSnippets.forText(""));
    MethodCallUtil methodCallUtil = createMock(MethodCallUtil.class, "methodCallUtil");
    expect(methodCallUtil.createMethodCallWithInjection(capture(methodCapture), (String) anyObject(), (NameGenerator) anyObject(), (List<InjectorMethod>) anyObject())).andReturn(SourceSnippets.forText(""));
    GinjectorBindings bindings = createMock(GinjectorBindings.class, "bindings");
    expect(bindings.getNameGenerator()).andStubReturn(nameGenerator);
    expect(bindings.getStaticInjectionRequests()).andStubReturn(Arrays.<Class<?>>asList(SubClass.class, SubPackageClass.class));
    String ginjectorPackageName = "com.google.gwt.inject.rebind.output";
    String ginjectorClassName = "GinjectorFragmentOutputterTest$FakeGinjector";
    GinjectorFragmentOutputter.Factory fragmentOutputterFactory = createMock(GinjectorFragmentOutputter.Factory.class, "fragmentOutputterFactory");
    GinjectorFragmentOutputter fragmentOutputter = createMock(GinjectorFragmentOutputter.class, "fragmentOutputter");
    GinjectorFragmentOutputter fragmentOutputterSubpackage = createMock(GinjectorFragmentOutputter.class, "fragmentOutputterSubpackage");
    expect(fragmentOutputterFactory.create(bindings, new FragmentPackageName(null, "com.google.gwt.inject.rebind.output"), ginjectorPackageName, ginjectorClassName)).andStubReturn(fragmentOutputter);
    expect(fragmentOutputterFactory.create(bindings, new FragmentPackageName(null, "com.google.gwt.inject.rebind.output.subpackage"), ginjectorPackageName, ginjectorClassName)).andStubReturn(fragmentOutputterSubpackage);
    fragmentOutputter.outputMethod((InjectorMethod) anyObject());
    fragmentOutputterSubpackage.outputMethod((InjectorMethod) anyObject());
    fragmentOutputter.invokeInInitializeStaticInjections("test_injectSubClass");
    fragmentOutputterSubpackage.invokeInInitializeStaticInjections("test_injectSubPackageClass");
    replay();
    GinjectorBindingsOutputter outputter = new GinjectorBindingsOutputter(ctx, null, fragmentOutputterFactory, new TestFragmentPackageNameFactory(), null, TreeLogger.NULL, methodCallUtil, null, null);
    GinjectorBindingsOutputter.FragmentMap fragments = new GinjectorBindingsOutputter.FragmentMap(bindings, ginjectorPackageName, ginjectorClassName, fragmentOutputterFactory);
    outputter.outputStaticInjections(bindings, fragments, sourceWriteUtil);
    verify();
    TypeLiteral<SuperClass> superClass = TypeLiteral.get(SuperClass.class);
    assertEquals(superClass, methodCapture.getValue().getDeclaringType());
    assertEquals(superClass, fieldCapture.getValue().getDeclaringType());
}
Also used : InjectorMethod(com.google.gwt.inject.rebind.util.InjectorMethod) NameGenerator(com.google.gwt.inject.rebind.util.NameGenerator) GeneratorContext(com.google.gwt.core.ext.GeneratorContext) Capture(org.easymock.Capture) SourceWriteUtil(com.google.gwt.inject.rebind.util.SourceWriteUtil) MethodCallUtil(com.google.gwt.inject.rebind.util.MethodCallUtil) SubPackageClass(com.google.gwt.inject.rebind.output.subpackage.SubPackageClass) MethodLiteral(com.google.gwt.inject.rebind.reflect.MethodLiteral) PrintWriter(java.io.PrintWriter) GinjectorBindings(com.google.gwt.inject.rebind.GinjectorBindings) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Method(java.lang.reflect.Method) InjectorMethod(com.google.gwt.inject.rebind.util.InjectorMethod) FieldLiteral(com.google.gwt.inject.rebind.reflect.FieldLiteral)

Example 2 with FieldLiteral

use of com.google.gwt.inject.rebind.reflect.FieldLiteral in project google-gin by gwtplus.

the class MemberCollector method getTypeFields.

private <T> Iterable<FieldLiteral<T>> getTypeFields(TypeLiteral<T> typeLiteral) {
    List<FieldLiteral<T>> fields = new ArrayList<FieldLiteral<T>>();
    for (Field field : typeLiteral.getRawType().getDeclaredFields()) {
        fields.add(FieldLiteral.get(field, typeLiteral));
    }
    Collections.sort(fields, FIELD_COMPARATOR);
    return fields;
}
Also used : Field(java.lang.reflect.Field) FieldLiteral(com.google.gwt.inject.rebind.reflect.FieldLiteral) ArrayList(java.util.ArrayList)

Example 3 with FieldLiteral

use of com.google.gwt.inject.rebind.reflect.FieldLiteral in project google-gin by gwtplus.

the class GinjectorBindingsOutputter method outputStaticInjectionMethods.

/**
 * Outputs all the static injection methods for the given class.
 */
void outputStaticInjectionMethods(Class<?> type, FragmentMap fragments, NameGenerator nameGenerator, SourceWriteUtil sourceWriteUtil) {
    String methodName = nameGenerator.convertToValidMemberName("injectStatic_" + type.getName());
    SourceSnippetBuilder body = new SourceSnippetBuilder();
    for (InjectionPoint injectionPoint : InjectionPoint.forStaticMethodsAndFields(type)) {
        Member member = injectionPoint.getMember();
        try {
            List<InjectorMethod> staticInjectionHelpers = new ArrayList<InjectorMethod>();
            if (member instanceof Method) {
                MethodLiteral<?, Method> method = MethodLiteral.get((Method) member, TypeLiteral.get(member.getDeclaringClass()));
                body.append(methodCallUtil.createMethodCallWithInjection(method, null, nameGenerator, staticInjectionHelpers));
            } else if (member instanceof Field) {
                FieldLiteral<?> field = FieldLiteral.get((Field) member, TypeLiteral.get(member.getDeclaringClass()));
                body.append(sourceWriteUtil.createFieldInjection(field, null, nameGenerator, staticInjectionHelpers));
            }
            outputMethods(staticInjectionHelpers, fragments);
        } catch (NoSourceNameException e) {
            errorManager.logError(e.getMessage(), e);
        }
    }
    // Note that the top-level method that performs static injection will only
    // invoke a bunch of other injector methods.  Therefore, it doesn't matter
    // which package it goes in, and we don't need to invoke getUserPackageName
    // (which is good, because in practice users statically inject types that
    // have no user package name because they're private inner classes!)
    String packageName = type.getPackage().getName();
    InjectorMethod method = SourceSnippets.asMethod(false, "private void " + methodName + "()", packageName, body.build());
    GinjectorFragmentOutputter fragment = fragments.get(fragmentPackageNameFactory.create(packageName));
    fragment.outputMethod(method);
    fragment.invokeInInitializeStaticInjections(methodName);
}
Also used : InjectionPoint(com.google.inject.spi.InjectionPoint) InjectorMethod(com.google.gwt.inject.rebind.util.InjectorMethod) NoSourceNameException(com.google.gwt.inject.rebind.reflect.NoSourceNameException) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) InjectorMethod(com.google.gwt.inject.rebind.util.InjectorMethod) Field(java.lang.reflect.Field) FieldLiteral(com.google.gwt.inject.rebind.reflect.FieldLiteral) SourceSnippetBuilder(com.google.gwt.inject.rebind.util.SourceSnippetBuilder) Member(java.lang.reflect.Member)

Example 4 with FieldLiteral

use of com.google.gwt.inject.rebind.reflect.FieldLiteral in project google-gin by gwtplus.

the class ReachabilityAnalyzer method traceStaticInjectionsFor.

private void traceStaticInjectionsFor(Class<?> klass, GinjectorBindings bindings) {
    TypeLiteral<?> type = TypeLiteral.get(klass);
    for (InjectionPoint injectionPoint : InjectionPoint.forStaticMethodsAndFields(klass)) {
        Member member = injectionPoint.getMember();
        if (member instanceof Method) {
            Method methodRaw = (Method) member;
            TypeLiteral<?> declaringClass = TypeLiteral.get(methodRaw.getDeclaringClass());
            MethodLiteral<?, ?> method = MethodLiteral.get(methodRaw, declaringClass);
            for (Key<?> key : method.getParameterKeys()) {
                PrettyPrinter.log(logger, TreeLogger.DEBUG, "ROOT -> %s:%s [static injection: %s]", bindings, key, method);
                traceKey(key, bindings);
            }
        } else if (member instanceof Field) {
            Field fieldRaw = (Field) member;
            TypeLiteral<?> declaringClass = TypeLiteral.get(fieldRaw.getDeclaringClass());
            FieldLiteral<?> field = FieldLiteral.get(fieldRaw, declaringClass);
            Key<?> key = guiceUtil.getKey(field);
            PrettyPrinter.log(logger, TreeLogger.DEBUG, "ROOT -> %s:%s [static injection: %s]", bindings, key, field);
            traceKey(key, bindings);
        }
    }
}
Also used : Field(java.lang.reflect.Field) FieldLiteral(com.google.gwt.inject.rebind.reflect.FieldLiteral) TypeLiteral(com.google.inject.TypeLiteral) InjectionPoint(com.google.inject.spi.InjectionPoint) Method(java.lang.reflect.Method) Member(java.lang.reflect.Member) Key(com.google.inject.Key)

Example 5 with FieldLiteral

use of com.google.gwt.inject.rebind.reflect.FieldLiteral in project google-gin by gwtplus.

the class GinjectorBindings method addStaticInjectionRequest.

void addStaticInjectionRequest(Class<?> type, Object source) {
    assertNotFinalized();
    staticInjectionRequests.add(type);
    // Calculate required bindings and add to dependencies
    for (InjectionPoint injectionPoint : InjectionPoint.forStaticMethodsAndFields(type)) {
        Member member = injectionPoint.getMember();
        if (member instanceof Method) {
            addDependencies(guiceUtil.getDependencies(Dependency.GINJECTOR, MethodLiteral.get((Method) member, TypeLiteral.get(member.getDeclaringClass()))));
        } else if (member instanceof Field) {
            FieldLiteral<?> field = FieldLiteral.get((Field) member, TypeLiteral.get(member.getDeclaringClass()));
            Key<?> key = guiceUtil.getKey(field);
            addDependency(new Dependency(Dependency.GINJECTOR, key, guiceUtil.isOptional(field), false, source.toString()));
        }
    }
}
Also used : Field(java.lang.reflect.Field) FieldLiteral(com.google.gwt.inject.rebind.reflect.FieldLiteral) InjectionPoint(com.google.inject.spi.InjectionPoint) Method(java.lang.reflect.Method) Dependency(com.google.gwt.inject.rebind.binding.Dependency) Member(java.lang.reflect.Member) Key(com.google.inject.Key)

Aggregations

FieldLiteral (com.google.gwt.inject.rebind.reflect.FieldLiteral)5 Field (java.lang.reflect.Field)4 Method (java.lang.reflect.Method)4 InjectionPoint (com.google.inject.spi.InjectionPoint)3 Member (java.lang.reflect.Member)3 InjectorMethod (com.google.gwt.inject.rebind.util.InjectorMethod)2 Key (com.google.inject.Key)2 ArrayList (java.util.ArrayList)2 GeneratorContext (com.google.gwt.core.ext.GeneratorContext)1 GinjectorBindings (com.google.gwt.inject.rebind.GinjectorBindings)1 Dependency (com.google.gwt.inject.rebind.binding.Dependency)1 SubPackageClass (com.google.gwt.inject.rebind.output.subpackage.SubPackageClass)1 MethodLiteral (com.google.gwt.inject.rebind.reflect.MethodLiteral)1 NoSourceNameException (com.google.gwt.inject.rebind.reflect.NoSourceNameException)1 MethodCallUtil (com.google.gwt.inject.rebind.util.MethodCallUtil)1 NameGenerator (com.google.gwt.inject.rebind.util.NameGenerator)1 SourceSnippetBuilder (com.google.gwt.inject.rebind.util.SourceSnippetBuilder)1 SourceWriteUtil (com.google.gwt.inject.rebind.util.SourceWriteUtil)1 TypeLiteral (com.google.inject.TypeLiteral)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1