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

The following examples show how to use java.lang.reflect.Constructor#getAnnotation() . 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 check out the related API usage on the sidebar.
Example 1
Source File: AnnotationProcessor.java    From PlugFace with 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 2
Source File: OverriddenObjectsMap.java    From DaggerMock with 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 3
Source File: Permissions.java    From commons-jexl with 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 4
Source File: ReflectiveJustInTimeLookupFactory.java    From dagger-reflect with Apache License 2.0 6 votes vote down vote up
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 5
Source File: ObjectContainer.java    From stateful-functions with Apache License 2.0 6 votes vote down vote up
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 6
Source File: ConstructorResolver.java    From blog_demos with 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 7
Source File: Snake.java    From Snake with 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 8
Source File: InternalContext.java    From gadtry with 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 9
Source File: DataBoundConfigurator.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@CheckForNull
public static Constructor getDataBoundConstructor(@NonNull Class type) {
    for (Constructor c : type.getConstructors()) {
        if (c.getAnnotation(DataBoundConstructor.class) != null) return c;
    }
    return null;

}
 
Example 10
Source File: Introspector.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String[] getConstructorPropertiesValue(Constructor<?> ctr) {
    ConstructorProperties cp = ctr.getAnnotation(ConstructorProperties.class);
    String [] ret = cp != null ? cp.value() : null;
    return ret;
}
 
Example 11
Source File: MetaData.java    From jdk1.8-source-analysis with 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
Source File: MetaData.java    From openjdk-jdk8u-backup with 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 13
Source File: MetaData.java    From Java8CN with 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 14
Source File: MetaData.java    From hottub with 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 15
Source File: MetaData.java    From openjdk-8-source with 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
Source File: MetaData.java    From jdk8u-jdk with 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 17
Source File: MetaData.java    From jdk8u60 with 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 18
Source File: InjectionPoint.java    From businessworks with 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 19
Source File: MetaData.java    From TencentKona-8 with 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 20
Source File: MetaData.java    From dragonwell8_jdk with 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;
}