Search in sources :

Example 6 with MethodCall

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

the class LocationSearcher method searchMethodCall.

private Optional<Location> searchMethodCall(final Source source, final int line, final int 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<Location> result = methodCall.flatMap(mc -> {
        final String methodName = mc.name;
        final List<String> arguments = mc.getArguments();
        final String declaringClass = mc.declaringClass;
        if (declaringClass == null) {
            return Optional.empty();
        }
        final List<String> searchTargets = new ArrayList<>(2);
        searchTargets.add(declaringClass);
        final CachedASMReflector reflector = CachedASMReflector.getInstance();
        searchTargets.addAll(reflector.getSuperClass(declaringClass));
        return searchTargets.stream().map(targetFqcn -> existsFQCN(project.getAllSourcesWithDependencies(), targetFqcn).flatMap(file -> getMethodLocationFromProject(methodName, arguments, file)).orElseGet(wrapIO(() -> {
            final SearchContext context = new SearchContext(targetFqcn, SearchKind.METHOD);
            context.name = methodName;
            context.arguments = arguments;
            return Optional.ofNullable(searchFromSrcZip(context)).orElseGet(wrapIO(() -> searchFromDependency(context)));
        }))).filter(Objects::nonNull).findFirst();
    });
    log.traceExit(entryMessage);
    return result;
}
Also used : CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) ArrayList(java.util.ArrayList) EntryMessage(org.apache.logging.log4j.message.EntryMessage) MethodCall(meghanada.analyze.MethodCall)

Example 7 with MethodCall

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

the class JavaCompletion method completionFieldsOrMethods.

private static Collection<? extends CandidateUnit> completionFieldsOrMethods(final Source source, final int line, final String var, final String target) {
    // completionAt methods or fields
    if (var.equals("this")) {
        return JavaCompletion.completionThis(source, line, target);
    }
    if (var.equals("super")) {
        return JavaCompletion.completionSuper(source, line, target);
    }
    log.debug("search '{}' field or method", var);
    String ownPackage = source.getPackageName();
    final Set<CandidateUnit> res = new HashSet<>(32);
    {
        // completion static method
        String fqcn = source.getImportedClassFQCN(var, null);
        if (nonNull(fqcn)) {
            if (!fqcn.contains(".") && !ownPackage.isEmpty()) {
                fqcn = ownPackage + '.' + fqcn;
            }
            final Collection<? extends CandidateUnit> result = JavaCompletion.reflect(ownPackage, fqcn, true, false, target);
            res.addAll(result);
            // add inner class
            final Collection<? extends CandidateUnit> inners = CachedASMReflector.getInstance().searchInnerClasses(fqcn);
            res.addAll(inners);
            if (!res.isEmpty()) {
                return res;
            }
        }
    }
    {
        final Map<String, Variable> symbols = source.getDeclaratorMap(line);
        final Variable variable = symbols.get(var);
        if (nonNull(variable)) {
            // get data from reflector
            String fqcn = variable.fqcn;
            if (!fqcn.contains(".")) {
                fqcn = ownPackage + '.' + fqcn;
            }
            final Collection<? extends CandidateUnit> reflect = JavaCompletion.reflect(ownPackage, fqcn, target);
            res.addAll(reflect);
        }
    }
    {
        for (final ClassScope cs : source.getClassScopes()) {
            final String fqcn = cs.getFQCN();
            final Optional<MemberDescriptor> fieldResult = JavaCompletion.reflectSelf(fqcn, true, target).stream().filter(c -> c instanceof FieldDescriptor && c.getName().equals(var)).findFirst();
            if (fieldResult.isPresent()) {
                final MemberDescriptor memberDescriptor = fieldResult.orElse(null);
                final String returnType = memberDescriptor.getRawReturnType();
                final Collection<? extends CandidateUnit> reflect = reflect(ownPackage, returnType, target);
                res.addAll(reflect);
            }
        }
    }
    {
        // java.lang
        final String fqcn = "java.lang." + var;
        final Collection<? extends CandidateUnit> result = JavaCompletion.reflect(ownPackage, fqcn, true, false, target);
        res.addAll(result);
    }
    {
        String fqcn = var;
        if (!ownPackage.isEmpty()) {
            fqcn = ownPackage + '.' + var;
        }
        final Collection<? extends CandidateUnit> reflectResults = JavaCompletion.reflect(ownPackage, fqcn, true, false, target);
        res.addAll(reflectResults);
        final CachedASMReflector reflector = CachedASMReflector.getInstance();
        if (reflector.containsFQCN(fqcn)) {
            final Collection<? extends CandidateUnit> inners = reflector.searchInnerClasses(fqcn);
            res.addAll(inners);
        }
    }
    if (line > 0 && res.isEmpty()) {
        List<MethodCall> calls = source.getMethodCall(line - 1);
        long lastCol = 0;
        String lastFQCN = null;
        for (MethodCall call : calls) {
            long col = call.nameRange.begin.column;
            String name = ClassNameUtils.getSimpleName(call.name);
            if (name.equals(var) && col > lastCol) {
                lastFQCN = call.returnType;
                lastCol = col;
            }
        }
        if (nonNull(lastFQCN)) {
            res.addAll(reflectWithFQCN(lastFQCN, ""));
        }
    }
    return res;
}
Also used : CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) Variable(meghanada.analyze.Variable) Optional(java.util.Optional) MemberDescriptor(meghanada.reflect.MemberDescriptor) MethodCall(meghanada.analyze.MethodCall) ClassScope(meghanada.analyze.ClassScope) FieldDescriptor(meghanada.reflect.FieldDescriptor) Collection(java.util.Collection) CandidateUnit(meghanada.reflect.CandidateUnit) Map(java.util.Map) HashSet(java.util.HashSet)

Example 8 with MethodCall

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

the class ReferenceSearcher method searchMethodCallReferences.

private static List<Reference> searchMethodCallReferences(Source src, SearchCondition sc) throws IOException {
    List<String> lines = FileUtils.readLines(src.getFile());
    int size = lines.size();
    List<Reference> result = new ArrayList<>(size);
    Collection<MethodCall> methodCalls = src.getMethodCalls();
    for (MethodCall mc : methodCalls) {
        String declaringClass = mc.declaringClass;
        String methodName = mc.name;
        boolean compare = ClassNameUtils.compareArgumentType(mc.getArguments(), sc.arguments);
        if (compare && sc.declaringClass.equals(declaringClass) && sc.name.equals(methodName)) {
            Range range = mc.nameRange;
            long line = range.begin.line;
            long column = range.begin.column;
            String code = StringEscapeUtils.escapeJava(lines.get((int) line - 1));
            Reference ref = new Reference(src.filePath, line, column, code);
            result.add(ref);
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Range(meghanada.analyze.Range) MethodCall(meghanada.analyze.MethodCall)

Example 9 with MethodCall

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

the class JavaVariableCompletionTest method createLocalVariable5.

@Test
public void createLocalVariable5() throws Exception {
    JavaVariableCompletion completion = getCompilation();
    Optional<LocalVariable> olv = timeIt(() -> {
        final Range range = new Range(0, 0, 0, 0);
        final Range nameRange = new Range(0, 0, 0, 0);
        final MethodCall mcs = new MethodCall("parser", "parse", 0, nameRange, range);
        return completion.createLocalVariable(mcs, "org.apache.commons.cli.CommandLine");
    });
    LocalVariable lv = olv.get();
    System.out.println(lv);
    assertEquals(4, lv.getCandidates().size());
}
Also used : Range(meghanada.analyze.Range) MethodCall(meghanada.analyze.MethodCall) Test(org.junit.Test)

Example 10 with MethodCall

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

the class ReferenceSearcher method searchClassReferences.

private static List<Reference> searchClassReferences(Source src, SearchCondition sc) throws IOException {
    List<String> lines = FileUtils.readLines(src.getFile());
    int size = lines.size();
    List<Reference> result = new ArrayList<>(size);
    Collection<MethodCall> methodCalls = src.getMethodCalls();
    for (MethodCall mc : methodCalls) {
        String declaringClass = mc.declaringClass;
        if (sc.declaringClass.equals(declaringClass) && mc.scope.contains(sc.name)) {
            Range range = mc.nameRange;
            long line = range.begin.line;
            long column = range.begin.column;
            String code = StringEscapeUtils.escapeJava(lines.get((int) line - 1));
            Reference ref = new Reference(src.filePath, line, column, code);
            result.add(ref);
        }
    }
    Collection<FieldAccess> fieldAccesses = src.getFieldAccesses();
    for (FieldAccess fa : fieldAccesses) {
        String declaringClass = fa.declaringClass;
        if (sc.declaringClass.equals(declaringClass) && fa.scope.contains(sc.name)) {
            Range range = fa.range;
            long line = range.begin.line;
            long column = range.begin.column;
            String code = StringEscapeUtils.escapeJava(lines.get((int) line - 1));
            Reference ref = new Reference(src.filePath, line, column, code);
            result.add(ref);
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Range(meghanada.analyze.Range) FieldAccess(meghanada.analyze.FieldAccess) MethodCall(meghanada.analyze.MethodCall)

Aggregations

MethodCall (meghanada.analyze.MethodCall)11 Range (meghanada.analyze.Range)7 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)3 MemberDescriptor (meghanada.reflect.MemberDescriptor)3 CachedASMReflector (meghanada.reflect.asm.CachedASMReflector)3 EntryMessage (org.apache.logging.log4j.message.EntryMessage)3 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Optional (java.util.Optional)1 ClassScope (meghanada.analyze.ClassScope)1 FieldAccess (meghanada.analyze.FieldAccess)1 Variable (meghanada.analyze.Variable)1 CandidateUnit (meghanada.reflect.CandidateUnit)1 FieldDescriptor (meghanada.reflect.FieldDescriptor)1 MethodDescriptor (meghanada.reflect.MethodDescriptor)1