Search in sources :

Example 66 with NotFoundException

use of javassist.NotFoundException in project javaparser by javaparser.

the class JarTypeSolver method addPathToJar.

private void addPathToJar(String pathToJar) throws IOException {
    try {
        classPool.appendClassPath(pathToJar);
        classPool.appendSystemPath();
    } catch (NotFoundException e) {
        throw new RuntimeException(e);
    }
    JarFile jarFile = new JarFile(pathToJar);
    JarEntry entry = null;
    Enumeration<JarEntry> e = jarFile.entries();
    while (e.hasMoreElements()) {
        entry = e.nextElement();
        if (entry != null && !entry.isDirectory() && entry.getName().endsWith(".class")) {
            String name = entryPathToClassName(entry.getName());
            classpathElements.put(name, new ClasspathElement(jarFile, entry, name));
        }
    }
}
Also used : NotFoundException(javassist.NotFoundException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 67 with NotFoundException

use of javassist.NotFoundException in project javaparser by javaparser.

the class JavassistInterfaceDeclaration method getAncestors.

@Override
public List<ResolvedReferenceType> getAncestors() {
    List<ResolvedReferenceType> ancestors = new ArrayList<>();
    try {
        for (CtClass interfaze : ctClass.getInterfaces()) {
            ResolvedReferenceType superInterfaze = JavassistFactory.typeUsageFor(interfaze, typeSolver).asReferenceType();
            ancestors.add(superInterfaze);
        }
    } catch (NotFoundException e) {
        throw new RuntimeException(e);
    }
    ancestors = ancestors.stream().filter(a -> a.getQualifiedName() != Object.class.getCanonicalName()).collect(Collectors.toList());
    ancestors.add(new ReferenceTypeImpl(typeSolver.solveType(Object.class.getCanonicalName()), typeSolver));
    return ancestors;
}
Also used : CtClass(javassist.CtClass) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) NotFoundException(javassist.NotFoundException)

Example 68 with NotFoundException

use of javassist.NotFoundException in project javaparser by javaparser.

the class JavassistInterfaceDeclaration method solveMethod.

@Deprecated
public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> argumentsTypes, boolean staticOnly) {
    List<ResolvedMethodDeclaration> candidates = new ArrayList<>();
    Predicate<CtMethod> staticOnlyCheck = m -> !staticOnly || (staticOnly && Modifier.isStatic(m.getModifiers()));
    for (CtMethod method : ctClass.getDeclaredMethods()) {
        boolean isSynthetic = method.getMethodInfo().getAttribute(SyntheticAttribute.tag) != null;
        boolean isNotBridge = (method.getMethodInfo().getAccessFlags() & AccessFlag.BRIDGE) == 0;
        if (method.getName().equals(name) && !isSynthetic && isNotBridge && staticOnlyCheck.test(method)) {
            candidates.add(new JavassistMethodDeclaration(method, typeSolver));
        }
    }
    try {
        CtClass superClass = ctClass.getSuperclass();
        if (superClass != null) {
            SymbolReference<ResolvedMethodDeclaration> ref = new JavassistClassDeclaration(superClass, typeSolver).solveMethod(name, argumentsTypes, staticOnly);
            if (ref.isSolved()) {
                candidates.add(ref.getCorrespondingDeclaration());
            }
        }
    } catch (NotFoundException e) {
        throw new RuntimeException(e);
    }
    try {
        for (CtClass interfaze : ctClass.getInterfaces()) {
            SymbolReference<ResolvedMethodDeclaration> ref = new JavassistInterfaceDeclaration(interfaze, typeSolver).solveMethod(name, argumentsTypes, staticOnly);
            if (ref.isSolved()) {
                candidates.add(ref.getCorrespondingDeclaration());
            }
        }
    } catch (NotFoundException e) {
        throw new RuntimeException(e);
    }
    return MethodResolutionLogic.findMostApplicable(candidates, name, argumentsTypes, typeSolver);
}
Also used : CtMethod(javassist.CtMethod) java.util(java.util) AccessFlag(javassist.bytecode.AccessFlag) UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) Predicate(java.util.function.Predicate) MethodUsage(com.github.javaparser.resolution.MethodUsage) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) AccessSpecifier(com.github.javaparser.ast.AccessSpecifier) CtClass(javassist.CtClass) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) com.github.javaparser.resolution.declarations(com.github.javaparser.resolution.declarations) Collectors(java.util.stream.Collectors) CtField(javassist.CtField) AbstractTypeDeclaration(com.github.javaparser.symbolsolver.logic.AbstractTypeDeclaration) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodResolutionLogic(com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic) Context(com.github.javaparser.symbolsolver.core.resolution.Context) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) Modifier(java.lang.reflect.Modifier) SyntheticAttribute(javassist.bytecode.SyntheticAttribute) NotFoundException(javassist.NotFoundException) SymbolSolver(com.github.javaparser.symbolsolver.resolution.SymbolSolver) NotFoundException(javassist.NotFoundException) CtClass(javassist.CtClass) CtMethod(javassist.CtMethod)

