Java Code Examples for java.lang.reflect.Constructor#getAnnotation()

The following examples show how to use java.lang.reflect.Constructor#getAnnotation() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
private static <T> @Nullable Constructor<T> findSingleInjectConstructor(Class<T> cls) {
  // Not modifying it, safe to use generics; see Class#getConstructors() for more info.
  @SuppressWarnings("unchecked")
  Constructor<T>[] constructors = (Constructor<T>[]) cls.getDeclaredConstructors();
  Constructor<T> target = null;
  for (Constructor<T> constructor : constructors) {
    if (constructor.getAnnotation(Inject.class) != null) {
      if (target != null) {
        throw new IllegalStateException(
            cls.getCanonicalName() + " defines multiple @Inject-annotations constructors");
      }
      target = constructor;
    }
  }
  return target;
}
 
Example 2
private static Constructor<?> findConstructorForInjection(Class<?> type) {
  Constructor<?>[] constructors = type.getDeclaredConstructors();
  Constructor<?> defaultCont = null;
  for (Constructor<?> constructor : constructors) {
    Annotation annotation = constructor.getAnnotation(Inject.class);
    if (annotation != null) {
      return constructor;
    }
    if (constructor.getParameterCount() == 0) {
      defaultCont = constructor;
    }
  }
  if (defaultCont != null) {
    return defaultCont;
  }
  throw new RuntimeException("not injectable type " + type);
}
 
Example 3
Source Project: PlugFace   File: AnnotationProcessor.java    License: MIT License 6 votes vote down vote up
public Collection<Node<?>> getDependencies(Class<?> pluginClass) {
    final Constructor<?>[] constructors = pluginClass.getConstructors();
    if (constructors.length == 0) {
        throw new IllegalArgumentException(String.format("Class %s doesn't have a public constructor. Class: %s", pluginClass.getSimpleName(), pluginClass.getName()));
    }

    for (Constructor<?> constructor : constructors) {
        final Inject annotation = constructor.getAnnotation(Inject.class);
        if (annotation == null) {
            continue;
        }
        final Collection<Node<?>> dependencies = new ArrayList<>();
        final Class<?>[] parameterTypes = constructor.getParameterTypes();
        for (final Class<?> param : parameterTypes) {
            dependencies.add(new Node<>(param));

        }
        return dependencies;

    }
    return new ArrayList<>();
}
 
Example 4
Source Project: commons-jexl   File: Permissions.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether a constructor explicitly disallows JEXL introspection.
 * @param ctor the constructor to check
 * @return true if JEXL is allowed to introspect, false otherwise
 */
public boolean allow(Constructor<?> ctor) {
    if (ctor == null) {
        return false;
    }
    if (!Modifier.isPublic(ctor.getModifiers())) {
        return false;
    }
    Class<?> clazz = ctor.getDeclaringClass();
    if (!allow(clazz, false)) {
        return false;
    }
    // is ctor annotated with nojexl ?
    NoJexl nojexl = ctor.getAnnotation(NoJexl.class);
    if (nojexl != null) {
        return false;
    }
    return true;
}
 
Example 5
Source Project: DaggerMock   File: OverriddenObjectsMap.java    License: Apache License 2.0 6 votes vote down vote up
public void checkOverriddenInjectAnnotatedClass(List<Object> modules) {
    Set<String> errors = new HashSet<>();
    for (Map.Entry<ObjectId, Provider> entry : fields.entrySet()) {
        ObjectId objectId = entry.getKey();
        Constructor[] constructors = objectId.objectClass.getConstructors();
        for (Constructor constructor : constructors) {
            if (constructor.getAnnotation(Inject.class) != null && !existProvidesMethodInModule(objectId, modules)) {
                errors.add(objectId.objectClass.getName());
            }
        }
    }
    ErrorsFormatter.throwExceptionOnErrors(
            "Error while trying to override objects",
            errors,
            "You must define overridden objects using a @Provides annotated method instead of using @Inject annotation");
}
 
Example 6
Source Project: Snake   File: Snake.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create a fragment proxy object using the specified constructor.
 * It will be use the empty parameter constructor if not specify the primaryconstructor annotation
 * in constructor.
 *
 * @param fragment specified fragment class
 * @param args specified constructor parameters
 * @param <T> the child classes of android.app.fragment.
 *
 * @return fragment proxy object
 */
