Search in sources :

Example 11 with Inject

use of javax.inject.Inject in project gradle-retrolambda by evant.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DaggerMyComponent.builder().myModule(new MyModule()).build().inject(this);
    setContentView(R.layout.activity_main);
    TextView text = (TextView) findViewById(R.id.text);
    text.setText(hello.run(getResources()));
    TextView textLib = (TextView) findViewById(R.id.text_lib);
    textLib.setText(libHello.run());
    ResFunction lambda = (res) -> "Foo2";
    Toast.makeText(this, lambda.run(null), Toast.LENGTH_SHORT).show();
}
Also used : Inject(javax.inject.Inject) Bundle(android.os.Bundle) TextView(android.widget.TextView) Toast(android.widget.Toast) AppCompatActivity(android.support.v7.app.AppCompatActivity) TextView(android.widget.TextView)

Example 12 with Inject

use of javax.inject.Inject in project jersey by jersey.

the class JerseyClassAnalyzer method getConstructor.

@SuppressWarnings("unchecked")
@Override
public <T> Constructor<T> getConstructor(final Class<T> clazz) throws MultiException, NoSuchMethodException {
    if (clazz.isLocalClass()) {
        throw new NoSuchMethodException(LocalizationMessages.INJECTION_ERROR_LOCAL_CLASS_NOT_SUPPORTED(clazz.getName()));
    }
    if (clazz.isMemberClass() && !Modifier.isStatic(clazz.getModifiers())) {
        throw new NoSuchMethodException(LocalizationMessages.INJECTION_ERROR_NONSTATIC_MEMBER_CLASS_NOT_SUPPORTED(clazz.getName()));
    }
    final Constructor<T> retVal;
    try {
        retVal = defaultAnalyzer.getConstructor(clazz);
        final Class<?>[] args = retVal.getParameterTypes();
        if (args.length != 0) {
            return retVal;
        }
        // Is zero length, but is it specifically marked?
        final Inject i = retVal.getAnnotation(Inject.class);
        if (i != null) {
            return retVal;
        }
    // In this case, the default chose a zero-arg constructor since it could find no other
    } catch (final NoSuchMethodException ignored) {
    // In this case, the default failed because it found no constructor it could use
    } catch (final MultiException me) {
        if (me.getErrors().size() != 1 && !(me.getErrors().get(0) instanceof IllegalArgumentException)) {
            throw me;
        }
    // Otherwise, the default failed because it found more than one constructor
    }
    // At this point, we simply need to find the constructor with the largest number of parameters
    final Constructor<?>[] constructors = AccessController.doPrivileged(new PrivilegedAction<Constructor<?>[]>() {

        @Override
        public Constructor<?>[] run() {
            return clazz.getDeclaredConstructors();
        }
    });
    Constructor<?> selected = null;
    int selectedSize = 0;
    int maxParams = -1;
    for (final Constructor<?> constructor : constructors) {
        final Class<?>[] params = constructor.getParameterTypes();
        if (params.length >= maxParams && isCompatible(constructor)) {
            if (params.length > maxParams) {
                maxParams = params.length;
                selectedSize = 0;
            }
            selected = constructor;
            selectedSize++;
        }
    }
    if (selectedSize == 0) {
        throw new NoSuchMethodException(LocalizationMessages.INJECTION_ERROR_SUITABLE_CONSTRUCTOR_NOT_FOUND(clazz.getName()));
    }
    if (selectedSize > 1) {
        // Found {0} constructors with {1} parameters in {2} class. Selecting the first found constructor: {3}
        Errors.warning(clazz, LocalizationMessages.MULTIPLE_MATCHING_CONSTRUCTORS_FOUND(selectedSize, maxParams, clazz.getName(), selected.toGenericString()));
    }
    return (Constructor<T>) selected;
}
Also used : Inject(javax.inject.Inject) Constructor(java.lang.reflect.Constructor) MultiException(org.glassfish.hk2.api.MultiException)

Example 13 with Inject

use of javax.inject.Inject in project rest.li by linkedin.

the class Jsr330Adapter method scanInjectableConstructors.

