Search in sources :

Example 16 with CachedASMReflector

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

the class JavaCompletion method completionSymbols.

private static Collection<? extends CandidateUnit> completionSymbols(Source source, int line, String prefix) {
    Set<CandidateUnit> result = new HashSet<>(32);
    // prefix search
    log.debug("Search variables prefix:{} line:{}", prefix, line);
    Optional<TypeScope> typeScope = source.getTypeScope(line);
    if (!typeScope.isPresent()) {
        return result;
    }
    String fqcn = typeScope.get().getFQCN();
    // add this member
    for (MemberDescriptor c : JavaCompletion.reflectSelf(fqcn, true, prefix)) {
        if (c.getName().startsWith(prefix)) {
            result.add(c);
        }
    }
    if (fqcn.contains(ClassNameUtils.INNER_MARK)) {
        // add parent
        String parentClass = fqcn;
        while (true) {
            int i = parentClass.lastIndexOf('$');
            if (i < 0) {
                break;
            }
            parentClass = parentClass.substring(0, i);
            for (MemberDescriptor c : JavaCompletion.reflectSelf(parentClass, true, prefix)) {
                if (c.getName().startsWith(prefix)) {
                    result.add(c);
                }
            }
        }
    }
    log.debug("self fqcn:{}", fqcn);
    Map<String, Variable> symbols = source.getDeclaratorMap(line);
    log.debug("search variables size:{} result:{}", symbols.size(), symbols);
    for (Map.Entry<String, Variable> e : symbols.entrySet()) {
        String k = e.getKey();
        Variable v = e.getValue();
        log.debug("check variable name:{}", k);
        if (k.startsWith(prefix)) {
            log.debug("match variable name:{}", k);
            if (!v.isField) {
                result.add(v.toCandidateUnit());
            }
        }
    }
    // import
    for (Map.Entry<String, String> e : source.getImportedClassMap().entrySet()) {
        String k = e.getKey();
        String v = e.getValue();
        if (k.startsWith(prefix)) {
            result.add(ClassIndex.createClass(v));
        }
    }
    // static import
    for (Map.Entry<String, String> e : source.staticImportClass.entrySet()) {
        String methodName = e.getKey();
        String clazz = e.getValue();
        for (MemberDescriptor md : JavaCompletion.reflectWithFQCN(clazz, methodName)) {
            if (md.getName().equals(methodName)) {
                result.add(md);
            }
        }
    }
    // Add class
    if (Character.isUpperCase(prefix.charAt(0))) {
        // completion
        CachedASMReflector reflector = CachedASMReflector.getInstance();
        boolean fuzzySearch = Config.load().useClassFuzzySearch();
        if (fuzzySearch) {
            result.addAll(reflector.fuzzySearchClasses(prefix.toLowerCase()));
        } else {
            result.addAll(reflector.searchClasses(prefix.toLowerCase()));
        }
    }
    List<CandidateUnit> list = new ArrayList<>(result);
    list.sort(comparing(source, prefix));
    return list;
}
Also used : CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) Variable(meghanada.analyze.Variable) MemberDescriptor(meghanada.reflect.MemberDescriptor) ArrayList(java.util.ArrayList) TypeScope(meghanada.analyze.TypeScope) CandidateUnit(meghanada.reflect.CandidateUnit) Map(java.util.Map) HashSet(java.util.HashSet)

Example 17 with CachedASMReflector

use of meghanada.reflect.asm.CachedASMReflector 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 18 with CachedASMReflector

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

the class JavaCompletion method completionNewKeyword.

private Collection<? extends CandidateUnit> completionNewKeyword(Source source, String searchWord) {
    final boolean useFuzzySearch = Config.load().useClassFuzzySearch();
    // list all classes
    final int idx = searchWord.lastIndexOf(':');
    if (idx > 0) {
        final List<ClassIndex> result;
        final String classPrefix = searchWord.substring(idx + 1, searchWord.length());
        CachedASMReflector reflector = CachedASMReflector.getInstance();
        if (useFuzzySearch) {
            result = reflector.fuzzySearchClasses(classPrefix.toLowerCase());
        } else {
            result = reflector.searchClasses(classPrefix.toLowerCase());
        }
        result.sort(comparing(source, classPrefix));
        return result;
    }
    return JavaCompletion.completionNewKeyword(source).stream().sorted(comparing(source, "")).collect(Collectors.toList());
}
Also used : ClassIndex(meghanada.reflect.ClassIndex) CachedASMReflector(meghanada.reflect.asm.CachedASMReflector)

Example 19 with CachedASMReflector

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

the class ClassIndex method isImplements.

private boolean isImplements(final String fqcn) {
    final String className = ClassNameUtils.removeTypeParameter(this.declaration);
    if (className.equals(fqcn)) {
        return true;
    }
    final CachedASMReflector reflector = CachedASMReflector.getInstance();
    final Optional<String> result = this.supers.stream().filter(s -> {
        final String name = ClassNameUtils.removeTypeParameter(s);
        return reflector.containsClassIndex(name).map(classIndex -> classIndex.isImplements(fqcn)).orElse(false);
    }).findFirst();
    return result.isPresent();
}
Also used : StoreTransaction(jetbrains.exodus.entitystore.StoreTransaction) CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) Entity(jetbrains.exodus.entitystore.Entity) Serializable(java.io.Serializable) List(java.util.List) ClassNameUtils(meghanada.utils.ClassNameUtils) Optional(java.util.Optional) Objects.isNull(java.util.Objects.isNull) Objects.nonNull(java.util.Objects.nonNull) Objects(com.google.common.base.Objects) Collections(java.util.Collections) EntityId(jetbrains.exodus.entitystore.EntityId) Storable(meghanada.store.Storable) Joiner(com.google.common.base.Joiner) CachedASMReflector(meghanada.reflect.asm.CachedASMReflector)

Example 20 with CachedASMReflector

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

the class Session method start.

public void start() throws IOException {
    if (this.started) {
        return;
    }
    this.setupSubscribes();
    log.debug("session start");
    final Set<File> temp = new HashSet<>(currentProject.getSources());
    temp.addAll(currentProject.getTestSources());
    this.sessionEventBus.requestWatchFiles(new ArrayList<>(temp));
    // load once
    final CachedASMReflector reflector = CachedASMReflector.getInstance();
    reflector.addClasspath(Session.getSystemJars());
    this.sessionEventBus.requestCreateCache();
    this.projects.values().forEach(project -> this.sessionEventBus.requestWatchFile(project.getProjectRoot()));
    log.debug("session started");
    this.started = true;
}
Also used : CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) File(java.io.File) HashSet(java.util.HashSet)

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