public static <T extends android.app.Fragment> T newProxy(Class<? extends T> fragment, Object... args) {
    checkAnnotationNotEmpty(fragment);

    try {
        String className = fragment.getName() + "_SnakeProxy";
        if(com.youngfeng.snake.app.Fragment.class.isAssignableFrom(fragment)) {
            className = fragment.getName();
        }
        Class<?> snakeProxyClass = Class.forName(className);

        Constructor<?>[] constructors = snakeProxyClass.getConstructors();

        Constructor<?> primaryConstructor = null;

        for (Constructor<?> constructor : constructors) {
            PrimaryConstructor primaryConstructorAnno = constructor.getAnnotation(PrimaryConstructor.class);
            if(null != primaryConstructorAnno) {
                primaryConstructor = constructor;
                break;
            }
        }

        T result = null;
        if(null != primaryConstructor) {
            result = (T) primaryConstructor.newInstance(args);
        } else {
            result = (T) snakeProxyClass.newInstance();
        }

        return result;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}
 
Example 7
Source Project: gadtry   File: InternalContext.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T> Constructor<T> selectConstructor(Class<T> driver)
{
    Constructor<T>[] constructors;
    if (Creator.class.isAssignableFrom(driver)) {
        constructors = (Constructor<T>[]) driver.getDeclaredConstructors();
    }
    else {
        if (driver.isInterface() || Modifier.isAbstract(driver.getModifiers())) {
            throw new IllegalStateException(driver + " cannot be instantiated, No binding entity class");
        }
        constructors = (Constructor<T>[]) driver.getConstructors(); //public
    }

    Constructor<T> noParameter = null;
    for (Constructor<T> constructor : constructors) {
        Autowired autowired = constructor.getAnnotation(Autowired.class);
        if (autowired != null) {
            return constructor;
        }
        if (constructor.getParameterCount() == 0) {
            //find 'no parameter' Constructor, using class.newInstance()";
            noParameter = constructor;
        }
    }

    if (noParameter != null) {
        return noParameter;
    }

    checkState(constructors.length == 1, String.format("%s has multiple public constructors, please ensure that there is only one", driver));
    return constructors[0];
}
 
Example 8
@CheckForNull
public static Constructor getDataBoundConstructor(@NonNull Class type) {
    for (Constructor c : type.getConstructors()) {
        if (c.getAnnotation(DataBoundConstructor.class) != null) return c;
    }
    return null;

}
 
Example 9
Source Project: blog_demos   File: ConstructorResolver.java    License: Apache License 2.0 5 votes vote down vote up
public static String[] evaluate(Constructor<?> candidate, int paramCount) {
	ConstructorProperties cp = candidate.getAnnotation(ConstructorProperties.class);
	if (cp != null) {
		String[] names = cp.value();
		if (names.length != paramCount) {
			throw new IllegalStateException("Constructor annotated with @ConstructorProperties but not " +
					"corresponding to actual number of parameters (" + paramCount + "): " + candidate);
		}
		return names;
	}
	else {
		return null;
	}
}
 
Example 10
Source Project: jdk8u-jdk   File: MetaData.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static String[] getAnnotationValue(Constructor<?> constructor) {
    ConstructorProperties annotation = constructor.getAnnotation(ConstructorProperties.class);
    return (annotation != null)
            ? annotation.value()
            : null;
}
 
Example 11
Source Project: jdk1.8-source-analysis   File: MetaData.java    License: Apache License 2.0 4 votes vote down vote up
private static String[] getAnnotationValue(Constructor<?> constructor) {
    ConstructorProperties annotation = constructor.getAnnotation(ConstructorProperties.class);
    return (annotation != null)
            ? annotation.value()
            : null;
}
 
Example 12
private static String[] getAnnotationValue(Constructor<?> constructor) {
    ConstructorProperties annotation = constructor.getAnnotation(ConstructorProperties.class);
    return (annotation != null)
            ? annotation.value()
            : null;
}
 
Example 13
private static String[] getAnnotationValue(Constructor<?> constructor) {
    ConstructorProperties annotation = constructor.getAnnotation(ConstructorProperties.class);
    return (annotation != null)
            ? annotation.value()
            : null;
}
 
Example 14
Source Project: businessworks   File: InjectionPoint.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Returns a new injection point for the injectable constructor of {@code type}.
 *
 * @param type a concrete type with exactly one constructor annotated {@literal @}{@link Inject},
 *     or a no-arguments constructor that is not private.
 * @throws ConfigurationException if there is no injectable constructor, more than one injectable
 *     constructor, or if parameters of the injectable constructor are malformed, such as a
 *     parameter with multiple binding annotations.
 */
public static InjectionPoint forConstructorOf(TypeLiteral<?> type) {
  Class<?> rawType = getRawType(type.getType());
  Errors errors = new Errors(rawType);

  Constructor<?> injectableConstructor = null;
  for (Constructor<?> constructor : rawType.getDeclaredConstructors()) {

    boolean optional;
    Inject guiceInject = constructor.getAnnotation(Inject.class);
    if (guiceInject == null) {
      javax.inject.Inject javaxInject = constructor.getAnnotation(javax.inject.Inject.class);
      if (javaxInject == null) {
        continue;
      }
      optional = false;
    } else {
      optional = guiceInject.optional();
    }

    if (optional) {
      errors.optionalConstructor(constructor);
    }

    if (injectableConstructor != null) {
      errors.tooManyConstructors(rawType);
    }

    injectableConstructor = constructor;
    checkForMisplacedBindingAnnotations(injectableConstructor, errors);
  }

  errors.throwConfigurationExceptionIfErrorsExist();

  if (injectableConstructor != null) {
    return new InjectionPoint(type, injectableConstructor);
  }

  // If no annotated constructor is found, look for a no-arg constructor instead.
  try {
    Constructor<?> noArgConstructor = rawType.getDeclaredConstructor();

    // Disallow private constructors on non-private classes (unless they have @Inject)
    if (Modifier.isPrivate(noArgConstructor.getModifiers())
        && !Modifier.isPrivate(rawType.getModifiers())) {
      errors.missingConstructor(rawType);
      throw new ConfigurationException(errors.getMessages());
    }

    checkForMisplacedBindingAnnotations(noArgConstructor, errors);
    return new InjectionPoint(type, noArgConstructor);
  } catch (NoSuchMethodException e) {
    errors.missingConstructor(rawType);
    throw new ConfigurationException(errors.getMessages());
  }
}
 
Example 15
Source Project: jdk8u60   File: MetaData.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static String[] getAnnotationValue(Constructor<?> constructor) {
    ConstructorProperties annotation = constructor.getAnnotation(ConstructorProperties.class);
    return (annotation != null)
            ? annotation.value()
            : null;
}
 
Example 16
@Override
public String[] getConstructorPropertiesValue(Constructor<?> ctr) {
    ConstructorProperties cp = ctr.getAnnotation(ConstructorProperties.class);
    String [] ret = cp != null ? cp.value() : null;
    return ret;
}
 
Example 17
private static String[] getAnnotationValue(Constructor<?> constructor) {
    ConstructorProperties annotation = constructor.getAnnotation(ConstructorProperties.class);
    return (annotation != null)
            ? annotation.value()
            : null;
}
 
Example 18
Source Project: hottub   File: MetaData.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static String[] getAnnotationValue(Constructor<?> constructor) {
    ConstructorProperties annotation = constructor.getAnnotation(ConstructorProperties.class);
    return (annotation != null)
            ? annotation.value()
            : null;
}
 
Example 19
Source Project: Java8CN   File: MetaData.java    License: Apache License 2.0 4 votes vote down vote up
private static String[] getAnnotationValue(Constructor<?> constructor) {
    ConstructorProperties annotation = constructor.getAnnotation(ConstructorProperties.class);
    return (annotation != null)
            ? annotation.value()
            : null;
}
 
Example 20
private static String[] getAnnotationValue(Constructor<?> constructor) {
    ConstructorProperties annotation = constructor.getAnnotation(ConstructorProperties.class);
    return (annotation != null)
            ? annotation.value()
            : null;
}