Search in sources :

Example 16 with ClassIndex

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

the class ASMReflector method getClasses.

Map<ClassIndex, File> getClasses(final File file) throws IOException {
    final Map<ClassIndex, File> indexes = new ConcurrentHashMap<>(32);
    if (ModuleHelper.isJrtFsFile(file)) {
        ModuleHelper.walkModule(path -> ModuleHelper.pathToClassData(path).ifPresent(cd -> {
            String className = cd.getClassName();
            String moduleName = cd.getModuleName();
            if (this.ignorePackage(className)) {
                return;
            }
            try (final InputStream in = cd.getInputStream()) {
                ASMReflector.readClassIndex(indexes, in, file, false);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }));
    } else if (file.isFile() && file.getName().endsWith("jar")) {
        try (final JarFile jarFile = new JarFile(file);
            final Stream<JarEntry> jarStream = jarFile.stream().parallel();
            final Stream<JarEntry> stream = jarStream.filter(jarEntry -> jarEntry.getName().endsWith(".class")).collect(Collectors.toList()).parallelStream()) {
            stream.forEach(wrapIOConsumer(jarEntry -> {
                final String entryName = jarEntry.getName();
                if (!entryName.endsWith(".class")) {
                    return;
                }
                final String className = ClassNameUtils.replaceSlash(entryName.substring(0, entryName.length() - 6));
                if (this.ignorePackage(className)) {
                    return;
                }
                try (final InputStream in = jarFile.getInputStream(jarEntry)) {
                    ASMReflector.readClassIndex(indexes, in, file, false);
                }
            }));
        }
    } else if (file.isFile() && file.getName().endsWith(".class")) {
        final String entryName = file.getName();
        if (!entryName.endsWith(".class")) {
            return indexes;
        }
        final String className = ClassNameUtils.replaceSlash(entryName.substring(0, entryName.length() - 6));
        if (this.ignorePackage(className)) {
            return indexes;
        }
        try (final InputStream in = new FileInputStream(file)) {
            ASMReflector.readClassIndex(indexes, in, file, true);
        }
    } else if (file.isDirectory()) {
        try (final Stream<Path> pathStream = Files.walk(file.toPath());
            final Stream<File> stream = pathStream.map(Path::toFile).filter(f -> f.isFile() && f.getName().endsWith(".class")).collect(Collectors.toList()).stream()) {
            stream.forEach(wrapIOConsumer(classFile -> {
                final String entryName = classFile.getName();
                if (!entryName.endsWith(".class")) {
                    return;
                }
                final String className = ClassNameUtils.replaceSlash(entryName.substring(0, entryName.length() - 6));
                if (this.ignorePackage(className)) {
                    return;
                }
                try (final InputStream in = new FileInputStream(classFile)) {
                    ASMReflector.readClassIndex(indexes, in, file, true);
                }
            }));
        }
    }
    return indexes;
}
Also used : Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) JarFile(java.util.jar.JarFile) FunctionUtils.wrapIOConsumer(meghanada.utils.FunctionUtils.wrapIOConsumer) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) JarEntry(java.util.jar.JarEntry) Map(java.util.Map) CandidateUnit(meghanada.reflect.CandidateUnit) Objects.isNull(java.util.Objects.isNull) Path(java.nio.file.Path) FunctionUtils.wrapIO(meghanada.utils.FunctionUtils.wrapIO) Opcodes(org.objectweb.asm.Opcodes) Iterator(java.util.Iterator) Files(java.nio.file.Files) ClassIndex(meghanada.reflect.ClassIndex) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) File(java.io.File) UncheckedIOException(java.io.UncheckedIOException) MemberDescriptor(meghanada.reflect.MemberDescriptor) List(java.util.List) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) ClassReader(org.objectweb.asm.ClassReader) ClassNameUtils(meghanada.utils.ClassNameUtils) ModuleHelper(meghanada.module.ModuleHelper) Optional(java.util.Optional) Objects.nonNull(java.util.Objects.nonNull) Collections(java.util.Collections) Config(meghanada.config.Config) LogManager(org.apache.logging.log4j.LogManager) InputStream(java.io.InputStream) Path(java.nio.file.Path) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) JarFile(java.util.jar.JarFile) FileInputStream(java.io.FileInputStream) ClassIndex(meghanada.reflect.ClassIndex) FileInputStream(java.io.FileInputStream) Stream(java.util.stream.Stream) InputStream(java.io.InputStream) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 17 with ClassIndex

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

