use of javassist.bytecode.ClassFile in project BroadleafCommerce by BroadleafCommerce.
the class QueryConfigurationClassTransformer method transform.
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
if (className == null || isExecuted) {
return null;
}
String convertedClassName = className.replace('/', '.');
if (managedClassNames.contains(convertedClassName)) {
try {
ClassFile classFile = new ClassFile(new DataInputStream(new ByteArrayInputStream(classfileBuffer)));
if (isExecuted) {
return null;
}
ConstPool constantPool = classFile.getConstPool();
ClassPool pool = ClassPool.getDefault();
pool.importPackage("javax.persistence");
pool.importPackage("java.lang");
List<?> attributes = classFile.getAttributes();
Iterator<?> itr = attributes.iterator();
while (itr.hasNext()) {
Object object = itr.next();
processClassLevelAnnotations(constantPool, pool, object);
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(bos);
classFile.write(os);
os.close();
isExecuted = true;
return bos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
throw new IllegalClassFormatException("Unable to convert " + convertedClassName + " to a SingleTable inheritance strategy: " + e.getMessage());
}
} else {
return null;
}
}
use of javassist.bytecode.ClassFile in project javassist-maven-plugin by icon-Systemhaus-GmbH.
the class TestJavassistTransformerExecuter method applyStamp_on_Class.
@Test
public void applyStamp_on_Class() throws CannotCompileException, NotFoundException {
// given
final String className = "test.TestClass";
final CtClass candidateClass = mock("candidateClass", CtClass.class);
final Capture<CtField> fieldCaptures = newInstance();
// createStampField
expect(candidateClass.isInterface()).andReturn(false);
expect(candidateClass.getClassPool()).andReturn(classPool).anyTimes();
expect(candidateClass.getClassFile2()).andReturn(new ClassFile(false, className, null));
expect(candidateClass.isFrozen()).andReturn(false);
expect(candidateClass.getName()).andReturn(className).anyTimes();
candidateClass.addField(capture(fieldCaptures), anyObject(CtField.Initializer.class));
replay(candidateClass, classPool);
// when
sut.applyStamp(candidateClass);
// then
verify(candidateClass, classPool);
assertThat("generated stamp field starts with the constant prefix.", fieldCaptures.getValue().getName(), org.hamcrest.core.StringStartsWith.startsWith(JavassistTransformerExecutor.STAMP_FIELD_NAME));
assertThat("generated stamp field must have the right modifiers.", fieldCaptures.getValue().getModifiers(), is(STATIC | FINAL | PRIVATE));
assertThat("generated stamp field is a boolean.", fieldCaptures.getValue().getType(), is(booleanType));
}
use of javassist.bytecode.ClassFile in project javassist-maven-plugin by icon-Systemhaus-GmbH.
the class TestJavassistTransformerExecuter method removeStamp_on_Class.
@Test
public void removeStamp_on_Class() throws CannotCompileException, NotFoundException {
// given
final String className = "test.TestClass";
final CtClass candidateClass = mock("candidateClass", CtClass.class);
// createStampField
expect(candidateClass.isInterface()).andReturn(false);
expect(candidateClass.getClassPool()).andReturn(classPool).anyTimes();
expect(candidateClass.getClassFile2()).andReturn(new ClassFile(false, className, null));
expect(candidateClass.isFrozen()).andReturn(false);
expect(candidateClass.getName()).andReturn(className).anyTimes();
final Capture<CtField> fieldCaptures = newInstance();
candidateClass.removeField(capture(fieldCaptures));
replay(candidateClass, classPool);
// when
sut.removeStamp(candidateClass);
// then
verify(candidateClass, classPool);
assertThat("generated stamp field starts with the constant prefix.", fieldCaptures.getValue().getName(), org.hamcrest.core.StringStartsWith.startsWith(JavassistTransformerExecutor.STAMP_FIELD_NAME));
assertThat("generated stamp field must have the right modifiers.", fieldCaptures.getValue().getModifiers(), is(STATIC | FINAL | PRIVATE));
assertThat("generated stamp field is a boolean.", fieldCaptures.getValue().getType(), is(booleanType));
}
use of javassist.bytecode.ClassFile in project javassist-maven-plugin by icon-Systemhaus-GmbH.
the class TestJavassistTransformerExecuter method removeStamp_on_Interface.
@Test
public void removeStamp_on_Interface() throws CannotCompileException, NotFoundException {
// given
final String className = "test.TestClass";
final CtClass candidateClass = mock("candidateClass", CtClass.class);
// createStampField
expect(candidateClass.isInterface()).andReturn(true);
expect(candidateClass.getClassPool()).andReturn(classPool).anyTimes();
expect(candidateClass.getClassFile2()).andReturn(new ClassFile(false, className, null));
expect(candidateClass.isFrozen()).andReturn(false);
expect(candidateClass.getName()).andReturn(className).anyTimes();
final Capture<CtField> fieldCaptures = newInstance();
candidateClass.removeField(capture(fieldCaptures));
replay(candidateClass, classPool);
// when
sut.removeStamp(candidateClass);
// then
verify(candidateClass, classPool);
assertThat("generated stamp field starts with the constant prefix.", fieldCaptures.getValue().getName(), org.hamcrest.core.StringStartsWith.startsWith(JavassistTransformerExecutor.STAMP_FIELD_NAME));
assertThat("generated stamp field must have the right modifiers.", fieldCaptures.getValue().getModifiers(), is(STATIC | FINAL | PUBLIC));
assertThat("generated stamp field is a boolean.", fieldCaptures.getValue().getType(), is(booleanType));
}
use of javassist.bytecode.ClassFile in project javassist-maven-plugin by icon-Systemhaus-GmbH.
the class TestJavassistTransformerExecuter method removeStamp_ignores_internal_NotFoundException.
@Test
public void removeStamp_ignores_internal_NotFoundException() throws CannotCompileException, NotFoundException {
// given
final String className = "test.TestClass";
final NotFoundException internalException = new NotFoundException("expected exception");
final CtClass candidateClass = mock("candidateClass", CtClass.class);
// createStampField
expect(candidateClass.isInterface()).andReturn(false);
expect(candidateClass.getClassPool()).andReturn(classPool).anyTimes();
expect(candidateClass.getClassFile2()).andReturn(new ClassFile(false, className, null));
expect(candidateClass.isFrozen()).andReturn(false);
expect(candidateClass.getName()).andReturn(className).anyTimes();
final Capture<CtField> fieldCaptures = newInstance();
candidateClass.removeField(capture(fieldCaptures));
EasyMock.expectLastCall().andThrow(internalException);
replay(candidateClass, classPool);
// when
sut.removeStamp(candidateClass);
// then
verify(candidateClass, classPool);
assertThat("generated stamp field starts with the constant prefix.", fieldCaptures.getValue().getName(), org.hamcrest.core.StringStartsWith.startsWith(JavassistTransformerExecutor.STAMP_FIELD_NAME));
assertThat("generated stamp field must have the right modifiers.", fieldCaptures.getValue().getModifiers(), is(STATIC | FINAL | PRIVATE));
assertThat("generated stamp field is a boolean.", fieldCaptures.getValue().getType(), is(booleanType));
}
Aggregations