public void testJIRA256() throws Exception { // CtClass ec = sloader.get("test5.Entity"); CtClass cc = sloader.makeClass("test5.JIRA256"); ClassFile ccFile = cc.getClassFile(); ConstPool constpool = ccFile.getConstPool(); AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag); javassist.bytecode.annotation.Annotation entityAnno = new javassist.bytecode.annotation.Annotation("test5.Entity", constpool); // = new javassist.bytecode.annotation.Annotation(constpool, ec); entityAnno.addMemberValue( "value", new javassist.bytecode.annotation.ArrayMemberValue(constpool)); attr.addAnnotation(entityAnno); ccFile.addAttribute(attr); cc.writeFile(); Object o = make(cc.getName()); assertTrue(o.getClass().getName().equals("test5.JIRA256")); java.lang.annotation.Annotation[] annotations = o.getClass().getDeclaredAnnotations(); assertEquals(1, annotations.length); }
protected void buildClassLevelAnnotations( ClassFile classFile, ClassFile templateClassFile, ConstPool constantPool) throws NotFoundException { List<?> templateAttributes = templateClassFile.getAttributes(); Iterator<?> templateItr = templateAttributes.iterator(); Annotation templateEntityListeners = null; while (templateItr.hasNext()) { Object object = templateItr.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(EntityListeners.class.getName())) { templateEntityListeners = annotation; } } } } if (templateEntityListeners != null) { AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(constantPool, AnnotationsAttribute.visibleTag); List<?> attributes = classFile.getAttributes(); Iterator<?> itr = attributes.iterator(); Annotation existingEntityListeners = null; 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(EntityListeners.class.getName())) { logger.debug( "Stripping out previous EntityListeners annotation at the class level - will merge into new EntityListeners"); existingEntityListeners = annotation; continue; } annotationsAttribute.addAnnotation(annotation); } itr.remove(); } } Annotation entityListeners = getEntityListeners(constantPool, existingEntityListeners, templateEntityListeners); annotationsAttribute.addAnnotation(entityListeners); classFile.addAttribute(annotationsAttribute); } }
public byte[] transform( ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { if (infos.isEmpty()) { return null; } String convertedClassName = className.replace('/', '.'); SingleTableInheritanceInfo key = new SingleTableInheritanceInfo(); key.setClassName(convertedClassName); int pos = infos.indexOf(key); if (pos >= 0) { try { if (LOG.isDebugEnabled()) { LOG.debug("Converting " + convertedClassName + " to a SingleTable inheritance strategy."); } SingleTableInheritanceInfo myInfo = infos.get(pos); 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()) { 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(Inheritance.class.getName()) && ((myInfo.getDiscriminatorName() == null && !typeName.equals(Table.class.getName())) || myInfo.getDiscriminatorName() != null)) { annotationsAttribute.addAnnotation(annotation); } } itr.remove(); } } 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); StringMemberValue name = new StringMemberValue(constantPool); 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()); discriminator.addMemberValue("discriminatorType", discriminatorType); IntegerMemberValue length = new IntegerMemberValue(constantPool); length.setValue(myInfo.getDiscriminatorLength()); discriminator.addMemberValue("length", length); annotationsAttribute.addAnnotation(discriminator); } classFile.addAttribute(annotationsAttribute); ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream os = new DataOutputStream(bos); classFile.write(os); os.close(); return bos.toByteArray(); } catch (Exception ex) { ex.printStackTrace(); throw new IllegalClassFormatException( "Unable to convert " + convertedClassName + " to a SingleTable inheritance strategy: " + ex.getMessage()); } } else { return null; } }
@Override protected void addAttribute(AnnotationsAttribute annotationsAttribute) { classFile.addAttribute(annotationsAttribute); }