Refine search
ClassPool pool = ClassGenerator.getClassPool(clazz.getClassLoader()); CtClass ctClass = pool.makeClass(parameterClassName); ClassFile classFile = ctClass.getClassFile(); classFile.setVersionToJava5(); ctClass.addConstructor(CtNewConstructor.defaultConstructor(pool.getCtClass(parameterClassName))); Class<?> type = parameterTypes[i]; Annotation[] annotations = parameterAnnotations[i]; AnnotationsAttribute attribute = new AnnotationsAttribute(classFile.getConstPool(), AnnotationsAttribute.visibleTag); for (Annotation annotation : annotations) { if (annotation.annotationType().isAnnotationPresent(Constraint.class)) { javassist.bytecode.annotation.Annotation ja = new javassist.bytecode.annotation.Annotation( classFile.getConstPool(), pool.getCtClass(annotation.annotationType().getName())); Method[] members = annotation.annotationType().getMethods(); for (Method member : members) { MemberValue memberValue = createMemberValue( classFile.getConstPool(), pool.get(member.getReturnType().getName()), value); ja.addMemberValue(member.getName(), memberValue); attribute.addAnnotation(ja); ctField.getFieldInfo().addAttribute(attribute); ctClass.addField(ctField); parameterClass = ctClass.toClass(clazz.getClassLoader(), null);
List<?> attributes = classFile.getAttributes(); Iterator<?> itr = attributes.iterator(); while (itr.hasNext()) { Object object = itr.next(); if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) { containsTypeLevelAnnotation = containsTypeLevelPersistenceAnnotation(((AnnotationsAttribute) object).getAnnotations()); List<?> attributes = myField.getAttributes(); Iterator<?> itr = attributes.iterator(); AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(constantPool, AnnotationsAttribute.visibleTag); while (itr.hasNext()) { Object object = itr.next(); if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) { AnnotationsAttribute attr = (AnnotationsAttribute) object; Annotation[] items = attr.getAnnotations(); for (Annotation annotation : items) { String typeName = annotation.getTypeName(); if (typeName.equals(Type.class.getName())) { StringMemberValue annot = (StringMemberValue) annotation.getMemberValue("type"); if (annot != null && annot.getValue().equals(StringClobType.class.getName())) { Annotation clobType = new Annotation(Type.class.getName(), constantPool); StringMemberValue type = new StringMemberValue(constantPool); annotationsAttribute.addAnnotation(clobType); transformed = true; } else { annotationsAttribute.addAnnotation(annotation); annotationsAttribute.addAnnotation(annotation);
private ClassDescriptor toClassDescriptor(ClassFile classFile, ArchiveEntry entry) { ClassDescriptor.Categorization categorization = ClassDescriptor.Categorization.OTHER;; final AnnotationsAttribute visibleAnnotations = (AnnotationsAttribute) classFile.getAttribute( AnnotationsAttribute.visibleTag ); if ( visibleAnnotations != null ) { if ( visibleAnnotations.getAnnotation( Entity.class.getName() ) != null || visibleAnnotations.getAnnotation( MappedSuperclass.class.getName() ) != null || visibleAnnotations.getAnnotation( Embeddable.class.getName() ) != null ) { categorization = ClassDescriptor.Categorization.MODEL; } else if ( visibleAnnotations.getAnnotation( Converter.class.getName() ) != null ) { categorization = ClassDescriptor.Categorization.CONVERTER; } } return new ClassDescriptorImpl( classFile.getName(), categorization, entry.getStreamAccess() ); } }
/** * Adds an annotation. If there is an annotation with the same type, * it is removed before the new annotation is added. * * @param annotation the added annotation. */ public void addAnnotation(Annotation annotation) { String type = annotation.getTypeName(); Annotation[] annotations = getAnnotations(); for (int i = 0; i < annotations.length; i++) { if (annotations[i].getTypeName().equals(type)) { annotations[i] = annotation; setAnnotations(annotations); return; } } Annotation[] newlist = new Annotation[annotations.length + 1]; System.arraycopy(annotations, 0, newlist, 0, annotations.length); newlist[annotations.length] = annotation; setAnnotations(newlist); }
private static void addAnnotations(FieldInfo fieldInfo, Class<?>[] annotations) { AnnotationsAttribute annotationsAttribute = (AnnotationsAttribute) fieldInfo.getAttribute( AnnotationsAttribute.visibleTag ); if ( annotationsAttribute == null ) { annotationsAttribute = new AnnotationsAttribute( fieldInfo.getConstPool(), AnnotationsAttribute.visibleTag ); fieldInfo.addAttribute( annotationsAttribute ); } for (Class<?> annotation : annotations) { annotationsAttribute.addAnnotation( new Annotation( annotation.getName(), fieldInfo.getConstPool() ) ); } }
/** * Copies this attribute and returns a new copy. */ public AttributeInfo copy(ConstPool newCp, Map classnames) { Copier copier = new Copier(info, constPool, newCp, classnames); try { copier.annotationArray(); return new AnnotationsAttribute(newCp, getName(), copier.close()); } catch (Exception e) { throw new RuntimeException(e); } }
public Class<?> toClass() { if (mCtc != null) mCtc.detach(); long id = CLASS_NAME_COUNTER.getAndIncrement(); try { CtClass ctcs = mSuperClass == null ? null : mPool.get(mSuperClass); if (mClassName == null) mClassName = (mSuperClass == null || javassist.Modifier.isPublic(ctcs.getModifiers()) ? TccClassGenerator.class.getName() : mSuperClass + "$sc") + id; mCtc = mPool.makeClass(mClassName); if (mSuperClass != null) mCtc.setSuperclass(ctcs); mCtc.addInterface(mPool.get(DC.class.getName())); // add dynamic class tag. if (mInterfaces != null) ConstPool constpool = mCtc.getClassFile().getConstPool(); AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag); Annotation annot = new Annotation("org.mengyun.tcctransaction.api.Compensable", constpool); EnumMemberValue enumMemberValue = new EnumMemberValue(constpool); enumMemberValue.setType("org.mengyun.tcctransaction.api.Propagation"); enumMemberValue.setValue("SUPPORTS"); annot.addMemberValue("propagation", enumMemberValue); annot.addMemberValue("confirmMethod", new StringMemberValue(ctMethod.getName(), constpool)); annot.addMemberValue("cancelMethod", new StringMemberValue(ctMethod.getName(), constpool)); annot.addMemberValue("transactionContextEditor", classMemberValue); attr.addAnnotation(annot); ctMethod.getMethodInfo().addAttribute(attr);
@SneakyThrows public Proxy<I> addField(String code, Class<? extends java.lang.annotation.Annotation> annotation, Map<String, Object> annotationProperties) { return handleException(() -> { CtField ctField = CtField.make(code, ctClass); if (null != annotation) { ConstPool constPool = ctClass.getClassFile().getConstPool(); AnnotationsAttribute attributeInfo = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); Annotation ann = new javassist.bytecode.annotation.Annotation(annotation.getName(), constPool); if (null != annotationProperties) { annotationProperties.forEach((key, value) -> { MemberValue memberValue = createMemberValue(value, constPool); if (memberValue != null) { ann.addMemberValue(key, memberValue); } }); } attributeInfo.addAnnotation(ann); ctField.getFieldInfo().addAttribute(attributeInfo); } ctClass.addField(ctField); }); }
cl.setSuperclass(cp.get(cls.getName())); ClassFile ccFile = cl.getClassFile(); ConstPool constpool = ccFile.getConstPool(); AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag); Annotation annot = new Annotation("org.junit.BeforeClass", constpool); attr.addAnnotation(annot); mtd.getMethodInfo().addAttribute(attr); cl.addMethod(mtd); return cl.toClass();
clazz.defrost(); CtField[] fieldsToCopy = template.getDeclaredFields(); for (CtField field : fieldsToCopy) { if (field.hasAnnotation(NonCopied.class)) { logger.debug(String.format("Copying annotation from field [%s]", field.getName())); ConstPool constPool = clazz.getClassFile().getConstPool(); CtField fieldFromMainClass = clazz.getField(field.getName()); for (Object o : field.getFieldInfo().getAttributes()) { if (o instanceof AnnotationsAttribute) { AnnotationsAttribute templateAnnotations = (AnnotationsAttribute) o; AnnotationsAttribute copied = (AnnotationsAttribute) templateAnnotations.copy(constPool, null); for (Object attribute : fieldFromMainClass.getFieldInfo().getAttributes()) { if (attribute instanceof AnnotationsAttribute) { for (Annotation annotation : copied.getAnnotations()) { ((AnnotationsAttribute) attribute).addAnnotation(annotation);
public static void createAnnotation(CtClass ctClass, Class<? extends Annotation> annotationType, Map<String, MemberValue> members) { if (ctClass.hasAnnotation(annotationType)) return; AnnotationsAttribute attr = new AnnotationsAttribute(ctClass.getClassFile().getConstPool(), AnnotationsAttribute.visibleTag); boolean isNewAttr = true; List<AttributeInfo> attributeInfos = ctClass.getClassFile2().getAttributes(); for (AttributeInfo attributeInfo : attributeInfos) { if (attributeInfo instanceof AnnotationsAttribute) { javassist.bytecode.annotation.Annotation[] annotations = attr.getAnnotations(); javassist.bytecode.annotation.Annotation annotation = new javassist.bytecode.annotation.Annotation(annotationType.getName(), attr.getConstPool()); if (annotation1.getTypeName().equals(annotationType.getName())) { annotation = annotation1; break; if (annotation.getMemberValue(member.getKey()) == null) { annotation.addMemberValue(member.getKey(), member.getValue()); attr.addAnnotation(annotation); ctClass.getClassFile().addAttribute(attr);
ClassFile classFile = new ClassFile(new DataInputStream(new ByteArrayInputStream(classfileBuffer))); ConstPool constantPool = classFile.getConstPool(); AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(constantPool, AnnotationsAttribute.visibleTag); List<?> attributes = classFile.getAttributes(); Iterator<?> itr = attributes.iterator(); while(itr.hasNext()) { if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) { AnnotationsAttribute attr = (AnnotationsAttribute) object; Annotation[] items = attr.getAnnotations(); for (Annotation annotation : items) { String typeName = annotation.getTypeName(); if (!typeName.equals(Inheritance.class.getName())) { annotationsAttribute.addAnnotation(annotation); Annotation inheritance = new Annotation(Inheritance.class.getName(), constantPool); ClassPool pool = ClassPool.getDefault(); 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()); inheritance.addMemberValue("strategy", strategy); annotationsAttribute.addAnnotation(inheritance); if (myInfo.getDiscriminatorName() != null) { Annotation discriminator = new Annotation(DiscriminatorColumn.class.getName(), constantPool); annotationsAttribute.addAnnotation(discriminator);
@Override public void enhance(CtField field, CtClass clz) { AnnotationsAttribute aa = (AnnotationsAttribute)field.getFieldInfo().getAttribute(AnnotationsAttribute.visibleTag); if (null == aa) { aa = new AnnotationsAttribute(clz.getClassFile().getConstPool(), AnnotationsAttribute.visibleTag); field.getFieldInfo().addAttribute(aa); } Annotation ann = new Annotation("controllers.CRUD$Hidden", clz.getClassFile().getConstPool()); aa.addAnnotation(ann); } }
final ClassFile classFile = new ClassFile(classInputStream); final String className = classFile.getName(); final AnnotationsAttribute annotationsAttribute = (AnnotationsAttribute) classFile.getAttribute(AnnotationsAttribute.visibleTag); if (null == annotationsAttribute) { LOGGER.log(Level.TRACE, "The class [name={0}] is not a bean", className); final ConstPool constPool = classFile.getConstPool(); final Annotation[] annotations = annotationsAttribute.getAnnotations(); boolean maybeBeanClass = false; for (final Annotation annotation : annotations) { final String typeName = annotation.getTypeName(); if (typeName.equals(Singleton.class.getName())) { maybeBeanClass = true; final Annotation singletonAnnotation = new Annotation(Singleton.class.getName(), constPool); annotationsAttribute.addAnnotation(singletonAnnotation); classFile.addAttribute(annotationsAttribute); classFile.setVersionToJava5();
@Override public DynamicField text(boolean own) { Annotation annot = new Annotation(Text.class.getName(), cpool); annot.addMemberValue("own", new BooleanMemberValue(own, cpool)); attr.addAnnotation(annot); return this; }
private static void addAnnotation(final CtClass clazz, final String fieldName, final String annotationName, String member, int memberValue) throws Exception { final ClassFile cfile = clazz.getClassFile(); final ConstPool cpool = cfile.getConstPool(); final CtField cfield = clazz.getField(fieldName); final AnnotationsAttribute attr = new AnnotationsAttribute(cpool, AnnotationsAttribute.visibleTag); final Annotation annot = new Annotation(annotationName, cpool); annot.addMemberValue(member, new IntegerMemberValue(cpool, memberValue)); attr.addAnnotation(annot); cfield.getFieldInfo().addAttribute(attr); }
private static void annotateMethod(CtBehavior method, Annotation annotation) { ClassFile clsFile = method.getDeclaringClass().getClassFile(); ConstPool constPool = clsFile.getConstPool(); MethodInfo methodInfo = method.getMethodInfo2(); AnnotationsAttribute attr = (AnnotationsAttribute) methodInfo.getAttribute(AnnotationsAttribute.visibleTag); if (attr == null) { attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); methodInfo.addAttribute(attr); } attr.addAnnotation(annotation); }
private static void createAnchorConstructor(final CtClass impl, final CtClass anchor) throws Exception { final ClassPool classPool = impl.getClassPool(); final CtConstructor ctConstructor = CtNewConstructor.make( new CtClass[]{anchor}, null, CtNewConstructor.PASS_NONE, null, null, impl); final ConstPool constPool = impl.getClassFile().getConstPool(); final MethodInfo methodInfo = ctConstructor.getMethodInfo(); // add injection annotation final AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); final Annotation annotation = new Annotation(constPool, classPool.get(javax.inject.Inject.class.getName())); attr.addAnnotation(annotation); methodInfo.addAttribute(attr); impl.addConstructor(ctConstructor); }
public static void createAnnotation(CtField ctField, Class<? extends Annotation> annotationType, Map<String, MemberValue> members) { if (ctField.hasAnnotation(annotationType)) return; AnnotationsAttribute attr = new AnnotationsAttribute(ctField.getFieldInfo().getConstPool(), AnnotationsAttribute.visibleTag); boolean isNewAttr = true; List<AttributeInfo> attributeInfos = ctField.getFieldInfo().getAttributes(); for (AttributeInfo attributeInfo : attributeInfos) { if (attributeInfo instanceof AnnotationsAttribute) { javassist.bytecode.annotation.Annotation[] annotations = attr.getAnnotations(); javassist.bytecode.annotation.Annotation annotation = new javassist.bytecode.annotation.Annotation(annotationType.getName(), attr.getConstPool()); if (annotation1.getTypeName().equals(annotationType.getName())) { annotation = annotation1; break; if (annotation.getMemberValue(member.getKey()) == null) { annotation.addMemberValue(member.getKey(), member.getValue()); attr.addAnnotation(annotation); if (isNewAttr) { ctField.getFieldInfo().addAttribute(attr);
/* package private */ void markAsProcessed(final CtClass ctClass) { final ClassFile classFile = ctClass.getClassFile(); for (final Object attributeObject : classFile.getAttributes()) { if (attributeObject instanceof AnnotationsAttribute) { final AnnotationsAttribute annotationAttribute = (AnnotationsAttribute) attributeObject; final javassist.bytecode.annotation.Annotation annotation = annotationAttribute.getAnnotation(PROCESSED_ANNOTATION_CLASS); if (annotation != null) { return; } } } final javassist.bytecode.annotation.Annotation annotation = new javassist.bytecode.annotation.Annotation(PROCESSED_ANNOTATION_CLASS, classFile.getConstPool()); final AnnotationsAttribute annotationAttribute = new AnnotationsAttribute(classFile.getConstPool(), AnnotationsAttribute.visibleTag); annotationAttribute.addAnnotation(annotation); classFile.addAttribute(annotationAttribute); }