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

The following examples show how to use java.lang.reflect.Constructor#getParameterAnnotations() . 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 Optional<MethodHandle> getConstructor(Method method, Optional<Constructor<?>> optionalConstructor)
{
    if (isStatic(method.getModifiers())) {
        return Optional.empty();
    }

    checkArgument(optionalConstructor.isPresent(), "Method [%s] is an instance method. It must be in a class annotated with @ScalarFunction or @ScalarOperator, and the class is required to have a public constructor.", method);
    Constructor<?> constructor = optionalConstructor.get();
    Set<TypeParameter> constructorTypeParameters = Stream.of(constructor.getAnnotationsByType(TypeParameter.class))
            .collect(ImmutableSet.toImmutableSet());
    checkArgument(constructorTypeParameters.containsAll(typeParameters), "Method [%s] is an instance method and requires a public constructor containing all type parameters: %s", method, typeParameters);

    for (int i = 0; i < constructor.getParameterCount(); i++) {
        Annotation[] annotations = constructor.getParameterAnnotations()[i];
        checkArgument(containsImplementationDependencyAnnotation(annotations), "Constructors may only have meta parameters [%s]", constructor);
        checkArgument(annotations.length == 1, "Meta parameters may only have a single annotation [%s]", constructor);
        Annotation annotation = annotations[0];
        if (annotation instanceof TypeParameter) {
            checkTypeParameters(parseTypeSignature(((TypeParameter) annotation).value(), ImmutableSet.of()), typeParameterNames, method);
        }
        constructorDependencies.add(createDependency(annotation, literalParameters));
    }
    MethodHandle result = constructorMethodHandle(FUNCTION_IMPLEMENTATION_ERROR, constructor);
    // Change type of return value to Object to make sure callers won't have classloader issues
    return Optional.of(result.asType(result.type().changeReturnType(Object.class)));
}
 
Example 2
Source Project: flink-statefun   File: ObjectContainer.java    License: Apache License 2.0 6 votes vote down vote up
private Object createReflectively(Class<?> type) {
  Constructor<?> constructor = findConstructorForInjection(type);
  Class<?>[] dependencies = constructor.getParameterTypes();
  Annotation[][] annotations = constructor.getParameterAnnotations();
  Object[] resolvedDependencies = new Object[dependencies.length];
  int i = 0;
  for (Class<?> dependency : dependencies) {
    @Nullable String label = findLabel(annotations[i]);
    Key key = new Key(dependency, label);
    resolvedDependencies[i] = getOrCreateInstance(key);
    i++;
  }
  try {
    constructor.setAccessible(true);
    return constructor.newInstance(resolvedDependencies);
  } catch (Throwable e) {
    throw new RuntimeException(e);
  }
}
 
Example 3
Source Project: pinpoint   File: ConstructorResolver.java    License: Apache License 2.0 6 votes vote down vote up
public boolean resolve() {
    final Constructor<?>[] constructors = type.getConstructors();
    Arrays.sort(constructors, CONSTRUCTOR_COMPARATOR);
    
    for (Constructor<?> constructor : constructors) {
        final Class<?>[] parameterTypes = constructor.getParameterTypes();
        final Annotation[][] parameterAnnotations = constructor.getParameterAnnotations();

        Object[] resolvedArguments = argumentsResolver.resolve(parameterTypes, parameterAnnotations);
        
        if (resolvedArguments != null) {
            this.resolvedConstructor = constructor;
            this.resolvedArguments = resolvedArguments;

            return true;
        }
    }
    if (logger.isWarnEnabled()) {
        resolveFailLog(type);
    }
    return false;
}
 
Example 4
@SuppressWarnings("unchecked")
public AssistedConstructor(Constructor<T> constructor, List<TypeLiteral<?>> parameterTypes) {
    this.constructor = constructor;

    Annotation[][] annotations = constructor.getParameterAnnotations();

    List<Type> typeList = new ArrayList<>();
    allParameters = new ArrayList<>();

    // categorize params as @Assisted or @Injected
    for (int i = 0; i < parameterTypes.size(); i++) {
        Parameter parameter = new Parameter(parameterTypes.get(i).getType(), annotations[i]);
        allParameters.add(parameter);
        if (parameter.isProvidedByFactory()) {
            typeList.add(parameter.getType());
        }
    }
    this.assistedParameters = new ParameterListKey(typeList);
}
 
