Search in sources :

Example 1 with CannotCompileException

use of javassist.CannotCompileException in project pinpoint by naver.

the class JavassistClass method weave.

@Override
public void weave(String adviceClassName) throws InstrumentException {
    pluginContext.injectClass(classLoader, adviceClassName);
    CtClass adviceClass;
    try {
        adviceClass = getCtClass(adviceClassName);
    } catch (NotFoundException e) {
        throw new NotFoundInstrumentException(adviceClassName + " not found. Caused:" + e.getMessage(), e);
    }
    try {
        AspectWeaverClass weaverClass = new AspectWeaverClass();
        weaverClass.weaving(ctClass, adviceClass);
    } catch (CannotCompileException e) {
        throw new InstrumentException("weaving fail. sourceClassName:" + ctClass.getName() + " adviceClassName:" + adviceClassName + " Caused:" + e.getMessage(), e);
    } catch (NotFoundException e) {
        throw new InstrumentException("weaving fail. sourceClassName:" + ctClass.getName() + " adviceClassName:" + adviceClassName + " Caused:" + e.getMessage(), e);
    }
}
Also used : CtClass(javassist.CtClass) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) AspectWeaverClass(com.navercorp.pinpoint.profiler.instrument.aspect.AspectWeaverClass)

Example 2 with CannotCompileException

use of javassist.CannotCompileException in project hibernate-orm by hibernate.

the class FieldWriter method addWithModifiers.

private static void addWithModifiers(CtClass target, CtClass type, String name, int modifiers, Class<?>... annotations) {
    try {
        final CtField f = new CtField(type, name, target);
        f.setModifiers(f.getModifiers() | modifiers);
        addAnnotations(f.getFieldInfo(), annotations);
        target.addField(f);
    } catch (CannotCompileException cce) {
        final String msg = String.format("Could not enhance class [%s] to add field [%s]", target.getName(), name);
        throw new EnhancementException(msg, cce);
    }
}
Also used : CtField(javassist.CtField) EnhancementException(org.hibernate.bytecode.enhance.spi.EnhancementException) CannotCompileException(javassist.CannotCompileException)

Example 3 with CannotCompileException

use of javassist.CannotCompileException in project hibernate-orm by hibernate.

the class MethodWriter method addSetter.

public static CtMethod addSetter(CtClass target, String field, String name) {
    CtField actualField = null;
    try {
        actualField = target.getField(field);
        log.debugf("Writing setter method [%s] into [%s] for field [%s]", name, target.getName(), field);
        CtMethod method = CtNewMethod.setter(name, actualField);
        target.addMethod(method);
        return method;
    } catch (CannotCompileException cce) {
        try {
            // Fall back to create a getter from delegation.
            CtMethod method = CtNewMethod.delegator(CtNewMethod.setter(name, actualField), target);
            target.addMethod(method);
            return method;
        } catch (CannotCompileException ignored) {
            String msg = String.format("Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field);
            throw new EnhancementException(msg, cce);
        }
    } catch (NotFoundException nfe) {
        String msg = String.format("Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field);
        throw new EnhancementException(msg, nfe);
    }
}
Also used : CtField(javassist.CtField) EnhancementException(org.hibernate.bytecode.enhance.spi.EnhancementException) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) CtMethod(javassist.CtMethod)

Example 4 with CannotCompileException

use of javassist.CannotCompileException in project ignite by apache.

the class GridActivationCacheAbstractTestSuit method transform.

/**
 * @param c Class to transform.
 * @return Transformed class.
 */
private static Class transform(Class c) {
    try {
        ClassPool pool = ClassPool.getDefault();
        pool.insertClassPath(new ClassClassPath(GridActivationCacheAbstractTestSuit.class));
        String path = c.getProtectionDomain().getCodeSource().getLocation().getPath();
        CtClass ct = pool.get(c.getName());
        String name = c.getName() + SUFFIX;
        CtClass pr = pool.get(GridActivateExtensionTest.class.getName());
        pr.setName(name);
        pr.setSuperclass(ct);
        pr.writeFile(path);
        return Class.forName(name);
    } catch (IOException e) {
        System.out.println("Io exception: " + e.getMessage());
        throw new IgniteException(e);
    } catch (CannotCompileException e) {
        System.out.println("Cannot compile exception: " + e.getMessage());
        throw new IgniteException(e);
    } catch (NotFoundException e) {
        System.out.println("Not found exception: " + e.getMessage());
        throw new IgniteException(e);
    } catch (ClassNotFoundException e) {
        System.out.println("Class not found exception: " + e.getMessage());
        throw new IgniteException(e);
    }
}
Also used : CtClass(javassist.CtClass) IgniteException(org.apache.ignite.IgniteException) ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException) IOException(java.io.IOException) CannotCompileException(javassist.CannotCompileException) ClassClassPath(javassist.ClassClassPath)

