Java Code Examples for javax.tools.JavaFileObject.Kind#SOURCE

The following examples show how to use javax.tools.JavaFileObject.Kind#SOURCE . 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: BaseFileManager.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static Kind getKind(String name) {
    if (name.endsWith(Kind.CLASS.extension))
        return Kind.CLASS;
    else if (name.endsWith(Kind.SOURCE.extension))
        return Kind.SOURCE;
    else if (name.endsWith(Kind.HTML.extension))
        return Kind.HTML;
    else
        return Kind.OTHER;
}
 
Example 2
Source File: ScopeTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static void verifyLambdaScopeCorrect(final String packageClause) throws Exception {
    JavacTool tool = JavacTool.create();
    JavaFileObject source = new SimpleJavaFileObject(URI.create("mem://Test.java"), Kind.SOURCE) {
        @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
            return packageClause + SOURCE_CODE;
        }
        @Override public boolean isNameCompatible(String simpleName, Kind kind) {
            return true;
        }
    };
    Iterable<? extends JavaFileObject> fos = Collections.singletonList(source);
    JavacTask task = tool.getTask(null, null, null, new ArrayList<String>(), null, fos);
    final Types types = JavacTypes.instance(((JavacTaskImpl) task).getContext());
    final Trees trees = Trees.instance(task);
    CompilationUnitTree cu = task.parse().iterator().next();

    task.analyze();

    new TreePathScanner<Void, Void>() {
        @Override public Void visitMemberSelect(MemberSelectTree node, Void p) {
            if (node.getIdentifier().contentEquals("correct")) {
                TypeMirror xType = trees.getTypeMirror(new TreePath(getCurrentPath(), node.getExpression()));
                Scope scope = trees.getScope(getCurrentPath());
                for (Element l : scope.getLocalElements()) {
                    if (!l.getSimpleName().contentEquals("x")) continue;
                    if (!types.isSameType(xType, l.asType())) {
                        throw new IllegalStateException("Incorrect variable type in scope: " + l.asType() + "; should be: " + xType);
                    }
                }
            }
            return super.visitMemberSelect(node, p);
        }
    }.scan(cu, null);
}
 
Example 3
Source File: ScopeTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static void verifyLambdaScopeCorrect(final String packageClause) throws Exception {
    JavacTool tool = JavacTool.create();
    JavaFileObject source = new SimpleJavaFileObject(URI.create("mem://Test.java"), Kind.SOURCE) {
        @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
            return packageClause + SOURCE_CODE;
        }
        @Override public boolean isNameCompatible(String simpleName, Kind kind) {
            return true;
        }
    };
    Iterable<? extends JavaFileObject> fos = Collections.singletonList(source);
    JavacTask task = tool.getTask(null, null, null, new ArrayList<String>(), null, fos);
    final Types types = JavacTypes.instance(((JavacTaskImpl) task).getContext());
    final Trees trees = Trees.instance(task);
    CompilationUnitTree cu = task.parse().iterator().next();

    task.analyze();

    new TreePathScanner<Void, Void>() {
        @Override public Void visitMemberSelect(MemberSelectTree node, Void p) {
            if (node.getIdentifier().contentEquals("correct")) {
                TypeMirror xType = trees.getTypeMirror(new TreePath(getCurrentPath(), node.getExpression()));
                Scope scope = trees.getScope(getCurrentPath());
                for (Element l : scope.getLocalElements()) {
                    if (!l.getSimpleName().contentEquals("x")) continue;
                    if (!types.isSameType(xType, l.asType())) {
                        throw new IllegalStateException("Incorrect variable type in scope: " + l.asType() + "; should be: " + xType);
                    }
                }
            }
            return super.visitMemberSelect(node, p);
        }
    }.scan(cu, null);
}
 
Example 4
Source File: ResourceFileJavaFileManager.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void gatherFiles(ResourceFile root, ResourceFile file, List<JavaFileObject> accumulator,
		Set<Kind> kinds, boolean recurse) {
	ResourceFile[] listFiles = file.listFiles();
	for (ResourceFile resourceFile : listFiles) {
		if (resourceFile.isDirectory()) {
			if (recurse) {
				gatherFiles(root, resourceFile, accumulator, kinds, recurse);
			}
		}
		else {
			for (Kind kind : kinds) {
				if (kind == Kind.CLASS) {
					if (resourceFile.getName().endsWith(".class")) {
						accumulator.add(createFileObject(root, resourceFile, kind));
						break;
					}
				}
				else if (kind == Kind.SOURCE) {
					if (resourceFile.getName().endsWith(".java")) {
						accumulator.add(createFileObject(root, resourceFile, kind));
						break;
					}
				}
			}
		}
	}

}
 
Example 5
Source File: EclipseFileManager.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private Kind getKind(String extension) {
	if (Kind.CLASS.extension.equals(extension)) {
		return Kind.CLASS;
	} else if (Kind.SOURCE.extension.equals(extension)) {
		return Kind.SOURCE;
	} else if (Kind.HTML.extension.equals(extension)) {
		return Kind.HTML;
	}
	return Kind.OTHER;
}
 
Example 6
Source File: InMemoryJavaFileManager.java    From Akatsuki with Apache License 2.0 5 votes vote down vote up
ImmutableList<JavaFileObject> getGeneratedSources() {
	ImmutableList.Builder<JavaFileObject> result = ImmutableList.builder();
	for (Entry<URI, JavaFileObject> entry : inMemoryFileObjects.asMap().entrySet()) {
		if (entry.getKey().getPath().startsWith("/" + StandardLocation.SOURCE_OUTPUT.name())
				&& (entry.getValue().getKind() == Kind.SOURCE)) {
			result.add(entry.getValue());
		}
	}
	return result.build();
}
 
