⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javacfiler.java

📁 是一款用JAVA 编写的编译器 具有很强的编译功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    Context context;    boolean lastRound;    private final boolean lint;    /**     * Logical names of all created files.  This set must be     * synchronized.     */    private final Set<FileObject> fileObjectHistory;    /**     * Names of types that have had files created but not closed.     */    private final Set<String> openTypeNames;    /**     * Names of source files closed in this round.  This set must be     * synchronized.  Its iterators should preserve insertion order.     */    private Set<String> generatedSourceNames;    /**     * Names and class files of the class files closed in this round.     * This set must be synchronized.  Its iterators should preserve     * insertion order.     */    private final Map<String, JavaFileObject> generatedClasses;    /**     * JavaFileObjects for source files closed in this round.  This     * set must be synchronized.  Its iterators should preserve     * insertion order.     */    private Set<JavaFileObject> generatedSourceFileObjects;    /**     * Names of all created source files.  Its iterators should     * preserve insertion order.     */    private final Set<String> aggregateGeneratedSourceNames;    /**     * Names of all created class files.  Its iterators should     * preserve insertion order.     */    private final Set<String> aggregateGeneratedClassNames;    JavacFiler(Context context) {        this.context = context;        fileManager = context.get(JavaFileManager.class);	        log = Log.instance(context);        fileObjectHistory = synchronizedSet(new LinkedHashSet<FileObject>());        generatedSourceNames = synchronizedSet(new LinkedHashSet<String>());        generatedSourceFileObjects = synchronizedSet(new LinkedHashSet<JavaFileObject>());        generatedClasses = synchronizedMap(new LinkedHashMap<String, JavaFileObject>());        openTypeNames  = synchronizedSet(new LinkedHashSet<String>());        aggregateGeneratedSourceNames = new LinkedHashSet<String>();        aggregateGeneratedClassNames  = new LinkedHashSet<String>();        lint = (Options.instance(context)).lint("processing");    }    public JavaFileObject createSourceFile(CharSequence name,					   Element... originatingElements) throws IOException {	return createSourceOrClassFile(true, name.toString());    }    public JavaFileObject createClassFile(CharSequence name,					   Element... originatingElements) throws IOException { 	return createSourceOrClassFile(false, name.toString());    }    private JavaFileObject createSourceOrClassFile(boolean isSourceFile, String name) throws IOException {        checkNameAndExistence(name, isSourceFile);	Location loc = (isSourceFile ? SOURCE_OUTPUT : CLASS_OUTPUT);	JavaFileObject.Kind kind = (isSourceFile ? 				    JavaFileObject.Kind.SOURCE :				    JavaFileObject.Kind.CLASS);        JavaFileObject fileObject =            fileManager.getJavaFileForOutput(loc, name, kind, null);        checkFileReopening(fileObject, true);        if (lastRound)            log.warning("proc.file.create.last.round", name);	if (isSourceFile)	    aggregateGeneratedSourceNames.add(name);	else	    aggregateGeneratedClassNames.add(name);        openTypeNames.add(name);        return new FilerOutputJavaFileObject(name, fileObject);    }    public FileObject createResource(JavaFileManager.Location location,				     CharSequence pkg,				     CharSequence relativeName,				     Element... originatingElements) throws IOException {	locationCheck(location);        String strPkg = pkg.toString();        if (strPkg.length() > 0)            checkName(strPkg);        FileObject fileObject =            fileManager.getFileForOutput(location, strPkg,                                         relativeName.toString(), null);        checkFileReopening(fileObject, true);		if (fileObject instanceof JavaFileObject)	    return new FilerOutputJavaFileObject(null, (JavaFileObject)fileObject);	else	    return new FilerOutputFileObject(null, fileObject);    }    private void locationCheck(JavaFileManager.Location location) {	if (location instanceof StandardLocation) { 	    StandardLocation stdLoc = (StandardLocation) location;	    if (!stdLoc.isOutputLocation())		throw new IllegalArgumentException("Resource creation not supported in location " +						   stdLoc);	}    }    public FileObject getResource(JavaFileManager.Location location,				  CharSequence pkg,				  CharSequence relativeName) throws IOException {        String strPkg = pkg.toString();        if (strPkg.length() > 0)            checkName(strPkg);	// TODO: Only support reading resources in selected output	// locations?  Only allow reading of non-source, non-class	// files from the supported input locations?	FileObject fileObject = fileManager.getFileForOutput(location,							     pkg.toString(),							     relativeName.toString(),							     null);	// If the path was already opened for writing, throw an exception.	checkFileReopening(fileObject, false);	return new FilerInputFileObject(fileObject);    }    private void checkName(String name) throws FilerException { 	checkName(name, false);    }    private void checkName(String name, boolean allowUnnamedPackageInfo) throws FilerException {        if (!SourceVersion.isName(name) && !isPackageInfo(name, allowUnnamedPackageInfo)) {            if (lint)                log.warning("proc.illegal.file.name", name);            throw new FilerException("Illegal name " + name);        }    }    private boolean isPackageInfo(String name, boolean allowUnnamedPackageInfo) {	// Is the name of the form "package-info" or	// "foo.bar.package-info"?	final String PKG_INFO = "package-info";	int periodIndex = name.lastIndexOf(".");	if (periodIndex == -1) {	    return allowUnnamedPackageInfo ? name.equals(PKG_INFO) : false;	} else {	    // "foo.bar.package-info." illegal	    String prefix = name.substring(0, periodIndex);	    String simple = name.substring(periodIndex+1);	    return SourceVersion.isName(prefix) && simple.equals(PKG_INFO);	}    }    private void checkNameAndExistence(String typename, boolean allowUnnamedPackageInfo) throws FilerException {	// TODO: Check if type already exists on source or class path?	// If so, use warning message key proc.type.already.exists        checkName(typename, allowUnnamedPackageInfo);        if (aggregateGeneratedSourceNames.contains(typename) ||            aggregateGeneratedClassNames.contains(typename)) {            if (lint)                log.warning("proc.type.recreate", typename);            throw new FilerException("Attempt to recreate a file for type " + typename);        }    }    /**     * Check to see if the file has already been opened; if so, throw     * an exception, otherwise add it to the set of files.     */    private void checkFileReopening(FileObject fileObject, boolean addToHistory) throws FilerException {	for(FileObject veteran : fileObjectHistory) {	    if (fileManager.isSameFile(veteran, fileObject)) {		if (lint)		    log.warning("proc.file.reopening", fileObject.getName());		throw new FilerException("Attempt to reopen a file for path " + fileObject.getName());	    }	}	if (addToHistory)	    fileObjectHistory.add(fileObject);    }    public boolean newFiles() {        return (!generatedSourceNames.isEmpty())            || (!generatedClasses.isEmpty());    }    public Set<String> getGeneratedSourceNames() {        return generatedSourceNames;    }    public Set<JavaFileObject> getGeneratedSourceFileObjects() {        return generatedSourceFileObjects;    }    public Map<String, JavaFileObject> getGeneratedClasses() {        return generatedClasses;    }    public void warnIfUnclosedFiles() {        if (!openTypeNames.isEmpty())            log.warning("proc.unclosed.type.files", openTypeNames.toString());    }    /**     * Update internal state for a new round.     */    public void newRound(Context context, boolean lastRound) {        this.context = context;        this.log = Log.instance(context);        this.lastRound = lastRound;        clearRoundState();    }    public void close() {        clearRoundState();        // Cross-round state        fileObjectHistory.clear();        openTypeNames.clear();        aggregateGeneratedSourceNames.clear();        aggregateGeneratedClassNames.clear();    }    private void clearRoundState() {        generatedSourceNames.clear();        generatedSourceFileObjects.clear();        generatedClasses.clear();    }    /**     * Debugging function to display internal state.     */    public void displayState() {        PrintWriter xout = context.get(Log.outKey);        xout.println("File Object History : " +  fileObjectHistory);        xout.println("Open Type Names     : " +  openTypeNames);        xout.println("Gen. Src Names      : " +  generatedSourceNames);        xout.println("Gen. Cls Names      : " +  generatedClasses.keySet());        xout.println("Agg. Gen. Src Names : " +  aggregateGeneratedSourceNames);        xout.println("Agg. Gen. Cls Names : " +  aggregateGeneratedClassNames);    }    public String toString() {        return "javac Filer";    }    /**     * 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;	    }	}    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -