Search in sources :

Example 1 with MethodScope

use of meghanada.analyze.MethodScope in project meghanada-server by mopemope.

the class LocationSearcher method getMethodLocationFromProject.

private Optional<Location> getMethodLocationFromProject(final String methodName, final List<String> arguments, final File file) {
    try {
        final Source declaringClassSrc = getSource(project, file);
        final String path = declaringClassSrc.getFile().getPath();
        return declaringClassSrc.getClassScopes().stream().flatMap(ts -> ts.getScopes().stream()).filter(bs -> {
            if (!methodName.equals(bs.getName())) {
                return false;
            }
            if (!(bs instanceof MethodScope)) {
                return false;
            }
            final MethodScope methodScope = (MethodScope) bs;
            final List<String> parameters = methodScope.getParameters();
            return ClassNameUtils.compareArgumentType(arguments, parameters);
        }).map(MethodScope.class::cast).map(ms -> new Location(path, ms.getBeginLine(), ms.getNameRange().begin.column)).findFirst();
    } catch (ExecutionException e) {
        throw new UncheckedExecutionException(e);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
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) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) List(java.util.List) ArrayList(java.util.ArrayList) UncheckedIOException(java.io.UncheckedIOException) MethodScope(meghanada.analyze.MethodScope) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Source(meghanada.analyze.Source)

Example 2 with MethodScope

use of meghanada.analyze.MethodScope in project meghanada-server by mopemope.

the class ReferenceSearcher method searchMemberCondition.

private static Optional<SearchCondition> searchMemberCondition(Source source, int line, int col, String symbol) {
    EntryMessage msg = log.traceEntry("line={} col={} symbol={}", line, col, symbol);
    for (ClassScope classScope : source.getClassScopes()) {
        for (Variable variable : classScope.getVariables()) {
            if (variable.isField) {
                Position pos = variable.range.begin;
                String name = variable.name;
                if (pos.line == line && name.equals(symbol)) {
                    String clazz = classScope.getFQCN();
                    SearchCondition condition = new SearchCondition(clazz, name, SearchCondition.Type.FIELD);
                    return Optional.of(condition);
                }
            }
        }
        for (BlockScope blockScope : classScope.getScopes()) {
            if (blockScope instanceof MethodScope) {
                MethodScope methodScope = ((MethodScope) blockScope);
                Position pos = methodScope.getNameRange().begin;
                String name = methodScope.getName();
                if (pos.line == line && name.equals(symbol)) {
                    String clazz = classScope.getFQCN();
                    SearchCondition condition = new SearchCondition(clazz, name, SearchCondition.Type.METHOD, methodScope.getParameters());
                    return Optional.of(condition);
                }
            }
        }
    }
    log.traceExit(msg);
    return Optional.empty();
}
Also used : Variable(meghanada.analyze.Variable) Position(meghanada.analyze.Position) BlockScope(meghanada.analyze.BlockScope) MethodScope(meghanada.analyze.MethodScope) EntryMessage(org.apache.logging.log4j.message.EntryMessage) ClassScope(meghanada.analyze.ClassScope)

Aggregations

ClassScope (meghanada.analyze.ClassScope)2 MethodScope (meghanada.analyze.MethodScope)2 Variable (meghanada.analyze.Variable)2 EntryMessage (org.apache.logging.log4j.message.EntryMessage)2 JavaParser (com.github.javaparser.JavaParser)1 Position (com.github.javaparser.Position)1 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 BodyDeclaration (com.github.javaparser.ast.body.BodyDeclaration)1 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)1 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)1 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 Parameter (com.github.javaparser.ast.body.Parameter)1 TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 SimpleName (com.github.javaparser.ast.expr.SimpleName)1 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1