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();
}
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;
}
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);
}
}
}
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());
}
}
}
}
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);
}
}
}
Aggregations