2

I have some problems with ANT. First a short description of my project. I've written a programm to analyse sourcecode and I'm using the CompilationUnitTree given by the Java lib tools.jar. Everything works fine, so I thought about an ant task. Here is my 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 should print a message if one the banned Elements appears in the source code. It works fine without using ant.

Here is the error I get with 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

Here is the method were the error occurs:

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);
    }
}

Hope there is someone who can help me with this problem, thanks!!!

Spring
  • 11,333
  • 29
  • 116
  • 185
Avian
  • 101
  • 7
  • What is the FQN of JavacTaskImpl in your Java code (probably auto-imported by IDE) ? – PeterMmm Dec 22 '12 at 15:20
  • its in my imports: import com.sun.tools.javac.api.JavacTaskImpl; – Avian Dec 22 '12 at 15:22
  • 1
    The class cast exception message is strange, maybe that will help you: http://stackoverflow.com/questions/2371967/java-getting-class-cast-exception-where-both-classes-are-exactly-the-same – PeterMmm Dec 22 '12 at 15:30
  • Do you have any progress on this? I am facing the same issue – jonatzin Jan 09 '14 at 12:45
  • I found a solution for my problem. Instead of the ToolProvider I used JavacTool.create() to create a JavaCompiler instance. – Avian Jan 10 '14 at 13:22

0 Answers0