Example 5 with CannotCompileException

use of javassist.CannotCompileException in project dubbo by alibaba.

the class ClassGenerator method toClass.

public Class<?> toClass(ClassLoader loader, ProtectionDomain pd) {
    if (mCtc != null)
        mCtc.detach();
    long id = CLASS_NAME_COUNTER.getAndIncrement();
    try {
        CtClass ctcs = mSuperClass == null ? null : mPool.get(mSuperClass);
        if (mClassName == null)
            mClassName = (mSuperClass == null || javassist.Modifier.isPublic(ctcs.getModifiers()) ? ClassGenerator.class.getName() : mSuperClass + "$sc") + id;
        mCtc = mPool.makeClass(mClassName);
        if (mSuperClass != null)
            mCtc.setSuperclass(ctcs);
        // add dynamic class tag.
        mCtc.addInterface(mPool.get(DC.class.getName()));
        if (mInterfaces != null)
            for (String cl : mInterfaces) mCtc.addInterface(mPool.get(cl));
        if (mFields != null)
            for (String code : mFields) mCtc.addField(CtField.make(code, mCtc));
        if (mMethods != null) {
            for (String code : mMethods) {
                if (code.charAt(0) == ':')
                    mCtc.addMethod(CtNewMethod.copy(getCtMethod(mCopyMethods.get(code.substring(1))), code.substring(1, code.indexOf('(')), mCtc, null));
                else
                    mCtc.addMethod(CtNewMethod.make(code, mCtc));
            }
        }
        if (mDefaultConstructor)
            mCtc.addConstructor(CtNewConstructor.defaultConstructor(mCtc));
        if (mConstructors != null) {
            for (String code : mConstructors) {
                if (code.charAt(0) == ':') {
                    mCtc.addConstructor(CtNewConstructor.copy(getCtConstructor(mCopyConstructors.get(code.substring(1))), mCtc, null));
                } else {
                    // inner class name include $.
                    String[] sn = mCtc.getSimpleName().split("\\$+");
                    mCtc.addConstructor(CtNewConstructor.make(code.replaceFirst(SIMPLE_NAME_TAG, sn[sn.length - 1]), mCtc));
                }
            }
        }
        return mCtc.toClass(loader, pd);
    } catch (RuntimeException e) {
        throw e;
    } catch (NotFoundException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (CannotCompileException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : CtClass(javassist.CtClass) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException)

Aggregations

CannotCompileException (javassist.CannotCompileException)65 CtClass (javassist.CtClass)45 NotFoundException (javassist.NotFoundException)42 CtMethod (javassist.CtMethod)30 IOException (java.io.IOException)22 ClassPool (javassist.ClassPool)22 CtField (javassist.CtField)15 CtConstructor (javassist.CtConstructor)10 FileNotFoundException (java.io.FileNotFoundException)8 File (java.io.File)6 Method (java.lang.reflect.Method)5 ArrayList (java.util.ArrayList)5 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)5 FileFilter (java.io.FileFilter)4 BadBytecode (javassist.bytecode.BadBytecode)3 Bytecode (javassist.bytecode.Bytecode)3 CodeAttribute (javassist.bytecode.CodeAttribute)3 CodeIterator (javassist.bytecode.CodeIterator)3 CompileError (javassist.compiler.CompileError)3 Javac (javassist.compiler.Javac)3