/** * Constructs an annotation that can be accessed through the interface * represented by <code>clazz</code>. The values of the members are * not specified. * * @param cp the constant pool table. * @param clazz the interface. * @throws NotFoundException when the clazz is not found */ public Annotation(ConstPool cp, CtClass clazz) throws NotFoundException { // todo Enums are not supported right now. this(cp.addUtf8Info(Descriptor.of(clazz.getName())), cp); if (!clazz.isInterface()) throw new RuntimeException( "Only interfaces are allowed for Annotation creation."); CtMethod methods[] = clazz.getDeclaredMethods(); if (methods.length > 0) { members = new LinkedHashMap(); } for (int i = 0; i < methods.length; i++) { CtClass returnType = methods[i].getReturnType(); addMemberValue(methods[i].getName(), createMemberValue(cp, returnType)); } }
private static MemberValue createMemberValue(ConstPool cp, CtClass type, Object value) throws NotFoundException { MemberValue memberValue = javassist.bytecode.annotation.Annotation.createMemberValue(cp, type); if (memberValue instanceof BooleanMemberValue) { ((BooleanMemberValue) memberValue).setValue((Boolean) value);
private static MemberValue createMemberValue(ConstPool cp, CtClass type, Object value) throws NotFoundException { MemberValue memberValue = javassist.bytecode.annotation.Annotation.createMemberValue(cp, type); if (memberValue instanceof BooleanMemberValue) { ((BooleanMemberValue) memberValue).setValue((Boolean) value);
/** * Constructs an annotation that can be accessed through the interface * represented by <code>clazz</code>. The values of the members are * not specified. * * @param cp the constant pool table. * @param clazz the interface. * @throws NotFoundException when the clazz is not found */ public Annotation(ConstPool cp, CtClass clazz) throws NotFoundException { // todo Enums are not supported right now. this(cp.addUtf8Info(Descriptor.of(clazz.getName())), cp); if (!clazz.isInterface()) throw new RuntimeException( "Only interfaces are allowed for Annotation creation."); CtMethod[] methods = clazz.getDeclaredMethods(); if (methods.length > 0) members = new LinkedHashMap<String,Pair>(); for (CtMethod m:methods) addMemberValue(m.getName(), createMemberValue(cp, m.getReturnType())); }
pool.importPackage("javax.persistence"); pool.importPackage("java.lang"); EnumMemberValue strategy = (EnumMemberValue) Annotation.createMemberValue(constantPool, pool.makeClass("InheritanceType")); strategy.setType(InheritanceType.class.getName()); strategy.setValue(InheritanceType.SINGLE_TABLE.name()); name.setValue(myInfo.getDiscriminatorName()); discriminator.addMemberValue("name", name); EnumMemberValue discriminatorType = (EnumMemberValue) Annotation.createMemberValue(constantPool, pool.makeClass("DiscriminatorType")); discriminatorType.setType(DiscriminatorType.class.getName()); discriminatorType.setValue(myInfo.getDiscriminatorType().name());
namedQuery.addMemberValue("query", queryString); EnumMemberValue lockMode = (EnumMemberValue) Annotation.createMemberValue(constantPool, pool.makeClass("LockModeType")); lockMode.setType(LockModeType.class.getName()); lockMode.setValue(query.lockMode().toString());
else if (type.isArray()) { CtClass arrayType = type.getComponentType(); MemberValue member = createMemberValue(cp, arrayType); return new ArrayMemberValue(member, cp);
else if (type.isArray()) { CtClass arrayType = type.getComponentType(); MemberValue member = createMemberValue(cp, arrayType); return new ArrayMemberValue(member, cp);
/** * Makes an instance of <code>MemberValue</code>. * @param cp the constant pool table. * @param type the type of the member. * @return the member value * @throws NotFoundException if not found */ public static MemberValue createMemberValue(ConstPool cp, CtClass type) throws NotFoundException { return javassist.bytecode.annotation.Annotation.createMemberValue(cp, type); }
/** * @param classPool class pool to use * @param constPool constants pool * @param ann annotation to copy * @return javassist annotation object (copy of original annotation) * @throws Exception on errors */ @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") public static Annotation copyAnnotation(final ClassPool classPool, final ConstPool constPool, final java.lang.annotation.Annotation ann) throws Exception { final Class<? extends java.lang.annotation.Annotation> annotationType = ann.annotationType(); final Annotation copy = new Annotation(annotationType.getName(), constPool); final Method[] methods = annotationType.getDeclaredMethods(); for (final Method method : methods) { final CtClass ctType = classPool.get(method.getReturnType().getName()); final MemberValue memberValue = Annotation.createMemberValue(constPool, ctType); final Object value = method.invoke(ann); memberValue.accept(new AnnotationMemberValueVisitor(classPool, constPool, value)); copy.addMemberValue(method.getName(), memberValue); } return copy; }
private Annotation copyAnnotation(ConstPool constPool, java.lang.annotation.Annotation annotation) throws NotFoundException { // Create annotation from specified type Annotation byteCodeAnnotation = createAnnotation( constPool, annotation.annotationType() ); // Copy annotation methods for (Method m : annotation.annotationType().getDeclaredMethods()) { Object value = invoke(m, annotation); MemberValue memberValue = createMemberValue( constPool, classPool.get(value.getClass().getName()) ); invoke(Classes.from(memberValue.getClass()) .method("setValue", value.getClass()) .orElseThrow(() -> new NotFoundException("Cannot copy value of qualifier parameter " + m.getName())), memberValue, value); byteCodeAnnotation.addMemberValue( m.getName(), memberValue ); } return byteCodeAnnotation; }
private MemberValue createValue(final Object value) throws Exception { final MemberValue memberValue = Annotation.createMemberValue( this.constPool, getCtClass(classPool, getClass(value))); memberValue.accept(new AnnotationMemberValueVisitor(this.classPool, this.constPool, value)); return memberValue; }
/** * Constructs an annotation that can be accessed through the interface * represented by <code>clazz</code>. The values of the members are * not specified. * * @param cp the constant pool table. * @param clazz the interface. * @throws NotFoundException when the clazz is not found */ public Annotation(ConstPool cp, CtClass clazz) throws NotFoundException { // todo Enums are not supported right now. this(cp.addUtf8Info(Descriptor.of(clazz.getName())), cp); if (!clazz.isInterface()) throw new RuntimeException( "Only interfaces are allowed for Annotation creation."); CtMethod methods[] = clazz.getDeclaredMethods(); if (methods.length > 0) { members = new LinkedHashMap(); } for (int i = 0; i < methods.length; i++) { CtClass returnType = methods[i].getReturnType(); addMemberValue(methods[i].getName(), createMemberValue(cp, returnType)); } }
/** * Constructs an annotation that can be accessed through the interface * represented by <code>clazz</code>. The values of the members are * not specified. * * @param cp the constant pool table. * @param clazz the interface. * @throws NotFoundException when the clazz is not found */ public Annotation(ConstPool cp, CtClass clazz) throws NotFoundException { // todo Enums are not supported right now. this(cp.addUtf8Info(Descriptor.of(clazz.getName())), cp); if (!clazz.isInterface()) throw new RuntimeException( "Only interfaces are allowed for Annotation creation."); CtMethod methods[] = clazz.getDeclaredMethods(); if (methods.length > 0) { members = new HashMap(); } for (int i = 0; i < methods.length; i++) { CtClass returnType = methods[i].getReturnType(); addMemberValue(methods[i].getName(), createMemberValue(cp, returnType)); } }
/** * Constructs an annotation that can be accessed through the interface * represented by <code>clazz</code>. The values of the members are * not specified. * * @param cp the constant pool table. * @param clazz the interface. * @throws NotFoundException when the clazz is not found */ public Annotation(ConstPool cp, CtClass clazz) throws NotFoundException { // todo Enums are not supported right now. this(cp.addUtf8Info(Descriptor.of(clazz.getName())), cp); if (!clazz.isInterface()) throw new RuntimeException( "Only interfaces are allowed for Annotation creation."); CtMethod methods[] = clazz.getDeclaredMethods(); if (methods.length > 0) { members = new LinkedHashMap(); } for (int i = 0; i < methods.length; i++) { CtClass returnType = methods[i].getReturnType(); addMemberValue(methods[i].getName(), createMemberValue(cp, returnType)); } }
/** * Constructs an annotation that can be accessed through the interface * represented by <code>clazz</code>. The values of the members are * not specified. * * @param cp the constant pool table. * @param clazz the interface. * @throws NotFoundException when the clazz is not found */ public Annotation(ConstPool cp, CtClass clazz) throws NotFoundException { // todo Enums are not supported right now. this(cp.addUtf8Info(Descriptor.of(clazz.getName())), cp); if (!clazz.isInterface()) throw new RuntimeException( "Only interfaces are allowed for Annotation creation."); CtMethod methods[] = clazz.getDeclaredMethods(); if (methods.length > 0) { members = new LinkedHashMap(); } for (int i = 0; i < methods.length; i++) { CtClass returnType = methods[i].getReturnType(); addMemberValue(methods[i].getName(), createMemberValue(cp, returnType)); } }
/** * Constructs an annotation that can be accessed through the interface * represented by <code>clazz</code>. The values of the members are * not specified. * * @param cp the constant pool table. * @param clazz the interface. * @throws NotFoundException when the clazz is not found */ public Annotation(ConstPool cp, CtClass clazz) throws NotFoundException { // todo Enums are not supported right now. this(cp.addUtf8Info(Descriptor.of(clazz.getName())), cp); if (!clazz.isInterface()) throw new RuntimeException( "Only interfaces are allowed for Annotation creation."); CtMethod methods[] = clazz.getDeclaredMethods(); if (methods.length > 0) { members = new LinkedHashMap(); } for (int i = 0; i < methods.length; i++) { CtClass returnType = methods[i].getReturnType(); addMemberValue(methods[i].getName(), createMemberValue(cp, returnType)); } }
private static MemberValue createMemberValue(ConstPool cp, CtClass type, Object value) throws NotFoundException { MemberValue memberValue = javassist.bytecode.annotation.Annotation.createMemberValue(cp, type); if (memberValue instanceof BooleanMemberValue) { ((BooleanMemberValue) memberValue).setValue((Boolean) value);
private static MemberValue createMemberValue(ConstPool cp, CtClass type, Object value) throws NotFoundException { MemberValue memberValue = javassist.bytecode.annotation.Annotation.createMemberValue(cp, type); if (memberValue instanceof BooleanMemberValue) { ((BooleanMemberValue) memberValue).setValue((Boolean) value);
private static MemberValue createMemberValue(ConstPool cp, CtClass type, Object value) throws NotFoundException { MemberValue memberValue = javassist.bytecode.annotation.Annotation.createMemberValue(cp, type); if (memberValue instanceof BooleanMemberValue) ((BooleanMemberValue) memberValue).setValue((Boolean) value);