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

The following examples show how to use java.lang.reflect.Constructor#getParameterAnnotations() . 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: ParametricScalarImplementation.java    From presto with Apache License 2.0 6 votes vote down vote up
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 File: AssistedConstructor.java    From crate with 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 3
Source File: AssistedConstructor.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@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 4
Source File: ObjectContainer.java    From flink-statefun with 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 5
Source File: ConstructorResolver.java    From pinpoint with 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 6
Source File: ResourceUtils.java    From cxf with 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 File: TestUtil.java    From openrtb-doubleclick with 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 File: TestUtil.java    From openrtb with 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 9
Source File: NMapConvert.java    From util4j with 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 10
Source File: BeanHandler.java    From wisdom with 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 11
Source File: ResourceUtils.java    From cxf with 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 12
Source File: BeanDefinition.java    From rest.vertx with 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 13
Source File: MBeanConstructorInfo.java    From jdk8u-jdk with GNU General Public License v2.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 14
Source File: MBeanConstructorInfo.java    From jdk8u-jdk with GNU General Public License v2.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 15
Source File: MBeanConstructorInfo.java    From openjdk-jdk8u with GNU General Public License v2.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
Source File: MBeanConstructorInfo.java    From JDKSourceCode1.8 with 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 17
Source File: MBeanConstructorInfo.java    From openjdk-8-source with GNU General Public License v2.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 18
Source File: MBeanConstructorInfo.java    From Java8CN with 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 19
Source File: MBeanConstructorInfo.java    From jdk8u-dev-jdk with GNU General Public License v2.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 20
Source File: MBeanConstructorInfo.java    From jdk8u_jdk with GNU General Public License v2.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);
}