use of javassist.bytecode.ClassFile in project BroadleafCommerce by BroadleafCommerce.
the class AlterTableNameClassTransformer method transform.
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
// Lambdas and anonymous methods in Java 8 do not have a class name defined and so no transformation should be done
if (className == null || StringUtils.isBlank(getTargetedClass()) || StringUtils.isBlank(getTableName())) {
return null;
}
byte[] classBytes = null;
String convertedClassName = className.replace('/', '.');
if (convertedClassName.equalsIgnoreCase(getTargetedClass())) {
try {
String targetValue = getTableName();
if (LOG.isDebugEnabled()) {
LOG.debug("Altering " + convertedClassName + " table name");
}
ClassFile classFile = new ClassFile(new DataInputStream(new ByteArrayInputStream(classfileBuffer)));
ConstPool constantPool = classFile.getConstPool();
alterTableAnnotation(classFile, targetValue, constantPool);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(bos);
classFile.write(os);
os.close();
classBytes = bos.toByteArray();
} catch (Exception ex) {
ex.printStackTrace();
throw new IllegalClassFormatException("Unable to convert " + convertedClassName + " to a SingleTable inheritance strategy: " + ex.getMessage());
}
}
return classBytes;
}
use of javassist.bytecode.ClassFile in project BroadleafCommerce by BroadleafCommerce.
the class EntityMarkerClassTransformer method transform.
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
// Lambdas and anonymous methods in Java 8 do not have a class name defined and so no transformation should be done
if (className == null) {
return null;
}
String convertedClassName = className.replace('/', '.');
if (isIgnored(convertedClassName)) {
return null;
}
try {
ClassFile classFile = new ClassFile(new DataInputStream(new ByteArrayInputStream(classfileBuffer)));
List<?> attributes = classFile.getAttributes();
Iterator<?> itr = attributes.iterator();
while (itr.hasNext()) {
Object object = itr.next();
if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) {
boolean containsTypeLevelAnnotation = containsTypeLevelPersistenceAnnotation(((AnnotationsAttribute) object).getAnnotations());
if (containsTypeLevelAnnotation) {
LOG.debug("Marking " + convertedClassName + " as transformed");
transformedEntityClassNames.add(convertedClassName);
} else {
LOG.debug("Marking " + convertedClassName + " as picked up by the transformer but not detected as an entity");
transformedNonEntityClassNames.add(convertedClassName);
}
}
}
} catch (Exception e) {
LOG.error(e);
throw new IllegalClassFormatException("Unable to mark " + convertedClassName + " as transformed.");
}
// We don't need to transform anything, so we'll return null
return null;
}
use of javassist.bytecode.ClassFile in project BroadleafCommerce by BroadleafCommerce.
the class MaterializedClobTypeClassTransformer method transform.
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
if (className == null) {
return null;
}
String convertedClassName = className.replace('/', '.');
if (isIgnored(convertedClassName)) {
return null;
}
try {
boolean transformed = false;
ClassFile classFile = new ClassFile(new DataInputStream(new ByteArrayInputStream(classfileBuffer)));
boolean containsTypeLevelAnnotation = false;
{
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());
}
}
}
if (containsTypeLevelAnnotation) {
List<FieldInfo> fieldInfos = classFile.getFields();
ConstPool constantPool = classFile.getConstPool();
for (FieldInfo myField : fieldInfos) {
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);
type.setValue(MaterializedClobType.class.getName());
clobType.addMemberValue("type", type);
annotationsAttribute.addAnnotation(clobType);
transformed = true;
} else {
annotationsAttribute.addAnnotation(annotation);
}
} else {
annotationsAttribute.addAnnotation(annotation);
}
}
if (transformed) {
itr.remove();
}
}
}
if (transformed) {
myField.addAttribute(annotationsAttribute);
}
}
}
if (transformed) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(bos);
classFile.write(os);
os.close();
return bos.toByteArray();
} else {
return null;
}
} catch (Exception ex) {
ex.printStackTrace();
throw new IllegalClassFormatException("Unable to convert " + convertedClassName + " to sandbox: " + ex.getMessage());
}
}
use of javassist.bytecode.ClassFile in project BroadleafCommerce by BroadleafCommerce.
the class SingleTableInheritanceClassTransformer method transform.
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
// Lambdas and anonymous methods in Java 8 do not have a class name defined and so no transformation should be done
if (className == null) {
return null;
}
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())) {
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;
}
}
use of javassist.bytecode.ClassFile in project BroadleafCommerce by BroadleafCommerce.
the class RemoveAnnotationClassTransformer method transform.
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
// Lambdas and anonymous methods in Java 8 do not have a class name defined and so no transformation should be done
if (className == null) {
return null;
}
// Be careful with Apache library usage in this class (e.g. ArrayUtils). Usage will likely cause a ClassCircularityError
// under JRebel. Favor not including outside libraries and unnecessary classes.
CtClass clazz = null;
try {
String convertedClassName = className.replace('/', '.');
if (!classNames.isEmpty() && classNames.contains(convertedClassName) && (conditionalPropertyName == null || isPropertyEnabled(conditionalPropertyName))) {
ClassPool classPool = ClassPool.getDefault();
clazz = classPool.makeClass(new ByteArrayInputStream(classfileBuffer), false);
clazz.defrost();
ClassFile classFile = clazz.getClassFile();
ConstPool constantPool = classFile.getConstPool();
{
List<?> attributes = classFile.getAttributes();
AnnotationsAttribute annotationsAttribute = stripAnnotation(constantPool, attributes);
classFile.addAttribute(annotationsAttribute);
}
{
List<FieldInfo> fieldInfos = classFile.getFields();
for (FieldInfo myField : fieldInfos) {
List<?> attributes = myField.getAttributes();
AnnotationsAttribute annotationsAttribute = stripAnnotation(constantPool, attributes);
myField.addAttribute(annotationsAttribute);
}
}
return clazz.toBytecode();
}
} catch (ClassCircularityError error) {
error.printStackTrace();
throw error;
} catch (Exception e) {
throw new RuntimeException("Unable to transform class", e);
} finally {
if (clazz != null) {
try {
clazz.detach();
} catch (Exception e) {
// do nothing
}
}
}
return null;
}
Aggregations