我对 ANT 有一些问题。首先简要介绍一下我的项目。我编写了一个程序来分析源代码,我正在使用 Java lib tools.jar 提供的 CompilationUnitTree。一切正常,所以我想到了一个蚂蚁任务。这是我的 build.xml:
<?xml version="1.0"?>
<project default="main">
<taskdef name="banElements" classname="com.tools.scs.routines.BanElements"/>
<target name="main">
<banElements banned="IF,FOR,SWITCH,WHILE,VARIABLE[type!=int]" filename="D:/Studium/Bachelor-Thesis/src/Farbe.java" />
</target>
</project>
如果一个被禁止的元素出现在源代码中,banElements 应该打印一条消息。不使用蚂蚁也能正常工作。
这是我在 ant 中遇到的错误:
D:\Studium\Bachelor-Thesis\Project\buildtest.xml:8: java.lang.ClassCastException: com.sun.tools.javac.api.JavacTaskImpl cannot be cast to com.sun.tools.javac.api.JavacTaskImpl
at com.tools.scs.SourceCodeSelector.generateTree(SourceCodeSelector.java:146)
at com.tools.scs.SourceCodeSelector.<init>(SourceCodeSelector.java:32)
at com.tools.scs.routines.BanElements.setFilename(BanElements.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.tools.ant.IntrospectionHelper$AttributeSetter.setObject(IntrospectionHelper.java:1498)
at org.apache.tools.ant.IntrospectionHelper.setAttribute(IntrospectionHelper.java:405)
at org.apache.tools.ant.RuntimeConfigurable.maybeConfigure(RuntimeConfigurable.java:403)
at org.apache.tools.ant.RuntimeConfigurable.maybeConfigure(RuntimeConfigurable.java:346)
at org.apache.tools.ant.Task.maybeConfigure(Task.java:202)
at org.apache.tools.ant.UnknownElement.configure(UnknownElement.java:196)
at org.apache.tools.ant.UnknownElement.maybeConfigure(UnknownElement.java:163)
at org.apache.tools.ant.Task.perform(Task.java:347)
at org.apache.tools.ant.Target.execute(Target.java:392)
at org.apache.tools.ant.Target.performTasks(Target.java:413)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.Main.runBuild(Main.java:811)
at org.apache.tools.ant.Main.startAnt(Main.java:217)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Total time: 0 seconds
这是发生错误的方法:
private void generateTree() throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> fileObjects = fileManager
.getJavaFileObjectsFromStrings(Arrays.asList(this.srcFile));
**JavacTaskImpl javacTask = (JavacTaskImpl) compiler.getTask(null, null, null, null, null, fileObjects);**
SourcePositions sourcePositions = Trees.instance(javacTask).getSourcePositions();
for (CompilationUnitTree tree : javacTask.parse()) {
this.srcCodeTree = new SourceScanner(sourcePositions).scan(tree);
}
}
希望有人能帮我解决这个问题,谢谢!!!