我在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应该打印一条消息。 它不使用蚂蚁就可以正常工作。
这是我用蚂蚁得到的错误:
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);
}
}
希望有人可以帮我解决这个问题,谢谢!