Search in sources :

Example 21 with CachedASMReflector

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

the class Session method reloadProject.

public void reloadProject() throws IOException {
    final Project currentProject = this.currentProject;
    final File projectRoot = currentProject.getProjectRoot();
    this.projects.clear();
    if (currentProject instanceof GradleProject) {
        loadProject(projectRoot, Project.GRADLE_PROJECT_FILE).ifPresent(project -> {
            boolean ret = setProject(projectRoot, project);
        });
    } else if (currentProject instanceof MavenProject) {
        loadProject(projectRoot, Project.MVN_PROJECT_FILE).ifPresent(project -> {
            boolean ret = setProject(projectRoot, project);
        });
    } else {
        loadProject(projectRoot, Config.MEGHANADA_CONF_FILE).ifPresent(project -> {
            boolean ret = setProject(projectRoot, project);
        });
    }
    final Set<File> temp = new HashSet<>(this.currentProject.getSources());
    temp.addAll(this.currentProject.getTestSources());
    this.sessionEventBus.requestWatchFiles(new ArrayList<>(temp));
    final CachedASMReflector reflector = CachedASMReflector.getInstance();
    reflector.addClasspath(Session.getSystemJars());
    this.sessionEventBus.requestCreateCache();
    this.projects.values().forEach(project -> this.sessionEventBus.requestWatchFile(project.getProjectRoot()));
}
Also used : MavenProject(meghanada.project.maven.MavenProject) Matcher(java.util.regex.Matcher) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) CandidateUnit(meghanada.reflect.CandidateUnit) Objects.isNull(java.util.Objects.isNull) Path(java.nio.file.Path) GlobalCache(meghanada.cache.GlobalCache) TypeInfoSearcher(meghanada.typeinfo.TypeInfoSearcher) Collection(java.util.Collection) CompileResult(meghanada.analyze.CompileResult) StandardOpenOption(java.nio.file.StandardOpenOption) GradleProject(meghanada.project.gradle.GradleProject) Set(java.util.Set) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) JavaFileObject(javax.tools.JavaFileObject) List(java.util.List) Declaration(meghanada.docs.declaration.Declaration) Stream(java.util.stream.Stream) TypeInfo(meghanada.typeinfo.TypeInfo) Logger(org.apache.logging.log4j.Logger) Optional(java.util.Optional) Project(meghanada.project.Project) Pattern(java.util.regex.Pattern) Objects.nonNull(java.util.Objects.nonNull) ProjectDependency(meghanada.project.ProjectDependency) Config(meghanada.config.Config) Joiner(com.google.common.base.Joiner) Location(meghanada.location.Location) Stopwatch(com.google.common.base.Stopwatch) HashMap(java.util.HashMap) JavaCompletion(meghanada.completion.JavaCompletion) Deque(java.util.Deque) ArrayList(java.util.ArrayList) JavaVariableCompletion(meghanada.completion.JavaVariableCompletion) HashSet(java.util.HashSet) Reference(meghanada.reference.Reference) ReferenceSearcher(meghanada.reference.ReferenceSearcher) LocationSearcher(meghanada.location.LocationSearcher) LocalVariable(meghanada.completion.LocalVariable) Properties(java.util.Properties) Files(java.nio.file.Files) IOException(java.io.IOException) CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) FileUtils(meghanada.utils.FileUtils) EntryMessage(org.apache.logging.log4j.message.EntryMessage) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) MeghanadaProject(meghanada.project.meghanada.MeghanadaProject) Paths(java.nio.file.Paths) ModuleHelper(meghanada.module.ModuleHelper) Source(meghanada.analyze.Source) DeclarationSearcher(meghanada.docs.declaration.DeclarationSearcher) ArrayDeque(java.util.ArrayDeque) Comparator(java.util.Comparator) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) InputStream(java.io.InputStream) MavenProject(meghanada.project.maven.MavenProject) GradleProject(meghanada.project.gradle.GradleProject) Project(meghanada.project.Project) MeghanadaProject(meghanada.project.meghanada.MeghanadaProject) CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) MavenProject(meghanada.project.maven.MavenProject) GradleProject(meghanada.project.gradle.GradleProject) File(java.io.File) HashSet(java.util.HashSet)

Example 22 with CachedASMReflector

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

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

the class ClassNameUtils method compareArgumentType.

