Search in sources :

Example 1 with Filter

use of org.revapi.query.Filter in project revapi by revapi.

the class ClasspathScanner method initTree.

void initTree() throws IOException {
    List<ArchiveLocation> classPathLocations = classPath.keySet().stream().map(ArchiveLocation::new).collect(toList());
    Scanner scanner = new Scanner();
    for (ArchiveLocation loc : classPathLocations) {
        scanner.scan(loc, classPath.get(loc.getArchive()), true);
    }
    SyntheticLocation allLoc = new SyntheticLocation();
    fileManager.setLocation(allLoc, classPath.values());
    Function<String, JavaFileObject> searchHard = className -> Stream.concat(Stream.of(allLoc), Stream.of(POSSIBLE_SYSTEM_CLASS_LOCATIONS)).map(l -> {
        try {
            return fileManager.getJavaFileForInput(l, className, JavaFileObject.Kind.CLASS);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }).filter(Objects::nonNull).findFirst().orElse(null);
    Set<TypeElement> lastUnknowns = Collections.emptySet();
    Map<String, ArchiveLocation> cachedArchives = new HashMap<>(additionalClassPath.size());
    while (!scanner.requiredTypes.isEmpty() && !lastUnknowns.equals(scanner.requiredTypes.keySet())) {
        lastUnknowns = new HashSet<>(scanner.requiredTypes.keySet());
        for (TypeElement t : lastUnknowns) {
            String name = environment.getElementUtils().getBinaryName(t).toString();
            JavaFileObject jfo = searchHard.apply(name);
            if (jfo == null) {
                // this type is really missing
                continue;
            }
            URI uri = jfo.toUri();
            String path;
            if ("jar".equals(uri.getScheme())) {
                // we pass our archives as jars, so let's dig only into those
                path = uri.getSchemeSpecificPart();
                // jar:file:/path .. let's get rid of the "file:" part
                int colonIdx = path.indexOf(':');
                if (colonIdx >= 0) {
                    path = path.substring(colonIdx + 1);
                }
                // separate the file path from the in-jar path
                path = path.substring(0, path.lastIndexOf('!'));
                // remove superfluous forward slashes at the start of the path, if any
                int lastSlashIdx = -1;
                for (int i = 0; i < path.length() - 1; ++i) {
                    if (path.charAt(i) == '/' && path.charAt(i + 1) != '/') {
                        lastSlashIdx = i;
                        break;
                    }
                }
                if (lastSlashIdx > 0) {
                    path = path.substring(lastSlashIdx);
                }
            } else {
                path = uri.getPath();
            }
            ArchiveLocation loc = cachedArchives.get(path);
            if (loc == null) {
                Archive ar = null;
                for (Map.Entry<Archive, File> e : additionalClassPath.entrySet()) {
                    if (e.getValue().getAbsolutePath().equals(path)) {
                        ar = e.getKey();
                        break;
                    }
                }
                if (ar != null) {
                    loc = new ArchiveLocation(ar);
                    cachedArchives.put(path, loc);
                }
            }
            if (loc != null) {
                scanner.scanClass(loc, t, false);
            }
        }
    }
    // ok, so scanning the archives doesn't give us any new resolved classes that we need in the API...
    // let's scan the system classpath. What will be left after this will be the truly missing classes.
    // making a copy because the required types might be modified during scanning
    Map<TypeElement, Boolean> rts = new HashMap<>(scanner.requiredTypes);
    ArchiveLocation systemClassPath = new ArchiveLocation(new Archive() {

        @Nonnull
        @Override
        public String getName() {
            return SYSTEM_CLASSPATH_NAME;
        }

        @Nonnull
        @Override
        public InputStream openStream() throws IOException {
            throw new UnsupportedOperationException();
        }
    });
    for (Map.Entry<TypeElement, Boolean> e : rts.entrySet()) {
        scanner.scanClass(systemClassPath, e.getKey(), false);
    }
    scanner.initEnvironment();
}
Also used : Arrays(java.util.Arrays) SimpleTypeVisitor8(javax.lang.model.util.SimpleTypeVisitor8) Modifier(javax.lang.model.element.Modifier) JavaFileManager(javax.tools.JavaFileManager) LoggerFactory(org.slf4j.LoggerFactory) Random(java.util.Random) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) URI(java.net.URI) WildcardType(javax.lang.model.type.WildcardType) SimpleElementVisitor8(javax.lang.model.util.SimpleElementVisitor8) Collectors.toSet(java.util.stream.Collectors.toSet) EnumSet(java.util.EnumSet) ArrayType(javax.lang.model.type.ArrayType) Filter(org.revapi.query.Filter) StandardLocation(javax.tools.StandardLocation) IdentityHashMap(java.util.IdentityHashMap) EnumMap(java.util.EnumMap) AnnotationElement(org.revapi.java.model.AnnotationElement) Set(java.util.Set) Element(javax.lang.model.element.Element) Types(javax.lang.model.util.Types) Collectors(java.util.stream.Collectors) TypeKind(javax.lang.model.type.TypeKind) Objects(java.util.Objects) JavaFileObject(javax.tools.JavaFileObject) List(java.util.List) IntersectionType(javax.lang.model.type.IntersectionType) Stream(java.util.stream.Stream) TypeVisitor(javax.lang.model.type.TypeVisitor) TypeVariable(javax.lang.model.type.TypeVariable) REPORT(org.revapi.java.AnalysisConfiguration.MissingClassReporting.REPORT) IgnoreCompletionFailures(org.revapi.java.spi.IgnoreCompletionFailures) ErrorType(javax.lang.model.type.ErrorType) AnalysisConfiguration(org.revapi.java.AnalysisConfiguration) VariableElement(javax.lang.model.element.VariableElement) HashMap(java.util.HashMap) Function(java.util.function.Function) HashSet(java.util.HashSet) JavaElementFactory.elementFor(org.revapi.java.model.JavaElementFactory.elementFor) ERROR(org.revapi.java.AnalysisConfiguration.MissingClassReporting.ERROR) Util(org.revapi.java.spi.Util) DeclaredType(javax.lang.model.type.DeclaredType) Archive(org.revapi.Archive) JavaElementBase(org.revapi.java.model.JavaElementBase) ElementFilter(javax.lang.model.util.ElementFilter) MissingClassElement(org.revapi.java.model.MissingClassElement) Nonnull(javax.annotation.Nonnull) LinkedHashSet(java.util.LinkedHashSet) Logger(org.slf4j.Logger) ElementKind(javax.lang.model.element.ElementKind) ExecutableType(javax.lang.model.type.ExecutableType) NoType(javax.lang.model.type.NoType) ExecutableElement(javax.lang.model.element.ExecutableElement) IOException(java.io.IOException) AnnotationMirror(javax.lang.model.element.AnnotationMirror) File(java.io.File) FlatFilter(org.revapi.java.FlatFilter) JavaElementFactory(org.revapi.java.model.JavaElementFactory) StandardJavaFileManager(javax.tools.StandardJavaFileManager) Collectors.toList(java.util.stream.Collectors.toList) AbstractMap(java.util.AbstractMap) TypeMirror(javax.lang.model.type.TypeMirror) MethodElement(org.revapi.java.model.MethodElement) PrimitiveType(javax.lang.model.type.PrimitiveType) UseSite(org.revapi.java.spi.UseSite) ArrayDeque(java.util.ArrayDeque) Comparator(java.util.Comparator) Collections(java.util.Collections) InputStream(java.io.InputStream) Archive(org.revapi.Archive) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) URI(java.net.URI) JavaFileObject(javax.tools.JavaFileObject) Nonnull(javax.annotation.Nonnull) TypeElement(javax.lang.model.element.TypeElement) InputStream(java.io.InputStream) IOException(java.io.IOException) Objects(java.util.Objects) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) File(java.io.File)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 AbstractMap (java.util.AbstractMap)1 ArrayDeque (java.util.ArrayDeque)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 EnumMap (java.util.EnumMap)1 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 IdentityHashMap (java.util.IdentityHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Random (java.util.Random)1 Set (java.util.Set)1