private void scanInjectableConstructors(Class<?> beanClazz) {
    int annotatedConstructors = 0;
    for (Constructor<?> constructor : beanClazz.getConstructors()) {
        Inject injectAnnotation = constructor.getAnnotation(Inject.class);
        if (injectAnnotation != null) {
            ++annotatedConstructors;
            if (annotatedConstructors > 1) {
                throw new RestLiInternalException("Found multiple constructors annotated with @Inject in " + "class '" + beanClazz.getCanonicalName() + "'.  At most one constructor can be annotated " + "with @Inject.");
            }
            Class<?>[] parameters = constructor.getParameterTypes();
            Annotation[][] parameterAnnotations = constructor.getParameterAnnotations();
            List<DependencyDecl> parameterDecls = new ArrayList<DependencyDecl>(parameters.length);
            for (int i = 0; i < parameters.length; ++i) {
                Class<?> parameter = parameters[i];
                AnnotationSet annotations = new AnnotationSet(parameterAnnotations[i]);
                Named namedAnno = annotations.get(Named.class);
                parameterDecls.add(new DependencyDecl(parameter, namedAnno != null ? namedAnno.value() : null));
            }
            constructor.setAccessible(true);
            _constructorParameterDependencies.put(beanClazz, new InjectableConstructor(constructor, parameterDecls));
        }
    }
    if (annotatedConstructors == 0) {
        try {
            Constructor<?> defaultConstructor = beanClazz.getConstructor();
            defaultConstructor.setAccessible(true);
            _constructorParameterDependencies.put(beanClazz, new InjectableConstructor(defaultConstructor, Collections.<DependencyDecl>emptyList()));
        } catch (NoSuchMethodException e) {
            throw new RestLiInternalException(String.format("No injectable constructor defined for class %s.  Classes must define" + " either a default constructor or a constructor annotated " + "with @Inject.", beanClazz.getName()), e);
        }
    }
}
Also used : Inject(javax.inject.Inject) Named(javax.inject.Named) ArrayList(java.util.ArrayList) AnnotationSet(com.linkedin.restli.internal.server.model.AnnotationSet) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException)

Example 14 with Inject

use of javax.inject.Inject in project dagger by square.

the class GraphAnalysisStaticInjection method attach.

@Override
public void attach(Linker linker) {
    for (Element enclosedElement : enclosingClass.getEnclosedElements()) {
        if (enclosedElement.getKind().isField() && isStatic(enclosedElement)) {
            Inject injectAnnotation = enclosedElement.getAnnotation(Inject.class);
            if (injectAnnotation != null) {
                String key = GeneratorKeys.get(enclosedElement.asType());
                linker.requestBinding(key, enclosingClass.toString(), getClass().getClassLoader());
            }
        }
    }
}
Also used : Inject(javax.inject.Inject) Element(javax.lang.model.element.Element)

Example 15 with Inject

use of javax.inject.Inject in project toydi by tokuhirom.

the class ToyDI method instantiateMembers.

public void instantiateMembers(Object object) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
    for (final Field field : object.getClass().getDeclaredFields()) {
        Inject inject = field.getAnnotation(Inject.class);
        if (inject != null) {
            field.setAccessible(true);
            instantiateMember(object, field);
        }
    }
}
Also used : Inject(javax.inject.Inject) Field(java.lang.reflect.Field)

Aggregations

Inject (javax.inject.Inject)23 Field (java.lang.reflect.Field)8 Provides (com.google.inject.Provides)6 ArrayList (java.util.ArrayList)5 Named (javax.inject.Named)5 Singleton (javax.inject.Singleton)3 Element (javax.lang.model.element.Element)2 Crypto (org.apache.ws.security.components.crypto.Crypto)2 Bundle (android.os.Bundle)1 AppCompatActivity (android.support.v7.app.AppCompatActivity)1 TextView (android.widget.TextView)1 Toast (android.widget.Toast)1 RestLiInternalException (com.linkedin.restli.internal.server.RestLiInternalException)1 AnnotationSet (com.linkedin.restli.internal.server.model.AnnotationSet)1 Annotation (java.lang.annotation.Annotation)1 Constructor (java.lang.reflect.Constructor)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1