Search in sources :

Example 81 with NotFoundException

use of javassist.NotFoundException in project gwt-test-utils by gwt-test-utils.

the class AutomaticPatcher method getInitMethod.

private CtMethod getInitMethod(Set<CtClass> patchClasses) {
    List<CtMethod> initMethods = new ArrayList<>();
    for (CtClass patchClass : patchClasses) {
        for (CtMethod ctMethod : patchClass.getDeclaredMethods()) {
            if (ctMethod.hasAnnotation(InitMethod.class)) {
                if (!Modifier.isStatic(ctMethod.getModifiers())) {
                    throw new GwtTestPatchException("@" + InitMethod.class.getSimpleName() + " has to be static : '" + ctMethod.getLongName() + "'");
                }
                try {
                    if (ctMethod.getParameterTypes().length != 1 || ctMethod.getParameterTypes()[0] != GwtClassPool.getCtClass(CtClass.class)) {
                        throw new GwtTestPatchException("@" + InitMethod.class.getName() + " method must have one and only one parameter of type '" + CtClass.class.getName() + "'");
                    }
                } catch (NotFoundException e) {
                    // should never happen
                    throw new GwtTestPatchException(e);
                }
                initMethods.add(ctMethod);
            }
        }
    }
    CtMethod initMethod = getMethodToUse(initMethods, InitMethod.class);
    if (initMethod != null) {
        initMethod.setModifiers(Modifier.PUBLIC + Modifier.STATIC);
    }
    return initMethod;
}
Also used : GwtTestPatchException(com.googlecode.gwt.test.exceptions.GwtTestPatchException) InitMethod(com.googlecode.gwt.test.patchers.InitMethod) CtClass(javassist.CtClass) NotFoundException(javassist.NotFoundException) CtMethod(javassist.CtMethod)

Example 82 with NotFoundException

use of javassist.NotFoundException in project reflections by ronmamo.

the class MemberUsageScanner method scanMember.

private void scanMember(CtBehavior member, List<Map.Entry<String, String>> entries) throws CannotCompileException {
    // key contains this$/val$ means local field/parameter closure
    final String key = member.getDeclaringClass().getName() + "." + member.getMethodInfo().getName() + "(" + parameterNames(member.getMethodInfo()) + // + " #" + member.getMethodInfo().getLineNumber(0)
    ")";
    member.instrument(new ExprEditor() {

        @Override
        public void edit(NewExpr e) {
            try {
                add(entries, e.getConstructor().getDeclaringClass().getName() + "." + "<init>" + "(" + parameterNames(e.getConstructor().getMethodInfo()) + ")", key + " #" + e.getLineNumber());
            } catch (NotFoundException e1) {
                throw new ReflectionsException("Could not find new instance usage in " + key, e1);
            }
        }

        @Override
        public void edit(MethodCall m) {
            try {
                add(entries, m.getMethod().getDeclaringClass().getName() + "." + m.getMethodName() + "(" + parameterNames(m.getMethod().getMethodInfo()) + ")", key + " #" + m.getLineNumber());
            } catch (NotFoundException e) {
                throw new ReflectionsException("Could not find member " + m.getClassName() + " in " + key, e);
            }
        }

        @Override
        public void edit(ConstructorCall c) {
            try {
                add(entries, c.getConstructor().getDeclaringClass().getName() + "." + "<init>" + "(" + parameterNames(c.getConstructor().getMethodInfo()) + ")", key + " #" + c.getLineNumber());
            } catch (NotFoundException e) {
                throw new ReflectionsException("Could not find member " + c.getClassName() + " in " + key, e);
            }
        }

        @Override
        public void edit(FieldAccess f) {
            try {
                add(entries, f.getField().getDeclaringClass().getName() + "." + f.getFieldName(), key + " #" + f.getLineNumber());
            } catch (NotFoundException e) {
                throw new ReflectionsException("Could not find member " + f.getFieldName() + " in " + key, e);
            }
        }
    });
}
Also used : ReflectionsException(org.reflections.ReflectionsException) ExprEditor(javassist.expr.ExprEditor) NewExpr(javassist.expr.NewExpr) NotFoundException(javassist.NotFoundException) ConstructorCall(javassist.expr.ConstructorCall) FieldAccess(javassist.expr.FieldAccess) MethodCall(javassist.expr.MethodCall)

Example 83 with NotFoundException

use of javassist.NotFoundException in project powermock by powermock.

the class PowerMockExpressionEditor method addNewDeferConstructor.

/**
 * Create a defer constructor in the class which will be called when the
 * constructor is suppressed.
 *
 * @param clazz The class whose super constructor will get a new defer
 *              constructor if it doesn't already have one.
 * @throws CannotCompileException If an unexpected compilation error occurs.
 */