public static boolean compareArgumentType(final List<String> arguments, final List<String> parameters) {
    if (!parameters.isEmpty() && parameters.size() > 0 && arguments.size() > parameters.size()) {
        final String last = parameters.get(parameters.size() - 1);
        final boolean isVarargs = last.endsWith(ClassNameUtils.ARRAY);
        if (isVarargs) {
            return compareVarArgumentType(arguments, parameters);
        }
    }
    if (arguments.size() != parameters.size()) {
        return false;
    }
    final Iterator<String> iteratorA = arguments.iterator();
    final CachedASMReflector reflector = CachedASMReflector.getInstance();
    for (final String paramStr : parameters) {
        final String realArgStr = iteratorA.next();
        if (realArgStr == null) {
            return false;
        }
        if (realArgStr.equals("<null>")) {
            // match all
            continue;
        }
        if (ClassNameUtils.isArray(paramStr) != ClassNameUtils.isArray(realArgStr)) {
            return false;
        }
        final ClassName paramClass = new ClassName(paramStr);
        final ClassName argClass = new ClassName(realArgStr);
        final String paramClassName = autoBoxing(paramClass.getName());
        final String argClassName = autoBoxing(argClass.getName());
        if (paramClassName.equals(argClassName)) {
            continue;
        }
        if (implicitTypeCastMap.containsKey(argClassName)) {
            Set<String> types = implicitTypeCastMap.get(argClassName);
            if (types.contains(paramClassName)) {
                continue;
            }
        }
        final boolean result = reflector.getSuperClassStream(argClassName).anyMatch(s -> {
            final String cls = new ClassName(s).getName();
            return cls.equals(paramClassName);
        });
        if (!result) {
            return false;
        }
    }
    return true;
}
Also used : CachedASMReflector(meghanada.reflect.asm.CachedASMReflector)

Example 24 with CachedASMReflector

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

the class GradleProjectTest method setupReflector.

private void setupReflector(Project p) {
    CachedASMReflector cachedASMReflector = CachedASMReflector.getInstance();
    cachedASMReflector.addClasspath(getSystemJars());
    cachedASMReflector.addClasspath(getJars(p));
    cachedASMReflector.addClasspath(p.getOutput());
    cachedASMReflector.addClasspath(p.getTestOutput());
    cachedASMReflector.createClassIndexes();
}
Also used : CachedASMReflector(meghanada.reflect.asm.CachedASMReflector)

Example 25 with CachedASMReflector

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

the class TreeAnalyzer method markFQCN.

private static String markFQCN(Source src, String fqcn, boolean markUnUse) {
    if (fqcn.equals("void")) {
        return fqcn;
    }
    if (fqcn.startsWith("capture of") || fqcn.equals("any") || fqcn.equals("<any>")) {
        // log.warn("unknown type={}", fqcn);
        return fqcn;
    }
    String simpleName = ClassNameUtils.removeTypeAndArray(fqcn);
    simpleName = ClassNameUtils.removeWildcard(simpleName);
    if (ClassNameUtils.isPrimitive(simpleName)) {
        return fqcn;
    }
    // checkLoadable(src, fqcn, simpleName);
    for (String s : ClassNameUtils.parseTypeParameter(fqcn)) {
        if (s.startsWith(ClassNameUtils.CAPTURE_OF)) {
            String cls = ClassNameUtils.removeCapture(s);
            if (cls.equals(ClassNameUtils.CAPTURE_OF)) {
                String ignore = markFQCN(src, ClassNameUtils.OBJECT_CLASS);
            } else {
                String ignore = markFQCN(src, cls);
            }
        } else {
            String ignore = markFQCN(src, s);
        }
    }
    CachedASMReflector cachedASMReflector = CachedASMReflector.getInstance();
    Map<String, ClassIndex> classIndex = cachedASMReflector.getGlobalClassIndex();
    if (!src.importClasses.contains(simpleName) && !classIndex.containsKey(simpleName)) {
        src.addUnknown(simpleName);
    } else {
        if (markUnUse) {
            // contains
            String name = ClassNameUtils.replaceInnerMark(simpleName);
            if (src.unused.contains(name)) {
                src.unused.remove(name);
            }
            int i = simpleName.indexOf('$');
            if (i > 0) {
                String parentClass = simpleName.substring(0, i);
                if (src.unused.contains(parentClass)) {
                    src.unused.remove(parentClass);
                }
            }
        }
        File classFile = cachedASMReflector.getClassFile(simpleName);
        if (isNull(classFile) || !classFile.getName().endsWith(".jar")) {
            src.usingClasses.add(simpleName);
        }
    }
    return fqcn;
}
Also used : ClassIndex(meghanada.reflect.ClassIndex) CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) File(java.io.File)

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