Example 7
Source File: EclipseFileManager.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public JavaFileObject getJavaFileForInput(Location location, String className, Kind kind) throws IOException {
	if (kind != Kind.CLASS && kind != Kind.SOURCE) {
		throw new IllegalArgumentException("Invalid kind : " + kind);//$NON-NLS-1$
	}
	Iterable<? extends File> files = getLocation(location);
	if (files == null) {
		throw new IllegalArgumentException("Unknown location : " + location);//$NON-NLS-1$
	}
	String normalizedFileName = normalized(className);
	normalizedFileName += kind.extension;
	for (File file : files) {
		if (file.isDirectory()) {
			// handle directory
			File f = new File(file, normalizedFileName);
			if (f.exists()) {
				return new EclipseFileObject(className, f.toURI(), kind, this.charset);
			} else {
				continue; // go to next entry in the location
			}
		} else if (isArchive(file)) {
			// handle archive file
			Archive archive = getArchive(file);
			if (archive != Archive.UNKNOWN_ARCHIVE) {
				if (archive.contains(normalizedFileName)) {
					return archive.getArchiveFileObject(normalizedFileName, this.charset);
				}
			}
		}
	}
	return null;
}
 
Example 8
Source File: TestNonSerializableLambdaNameStability.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public SourceJavaFileObject(String name, String code) throws URISyntaxException {
    super(new URI("mem:///" + name + ".java"), Kind.SOURCE);
    this.code = code;
}
 
Example 9
Source File: Processor.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public TestFO(URI uri, String content) {
    super(uri, Kind.SOURCE);
    this.content = content;
}
 
Example 10
Source File: MemoryClassLoader.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
Source(String name, String content) {
    super(name, Kind.SOURCE);
    this.content = content;
}
 
Example 11
Source File: InMemoryJavaCompiler.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public MemoryJavaFileObject(String className, CharSequence sourceCode) {
    super(URI.create("string:///" + className.replace('.','/') + Kind.SOURCE.extension), Kind.SOURCE);
    this.className = className;
    this.sourceCode = sourceCode;
    this.byteCode = new ByteArrayOutputStream();
}
 
Example 12
Source File: MemoryClassLoader.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
Source(String name, String content) {
    super(name, Kind.SOURCE);
    this.content = content;
}
 
Example 13
Source File: JdkCompiler.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public JavaFileObjectImpl(final String baseName, final CharSequence source){
    super(ClassUtils.toURI(baseName + ClassUtils.JAVA_EXTENSION), Kind.SOURCE);
    this.source = source;
}
 
Example 14
Source File: InMemoryJavaCompiler.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public MemoryJavaFileObject(String className, CharSequence sourceCode) {
    super(URI.create("string:///" + className.replace('.','/') + Kind.SOURCE.extension), Kind.SOURCE);
    this.className = className;
    this.sourceCode = sourceCode;
    this.byteCode = new ByteArrayOutputStream();
}
 
Example 15
Source File: JavaCodeScriptEngine.java    From chaosblade-exec-jvm with Apache License 2.0 4 votes vote down vote up
public InputStringJavaFileObject(String name, String code) {
    super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);
    this.code = code;
}
 
Example 16
Source File: JdkCompiler.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
public JavaFileObjectImpl(final String baseName, final CharSequence source){
    super(ClassUtils.toURI(baseName + ClassUtils.JAVA_EXTENSION), Kind.SOURCE);
    this.source = source;
}
 
Example 17
Source File: InMemoryJavaCompiler.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public MemoryJavaFileObject(String className, CharSequence sourceCode) {
    super(URI.create("string:///" + className.replace('.','/') + Kind.SOURCE.extension), Kind.SOURCE);
    this.className = className;
    this.sourceCode = sourceCode;
    this.byteCode = new ByteArrayOutputStream();
}
 
Example 18
Source File: CharSequenceJavaFileObject.java    From jpexs-decompiler with GNU General Public License v3.0 2 votes vote down vote up
/**
 * This constructor will store the source code in the internal "content"
 * variable and register it as a source code, using a URI containing the
 * class full name
 *
 * @param className name of the public class in the source code
 * @param content source code to compile
 */
public CharSequenceJavaFileObject(String className, CharSequence content) {
    super(URI.create("string:///" + className.replace('.', '/')
            + Kind.SOURCE.extension), Kind.SOURCE);
    this.content = content;
}
 
Example 19
Source File: CharSequenceCompiler.java    From openbd-core with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Construct a new instance which stores source
 * 
 * @param baseName
 *          the base name
 * @param source
 *          the source code
 */
JavaFileObjectImpl(final String baseName, final CharSequence source) {
	super(CharSequenceCompiler.toURI(baseName + CharSequenceCompiler.JAVA_EXTENSION), Kind.SOURCE);
	this.source = source;
}
 
Example 20
Source File: CharSequenceCompiler.java    From light with Apache License 2.0 2 votes vote down vote up
/**
 * Construct a new instance which stores source
 * 
 * @param baseName
 *            the base name
 * @param source
 *            the source code
 */
JavaFileObjectImpl(final String baseName, final CharSequence source) {
	super(CharSequenceCompiler.toURI(baseName
			+ CharSequenceCompiler.JAVA_EXTENSION), Kind.SOURCE);
	this.source = source;
}