Example 5
Source Project: crate   File: AssistedConstructor.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
AssistedConstructor(Constructor<T> constructor, List<TypeLiteral<?>> parameterTypes) {
    this.constructor = constructor;

    Annotation[][] annotations = constructor.getParameterAnnotations();

    List<Type> typeList = new ArrayList<>();
    allParameters = new ArrayList<>(parameterTypes.size());

    // categorize params as @Assisted or @Injected
    for (int i = 0; i < parameterTypes.size(); i++) {
        Parameter parameter = new Parameter(parameterTypes.get(i).getType(), annotations[i]);
        allParameters.add(parameter);
        if (parameter.isProvidedByFactory()) {
            typeList.add(parameter.getType());
        }
    }
    this.assistedParameters = new ParameterListKey(typeList);
}
 
Example 6
Source Project: cxf   File: ResourceUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static Object[] createConstructorArguments(Constructor<?> c,
                                                  Message m,
                                                  boolean perRequest,
                                                  Map<Class<?>, Object> contextValues) {
    Class<?>[] params = c.getParameterTypes();
    Annotation[][] anns = c.getParameterAnnotations();
    Type[] genericTypes = c.getGenericParameterTypes();
    return createConstructorArguments(c, m, perRequest, contextValues, params, anns, genericTypes);
}
 
Example 7
Source Project: openrtb   File: TestUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static <T extends Throwable> void testParameterAnnotations(Constructor<T> constr) {
  Annotation[][] anns = constr.getParameterAnnotations();

  for (Annotation[] ann : anns) {
    assertThat(ann.length == 1 && ann[0] instanceof Nullable).isTrue();
  }
}
 
Example 8
Source Project: wisdom   File: BeanHandler.java    License: Apache License 2.0 5 votes vote down vote up
private Constructor<?> findConstructor(Class<?> rawType) {
    for (Constructor constructor : rawType.getConstructors()) {
        Annotation[][] annotations = constructor.getParameterAnnotations();
        // Just check that all parameters are annotated, a more in-depth check is done during the creation of the
        // actual parameter.
        for (Annotation[] param : annotations) {
            if (param.length == 0) {
                // Not suitable.
                continue;
            }
            return constructor;
        }
    }
    return null;
}
 
Example 9
Source Project: cxf   File: ResourceUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static Constructor<?> findResourceConstructor(Class<?> resourceClass, boolean perRequest) {
    List<Constructor<?>> cs = new LinkedList<>();
    for (Constructor<?> c : resourceClass.getConstructors()) {
        Class<?>[] params = c.getParameterTypes();
        Annotation[][] anns = c.getParameterAnnotations();
        boolean match = true;
        for (int i = 0; i < params.length; i++) {
            if (!perRequest) {
                if (AnnotationUtils.getAnnotation(anns[i], Context.class) == null) {
                    match = false;
                    break;
                }
            } else if (!AnnotationUtils.isValidParamAnnotations(anns[i])) {
                match = false;
                break;
            }
        }
        if (match) {
            cs.add(c);
        }
    }
    Collections.sort(cs, new Comparator<Constructor<?>>() {

        public int compare(Constructor<?> c1, Constructor<?> c2) {
            int p1 = c1.getParameterTypes().length;
            int p2 = c2.getParameterTypes().length;
            return p1 > p2 ? -1 : p1 < p2 ? 1 : 0;
        }

    });
    return cs.isEmpty() ? null : cs.get(0);
}
 
Example 10
Source Project: rest.vertx   File: BeanDefinition.java    License: Apache License 2.0 5 votes vote down vote up
private void init (Constructor<?> constructor) {
    Annotation[][] paramAnnotations = constructor.getParameterAnnotations();
    Class<?>[] types = constructor.getParameterTypes();

    for (int index = 0; index < paramAnnotations.length; index ++) {
        Annotation[] annotations = paramAnnotations[index];
        Class<?> type = types[index];
        MethodParameter paramValues = getValueFromAnnotations(annotations, type, index);
        parameters.put(String.format(CONSTRUCTOR_SUFFIX, index), paramValues);
    }
}
 
