Search in sources :

Example 1 with ClassScope

use of meghanada.analyze.ClassScope 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 2 with ClassScope

use of meghanada.analyze.ClassScope 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)

Example 3 with ClassScope

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

the class TypeInfoSearcher method searchClassCondition.

private static Optional<String> searchClassCondition(Source source, int line, int col, String symbol) {
    final CachedASMReflector reflector = CachedASMReflector.getInstance();
    Optional<String> result;
    String fqcn = source.getImportedClassFQCN(symbol, null);
    if (isNull(fqcn)) {
        if (!source.getPackageName().isEmpty() && !symbol.isEmpty()) {
            fqcn = source.getPackageName() + '.' + symbol;
            result = reflector.containsClassIndex(fqcn).map(index -> {
                return Optional.of(index.getDeclaration());
            }).orElseGet(() -> {
                final Set<String> parents = new HashSet<>(8);
                for (final ClassScope classScope : source.getClassScopes()) {
                    final String className = classScope.getFQCN();
                    parents.add(className);
                }
                parents.addAll(source.importClasses);
                for (final ClassIndex index : reflector.searchInnerClasses(parents)) {
                    final String returnType = index.getReturnType();
                    if (returnType.endsWith(symbol)) {
                        return Optional.of(returnType);
                    }
                }
                return Optional.empty();
            });
        } else {
            result = Optional.empty();
        }
    } else {
        result = Optional.of(fqcn);
    }
    return result;
}
Also used : ClassIndex(meghanada.reflect.ClassIndex) CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) HashSet(java.util.HashSet) Set(java.util.Set) ClassScope(meghanada.analyze.ClassScope)

Example 4 with ClassScope

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

the class DeclarationSearcher method searchClassOrInterface.

private static Optional<Declaration> searchClassOrInterface(final Source source, final Integer line, final Integer col, final String symbol) {
    // TODO need tune
    final EntryMessage entryMessage = log.traceEntry("line={} col={} symbol={}", line, col, symbol);
    final CachedASMReflector reflector = CachedASMReflector.getInstance();
    Optional<Declaration> result;
    String fqcn = source.getImportedClassFQCN(symbol, null);
    if (fqcn == null) {
        if (!source.getPackageName().isEmpty()) {
            fqcn = source.getPackageName() + '.' + symbol;
            result = reflector.containsClassIndex(fqcn).map(classIndex -> {
                final Declaration declaration = new Declaration(symbol, classIndex.getReturnType(), Declaration.Type.CLASS, 0);
                return Optional.of(declaration);
            }).orElseGet(() -> {
                final Set<String> parents = new HashSet<>(8);
                for (final ClassScope classScope : source.getClassScopes()) {
                    final String className = classScope.getFQCN();
                    parents.add(className);
                }
                parents.addAll(source.importClasses);
                for (final ClassIndex ci : reflector.searchInnerClasses(parents)) {
                    final String returnType = ci.getReturnType();
                    if (returnType.endsWith(symbol)) {
                        final Declaration d = new Declaration(symbol, returnType, Declaration.Type.CLASS, 0);
                        return Optional.of(d);
                    }
                }
                return Optional.empty();
            });
        } else {
            result = Optional.empty();
        }
    } else {
        final Declaration declaration = new Declaration(symbol, fqcn, Declaration.Type.CLASS, 0);
        result = Optional.of(declaration);
    }
    log.traceExit(entryMessage);
    return result;
}
Also used : ClassIndex(meghanada.reflect.ClassIndex) CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) Set(java.util.Set) HashSet(java.util.HashSet) EntryMessage(org.apache.logging.log4j.message.EntryMessage) ClassScope(meghanada.analyze.ClassScope)

Example 5 with ClassScope

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

the class ReferenceSearcher method searchClassCondition.

private static Optional<SearchCondition> searchClassCondition(Source source, int line, int col, String symbol) {
    final CachedASMReflector reflector = CachedASMReflector.getInstance();
    final EntryMessage entryMessage = log.traceEntry("line={} col={} symbol={}", line, col, symbol);
    Optional<SearchCondition> result;
    String fqcn = source.getImportedClassFQCN(symbol, null);
    if (isNull(fqcn)) {
        if (!source.getPackageName().isEmpty()) {
            fqcn = source.getPackageName() + '.' + symbol;
            result = reflector.containsClassIndex(fqcn).map(index -> {
                SearchCondition sc = new SearchCondition(index.getRawDeclaration(), index.getName(), SearchCondition.Type.CLASS);
                return Optional.of(sc);
            }).orElseGet(() -> {
                final Set<String> parents = new HashSet<>(8);
                for (final ClassScope classScope : source.getClassScopes()) {
                    final String className = classScope.getFQCN();
                    parents.add(className);
                }
                parents.addAll(source.importClasses);
                for (final ClassIndex index : reflector.searchInnerClasses(parents)) {
                    final String returnType = index.getReturnType();
                    if (returnType.endsWith(symbol)) {
                        SearchCondition sc = new SearchCondition(index.getRawDeclaration(), index.getName(), SearchCondition.Type.CLASS);
                        return Optional.of(sc);
                    }
                }
                return Optional.empty();
            });
        } else {
            result = Optional.empty();
        }
    } else {
        SearchCondition sc = new SearchCondition(fqcn, ClassNameUtils.getSimpleName(fqcn), SearchCondition.Type.CLASS);
        result = Optional.of(sc);
    }
    log.traceExit(entryMessage);
    return result;
}
Also used : ClassIndex(meghanada.reflect.ClassIndex) CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) HashSet(java.util.HashSet) Set(java.util.Set) EntryMessage(org.apache.logging.log4j.message.EntryMessage) ClassScope(meghanada.analyze.ClassScope)

Aggregations

ClassScope (meghanada.analyze.ClassScope)5 HashSet (java.util.HashSet)4 CachedASMReflector (meghanada.reflect.asm.CachedASMReflector)4 Set (java.util.Set)3 ClassIndex (meghanada.reflect.ClassIndex)3 EntryMessage (org.apache.logging.log4j.message.EntryMessage)3 Variable (meghanada.analyze.Variable)2 Collection (java.util.Collection)1 Map (java.util.Map)1 Optional (java.util.Optional)1 BlockScope (meghanada.analyze.BlockScope)1 MethodCall (meghanada.analyze.MethodCall)1 MethodScope (meghanada.analyze.MethodScope)1 Position (meghanada.analyze.Position)1 CandidateUnit (meghanada.reflect.CandidateUnit)1 FieldDescriptor (meghanada.reflect.FieldDescriptor)1 MemberDescriptor (meghanada.reflect.MemberDescriptor)1