the class ASMReflector method readSuperMembers.

private void readSuperMembers(File parent, ClassAnalyzeVisitor cv, List<MemberDescriptor> units) {
    final ClassIndex classIndex = cv.getClassIndex();
    List<List<MemberDescriptor>> lists = classIndex.getSupers().stream().parallel().map(wrapIO(s -> reflect(parent, s))).collect(Collectors.toList());
    lists.forEach(units::addAll);
}
Also used : ClassIndex(meghanada.reflect.ClassIndex) ArrayList(java.util.ArrayList) List(java.util.List)

Example 18 with ClassIndex

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

the class CachedASMReflector method addClassIndex.

private void addClassIndex(ClassIndex newIndex, File file) {
    final String fqcn = newIndex.getRawDeclaration();
    ClassIndex old = this.globalClassIndex.get(fqcn);
    if (nonNull(old)) {
        EntityId entityId = old.getEntityId();
        if (nonNull(entityId)) {
            // inheriting entityID
            newIndex.setEntityID(entityId);
        }
    }
    try {
        if (ModuleHelper.isJrtFsFile(file)) {
            newIndex.setFilePath(file.getPath());
        } else {
            newIndex.setFilePath(file.getCanonicalPath());
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    this.globalClassIndex.put(fqcn, newIndex);
}
Also used : EntityId(jetbrains.exodus.entitystore.EntityId) ClassIndex(meghanada.reflect.ClassIndex) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 19 with ClassIndex

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

the class ProjectDatabaseHelper method getClassIndexLinks.

public static void getClassIndexLinks(String fqcn, String linkName, Consumer<EntityIterable> fn) throws Exception {
    Map<String, ClassIndex> globalClassIndex = CachedASMReflector.getInstance().getGlobalClassIndex();
    if (!globalClassIndex.containsKey(fqcn)) {
        return;
    }
    ClassIndex index = globalClassIndex.get(fqcn);
    EntityId entityId = index.getEntityId();
    ProjectDatabase database = ProjectDatabase.getInstance();
    boolean result = database.execute(txn -> {
        Entity classEntity = txn.getEntity(entityId);
        EntityIterable iterable = classEntity.getLinks(linkName);
        fn.accept(iterable);
        return true;
    });
}
Also used : EntityId(jetbrains.exodus.entitystore.EntityId) ClassIndex(meghanada.reflect.ClassIndex) Entity(jetbrains.exodus.entitystore.Entity) EntityIterable(jetbrains.exodus.entitystore.EntityIterable)

Example 20 with ClassIndex

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

Aggregations

ClassIndex (meghanada.reflect.ClassIndex)42 File (java.io.File)25 Test (org.junit.Test)22 Config (meghanada.config.Config)16 List (java.util.List)15 Map (java.util.Map)15 MemberDescriptor (meghanada.reflect.MemberDescriptor)15 Stopwatch (com.google.common.base.Stopwatch)13 GradleTestBase (meghanada.GradleTestBase)12 Config.debugIt (meghanada.config.Config.debugIt)12 Config.timeIt (meghanada.config.Config.timeIt)12 MethodDescriptor (meghanada.reflect.MethodDescriptor)12 AfterClass (org.junit.AfterClass)12 Assert.assertEquals (org.junit.Assert.assertEquals)12 Assert.assertNotNull (org.junit.Assert.assertNotNull)12 BeforeClass (org.junit.BeforeClass)12 ArrayList (java.util.ArrayList)10 CachedASMReflector (meghanada.reflect.asm.CachedASMReflector)8 JarFile (java.util.jar.JarFile)7 IOException (java.io.IOException)4