Search in sources :

Example 1 with FieldDescriptor

use of meghanada.reflect.FieldDescriptor 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 FieldDescriptor

use of meghanada.reflect.FieldDescriptor in project meghanada-server by mopemope.

the class FieldAnalyzeVisitor method visitEnd.

@Override
public void visitEnd() {
    final EntryMessage m = log.traceEntry("fieldSignature={}", fieldSignature);
    final String modifier = ASMReflector.toModifier(access, false);
    final String fqcn = fieldSignatureVisitor.getResult();
    final FieldDescriptor fd = new FieldDescriptor(this.classAnalyzeVisitor.className, this.name, modifier, fqcn);
    fd.setTypeParameters(fieldSignatureVisitor.getTypeParameters());
    this.classAnalyzeVisitor.getMembers().add(fd);
    log.traceExit(m);
}
Also used : EntryMessage(org.apache.logging.log4j.message.EntryMessage) FieldDescriptor(meghanada.reflect.FieldDescriptor)

Aggregations

FieldDescriptor (meghanada.reflect.FieldDescriptor)2 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 MethodCall (meghanada.analyze.MethodCall)1 Variable (meghanada.analyze.Variable)1 CandidateUnit (meghanada.reflect.CandidateUnit)1 MemberDescriptor (meghanada.reflect.MemberDescriptor)1 CachedASMReflector (meghanada.reflect.asm.CachedASMReflector)1 EntryMessage (org.apache.logging.log4j.message.EntryMessage)1