javac ReadFile.java,Java JavacTask.parse方法代碼示例

本文整理匯總了Java中com.sun.source.util.JavacTask.parse方法的典型用法代碼示例。如果您正苦於以下問題:Java JavacTask.parse方法的具體用法?Java JavacTask.parse怎麽用?Java JavacTask.parse使用的例子?那麽恭喜您, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.sun.source.util.JavacTask的用法示例。

在下文中一共展示了JavacTask.parse方法的20個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於我們的係統推薦出更棒的Java代碼示例。

示例1: main

​點讚 3

import com.sun.source.util.JavacTask; //導入方法依賴的package包/類

public static void main(String[] args) throws Exception {

PrintWriter out = new PrintWriter(System.out, true);

JavacTool tool = JavacTool.create();

try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {

File testSrc = new File(System.getProperty("test.src"));

Iterable extends JavaFileObject> f =

fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "ArrayPositionConsistency.java")));

JavacTask task = tool.getTask(out, fm, null, null, null, f);

Iterable extends CompilationUnitTree> trees = task.parse();

out.flush();

Scanner s = new Scanner();

for (CompilationUnitTree t: trees)

s.scan(t, null);

}

}

開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:17,

示例2: run

​點讚 3

import com.sun.source.util.JavacTask; //導入方法依賴的package包/類

void run(boolean disableStringFolding) throws IOException {

List argsList = new ArrayList();

if (disableStringFolding) {

argsList.add("-XDallowStringFolding=false");

}

JavacTask ct = (JavacTask)tool.getTask(null, null, null,

argsList,

null,

Arrays.asList(source));

Iterable extends CompilationUnitTree> trees = ct.parse();

String text = trees.toString();

System.out.println(text);

if (disableStringFolding) {

if (text.contains("FOLDED")) {

throw new AssertionError("Expected no string folding");

}

if (!text.contains("\"F\"")) {

throw new AssertionError("Expected content not found");

}

} else {

if (!text.contains("FOLDED")) {

throw new AssertionError("Expected string folding");

}

}

}

開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:27,

示例3: main

​點讚 3

import com.sun.source.util.JavacTask; //導入方法依賴的package包/類

public static void main(String... args) throws IOException {

String testSrc = System.getProperty("test.src", ".");

String testClasses = System.getProperty("test.classes", ".");

JavacTool tool = JavacTool.create();

MyDiagListener dl = new MyDiagListener();

try (StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null)) {

fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(testClasses)));

Iterable extends JavaFileObject> files =

fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T6410706.class.getName()+".java")));

JavacTask task = tool.getTask(null, fm, dl, null, null, files);

task.parse();

task.analyze();

task.generate();

// expect 2 notes:

// Note: T6410706.java uses or overrides a deprecated API.

// Note: Recompile with -Xlint:deprecation for details.

if (dl.notes != 2)

throw new AssertionError(dl.notes + " notes given");

}

}

開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:23,

示例4: main

​點讚 3

import com.sun.source.util.JavacTask; //導入方法依賴的package包/類

public static void main(String[] args) throws Exception {

PrintWriter out = new PrintWriter(System.out, true);

JavacTool tool = JavacTool.create();

try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {

File testSrc = new File(System.getProperty("test.src"));

Iterable extends JavaFileObject> f =

fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "ArrayCreationTree.java")));

JavacTask task = tool.getTask(out, fm, null, null, null, f);

Iterable extends CompilationUnitTree> trees = task.parse();

out.flush();

Scanner s = new Scanner();

for (CompilationUnitTree t: trees)

s.scan(t, null);

}

}

開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:17,

示例5: run

​點讚 3

import com.sun.source.util.JavacTask; //導入方法依賴的package包/類

void run() throws Exception {

File testSrc = new File(System.getProperty("test.src"));

JavacTool tool = JavacTool.create();

try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {

File f = new File(testSrc, T6993305.class.getSimpleName() + ".java");

Iterable extends JavaFileObject> fos = fm.getJavaFileObjects(f);

JavacTask task = tool.getTask(null, fm, null, null, null, fos);

Iterable extends CompilationUnitTree> cus = task.parse();

TestScanner s = new TestScanner();

s.scan(cus, task);

if (errors > 0)

throw new Exception(errors + " errors occurred");

}

}

開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:19,

示例6: read

​點讚 3

import com.sun.source.util.JavacTask; //導入方法依賴的package包/類

/**

* Read a file.

* @param file the file to be read

* @return the tree for the content of the file

* @throws IOException if any IO errors occur

* @throws TreePosTest.ParseException if any errors occur while parsing the file

*/

JCCompilationUnit read(File file) throws IOException, ParseException {

JavacTool tool = JavacTool.create();

r.errors = 0;

Iterable extends JavaFileObject> files = fm.getJavaFileObjects(file);

JavacTask task = tool.getTask(pw, fm, r, Collections.emptyList(), null, files);

Iterable extends CompilationUnitTree> trees = task.parse();

pw.flush();

if (r.errors > 0)

throw new ParseException(sw.toString());

Iterator extends CompilationUnitTree> iter = trees.iterator();

if (!iter.hasNext())

throw new Error("no trees found");

JCCompilationUnit t = (JCCompilationUnit) iter.next();

if (iter.hasNext())

throw new Error("too many trees found");

return t;

}

開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:25,

示例7: main

​點讚 3

import com.sun.source.util.JavacTask; //導入方法依賴的package包/類

public static void main(String[] args) throws Exception {

PrintWriter out = new PrintWriter(System.out, true);

JavacTool tool = JavacTool.create();

try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {

File testSrc = new File(System.getProperty("test.src"));

Iterable extends JavaFileObject> f =

fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "AnnotatedArrayOrder.java")));

JavacTask task = tool.getTask(out, fm, null, null, null, f);

Iterable extends CompilationUnitTree> trees = task.parse();

out.flush();

Scanner s = new Scanner();

for (CompilationUnitTree t: trees)

s.scan(t, null);

}

}

開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:17,

示例8: read

​點讚 3

import com.sun.source.util.JavacTask; //導入方法依賴的package包/類

/**

* Read a file.

* @param file the file to be read

* @return the tree for the content of the file

* @throws IOException if any IO errors occur

* @throws TreePosTest.ParseException if any errors occur while parsing the file

*/

JCCompilationUnit read(File file) throws IOException, ParseException {

JavacTool tool = JavacTool.create();

r.errors = 0;

Iterable extends JavaFileObject> files = fm.getJavaFileObjects(file);

JavacTask task = tool.getTask(pw, fm, r, List.of("-proc:none"), null, files);

Iterable extends CompilationUnitTree> trees = task.parse();

pw.flush();

if (r.errors > 0)

throw new ParseException(sw.toString());

Iterator extends CompilationUnitTree> iter = trees.iterator();

if (!iter.hasNext())

throw new Error("no trees found");

JCCompilationUnit t = (JCCompilationUnit) iter.next();

if (iter.hasNext())

throw new Error("too many trees found");

return t;

}

開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:25,

示例9: run

​點讚 3

import com.sun.source.util.JavacTask; //導入方法依賴的package包/類

void run() throws Exception {

Context context = new Context();

JavacFileManager.preRegister(context);

Trees trees = JavacTrees.instance(context);

StringWriter strOut = new StringWriter();

PrintWriter pw = new PrintWriter(strOut);

DPrinter dprinter = new DPrinter(pw, trees);

final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();

JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, Arrays.asList(new JavaSource()));

Iterable extends CompilationUnitTree> elements = ct.parse();

ct.analyze();

Assert.check(elements.iterator().hasNext());

dprinter.treeTypes(true).printTree("", (JCTree)elements.iterator().next());

String output = strOut.toString();

Assert.check(!output.contains("java.lang.Object"), "there shouldn't be any type instantiated to Object");

}

開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:17,

示例10: main

​點讚 3

import com.sun.source.util.JavacTask; //導入方法依賴的package包/類

public static void main(String[] args) throws Exception {

PrintWriter out = new PrintWriter(System.out, true);

JavacTool tool = JavacTool.create();

try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {

File testSrc = new File(System.getProperty("test.src"));

Iterable extends JavaFileObject> f =

fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "T6345974.java")));

JavacTask task = tool.getTask(out, fm, null, null, null, f);

Iterable extends CompilationUnitTree> trees = task.parse();

out.flush();

Scanner s = new Scanner();

for (CompilationUnitTree t: trees)

s.scan(t, null);

}

}

開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:17,

示例11: main

​點讚 3

import com.sun.source.util.JavacTask; //導入方法依賴的package包/類

public static void main(String[] args) throws IOException {

Context context = new Context();

MyMessages.preRegister(context);

JavacTool tool = JavacTool.create();

JavacTask task = tool.getTask(null, null, null, null, null,

List.of(new MyFileObject()),

context);

task.parse();

for (Element e : task.analyze()) {

if (!e.getEnclosingElement().toString().equals("compiler.misc.unnamed.package"))

throw new AssertionError(e.getEnclosingElement());

System.out.println("OK: " + e.getEnclosingElement());

return;

}

throw new AssertionError("No top-level classes!");

}

開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:17,

示例12: main

​點讚 3

import com.sun.source.util.JavacTask; //導入方法依賴的package包/類

public static void main(String[] args) throws IOException {

JavaFileObject sfo = new SimpleJavaFileObject(URI.create("myfo:/Test.java"),Kind.SOURCE) {

public CharSequence getCharContent(boolean ignoreEncodingErrors) {

return "class BadName { Object o = j; }";

}

};

List extends JavaFileObject> files = Arrays.asList(sfo);

JavaCompiler tool = ToolProvider.getSystemJavaCompiler();

JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, files);

Iterable extends CompilationUnitTree> compUnits = ct.parse();

CompilationUnitTree cu = compUnits.iterator().next();

ClassTree cdef = (ClassTree)cu.getTypeDecls().get(0);

JCVariableDecl vdef = (JCVariableDecl)cdef.getMembers().get(0);

TreePath path = TreePath.getPath(cu, vdef.init);

Trees.instance(ct).getScope(path);

}

開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:17,

示例13: run

​點讚 3

import com.sun.source.util.JavacTask; //導入方法依賴的package包/類

void run() throws Exception {

Context context = new Context();

JavacFileManager.preRegister(context);

final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();

JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, Arrays.asList(new JavaSource()));

Iterable extends CompilationUnitTree> elements = ct.parse();

ct.analyze();

Assert.check(elements.iterator().hasNext());

JCTree topLevel = (JCTree)elements.iterator().next();

new TreeScanner() {

@Override

public void visitReference(JCMemberReference tree) {

Assert.check(tree.getOverloadKind() != null);

}

}.scan(topLevel);

}

開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:17,

示例14: parseType

​點讚 2

import com.sun.source.util.JavacTask; //導入方法依賴的package包/類

public boolean parseType( String fqn, List trees, DiagnosticCollector errorHandler )

{

init();

Pair pair = findJavaSource( fqn, errorHandler );

if( pair == null )

{

return false;

}

StringWriter errors = new StringWriter();

JavacTask javacTask = (JavacTask)_javac.getTask( errors, _gfm, errorHandler, Collections.singletonList( "-proc:none" ), null, Collections.singletonList( pair.getFirst() ) );

try

{

initTypeProcessing( javacTask, Collections.singleton( fqn ) );

Iterable extends CompilationUnitTree> iterable = javacTask.parse();

for( CompilationUnitTree x : iterable )

{

trees.add( x );

}

return true;

}

catch( Exception e )

{

return false;

}

}

開發者ID:manifold-systems,項目名稱:manifold,代碼行數:28,

示例15: run

​點讚 2

import com.sun.source.util.JavacTask; //導入方法依賴的package包/類

void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {

JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,

null, null, Arrays.asList(source));

try {

ct.parse();

} catch (Throwable ex) {

throw new AssertionError("Error thron when parsing the following source:\n" + source.getCharContent(true));

}

check();

}

開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:11,

示例16: run

​點讚 2

import com.sun.source.util.JavacTask; //導入方法依賴的package包/類

void run() throws Exception {

List files = new ArrayList();

File testSrc = new File(System.getProperty("test.src"));

for (File f: testSrc.listFiles()) {

if (f.isFile() && f.getName().endsWith(".java"))

files.add(f);

}

JavacTool javac = JavacTool.create();

try (StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null)) {

Iterable extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);

JavacTask t = javac.getTask(null, fm, null, null, null, fos);

DocTrees trees = DocTrees.instance(t);

Iterable extends CompilationUnitTree> units = t.parse();

Set found = EnumSet.noneOf(DocTree.Kind.class);

DeclScanner ds = new DeclScanner(trees, found);

for (CompilationUnitTree unit: units) {

ds.scan(unit, null);

}

for (DocTree.Kind k: DocTree.Kind.values()) {

if (!found.contains(k) && k != DocTree.Kind.OTHER)

error("not found: " + k);

}

if (errors > 0)

throw new Exception(errors + " errors occurred");

}

}

開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:34,

示例17: main

​點讚 2

import com.sun.source.util.JavacTask; //導入方法依賴的package包/類

public static void main(String[] args) throws IOException {

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

JavacTask task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject()));

