Search in sources :

Example 21 with NotFoundException

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

the class PersistentAttributesEnhancer method generateFieldWriter.

protected CtMethod generateFieldWriter(CtClass managedCtClass, CtField persistentField, AttributeTypeDescriptor typeDescriptor) {
    String fieldName = persistentField.getName();
    String readerName = EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX + fieldName;
    String writerName = EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX + fieldName;
    CtMethod tmpSuperReader = null;
    CtMethod tmpSuperWriter = null;
    CtMethod writer;
    try {
        boolean declared = persistentField.getDeclaringClass().equals(managedCtClass);
        String declaredWriteFragment = "this." + fieldName + "=" + fieldName + ";";
        String superWriteFragment = "super." + writerName + "(" + fieldName + ");";
        if (!declared) {
            // create a temporary setter on the supper entity to be able to compile our code
            try {
                persistentField.getDeclaringClass().getDeclaredMethod(readerName);
                persistentField.getDeclaringClass().getDeclaredMethod(writerName);
            } catch (NotFoundException nfe) {
                tmpSuperReader = MethodWriter.addGetter(persistentField.getDeclaringClass(), persistentField.getName(), readerName);
                tmpSuperWriter = MethodWriter.addSetter(persistentField.getDeclaringClass(), persistentField.getName(), writerName);
            }
        }
        if (!enhancementContext.hasLazyLoadableAttributes(managedCtClass) || !enhancementContext.isLazyLoadable(persistentField)) {
            writer = MethodWriter.write(managedCtClass, "public void %s(%s %s) {%n  %s%n}", writerName, persistentField.getType().getName(), fieldName, declared ? declaredWriteFragment : superWriteFragment);
        } else {
            writer = MethodWriter.write(managedCtClass, "public void %s(%s %s) {%n%s%n}", writerName, persistentField.getType().getName(), fieldName, typeDescriptor.buildWriteInterceptionBodyFragment(fieldName));
        }
        if (enhancementContext.doDirtyCheckingInline(managedCtClass)) {
            if (enhancementContext.isCompositeClass(managedCtClass)) {
                writer.insertBefore(String.format("  if (%1$s != null) { %1$s.callOwner(\"\"); }%n", EnhancerConstants.TRACKER_COMPOSITE_FIELD_NAME));
            } else {
                writer.insertBefore(typeDescriptor.buildInLineDirtyCheckingBodyFragment(enhancementContext, persistentField));
            }
            handleCompositeField(managedCtClass, persistentField, writer);
        }
        if (enhancementContext.doBiDirectionalAssociationManagement(persistentField)) {
            handleBiDirectionalAssociation(managedCtClass, persistentField, writer);
        }
        if (tmpSuperReader != null) {
            persistentField.getDeclaringClass().removeMethod(tmpSuperReader);
        }
        if (tmpSuperWriter != null) {
            persistentField.getDeclaringClass().removeMethod(tmpSuperWriter);
        }
        return writer;
    } catch (CannotCompileException cce) {
        final String msg = String.format("Could not enhance entity class [%s] to add field writer method [%s]", managedCtClass.getName(), writerName);
        throw new EnhancementException(msg, cce);
    } catch (NotFoundException nfe) {
        final String msg = String.format("Could not enhance entity class [%s] to add field writer method [%s]", managedCtClass.getName(), writerName);
        throw new EnhancementException(msg, nfe);
    }
}
Also used : EnhancementException(org.hibernate.bytecode.enhance.spi.EnhancementException) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) CtMethod(javassist.CtMethod)

Example 22 with NotFoundException

use of javassist.NotFoundException in project ratpack by ratpack.

the class ClosureBackedHandler method describeTo.

@Override
public void describeTo(StringBuilder stringBuilder) {
    ClosureUtil.SourceInfo sourceInfo = ClosureUtil.getSourceInfo(invoker.getClosure());
    if (sourceInfo == null) {
        ClassPool pool = ClassPool.getDefault();
        try {
            CtClass ctClass = pool.get(invoker.getClosure().getClass().getName());
            CtMethod ctMethod = ctClass.getDeclaredMethod("doCall");
            int lineNumber = ctMethod.getMethodInfo().getLineNumber(0);
            ClassFile classFile = ctClass.getClassFile();
            String sourceFile = classFile.getSourceFile();
            if (lineNumber != -1 && sourceFile != null) {
                stringBuilder.append("closure at line ").append(lineNumber).append(" of ").append(sourceFile);
            } else {
                stringBuilder.append("closure ").append(invoker.getClosure().getClass().getName());
            }
        } catch (NotFoundException e) {
            stringBuilder.append(invoker.getClosure().getClass().getName());
        }
    } else {
        stringBuilder.append("closure at line ").append(sourceInfo.getLineNumber()).append(" of ").append(sourceInfo.getUri());
    }
}
Also used : CtClass(javassist.CtClass) ClassFile(javassist.bytecode.ClassFile) ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException) ClosureUtil(ratpack.groovy.internal.ClosureUtil) CtMethod(javassist.CtMethod)

