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; } }
private URL computeResourceURL(Method method) throws NotFoundException, URISyntaxException { List<ResourceFileEntry> filesSimpleNames = new ArrayList<ResourceFileEntry>(); boolean computeExtensions = false; CtMethod m = ctClass.getMethod(method.getName(), getDescriptor(method)); MethodInfo minfo = m.getMethodInfo2(); AnnotationsAttribute attr = (AnnotationsAttribute) minfo.getAttribute(AnnotationsAttribute.invisibleTag); if (attr != null) { Annotation an = attr.getAnnotation(Source.class.getName()); if (an != null) { MemberValue[] mvArray = ((ArrayMemberValue) an.getMemberValue("value")).getValue(); if (mvArray != null) { for (MemberValue mv : mvArray) { StringMemberValue smv = (StringMemberValue) mv; filesSimpleNames.add(new ResourceFileEntry(smv.getValue(), m)); } } } } if (filesSimpleNames.isEmpty()) { // no @Source annotation detected filesSimpleNames.add(new ResourceFileEntry(method.getName(), m)); computeExtensions = true; } List<URL> existingFiles = new ArrayList<URL>(); for (ResourceFileEntry resourceEntry : filesSimpleNames) { String resourceName = resourceEntry.resourceName; CtClass declaringClass = resourceEntry.resourceMethod.getDeclaringClass(); String baseDir = declaringClass.getPackageName().replaceAll("\\.", "/") + "/"; String fileName = (resourceName.startsWith(baseDir)) ? resourceName : baseDir + resourceName; if (computeExtensions) { String[] extensions = getResourceDefaultExtensions(method); for (String extension : extensions) { String possibleFile = fileName + extension; URL url = GwtPatcher.class.getClassLoader().getResource(possibleFile); if (url != null) { existingFiles.add(url); } } } else { URL url = GwtPatcher.class.getClassLoader().getResource(fileName); if (url != null) { existingFiles.add(url); } } } if (existingFiles.isEmpty()) { throw new RuntimeException( "No resource file found for method " + ctClass.getSimpleName() + "." + method.getName() + "()"); } else if (existingFiles.size() > 1) { throw new RuntimeException( "Too many resource files found for method " + ctClass.getSimpleName() + "." + method.getName() + "()"); } return existingFiles.get(0); }