trees = Trees.instance(task);

Iterable extends CompilationUnitTree> asts = task.parse();

task.analyze();

for (CompilationUnitTree ast : asts) {

new MyVisitor().scan(ast, null);

}

}

開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:11,

示例18: run

​點讚 2

import com.sun.source.util.JavacTask; //導入方法依賴的package包/類

void run() throws Exception {

List files = new ArrayList();

File testSrc = new File(System.getProperty("test.src"));

for (File f: testSrc.listFiles()) {

if (f.isFile() && f.getName().endsWith(".java"))

files.add(f);

}

JavacTool javac = JavacTool.create();

try (StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null)) {

Iterable extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);

JavacTask t = javac.getTask(null, fm, null, null, null, fos);

DocTrees trees = DocTrees.instance(t);

Iterable extends CompilationUnitTree> units = t.parse();

DeclScanner ds = new DeclScanner(trees);

for (CompilationUnitTree unit: units) {

ds.scan(unit, null);

}

if (errors > 0)

throw new Exception(errors + " errors occurred");

}

}

開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:28,

示例19: run

​點讚 2

import com.sun.source.util.JavacTask; //導入方法依賴的package包/類

public void run(PrintWriter out, String... args) throws BadArgs, IOException {

env = new Env();

processArgs(args);

if (needHelp)

showHelp(out);

if (javacFiles.isEmpty()) {

if (!needHelp)

out.println(localize("dc.main.no.files.given"));

}

JavacTool tool = JavacTool.create();

JavacFileManager fm = new JavacFileManager(new Context(), false, null);

fm.setSymbolFileEnabled(false);

fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, javacBootClassPath);

fm.setLocation(StandardLocation.CLASS_PATH, javacClassPath);

fm.setLocation(StandardLocation.SOURCE_PATH, javacSourcePath);

JavacTask task = tool.getTask(out, fm, null, javacOpts, null,

fm.getJavaFileObjectsFromFiles(javacFiles));

Iterable extends CompilationUnitTree> units = task.parse();

((JavacTaskImpl) task).enter();

env.init(task);

checker = new Checker(env);

DeclScanner ds = new DeclScanner() {

@Override

void visitDecl(Tree tree, Name name) {

TreePath p = getCurrentPath();

DocCommentTree dc = env.trees.getDocCommentTree(p);

checker.scan(dc, p);

}

};

ds.scan(units, null);

reportStats(out);

Context ctx = ((JavacTaskImpl) task).getContext();

JavaCompiler c = JavaCompiler.instance(ctx);

c.printCount("error", c.errorCount());

c.printCount("warn", c.warningCount());

}

開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:48,

示例20: run

​點讚 2

import com.sun.source.util.JavacTask; //導入方法依賴的package包/類

public void run(PrintWriter out, String... args) throws BadArgs, IOException {

env = new Env();

processArgs(args);

boolean noFiles = javacFiles.isEmpty();

if (needHelp) {

showHelp(out);

if (noFiles)

return;

} else if (noFiles) {

out.println(localize("dc.main.no.files.given"));

return;

}

JavacTool tool = JavacTool.create();

JavacFileManager fm = new JavacFileManager(new Context(), false, null);

fm.setSymbolFileEnabled(false);

if (javacBootClassPath != null) {

fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, javacBootClassPath);

}

if (javacClassPath != null) {

fm.setLocation(StandardLocation.CLASS_PATH, javacClassPath);

}

if (javacSourcePath != null) {

fm.setLocation(StandardLocation.SOURCE_PATH, javacSourcePath);

}

JavacTask task = tool.getTask(out, fm, null, javacOpts, null,

fm.getJavaFileObjectsFromFiles(javacFiles));

Iterable extends CompilationUnitTree> units = task.parse();

((JavacTaskImpl) task).enter();

env.init(task);

checker = new Checker(env);

DeclScanner ds = new DeclScanner(env) {

@Override

void visitDecl(Tree tree, Name name) {

TreePath p = getCurrentPath();

DocCommentTree dc = env.trees.getDocCommentTree(p);

checker.scan(dc, p);

}

};

ds.scan(units, null);

reportStats(out);

Context ctx = ((JavacTaskImpl) task).getContext();

JavaCompiler c = JavaCompiler.instance(ctx);

c.printCount("error", c.errorCount());

c.printCount("warn", c.warningCount());

}

開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:56,

注:本文中的com.sun.source.util.JavacTask.parse方法示例整理自Github/MSDocs等源碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值