Example 23 with NotFoundException

use of javassist.NotFoundException in project atlas by alibaba.

the class CodeInjectByJavassist method addField.

private static void addField(ClassPool pool, CtClass cc, String key, String value) throws NotFoundException, CannotCompileException {
    if (StringUtils.isNotBlank(value)) {
        try {
            cc.removeField(cc.getField(key));
        } catch (NotFoundException notfoundException) {
        }
        logger.info("inject key " + key + "->" + value);
        CtClass ctClass = pool.get("java.lang.String");
        CtField ctField = new CtField(ctClass, key, cc);
        ctField.setModifiers(Modifier.STATIC | Modifier.PUBLIC);
        CtField.Initializer initializer = CtField.Initializer.constant(value);
        cc.addField(ctField, initializer);
    }
}
Also used : CtClass(javassist.CtClass) CtField(javassist.CtField) NotFoundException(javassist.NotFoundException)

Example 24 with NotFoundException

use of javassist.NotFoundException in project pinpoint by naver.

the class JavassistClass method hasEnclosingMethod.

@Override
public boolean hasEnclosingMethod(String methodName, String... parameterTypes) {
    CtBehavior behavior;
    try {
        behavior = ctClass.getEnclosingBehavior();
    } catch (NotFoundException ignored) {
        return false;
    }
    if (behavior == null) {
        return false;
    }
    final MethodInfo methodInfo = behavior.getMethodInfo2();
    if (!methodInfo.getName().equals(methodName)) {
        return false;
    }
    final String jvmSignature = JavaAssistUtils.javaTypeToJvmSignature(parameterTypes);
    if (methodInfo.getDescriptor().startsWith(jvmSignature)) {
        return true;
    }
    return false;
}
Also used : CtBehavior(javassist.CtBehavior) NotFoundException(javassist.NotFoundException) MethodInfo(javassist.bytecode.MethodInfo)

Example 25 with NotFoundException

use of javassist.NotFoundException in project pinpoint by naver.

the class JavassistClass method addDelegatorMethod.

@Override
public InstrumentMethod addDelegatorMethod(String methodName, String... paramTypes) throws InstrumentException {
    if (getCtMethod0(ctClass, methodName, paramTypes) != null) {
        throw new InstrumentException(getName() + "already have method(" + methodName + ").");
    }
    try {
        final CtClass superClass = ctClass.getSuperclass();
        CtMethod superMethod = getCtMethod0(superClass, methodName, paramTypes);
        if (superMethod == null) {
            throw new NotFoundInstrumentException(methodName + Arrays.toString(paramTypes) + " is not found in " + superClass.getName());
        }
        CtMethod delegatorMethod = CtNewMethod.delegator(superMethod, ctClass);
        ctClass.addMethod(delegatorMethod);
        return new JavassistMethod(objectBinderFactory, pluginContext, interceptorRegistryBinder, apiMetaDataService, this, delegatorMethod);
    } catch (NotFoundException ex) {
        throw new InstrumentException(getName() + "don't have super class(" + getSuperClass() + "). Cause:" + ex.getMessage(), ex);
    } catch (CannotCompileException ex) {
        throw new InstrumentException(methodName + " addDelegatorMethod fail. Cause:" + ex.getMessage(), ex);
    }
}
Also used : CtClass(javassist.CtClass) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) CtMethod(javassist.CtMethod)

Aggregations

NotFoundException (javassist.NotFoundException)35 CtClass (javassist.CtClass)26 CannotCompileException (javassist.CannotCompileException)19 CtMethod (javassist.CtMethod)15 ClassPool (javassist.ClassPool)12 IOException (java.io.IOException)5 CtField (javassist.CtField)5 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)5 FileNotFoundException (java.io.FileNotFoundException)4 BadBytecode (javassist.bytecode.BadBytecode)4 CodeIterator (javassist.bytecode.CodeIterator)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 Bytecode (javassist.bytecode.Bytecode)3 CodeAttribute (javassist.bytecode.CodeAttribute)3 CompileError (javassist.compiler.CompileError)3 Javac (javassist.compiler.Javac)3 FileFilter (java.io.FileFilter)2 CtBehavior (javassist.CtBehavior)2 CtConstructor (javassist.CtConstructor)2