Java Code Examples for javax.tools.JavaFileObject.getKind()

The following are Jave code examples for showing how to use getKind() of the javax.tools.JavaFileObject class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: javaide   File: ClassReader.java   View Source Code Vote up 6 votes
private void fillIn(PackageSymbol p,
                    Location location,
                    Iterable<JavaFileObject> files) {
    currentLoc = location;
    for (JavaFileObject fo : files) {
        switch (fo.getKind()) {
            case CLASS:
            case SOURCE: {
                // TODO pass binaryName to includeClassFile
                String binaryName = fileManager.inferBinaryName(currentLoc, fo);
                String simpleName = binaryName.substring(binaryName.lastIndexOf(".") + 1);
                if (SourceVersion.isIdentifier(simpleName) ||
                        simpleName.equals("package-info"))
                    includeClassFile(p, fo);
                break;
            }
            default:
                extraFileActions(p, fo);
        }
    }
}
 
Example 2
Project: javaide   File: JavacProcessingEnvironment.java   View Source Code Vote up 6 votes
/** Enter a set of generated class files. */
private List<ClassSymbol> enterClassFiles(Map<String, JavaFileObject> classFiles) {
    ClassReader reader = ClassReader.instance(context);
    Names names = Names.instance(context);
    List<ClassSymbol> list = List.nil();

    for (Map.Entry<String,JavaFileObject> entry : classFiles.entrySet()) {
        Name name = names.fromString(entry.getKey());
        JavaFileObject file = entry.getValue();
        if (file.getKind() != JavaFileObject.Kind.CLASS)
            throw new AssertionError(file);
        ClassSymbol cs;
        if (isPkgInfo(file, JavaFileObject.Kind.CLASS)) {
            Name packageName = Convert.packagePart(name);
            PackageSymbol p = reader.enterPackage(packageName);
            if (p.package_info == null)
                p.package_info = reader.enterClass(Convert.shortName(name), p);
            cs = p.package_info;
            if (cs.classfile == null)
                cs.classfile = file;
        } else
            cs = reader.enterClass(name, file);
        list = list.prepend(cs);
    }
    return list.reverse();
}
 
Example 3
Project: OpenJSharp   File: ClassReader.java   View Source Code Vote up 6 votes
private void fillIn(PackageSymbol p,
                    Location location,
                    Iterable<JavaFileObject> files)
{
    currentLoc = location;
    for (JavaFileObject fo : files) {
        switch (fo.getKind()) {
        case CLASS:
        case SOURCE: {
            // TODO pass binaryName to includeClassFile
            String binaryName = fileManager.inferBinaryName(currentLoc, fo);
            String simpleName = binaryName.substring(binaryName.lastIndexOf(".") + 1);
            if (SourceVersion.isIdentifier(simpleName) ||
                simpleName.equals("package-info"))
                includeClassFile(p, fo);
            break;
        }
        default:
            extraFileActions(p, fo);
        }
    }
}
 
Example 4
Project: OpenJSharp   File: JavacProcessingEnvironment.java   View Source Code Vote up 6 votes
/** Enter a set of generated class files. */
private List<ClassSymbol> enterClassFiles(Map<String, JavaFileObject> classFiles) {
    ClassReader reader = ClassReader.instance(context);
    Names names = Names.instance(context);
    List<ClassSymbol> list = List.nil();

    for (Map.Entry<String,JavaFileObject> entry : classFiles.entrySet()) {
        Name name = names.fromString(entry.getKey());
        JavaFileObject file = entry.getValue();
        if (file.getKind() != JavaFileObject.Kind.CLASS)
            throw new AssertionError(file);
        ClassSymbol cs;
        if (isPkgInfo(file, JavaFileObject.Kind.CLASS)) {
            Name packageName = Convert.packagePart(name);
            PackageSymbol p = reader.enterPackage(packageName);
            if (p.package_info == null)
                p.package_info = reader.enterClass(Convert.shortName(name), p);
            cs = p.package_info;
            if (cs.classfile == null)
                cs.classfile = file;
        } else
            cs = reader.enterClass(name, file);
        list = list.prepend(cs);
    }
    return list.reverse();
}
 
Example 5
Project: beaker-notebook-archive   File: JavaFileObjectRegistryImpl.java   View Source Code Vote up 5 votes
@Override
public Collection<JavaFileObject> get(JavaFileObject.Kind kind) {
    List<JavaFileObject> result = new ArrayList<JavaFileObject>();
    for (JavaFileObject candidate : javaFileObjects.values()) {
        if (candidate.getKind() == kind) {
            result.add(candidate);
        }
    }

    return result;
}
 
Example 6
Project: javaide   File: ClassReader.java   View Source Code Vote up 5 votes
/**
 * Implement policy to choose to derive information from a source
 * file or a class file when both are present.  May be overridden
 * by subclasses.
 */
protected JavaFileObject preferredFileObject(JavaFileObject a,
                                             JavaFileObject b) {

    if (preferSource)
        return (a.getKind() == JavaFileObject.Kind.SOURCE) ? a : b;
    else {
        long adate = a.getLastModified();
        long bdate = b.getLastModified();
        // 6449326: policy for bad lastModifiedTime in ClassReader
        //assert adate >= 0 && bdate >= 0;
        return (adate > bdate) ? a : b;
    }
}
 
Example 7
Project: javaide   File: JavacFiler.java   View Source Code Vote up 5 votes
/**
 * Upon close, register files opened by create{Source, Class}File
 * for annotation processing.
 */
private void closeFileObject(String typeName, FileObject fileObject) {
    /*
     * If typeName is non-null, the file object was opened as a
     * source or class file by the user.  If a file was opened as
     * a resource, typeName will be null and the file is *not*
     * subject to annotation processing.
     */
    if ((typeName != null)) {
        if (!(fileObject instanceof JavaFileObject))
            throw new AssertionError("JavaFileOject not found for " + fileObject);
        JavaFileObject javaFileObject = (JavaFileObject)fileObject;
        switch(javaFileObject.getKind()) {
        case SOURCE:
            generatedSourceNames.add(typeName);
            generatedSourceFileObjects.add(javaFileObject);
            openTypeNames.remove(typeName);
            break;

        case CLASS:
            generatedClasses.put(typeName, javaFileObject);
            openTypeNames.remove(typeName);
            break;

        default:
            break;
        }
    }
}
 
Example 8
Project: OpenJSharp   File: ClassReader.java   View Source Code Vote up 5 votes
/** Implement policy to choose to derive information from a source
 *  file or a class file when both are present.  May be overridden
 *  by subclasses.
 */
protected JavaFileObject preferredFileObject(JavaFileObject a,
                                       JavaFileObject b) {

    if (preferSource)
        return (a.getKind() == JavaFileObject.Kind.SOURCE) ? a : b;
    else {
        long adate = a.getLastModified();
        long bdate = b.getLastModified();
        // 6449326: policy for bad lastModifiedTime in ClassReader
        //assert adate >= 0 && bdate >= 0;
        return (adate > bdate) ? a : b;
    }
}
 
Example 9
Project: openjdk-jdk10   File: ClassFinder.java   View Source Code Vote up 5 votes
/** Implement policy to choose to derive information from a source
 *  file or a class file when both are present.  May be overridden
 *  by subclasses.
 */
protected JavaFileObject preferredFileObject(JavaFileObject a,
                                       JavaFileObject b) {

    if (preferSource)
        return (a.getKind() == JavaFileObject.Kind.SOURCE) ? a : b;
    else {
        long adate = a.getLastModified();
        long bdate = b.getLastModified();
        // 6449326: policy for bad lastModifiedTime in ClassReader
        //assert adate >= 0 && bdate >= 0;
        return (adate > bdate) ? a : b;
    }
}
 
Example 10
Project: openjdk-jdk10   File: ClassFinder.java   View Source Code Vote up 5 votes
@SuppressWarnings("fallthrough")
private void fillIn(PackageSymbol p,
                    Location location,
                    Iterable<JavaFileObject> files)
{
    currentLoc = location;
    for (JavaFileObject fo : files) {
        switch (fo.getKind()) {
        case OTHER:
            if (!isSigFile(location, fo)) {
                extraFileActions(p, fo);
                break;
            }
            //intentional fall-through:
        case CLASS:
        case SOURCE: {
            // TODO pass binaryName to includeClassFile
            String binaryName = fileManager.inferBinaryName(currentLoc, fo);
            String simpleName = binaryName.substring(binaryName.lastIndexOf(".") + 1);
            if (SourceVersion.isIdentifier(simpleName) ||
                simpleName.equals("package-info"))
                includeClassFile(p, fo);
            break;
        }
        default:
            extraFileActions(p, fo);
            break;
        }
    }
}
 
Example 11
Project: javaide   File: ClassReader.java   View Source Code Vote up 4 votes
private JCDiagnostic createBadClassFileDiagnostic(JavaFileObject file, JCDiagnostic diag) {
    String key = (file.getKind() == JavaFileObject.Kind.SOURCE
            ? "bad.source.file.header" : "bad.class.file.header");
    return diagFactory.fragment(key, file, diag);
}
 
Example 12
Project: javaide   File: ClassReader.java   View Source Code Vote up 4 votes
/**
 * Fill in definition of class `c' from corresponding class or
 * source file.
 */
private void fillIn(ClassSymbol c) {
    if (completionFailureName == c.fullname) {
        throw new CompletionFailure(c, "user-selected completion failure by class name");
    }
    currentOwner = c;
    warnedAttrs.clear();
    JavaFileObject classfile = c.classfile;
    if (classfile != null) {
        JavaFileObject previousClassFile = currentClassFile;
        try {
            if (filling) {
                Assert.error("Filling " + classfile.toUri() + " during " + previousClassFile);
            }
            currentClassFile = classfile;
            if (verbose) {
                log.printVerbose("loading", currentClassFile.toString());
            }
            if (classfile.getKind() == JavaFileObject.Kind.CLASS) {
                filling = true;
                try {
                    bp = 0;
                    buf = readInputStream(buf, classfile.openInputStream());
                    readClassFile(c);
                    if (!missingTypeVariables.isEmpty() && !foundTypeVariables.isEmpty()) {
                        List<Type> missing = missingTypeVariables;
                        List<Type> found = foundTypeVariables;
                        missingTypeVariables = List.nil();
                        foundTypeVariables = List.nil();
                        filling = false;
                        ClassType ct = (ClassType) currentOwner.type;
                        ct.supertype_field =
                                types.subst(ct.supertype_field, missing, found);
                        ct.interfaces_field =
                                types.subst(ct.interfaces_field, missing, found);
                    } else if (missingTypeVariables.isEmpty() !=
                            foundTypeVariables.isEmpty()) {
                        Name name = missingTypeVariables.head.tsym.name;
                        throw badClassFile("undecl.type.var", name);
                    }
                } finally {
                    missingTypeVariables = List.nil();
                    foundTypeVariables = List.nil();
                    filling = false;
                }
            } else {
                if (sourceCompleter != null) {
                    sourceCompleter.complete(c);
                } else {
                    throw new IllegalStateException("Source completer required to read "
                            + classfile.toUri());
                }
            }
            return;
        } catch (IOException ex) {
            throw badClassFile("unable.to.access.file", ex.getMessage());
        } finally {
            currentClassFile = previousClassFile;
        }
    } else {
        JCDiagnostic diag =
                diagFactory.fragment("class.file.not.found", c.flatname);
        throw
                newCompletionFailure(c, diag);
    }
}
 
Example 13
Project: openjdk-jdk10   File: JavadocTool.java   View Source Code Vote up 4 votes
public DocumentationTask getTask(
        Writer out,
        JavaFileManager fileManager,
        DiagnosticListener<? super JavaFileObject> diagnosticListener,
        Class<?> docletClass,
        Iterable<String> options,
        Iterable<? extends JavaFileObject> compilationUnits,
        Context context) {
    try {
        ClientCodeWrapper ccw = ClientCodeWrapper.instance(context);

        if (options != null) {
            for (String option : options)
                Objects.requireNonNull(option);
        }

        if (compilationUnits != null) {
            compilationUnits = ccw.wrapJavaFileObjects(compilationUnits); // implicit null check
            for (JavaFileObject cu : compilationUnits) {
                if (cu.getKind() != JavaFileObject.Kind.SOURCE) {
                    final String kindMsg = "All compilation units must be of SOURCE kind";
                    throw new IllegalArgumentException(kindMsg);
                }
            }
        }

        if (diagnosticListener != null)
            context.put(DiagnosticListener.class, ccw.wrap(diagnosticListener));

        if (out == null)
            context.put(Log.errKey, new PrintWriter(System.err, true));
        else if (out instanceof PrintWriter)
            context.put(Log.errKey, ((PrintWriter) out));
        else
            context.put(Log.errKey, new PrintWriter(out, true));

        if (fileManager == null) {
            fileManager = getStandardFileManager(diagnosticListener, null, null);
            if (fileManager instanceof BaseFileManager) {
                ((BaseFileManager) fileManager).autoClose = true;
            }
        }
        fileManager = ccw.wrap(fileManager);
        context.put(JavaFileManager.class, fileManager);

        return new JavadocTaskImpl(context, docletClass, options, compilationUnits);
    } catch (ClientCodeException ex) {
        throw new RuntimeException(ex.getCause());
    }
}
 
Example 14
Project: OpenJSharp   File: ClassReader.java   View Source Code Vote up 4 votes
private JCDiagnostic createBadClassFileDiagnostic(JavaFileObject file, JCDiagnostic diag) {
    String key = (file.getKind() == JavaFileObject.Kind.SOURCE
                ? "bad.source.file.header" : "bad.class.file.header");
    return diagFactory.fragment(key, file, diag);
}
 
Example 15
Project: OpenJSharp   File: ClassReader.java   View Source Code Vote up 4 votes
/** Fill in definition of class `c' from corresponding class or
 *  source file.
 */
private void fillIn(ClassSymbol c) {
    if (completionFailureName == c.fullname) {
        throw new CompletionFailure(c, "user-selected completion failure by class name");
    }
    currentOwner = c;
    warnedAttrs.clear();
    JavaFileObject classfile = c.classfile;
    if (classfile != null) {
        JavaFileObject previousClassFile = currentClassFile;
        try {
            if (filling) {
                Assert.error("Filling " + classfile.toUri() + " during " + previousClassFile);
            }
            currentClassFile = classfile;
            if (verbose) {
                log.printVerbose("loading", currentClassFile.toString());
            }
            if (classfile.getKind() == JavaFileObject.Kind.CLASS) {
                filling = true;
                try {
                    bp = 0;
                    buf = readInputStream(buf, classfile.openInputStream());
                    readClassFile(c);
                    if (!missingTypeVariables.isEmpty() && !foundTypeVariables.isEmpty()) {
                        List<Type> missing = missingTypeVariables;
                        List<Type> found = foundTypeVariables;
                        missingTypeVariables = List.nil();
                        foundTypeVariables = List.nil();
                        filling = false;
                        ClassType ct = (ClassType)currentOwner.type;
                        ct.supertype_field =
                            types.subst(ct.supertype_field, missing, found);
                        ct.interfaces_field =
                            types.subst(ct.interfaces_field, missing, found);
                    } else if (missingTypeVariables.isEmpty() !=
                               foundTypeVariables.isEmpty()) {
                        Name name = missingTypeVariables.head.tsym.name;
                        throw badClassFile("undecl.type.var", name);
                    }
                } finally {
                    missingTypeVariables = List.nil();
                    foundTypeVariables = List.nil();
                    filling = false;
                }
            } else {
                if (sourceCompleter != null) {
                    sourceCompleter.complete(c);
                } else {
                    throw new IllegalStateException("Source completer required to read "
                                                    + classfile.toUri());
                }
            }
            return;
        } catch (IOException ex) {
            throw badClassFile("unable.to.access.file", ex.getMessage());
        } finally {
            currentClassFile = previousClassFile;
        }
    } else {
        JCDiagnostic diag =
            diagFactory.fragment("class.file.not.found", c.flatname);
        throw
            newCompletionFailure(c, diag);
    }
}
 
Example 16
Project: openjdk-jdk10   File: ToolEnvironment.java   View Source Code Vote up 4 votes
public Kind getFileKind(TypeElement te) {
    JavaFileObject jfo = ((ClassSymbol)te).outermostClass().classfile;
    return jfo == null ? Kind.SOURCE : jfo.getKind();
}
 
Example 17
Project: openjdk-jdk10   File: ModuleFinder.java   View Source Code Vote up 4 votes
private ModuleSymbol readModule(JavaFileObject fo) throws IOException {
    Name name;
    switch (fo.getKind()) {
        case SOURCE:
            name = moduleNameFromSourceReader.readModuleName(fo);
            if (name == null) {
                JCDiagnostic diag =
                    diags.fragment(Fragments.FileDoesNotContainModule);
                ClassSymbol errModuleInfo = syms.defineClass(names.module_info, syms.errModule);
                throw new ClassFinder.BadClassFile(errModuleInfo, fo, diag, diags);
            }
            break;
        case CLASS:
            try {
                name = names.fromString(readModuleName(fo));
            } catch (BadClassFile | IOException ex) {
                //fillIn will report proper errors:
                name = names.error;
            }
            break;
        default:
            Assert.error();
            name = names.error;
            break;
    }

    ModuleSymbol msym = syms.enterModule(name);
    msym.module_info.classfile = fo;
    if (fileManager.hasLocation(StandardLocation.PATCH_MODULE_PATH) && name != names.error) {
        msym.patchLocation = fileManager.getLocationForModule(StandardLocation.PATCH_MODULE_PATH, name.toString());

        if (msym.patchLocation != null) {
            JavaFileObject patchFO = getModuleInfoFromLocation(StandardLocation.CLASS_OUTPUT, Kind.CLASS);
            patchFO = preferredFileObject(getModuleInfoFromLocation(msym.patchLocation, Kind.CLASS), patchFO);
            patchFO = preferredFileObject(getModuleInfoFromLocation(msym.patchLocation, Kind.SOURCE), patchFO);

            if (patchFO != null) {
                msym.module_info.classfile = patchFO;
            }
        }
    }

    msym.completer = Completer.NULL_COMPLETER;
    classFinder.fillIn(msym.module_info);

    return msym;
}
 
Example 18
Project: openjdk-jdk10   File: ClassFinder.java   View Source Code Vote up 4 votes
/** Fill in definition of class `c' from corresponding class or
 *  source file.
 */
void fillIn(ClassSymbol c) {
    if (completionFailureName == c.fullname) {
        JCDiagnostic msg =
                diagFactory.fragment(Fragments.UserSelectedCompletionFailure);
        throw new CompletionFailure(c, msg);
    }
    currentOwner = c;
    JavaFileObject classfile = c.classfile;
    if (classfile != null) {
        JavaFileObject previousClassFile = currentClassFile;
        Symbol prevOwner = c.owner;
        Name prevName = c.fullname;
        try {
            if (reader.filling) {
                Assert.error("Filling " + classfile.toUri() + " during " + previousClassFile);
            }
            currentClassFile = classfile;
            if (verbose) {
                log.printVerbose("loading", currentClassFile.getName());
            }
            if (classfile.getKind() == JavaFileObject.Kind.CLASS ||
                classfile.getKind() == JavaFileObject.Kind.OTHER) {
                reader.readClassFile(c);
                c.flags_field |= getSupplementaryFlags(c);
            } else {
                if (!sourceCompleter.isTerminal()) {
                    sourceCompleter.complete(c);
                } else {
                    throw new IllegalStateException("Source completer required to read "
                                                    + classfile.toUri());
                }
            }
        } catch (BadClassFile cf) {
            //the symbol may be partially initialized, purge it:
            c.owner = prevOwner;
            c.members_field.getSymbols(sym -> sym.kind == TYP).forEach(sym -> {
                ClassSymbol csym = (ClassSymbol) sym;
                csym.owner = sym.packge();
                csym.owner.members().enter(sym);
                csym.fullname = sym.flatName();
                csym.name = Convert.shortName(sym.flatName());
                csym.reset();
            });
            c.fullname = prevName;
            c.name = Convert.shortName(prevName);
            c.reset();
            throw cf;
        } finally {
            currentClassFile = previousClassFile;
        }
    } else {
        throw classFileNotFound(c);
    }
}
 
Example 19
Project: openjdk-jdk10   File: ClassFinder.java   View Source Code Vote up 4 votes
Iterable<JavaFileObject> list(Location location,
                              PackageSymbol p,
                              String packageName,
                              Set<Kind> kinds) throws IOException {
    Iterable<JavaFileObject> listed = fileManager.list(location,
                                                       packageName,
                                                       EnumSet.allOf(Kind.class),
                                                       false);
    return () -> new Iterator<JavaFileObject>() {
        private final Iterator<JavaFileObject> original = listed.iterator();
        private JavaFileObject next;
        @Override
        public boolean hasNext() {
            if (next == null) {
                while (original.hasNext()) {
                    JavaFileObject fo = original.next();

                    if (fo.getKind() != Kind.CLASS &&
                        fo.getKind() != Kind.SOURCE &&
                        !isSigFile(currentLoc, fo)) {
                        p.flags_field |= Flags.HAS_RESOURCE;
                    }

                    if (kinds.contains(fo.getKind())) {
                        next = fo;
                        break;
                    }
                }
            }
            return next != null;
        }

        @Override
        public JavaFileObject next() {
            if (!hasNext())
                throw new NoSuchElementException();
            JavaFileObject result = next;
            next = null;
            return result;
        }

    };
}
 
Example 20
Project: openjdk-jdk10   File: ClassFinder.java   View Source Code Vote up 4 votes
private static JCDiagnostic createBadClassFileDiagnostic(
        JavaFileObject file, JCDiagnostic diag, JCDiagnostic.Factory diagFactory) {
    String key = (file.getKind() == JavaFileObject.Kind.SOURCE
                ? "bad.source.file.header" : "bad.class.file.header");
    return diagFactory.fragment(key, file, diag);
}