Example 69 with NotFoundException

use of javassist.NotFoundException in project javaparser by javaparser.

the class JavassistUtils method getMethodUsage.

static Optional<MethodUsage> getMethodUsage(CtClass ctClass, String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver, Context invokationContext) {
    // TODO avoid bridge and synthetic methods
    for (CtMethod method : ctClass.getDeclaredMethods()) {
        if (method.getName().equals(name)) {
            // TODO check typeParametersValues
            MethodUsage methodUsage = new MethodUsage(new JavassistMethodDeclaration(method, typeSolver));
            if (argumentsTypes.size() < methodUsage.getNoParams()) {
                // this method cannot be a good candidate (except if variadic ?)
                continue;
            }
            try {
                if (method.getGenericSignature() != null) {
                    SignatureAttribute.MethodSignature methodSignature = SignatureAttribute.toMethodSignature(method.getGenericSignature());
                    List<ResolvedType> parametersOfReturnType = parseTypeParameters(methodSignature.getReturnType().toString(), typeSolver, invokationContext);
                    ResolvedType newReturnType = methodUsage.returnType();
                    // consume one parametersOfReturnType at the time
                    if (newReturnType.isReferenceType() && parametersOfReturnType.size() > 0) {
                        newReturnType = newReturnType.asReferenceType().transformTypeParameters(tp -> parametersOfReturnType.remove(0));
                    }
                    methodUsage = methodUsage.replaceReturnType(newReturnType);
                }
                return Optional.of(methodUsage);
            } catch (BadBytecode e) {
                throw new RuntimeException(e);
            }
        }
    }
    try {
        CtClass superClass = ctClass.getSuperclass();
        if (superClass != null) {
            Optional<MethodUsage> ref = new JavassistClassDeclaration(superClass, typeSolver).solveMethodAsUsage(name, argumentsTypes, typeSolver, invokationContext, null);
            if (ref.isPresent()) {
                return ref;
            }
        }
    } catch (NotFoundException e) {
        throw new RuntimeException(e);
    }
    try {
        for (CtClass interfaze : ctClass.getInterfaces()) {
            Optional<MethodUsage> ref = new JavassistInterfaceDeclaration(interfaze, typeSolver).solveMethodAsUsage(name, argumentsTypes, typeSolver, invokationContext, null);
            if (ref.isPresent()) {
                return ref;
            }
        }
    } catch (NotFoundException e) {
        throw new RuntimeException(e);
    }
    return Optional.empty();
}
Also used : CtMethod(javassist.CtMethod) java.util(java.util) BadBytecode(javassist.bytecode.BadBytecode) UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) MethodUsage(com.github.javaparser.resolution.MethodUsage) com.github.javaparser.symbolsolver.model.typesystem(com.github.javaparser.symbolsolver.model.typesystem) SignatureAttribute(javassist.bytecode.SignatureAttribute) CtClass(javassist.CtClass) Collectors(java.util.stream.Collectors) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) Context(com.github.javaparser.symbolsolver.core.resolution.Context) com.github.javaparser.resolution.types(com.github.javaparser.resolution.types) ResolvedTypeParametrizable(com.github.javaparser.resolution.declarations.ResolvedTypeParametrizable) NotFoundException(javassist.NotFoundException) SymbolSolver(com.github.javaparser.symbolsolver.resolution.SymbolSolver) NotFoundException(javassist.NotFoundException) BadBytecode(javassist.bytecode.BadBytecode) SignatureAttribute(javassist.bytecode.SignatureAttribute) CtClass(javassist.CtClass) MethodUsage(com.github.javaparser.resolution.MethodUsage) CtMethod(javassist.CtMethod)

Example 70 with NotFoundException

use of javassist.NotFoundException in project motech by motech.

the class EntityBuilderImpl method getDeclaringClass.

private CtClass getDeclaringClass(EntityDto entity, EntityType type, Bundle bundle) throws NotFoundException {
    String className = type.getClassName(entity.getClassName());
    boolean isDDE = null != bundle;
    CtClass declaring = classPool.getOrNull(className);
    if (null != declaring) {
        // we can edit classes
        declaring.defrost();
    } else if (isDDE) {
        try {
            declaring = JavassistUtil.loadClass(bundle, entity.getClassName(), classPool);
        } catch (IOException e) {
            throw new NotFoundException(e.getMessage(), e);
        }
    }
    return isDDE ? declaring : classPool.makeClass(className);
}
Also used : CtClass(javassist.CtClass) NotFoundException(javassist.NotFoundException) IOException(java.io.IOException)

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