Search in sources :

Example 21 with ClassFile

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;
}
Also used : ClassFile(javassist.bytecode.ClassFile) CtField(javassist.CtField)

Example 22 with ClassFile

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));
}
Also used : CtClass(javassist.CtClass) ClassFile(javassist.bytecode.ClassFile) CtField(javassist.CtField) Test(org.junit.Test)

Example 23 with ClassFile

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;
}
Also used : SignatureAttribute(javassist.bytecode.SignatureAttribute) ClassFile(javassist.bytecode.ClassFile) MuonEventSourceRepository(io.muoncore.newton.eventsource.muon.MuonEventSourceRepository)

Example 24 with ClassFile

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);
    }
}
Also used : ConstPool(javassist.bytecode.ConstPool) ClassFile(javassist.bytecode.ClassFile) FieldInfo(javassist.bytecode.FieldInfo) DuplicateMemberException(javassist.bytecode.DuplicateMemberException) CtConstructor(javassist.CtConstructor)

Example 25 with ClassFile

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);
    }
}
Also used : ClassFile(javassist.bytecode.ClassFile) DuplicateMemberException(javassist.bytecode.DuplicateMemberException)

Aggregations

ClassFile (javassist.bytecode.ClassFile)51 DataInputStream (java.io.DataInputStream)15 ClassPool (javassist.ClassPool)15 DataOutputStream (java.io.DataOutputStream)14 CtClass (javassist.CtClass)14 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)14 ConstPool (javassist.bytecode.ConstPool)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 IOException (java.io.IOException)11 MethodInfo (javassist.bytecode.MethodInfo)11 CtField (javassist.CtField)10 NotFoundException (javassist.NotFoundException)9 Annotation (javassist.bytecode.annotation.Annotation)9 Test (org.junit.Test)9 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)8 HashSet (java.util.HashSet)7 CtMethod (javassist.CtMethod)7 FieldInfo (javassist.bytecode.FieldInfo)7 Bytecode (javassist.bytecode.Bytecode)5