Search in sources :

Example 6 with CachedASMReflector

use of meghanada.reflect.asm.CachedASMReflector in project meghanada-server by mopemope.

the class ProjectDependency method getDependencyFilePath.

public String getDependencyFilePath() {
    try {
        if (type.equals(Type.PROJECT)) {
            if (nonNull(this.dependencyFilePath)) {
                return this.dependencyFilePath;
            }
            // get gradle's project archive
            final File projectArchive = new File(file, "build" + File.separator + "libs" + File.separator + this.id + ".jar");
            if (projectArchive.exists()) {
                // add index
                final List<File> temp = new ArrayList<>(1);
                temp.add(projectArchive);
                final CachedASMReflector reflector = CachedASMReflector.getInstance();
                reflector.createClassIndexes(temp);
                this.dependencyFilePath = projectArchive.getCanonicalPath();
            } else {
                if (nonNull(this.dependencyFilePath)) {
                    return this.dependencyFilePath;
                }
                String root = Config.getProjectRoot();
                try {
                    final File output = getProjectOutput();
                    this.dependencyFilePath = output.getCanonicalPath();
                } finally {
                    Config.setProjectRoot(root);
                }
            }
            return this.dependencyFilePath;
        } else {
            return file.getCanonicalPath();
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) ArrayList(java.util.ArrayList) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) File(java.io.File)

Example 7 with CachedASMReflector

use of meghanada.reflect.asm.CachedASMReflector in project meghanada-server by mopemope.

the class DeclarationSearcher method searchMethodCall.

private static Optional<Declaration> searchMethodCall(final Source source, final Integer line, final Integer col, final String symbol) {
    final EntryMessage entryMessage = log.traceEntry("line={} col={} symbol={}", line, col, symbol);
    final Optional<MethodCall> methodCall = source.getMethodCall(line, col, true);
    final Optional<Declaration> result = methodCall.map(mc -> {
        final String methodName = mc.name;
        final List<String> arguments = mc.getArguments();
        final String declaringClass = mc.declaringClass;
        if (declaringClass == null) {
            return null;
        }
        final CachedASMReflector reflector = CachedASMReflector.getInstance();
        final MemberDescriptor method = searchMethod(declaringClass, methodName, arguments).orElseGet(() -> searchConstructor(declaringClass, arguments).orElse(null));
        String declaration;
        if (method != null) {
            declaration = method.getDeclaration();
        } else {
            final String args = Joiner.on(", ").join(arguments);
            declaration = mc.returnType + ' ' + methodName + '(' + args + ')';
        }
        String scope = mc.scope;
        if (scope != null && !scope.isEmpty()) {
            scope = scope + '.' + symbol;
        } else {
            scope = symbol;
        }
        return new Declaration(scope.trim(), declaration, Declaration.Type.METHOD, mc.argumentIndex);
    });
    log.traceExit(entryMessage);
    return result;
}
Also used : CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) MemberDescriptor(meghanada.reflect.MemberDescriptor) EntryMessage(org.apache.logging.log4j.message.EntryMessage) MethodCall(meghanada.analyze.MethodCall)

Example 8 with CachedASMReflector

use of meghanada.reflect.asm.CachedASMReflector in project meghanada-server by mopemope.

the class TestRunner method getTestClass.

private List<Class<?>> getTestClass(String testName) throws ClassNotFoundException {
    List<Class<?>> classes = new ArrayList<>();
    CachedASMReflector cachedASMReflector = CachedASMReflector.getInstance();
    for (ClassIndex classIndex : cachedASMReflector.getGlobalClassIndex().values()) {
        String fqcn = classIndex.getReturnType();
        String className = classIndex.getName();
        if (fqcn.equals(testName) || className.equals(testName) || fqcn.matches(testName)) {
            classes.add(Class.forName(fqcn));
        }
    }
    return classes;
}
Also used : ClassIndex(meghanada.reflect.ClassIndex) CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) ArrayList(java.util.ArrayList)

Example 9 with CachedASMReflector

use of meghanada.reflect.asm.CachedASMReflector in project meghanada-server by mopemope.

the class LocationSearcher method searchFromDependency.