Example 11
Source Project: util4j   File: NMapConvert.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * String类型全部用NUTF8代替
 * @param obj
 * @return
 * @throws Exception
 */
@Deprecated
@SuppressWarnings("unchecked")
public final NType<?> toNtype(Object obj) throws Exception
{
	if(obj==null)
	{
		return new NNull();
	}
	if(map.containsKey(obj.getClass()))
	{//如果有该类型的封装类型
		if(obj instanceof Map)
		{//如果是map类型,则
			return toNMap((Map<Object, Object>) obj);
		}
		Class<? extends NType<?>> c=(Class<? extends NType<?>>) map.get(obj.getClass());//拿到包装类对应的ntype类型
		Constructor<?>[] cons=c.getConstructors();
		for(Constructor<?> con:cons)
		{
			if(con.getParameterAnnotations().length==1)
			{//当子类存在构造参数的构造器时
				NType<?> ntype=c.getConstructor(con.getParameterTypes()[0]).newInstance(obj);
				return ntype;
			}
		}
	}
	throw new RuntimeException(obj+"["+obj.getClass().toString()+"]"+"转换为NType类型异常!");
}
 
Example 12
Source Project: openrtb-doubleclick   File: TestUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static <T extends Throwable> void testParameterAnnotations(Constructor<T> constr) {
  Annotation[][] anns = constr.getParameterAnnotations();

  for (Annotation[] ann : anns) {
    assertThat(ann.length == 1 && ann[0] instanceof Nullable).isTrue();
  }
}
 
Example 13
private static MBeanParameterInfo[] constructorSignature(Constructor<?> cn) {
    final Class<?>[] classes = cn.getParameterTypes();
    final Annotation[][] annots = cn.getParameterAnnotations();
    return MBeanOperationInfo.parameters(classes, annots);
}
 
Example 14
private static MBeanParameterInfo[] constructorSignature(Constructor<?> cn) {
    final Class<?>[] classes = cn.getParameterTypes();
    final Annotation[][] annots = cn.getParameterAnnotations();
    return MBeanOperationInfo.parameters(classes, annots);
}
 
Example 15
Source Project: Java8CN   File: MBeanConstructorInfo.java    License: Apache License 2.0 4 votes vote down vote up
private static MBeanParameterInfo[] constructorSignature(Constructor<?> cn) {
    final Class<?>[] classes = cn.getParameterTypes();
    final Annotation[][] annots = cn.getParameterAnnotations();
    return MBeanOperationInfo.parameters(classes, annots);
}
 
Example 16
private static MBeanParameterInfo[] constructorSignature(Constructor<?> cn) {
    final Class<?>[] classes = cn.getParameterTypes();
    final Annotation[][] annots = cn.getParameterAnnotations();
    return MBeanOperationInfo.parameters(classes, annots);
}
 
Example 17
Source Project: JDKSourceCode1.8   File: MBeanConstructorInfo.java    License: MIT License 4 votes vote down vote up
private static MBeanParameterInfo[] constructorSignature(Constructor<?> cn) {
    final Class<?>[] classes = cn.getParameterTypes();
    final Annotation[][] annots = cn.getParameterAnnotations();
    return MBeanOperationInfo.parameters(classes, annots);
}
 
Example 18
private static MBeanParameterInfo[] constructorSignature(Constructor<?> cn) {
    final Class<?>[] classes = cn.getParameterTypes();
    final Annotation[][] annots = cn.getParameterAnnotations();
    return MBeanOperationInfo.parameters(classes, annots);
}
 
Example 19
private static MBeanParameterInfo[] constructorSignature(Constructor<?> cn) {
    final Class<?>[] classes = cn.getParameterTypes();
    final Annotation[][] annots = cn.getParameterAnnotations();
    return MBeanOperationInfo.parameters(classes, annots);
}
 
Example 20
private static MBeanParameterInfo[] constructorSignature(Constructor<?> cn) {
    final Class<?>[] classes = cn.getParameterTypes();
    final Annotation[][] annots = cn.getParameterAnnotations();
    return MBeanOperationInfo.parameters(classes, annots);
}