private void addNewDeferConstructor(final CtClass clazz) throws CannotCompileException {
    final CtClass superClass;
    try {
        superClass = clazz.getSuperclass();
    } catch (NotFoundException e1) {
        throw new IllegalArgumentException("Internal error: Failed to get superclass for " + clazz.getName() + " when about to create a new default constructor.");
    }
    ClassPool classPool = clazz.getClassPool();
    /*
         * To make a unique defer constructor we create a new constructor
         * with one argument (IndicateReloadClass). So we get this class a
         * Javassist class below.
         */
    final CtClass constructorType;
    try {
        constructorType = classPool.get(IndicateReloadClass.class.getName());
    } catch (NotFoundException e) {
        throw new IllegalArgumentException("Internal error: failed to get the " + IndicateReloadClass.class.getName() + " when added defer constructor.");
    }
    clazz.defrost();
    if (superClass.getName().equals(Object.class.getName())) {
        try {
            clazz.addConstructor(CtNewConstructor.make(new CtClass[] { constructorType }, new CtClass[0], "{super();}", clazz));
        } catch (DuplicateMemberException e) {
        // OK, the constructor has already been added.
        }
    } else {
        addNewDeferConstructor(superClass);
        try {
            clazz.addConstructor(CtNewConstructor.make(new CtClass[] { constructorType }, new CtClass[0], "{super($$);}", clazz));
        } catch (DuplicateMemberException e) {
        // OK, the constructor has already been added.
        }
    }
}
Also used : DuplicateMemberException(javassist.bytecode.DuplicateMemberException) CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException)

Example 84 with NotFoundException

use of javassist.NotFoundException in project powermock by powermock.

the class PowerMockExpressionEditor method edit.

@Override
public void edit(FieldAccess f) throws CannotCompileException {
    if (f.isReader()) {
        CtClass returnTypeAsCtClass;
        FieldInfo fieldInfo;
        try {
            CtField field = f.getField();
            returnTypeAsCtClass = field.getType();
            fieldInfo = field.getFieldInfo2();
        } catch (NotFoundException e) {
            /*
                     * If multiple java agents are active (in INST_REDEFINE mode), the types implicitly loaded by javassist from disk
                     * might differ from the types available in memory. Thus, this error might occur.
                     *
                     * It may also happen if PowerMock is modifying an SPI where the SPI require some classes to be available in the classpath
                     * at runtime but they are not! This is valid in some cases such as slf4j.
                     */
            return;
        }
        if (isNotSyntheticField(fieldInfo)) {
            String code = "{Object value =  " + MockGateway.class.getName() + ".fieldCall(" + "$0,$class,\"" + f.getFieldName() + "\",$type);" + "if(value == " + MockGateway.class.getName() + ".PROCEED) {" + "	$_ = $proceed($$);" + "} else {" + "	$_ = " + getCorrectReturnValueType(returnTypeAsCtClass) + ";" + "}}";
            f.replace(code);
        }
    }
}
Also used : CtClass(javassist.CtClass) CtField(javassist.CtField) NotFoundException(javassist.NotFoundException) TransformerHelper.getReturnTypeAsString(org.powermock.core.transformers.javassist.support.TransformerHelper.getReturnTypeAsString) FieldInfo(javassist.bytecode.FieldInfo)

Example 85 with NotFoundException

use of javassist.NotFoundException in project powermock by powermock.

the class PowerMockExpressionEditor method edit.

@Override
public void edit(ConstructorCall c) throws CannotCompileException {
    /*
         * Note that constructor call only intercepts calls to super or this
         * from an instantiated class. This means that A a = new A(); will
         * NOT trigger a ConstructorCall for the default constructor in A.
         * If A where to extend B and A's constructor only delegates to
         * super(), the default constructor of B would trigger a
         * ConstructorCall. This means that we need to handle
         * "suppressConstructorCode" both here and in NewExpr.
         */
    if (strategy != INST_REDEFINE && !c.getClassName().startsWith("java.lang")) {
        final CtClass superclass;
        try {
            superclass = clazz.getSuperclass();
        } catch (NotFoundException e) {
            throw new RuntimeException(e);
        }
        /*
             * Create a default constructor in the super class if it doesn't
             * exist. This is needed because if the code in the current
             * constructor should be suppressed (which we don't know at this
             * moment of time) the parent class must have a default
             * constructor that we can delegate to.
             */
        addNewDeferConstructor(clazz);
        final StringBuilder code = new StringBuilder();
        code.append("{Object value =").append(mockGetawayClass.getName()).append(".constructorCall($class, $args, $sig);");
        code.append("if (value != ").append(MockGateway.class.getName()).append(".PROCEED){");
        /*
             * TODO Suppress and lazy inject field (when this feature is ready).
             */
        if (superclass.getName().equals(Object.class.getName())) {
            code.append(" super();");
        } else {
            code.append(" super((").append(IndicateReloadClass.class.getName()).append(") null);");
        }
        code.append("} else {");
        code.append("   $proceed($$);");
        code.append("}}");
        c.replace(code.toString());
    }
}
Also used : CtClass(javassist.CtClass) NotFoundException(javassist.NotFoundException)

Aggregations

NotFoundException (javassist.NotFoundException)88 CtClass (javassist.CtClass)64 CannotCompileException (javassist.CannotCompileException)42 CtMethod (javassist.CtMethod)36 ClassPool (javassist.ClassPool)32 IOException (java.io.IOException)19 CtField (javassist.CtField)16 FileNotFoundException (java.io.FileNotFoundException)9 CtConstructor (javassist.CtConstructor)9 File (java.io.File)7 Method (java.lang.reflect.Method)7 ClassFile (javassist.bytecode.ClassFile)7 ArrayList (java.util.ArrayList)6 Collectors (java.util.stream.Collectors)6 BadBytecode (javassist.bytecode.BadBytecode)6 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)5 MethodUsage (com.github.javaparser.resolution.MethodUsage)4 UnsolvedSymbolException (com.github.javaparser.resolution.UnsolvedSymbolException)4 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)4 Context (com.github.javaparser.symbolsolver.core.resolution.Context)4