Search in sources :

Example 26 with CachedASMReflector

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

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

the class LocationSearcher method searchFieldAccess.

private Optional<Location> searchFieldAccess(final Source src, final int line, final int col, final String symbol) {
    final EntryMessage entryMessage = log.traceEntry("line={} col={} symbol={}", line, col, symbol);
    final Optional<Location> result = src.searchFieldAccess(line, col, symbol).flatMap(fa -> {
        final String fieldName = fa.name;
        final String declaringClass = fa.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(fqcn -> existsFQCN(project.getAllSourcesWithDependencies(), fqcn).flatMap(file -> getFieldLocationFromProject(fqcn, fieldName, file)).orElseGet(wrapIO(() -> {
            final SearchContext context = new SearchContext(fqcn, SearchKind.FIELD);
            context.name = fieldName;
            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)

Example 28 with CachedASMReflector

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

Example 29 with CachedASMReflector

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

the class MemberCacheLoader method storeMembers.

private void storeMembers(final String fqcn, final List<MemberDescriptor> list) {
    final CachedASMReflector reflector = CachedASMReflector.getInstance();
    reflector.containsClassIndex(fqcn).map(wrapIO(index -> ProjectDatabaseHelper.saveMemberDescriptors(index.getRawDeclaration(), list))).orElseGet(() -> {
        final String innerFQCN = ClassNameUtils.replaceInnerMark(fqcn);
        reflector.containsClassIndex(innerFQCN).ifPresent(wrapIOConsumer(index -> ProjectDatabaseHelper.saveMemberDescriptors(index.getRawDeclaration(), list)));
        return true;
    });
}
Also used : ClassName(meghanada.utils.ClassName) Stopwatch(com.google.common.base.Stopwatch) FunctionUtils.wrapIOConsumer(meghanada.utils.FunctionUtils.wrapIOConsumer) InheritanceInfo(meghanada.reflect.asm.InheritanceInfo) Map(java.util.Map) CandidateUnit(meghanada.reflect.CandidateUnit) Objects.isNull(java.util.Objects.isNull) FunctionUtils.wrapIO(meghanada.utils.FunctionUtils.wrapIO) RemovalNotification(com.google.common.cache.RemovalNotification) ClassIndex(meghanada.reflect.ClassIndex) IOException(java.io.IOException) ProjectDatabaseHelper(meghanada.store.ProjectDatabaseHelper) CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) Collectors(java.util.stream.Collectors) File(java.io.File) CacheLoader(com.google.common.cache.CacheLoader) MemberDescriptor(meghanada.reflect.MemberDescriptor) List(java.util.List) Logger(org.apache.logging.log4j.Logger) RemovalCause(com.google.common.cache.RemovalCause) ClassNameUtils(meghanada.utils.ClassNameUtils) Optional(java.util.Optional) RemovalListener(com.google.common.cache.RemovalListener) ASMReflector(meghanada.reflect.asm.ASMReflector) Objects.nonNull(java.util.Objects.nonNull) Collections(java.util.Collections) Config(meghanada.config.Config) LogManager(org.apache.logging.log4j.LogManager) CachedASMReflector(meghanada.reflect.asm.CachedASMReflector)

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