use of javassist.bytecode.ClassFile in project javassist-maven-plugin by icon-Systemhaus-GmbH.
the class JavassistTransformerExecutorTestBase method stampedClass.
protected CtClass stampedClass(final String className, final CtClass ctClass) throws CannotCompileException, NotFoundException {
expect(ctClass.getDeclaredField(startsWith(STAMP_FIELD_NAME))).andReturn(null);
// real stamping
expect(ctClass.isInterface()).andReturn(false);
expect(ctClass.getClassFile2()).andReturn(new ClassFile(false, className, null));
expect(ctClass.isFrozen()).andReturn(false);
expect(ctClass.getName()).andReturn(className).anyTimes();
ctClass.addField(anyObject(CtField.class), anyObject(CtField.Initializer.class));
return ctClass;
}
use of javassist.bytecode.ClassFile in project javassist-maven-plugin by icon-Systemhaus-GmbH.
the class TestJavassistTransformerExecuter method applyStamp_on_Interface.
@Test
public void applyStamp_on_Interface() 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(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();
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 | PUBLIC));
assertThat("generated stamp field is a boolean.", fieldCaptures.getValue().getType(), is(booleanType));
}
use of javassist.bytecode.ClassFile in project newton by muoncore.
the class EnableNewtonRegistrar method makeRepo.
private Class makeRepo(Class param) {
ClassPool defaultClassPool = ClassPool.getDefault();
defaultClassPool.appendClassPath(new LoaderClassPath(param.getClassLoader()));
defaultClassPool.appendClassPath(new LoaderClassPath(MuonEventSourceRepository.class.getClassLoader()));
defaultClassPool.appendSystemPath();
try {
CtClass superInterface = defaultClassPool.getCtClass(MuonEventSourceRepository.class.getName());
String repoName = param.getName() + "Repository";
try {
return Class.forName(repoName);
} catch (ClassNotFoundException e) {
CtClass repositoryInterface = defaultClassPool.makeClass(repoName, superInterface);
ClassFile classFile = repositoryInterface.getClassFile();
String sig = "Ljava/lang/Object;Lio/muoncore/newton/eventsource/muon/MuonEventSourceRepository<L" + getSigName(param) + ";>;";
SignatureAttribute signatureAttribute = new SignatureAttribute(classFile.getConstPool(), sig);
classFile.addAttribute(signatureAttribute);
return repositoryInterface.toClass();
}
} catch (NotFoundException | CannotCompileException e) {
log.error("Unable to register a newton repository", e);
}
return null;
}
use of javassist.bytecode.ClassFile in project OpenTripPlanner by opentripplanner.
the class ClassCustomizer method addDoubleField.
/**
* Adds a new field of type double to the customized class
*/
public void addDoubleField(String fieldName) {
// FIXME: this should support default values but does not
ClassFile classFile = ctClass.getClassFile();
ConstPool constPool = classFile.getConstPool();
try {
// add field
FieldInfo fieldInfo = new FieldInfo(constPool, fieldName, "D");
classFile.addField(fieldInfo);
CtConstructor ctor = CtNewConstructor.defaultConstructor(ctClass);
ctClass.addConstructor(ctor);
addDoubleSetter(classFile, fieldName);
addDoubleGetter(classFile, fieldName);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of javassist.bytecode.ClassFile in project OpenTripPlanner by opentripplanner.
the class ClassCustomizer method saveClass.
/**
* Writes the class file to the classpath and returns a class object
*/
public Class<?> saveClass() {
ClassFile classFile = ctClass.getClassFile();
try {
if (!extraClassPath.exists()) {
extraClassPath.mkdirs();
}
FactoryHelper.writeFile(classFile, extraClassPath.getPath());
ClassLoader loader = getClass().getClassLoader();
Class<?> cls = FactoryHelper.toClass(classFile, loader);
return cls;
// load class
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations