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

The following examples show how to use javax.tools.JavaFileObject#getKind() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavacProcessingEnvironment.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/** 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 2
Source File: ClassReader.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
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 3
Source File: ClassReader.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
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
Source File: ClassReader.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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 5
Source File: ClassReader.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/** 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 6
Source File: JavacFiler.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * 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 7
Source File: MatcherFactoryGenerator.java    From hamcrest-pojo-matcher-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Consumer which writes classes as java files
 */
private static Consumer<JavaFile> write(ProcessingEnvironment processingEnv) {
    return file -> {
        try {
            file.writeTo(processingEnv.getFiler());
        } catch (IOException e) {
            JavaFileObject obj = file.toJavaFileObject();
            throw new ProcessingException("Can't write", obj.getName(), obj.getKind(), e);
        }
    };
}
 
Example 8
Source File: ClassReader.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
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 9
Source File: JavadocTool.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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 10
Source File: ClassFinder.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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 11
Source File: ClassReader.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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 12
Source File: ClassReader.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
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 13
Source File: JavadocTool.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
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)
                option.getClass(); // null check
        }

        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.outKey, new PrintWriter(System.err, true));
        else if (out instanceof PrintWriter)
            context.put(Log.outKey, ((PrintWriter) out));
        else
            context.put(Log.outKey, new PrintWriter(out, true));

        if (fileManager == null)
            fileManager = getStandardFileManager(diagnosticListener, null, null);
        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
Source File: ClassReader.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
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
Source File: ClassReader.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/** 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
Source File: ClassReader.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
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 17
Source File: JavadocTool.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
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)
                option.getClass(); // null check
        }

        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.outKey, new PrintWriter(System.err, true));
        else if (out instanceof PrintWriter)
            context.put(Log.outKey, ((PrintWriter) out));
        else
            context.put(Log.outKey, new PrintWriter(out, true));

        if (fileManager == null)
            fileManager = getStandardFileManager(diagnosticListener, null, null);
        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 18
Source File: ModuleFinder.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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("file.does.not.contain.module");
                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 19
Source File: JavadocTool.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
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)
                option.getClass(); // null check
        }

        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.outKey, new PrintWriter(System.err, true));
        else if (out instanceof PrintWriter)
            context.put(Log.outKey, ((PrintWriter) out));
        else
            context.put(Log.outKey, new PrintWriter(out, true));

        if (fileManager == null)
            fileManager = getStandardFileManager(diagnosticListener, null, null);
        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 20
Source File: ToolEnvironment.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public Kind getFileKind(TypeElement te) {
    JavaFileObject jfo = ((ClassSymbol)te).outermostClass().classfile;
    return jfo == null ? Kind.SOURCE : jfo.getKind();
}