本文整理汇总了Java中java.lang.reflect.Constructor.getDeclaredAnnotations方法的典型用法代码示例。如果您正苦于以下问题:Java Constructor.getDeclaredAnnotations方法的具体用法?Java Constructor.getDeclaredAnnotations怎么用?Java Constructor.getDeclaredAnnotations使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.reflect.Constructor
的用法示例。
在下文中一共展示了Constructor.getDeclaredAnnotations方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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)");
}
}
示例2: 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()");
}
}
示例3: addConstructor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
public void addConstructor(Constructor<?> constructor) throws Exception {
List<Type> paramTypes = new ArrayList<Type>();
for (Class<?> paramType : constructor.getParameterTypes()) {
paramTypes.add(Type.getType(paramType));
}
String methodDescriptor = Type.getMethodDescriptor(VOID_TYPE, paramTypes.toArray(EMPTY_TYPES));
MethodVisitor methodVisitor = visitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", methodDescriptor, signature(constructor), EMPTY_STRINGS);
for (Annotation annotation : constructor.getDeclaredAnnotations()) {
if (annotation.annotationType().getAnnotation(Inherited.class) != null) {
continue;
}
Retention retention = annotation.annotationType().getAnnotation(Retention.class);
AnnotationVisitor annotationVisitor = methodVisitor.visitAnnotation(Type.getType(annotation.annotationType()).getDescriptor(), retention != null && retention.value() == RetentionPolicy.RUNTIME);
annotationVisitor.visitEnd();
}
methodVisitor.visitCode();
// this.super(p0 .. pn)
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
for (int i = 0; i < constructor.getParameterTypes().length; i++) {
methodVisitor.visitVarInsn(Type.getType(constructor.getParameterTypes()[i]).getOpcode(Opcodes.ILOAD), i + 1);
}
methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superclassType.getInternalName(), "<init>", methodDescriptor, false);
methodVisitor.visitInsn(Opcodes.RETURN);
methodVisitor.visitMaxs(0, 0);
methodVisitor.visitEnd();
}
示例4: 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());
}
}