private Location searchFromDependency(final SearchContext context) throws IOException {
    final String searchFQCN = context.searchFQCN;
    final CachedASMReflector reflector = CachedASMReflector.getInstance();
    final File classFile = reflector.getClassFile(searchFQCN);
    final String tempDir = System.getProperty("java.io.tmpdir");
    if (classFile != null && classFile.exists() && classFile.getName().endsWith(FileUtils.JAR_EXT)) {
        final String androidHome = System.getenv("ANDROID_HOME");
        if (androidHome != null) {
            final Optional<ProjectDependency> dependencyOptional = this.project.getDependencies().stream().filter(dependency -> dependency.getFile().equals(classFile)).findFirst();
            if (dependencyOptional.isPresent()) {
                final ProjectDependency dependency = dependencyOptional.get();
                final String sourceJar = ClassNameUtils.getSimpleName(dependency.getId()) + '-' + dependency.getVersion() + "-sources.jar";
                final File root = new File(androidHome, "extras");
                if (root.exists()) {
                    return getLocationFromSrcOrDecompile(context, classFile, root, sourceJar);
                }
            }
        }
        final File depParent = classFile.getParentFile();
        final File dependencyDir = depParent.getParentFile();
        final String srcJarName = ClassNameUtils.replace(classFile.getName(), FileUtils.JAR_EXT, "-sources.jar");
        final String disable = System.getProperty("disable-source-jar");
        if (disable != null && disable.equals("true")) {
            return searchLocationFromDecompileFile(context, searchFQCN, classFile, tempDir);
        }
        return getLocationFromSrcOrDecompile(context, classFile, dependencyDir, srcJarName);
    }
    return null;
}
Also used : DecompilationListener(org.jboss.windup.decompiler.api.DecompilationListener) FunctionUtils.wrapIOConsumer(meghanada.utils.FunctionUtils.wrapIOConsumer) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Map(java.util.Map) ZipFile(java.util.zip.ZipFile) MethodCall(meghanada.analyze.MethodCall) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) FunctionUtils.wrapIO(meghanada.utils.FunctionUtils.wrapIO) GlobalCache(meghanada.cache.GlobalCache) SimpleName(com.github.javaparser.ast.expr.SimpleName) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) StandardOpenOption(java.nio.file.StandardOpenOption) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) MethodScope(meghanada.analyze.MethodScope) ClassNameUtils(meghanada.utils.ClassNameUtils) Optional(java.util.Optional) Project(meghanada.project.Project) Pattern(java.util.regex.Pattern) ProjectDependency(meghanada.project.ProjectDependency) Config(meghanada.config.Config) Parameter(com.github.javaparser.ast.body.Parameter) Position(com.github.javaparser.Position) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Variable(meghanada.analyze.Variable) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ClassScope(meghanada.analyze.ClassScope) OutputStream(java.io.OutputStream) Filter(org.jboss.windup.decompiler.util.Filter) DecompilationResult(org.jboss.windup.decompiler.api.DecompilationResult) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) FileOutputStream(java.io.FileOutputStream) FileUtils.existsFQCN(meghanada.utils.FileUtils.existsFQCN) IOException(java.io.IOException) CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) FileUtils(meghanada.utils.FileUtils) EntryMessage(org.apache.logging.log4j.message.EntryMessage) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Paths(java.nio.file.Paths) Source(meghanada.analyze.Source) FernflowerDecompiler(org.jboss.windup.decompiler.fernflower.FernflowerDecompiler) TypeScope(meghanada.analyze.TypeScope) LogManager(org.apache.logging.log4j.LogManager) JavaParser(com.github.javaparser.JavaParser) InputStream(java.io.InputStream) CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) ProjectDependency(meghanada.project.ProjectDependency) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 10 with CachedASMReflector

use of meghanada.reflect.asm.CachedASMReflector in project meghanada-server by mopemope.

the class LocationSearcher method searchClassOrInterface.

private Optional<Location> searchClassOrInterface(final Source source, final int line, final int col, String symbol) {
    final EntryMessage entryMessage = log.traceEntry("line={} col={} symbol={}", line, col, symbol);
    if (symbol.startsWith("@")) {
        symbol = symbol.substring(1);
    }
    final List<String> searchTargets = new ArrayList<>(4);
    String fqcn = source.getImportedClassFQCN(symbol, null);
    if (fqcn == null) {
        final CachedASMReflector reflector = CachedASMReflector.getInstance();
        final Map<String, String> standardClasses = reflector.getStandardClasses();
        fqcn = standardClasses.get(symbol);
        if (fqcn == null) {
            if (!source.getPackageName().isEmpty()) {
                fqcn = source.getPackageName() + '.' + symbol;
            } else {
                fqcn = symbol;
            }
            searchTargets.add(fqcn);
            // Add inner class
            final String finalSym = symbol;
            source.getTypeScope(line).ifPresent(typeScope -> {
                final String firstFQCN = typeScope.getFQCN();
                searchTargets.add(firstFQCN + ClassNameUtils.INNER_MARK + finalSym);
            });
        }
    } else {
        searchTargets.add(fqcn);
    }
    final Optional<Location> location = searchTargets.stream().map(this::getFQCNLocation).filter(Objects::nonNull).findFirst();
    log.traceExit(entryMessage);
    return location;
}
Also used : CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) ArrayList(java.util.ArrayList) EntryMessage(org.apache.logging.log4j.message.EntryMessage)

Aggregations

CachedASMReflector (meghanada.reflect.asm.CachedASMReflector)29 ArrayList (java.util.ArrayList)11 ClassIndex (meghanada.reflect.ClassIndex)9 EntryMessage (org.apache.logging.log4j.message.EntryMessage)9 File (java.io.File)8 HashSet (java.util.HashSet)8 CandidateUnit (meghanada.reflect.CandidateUnit)7 List (java.util.List)6 Map (java.util.Map)6 Optional (java.util.Optional)6 IOException (java.io.IOException)5 ClassScope (meghanada.analyze.ClassScope)5 Config (meghanada.config.Config)5 MemberDescriptor (meghanada.reflect.MemberDescriptor)5 LogManager (org.apache.logging.log4j.LogManager)5 Logger (org.apache.logging.log4j.Logger)5 Stopwatch (com.google.common.base.Stopwatch)4 UncheckedIOException (java.io.UncheckedIOException)4 Collection (java.util.Collection)4 Collections (java.util.Collections)4