本文整理汇总了Java中java.lang.reflect.Constructor.getParameterAnnotations方法的典型用法代码示例。如果您正苦于以下问题:Java Constructor.getParameterAnnotations方法的具体用法?Java Constructor.getParameterAnnotations怎么用?Java Constructor.getParameterAnnotations使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.reflect.Constructor
的用法示例。
在下文中一共展示了Constructor.getParameterAnnotations方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: extractConstructorParameters
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
/**
* Matches constructor parameters to method parameters for injection and records remaining parameters as required keys.
*/
private String[] extractConstructorParameters(Key<?> factoryKey, TypeLiteral<?> implementation, Constructor constructor, List<Key<?>> methodParams,
Errors errors, Set<Dependency> dependencyCollector) throws ErrorsException {
// Get parameters with annotations.
List<TypeLiteral<?>> ctorParams = implementation.getParameterTypes(constructor);
Annotation[][] ctorParamAnnotations = constructor.getParameterAnnotations();
int p = 0;
String[] parameterNames = new String[ctorParams.size()];
for (TypeLiteral<?> ctorParam : ctorParams) {
Key<?> ctorParamKey = getKey(ctorParam, constructor, ctorParamAnnotations[p], errors);
if (ctorParamKey.getAnnotationType() == Assisted.class) {
int location = methodParams.indexOf(ctorParamKey);
// This should never happen since the constructor was already checked
// in #[inject]constructorHasMatchingParams(..).
Preconditions.checkState(location != -1);
parameterNames[p] = ReflectUtil.formatParameterName(location);
} else {
dependencyCollector.add(new Dependency(factoryKey, ctorParamKey, false, true, constructor.toString()));
}
p++;
}
return parameterNames;
}
示例2: AssistedConstructor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
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);
}
示例3: testConstructor2
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
public void testConstructor2()
{
try
{
Constructor<TestClass1> ctor = TestClass1.class.getConstructor
(new Class[] {Integer.class});
// No annotations should be present on the constructor itself
Annotation[] ctor_annotations = ctor.getDeclaredAnnotations();
assertTrue( ctor_annotations.length == 0);
ctor_annotations = ctor.getAnnotations();
assertTrue( ctor_annotations.length == 0);
// 3 parameter annotation should be present on the constructor
Annotation[][] params = ctor.getParameterAnnotations();
assertTrue(params.length != 0); // there are parameters
// first parameter has 3 annotations
assertTrue(params[0].length == 3);
}
catch (NoSuchMethodException e)
{
fail("Failed to get TestClass1 constructor (Integer)");
}
}
示例4: testConstructor3
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
public void testConstructor3()
{
try
{
Constructor<TestClass1> ctor = TestClass1.class.getConstructor();
// No annotations should be present on the constructor itself
Annotation[] ctor_annotations = ctor.getDeclaredAnnotations();
assertTrue( ctor_annotations.length == 0);
ctor_annotations = ctor.getAnnotations();
assertTrue( ctor_annotations.length == 0);
// No parameters (and therefore no parameter annotations) should be
// present on the constructor
Annotation[][] params = ctor.getParameterAnnotations();
assertTrue(params.length == 0); // no parameters
}
catch (NoSuchMethodException e)
{
fail("Failed to get TestClass1 constructor()");
}
}
示例5: AssistedConstructor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
@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);
}
示例6: main
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
public static void main(String[] args) throws NoSuchMethodException {
Constructor<InnerClass> constructor = InnerClass.class.getDeclaredConstructor(
Regress.class, String.class, String.class, String.class);
int i = 0;
for (Annotation[] annotations : constructor.getParameterAnnotations()) {
System.out.print(i++ + ": ");
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
}
}
示例7: injectConstructorHasMatchingParams
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
/**
* Matching logic for {@literal @}{@link Inject} constructor and method parameters.
* <p/>
* This returns true if all assisted parameters required by the constructor are provided by the factory method.
*/
private boolean injectConstructorHasMatchingParams(TypeLiteral<?> type, Constructor<?> constructor, List<Key<?>> paramList, Errors errors)
throws ErrorsException {
List<TypeLiteral<?>> params = type.getParameterTypes(constructor);
Annotation[][] paramAnnotations = constructor.getParameterAnnotations();
int p = 0;
for (TypeLiteral<?> param : params) {
Key<?> paramKey = getKey(param, constructor, paramAnnotations[p++], errors);
if (paramKey.getAnnotationType() == Assisted.class && !paramList.contains(paramKey)) {
return false;
}
}
return true;
}
示例8: toNtype
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
/**
* 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类型异常!");
}
示例9: testConstructor1
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
public void testConstructor1()
{
try
{
Constructor<TestClass1> ctor = TestClass1.class.getConstructor
(new Class[] {Integer.class, String.class});
Annotation[] ctor_annotations = ctor.getDeclaredAnnotations();
assertTrue( ctor_annotations.length == 2);
boolean[] annot_count = new boolean[] {false, false};
for (int i=0; i<2; i++)
{
assertTrue( ctor_annotations[i].annotationType().equals(A0.class)
|| ctor_annotations[i].annotationType().equals(A5.class));
if (ctor_annotations[i] instanceof A0)
{
annot_count[0] = true;
}
else if (ctor_annotations[i] instanceof A5)
{
annot_count[1] = true;
}
}
A0 a0 = (A0) ctor.getAnnotation(A0.class);
assertTrue (ctor.getAnnotation(A0.class).equals(a0));
A5 a5 = (A5) ctor.getAnnotation(A5.class);
assertTrue (ctor.getAnnotation(A5.class).equals(a5));
// Make sure both @A0 and @A5 were found
assertTrue (annot_count[0] && annot_count[1]);
// Verify the annotations associated with the constructor parameters
Annotation[][] params = ctor.getParameterAnnotations();
assertTrue(params.length != 0);
// first parameter has 1 annotation
assertTrue(params[0].length == 1);
// second parameter has 4annotations
assertTrue(params[1].length == 4);
assertTrue(params[0][0] instanceof A0);
annot_count = new boolean[] {false, false, false, false};
for (int i=0; i<4; i++)
{
if (params[1][i] instanceof A0)
{
annot_count[0] = true;
}
else if (params[1][i] instanceof A1)
{
annot_count[1] = true;
}
else if (params[1][i] instanceof A2)
{
annot_count[2] = true;
}
else if (params[1][i] instanceof A3)
{
annot_count[3] = true;
}
}
assertTrue (annot_count[0] && annot_count[1] &&
annot_count[2] && annot_count[3]);
}
catch (NoSuchMethodException e)
{
fail("Failed to get TestClass1 constructor (Integer, String): " + e.getMessage());
}
}
示例10: build
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
public ExceptionGenerator build() {
Constructor<? extends Exception> constructor = getConstructor(exceptionType);
Class<?>[] parameterTypes = constructor.getParameterTypes();
Annotation[][] parametersAnnotations = constructor.getParameterAnnotations();
Integer bodyIndex = -1;
Integer headerMapIndex = -1;
Integer numOfParams = parameterTypes.length;
Class bodyType = null;
for( int i = 0; i < parameterTypes.length; i++) {
Annotation[] paramAnnotations = parametersAnnotations[i];
boolean foundAnnotation = false;
for(Annotation annotation: paramAnnotations) {
if(annotation.annotationType().equals(ResponseHeaders.class)) {
checkState(headerMapIndex == -1,
"Cannot have two parameters tagged with @ResponseHeaders");
checkState(parameterTypes[i].equals(Map.class),
"Response Header map must be of type Map, but was %s", parameterTypes[i]);
headerMapIndex = i;
foundAnnotation = true;
break;
}
}
if(!foundAnnotation) {
checkState(bodyIndex == -1,
"Cannot have two parameters either without annotations or with @ResponseBody annotation");
bodyIndex = i;
bodyType = parameterTypes[i];
}
}
ExceptionGenerator generator = new ExceptionGenerator(
bodyIndex,
headerMapIndex,
numOfParams,
bodyType,
exceptionType,
responseBodyDecoder
);
validateGeneratorCanBeUsedToGenerateExceptions(generator);
return generator;
}
示例11: constructorSignature
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
private static MBeanParameterInfo[] constructorSignature(Constructor<?> cn) {
final Class<?>[] classes = cn.getParameterTypes();
final Annotation[][] annots = cn.getParameterAnnotations();
return MBeanOperationInfo.parameters(classes, annots);
}
示例12: lookupParameterNames
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
public String[] lookupParameterNames(AccessibleObject methodOrCtor, boolean throwExceptionIfMissing) {
// Oh for some commonality between Constructor and Method !!
Class<?>[] types = null;
Class<?> declaringClass = null;
String name = null;
Annotation[][] anns = null;
if (methodOrCtor instanceof Method) {
Method method = (Method) methodOrCtor;
types = method.getParameterTypes();
name = method.getName();
declaringClass = method.getDeclaringClass();
anns = method.getParameterAnnotations();
} else {
Constructor<?> constructor = (Constructor<?>) methodOrCtor;
types = constructor.getParameterTypes();
declaringClass = constructor.getDeclaringClass();
name = "<init>";
anns = constructor.getParameterAnnotations();
}
if (types.length == 0) {
return EMPTY_NAMES;
}
final String[] names = new String[types.length];
boolean allDone = true;
for (int i = 0; i < names.length; i++) {
for (int j = 0; j < anns[i].length; j++) {
Annotation ann = anns[i][j];
if (isNamed(ann)) {
names[i] = getNamedValue(ann);
break;
}
}
if (names[i] == null) {
allDone = false;
}
}
// fill in blanks from fallback if possible.
if (!allDone) {
allDone = true;
String[] altNames = fallback.lookupParameterNames(methodOrCtor, false);
if (altNames.length > 0) {
for (int i = 0; i < names.length; i++) {
if (names[i] == null) {
if (altNames[i] != null) {
names[i] = altNames[i];
} else {
allDone = false;
}
}
}
} else {
allDone = false;
}
}
// error if applicable
if (!allDone) {
if (throwExceptionIfMissing) {
throw new ParameterNamesNotFoundException("One or more @Named annotations missing for class '"
+ declaringClass.getName() + "', methodOrCtor " + name + " and parameter types "
+ DefaultParanamer.getParameterTypeNamesCSV(types));
} else {
return Paranamer.EMPTY_NAMES;
}
}
return names;
}