Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6422327

JSR 199: JavaCompilerTool can compile and generate '.class' of non '.java' files

    Details

    • Subcomponent:
    • Resolved In Build:
      b91
    • CPU:
      generic
    • OS:
      generic
    • Verification:
      Verified

      Description

      JavaCompilerTool can compile and generate '.class' of non '.java' files.
      <code>
      import java.io.File;
      import java.util.ArrayList;
      import java.util.Arrays;
      import java.util.List;
      import javax.tools.*;
      import static javax.tools.StandardJavaFileManager.StandardLocation.*;
      import static javax.tools.JavaFileObject.Kind.*;

      public class Foo {
          private void test(String... args) throws Throwable {
              JavaCompilerTool javac = ToolProvider.getSystemJavaCompilerTool();
              DiagnosticListener<? super javax.tools.JavaFileObject> dl =
                          new DiagnosticListener<javax.tools.JavaFileObject>() {
                  public void report(Diagnostic message) {
                      System.out.println("\nFile Name : "+new File(
                              message.getSource().toString()).getName());
                  }
              };
              StandardJavaFileManager sjfm = javac.getStandardFileManager(dl);
              Iterable<? extends JavaFileObject> iteJFO =
                   sjfm.getJavaFileObjectsFromFiles(Arrays.asList(new File(args[0])));
              List<String> options = new ArrayList<String>();
              options.add("-Xlint:all");
              JavaCompilerTool.CompilationTask task =
                  javac.getTask(null,sjfm,null,options,null,iteJFO);
              task.run();
          }
          public static void main(String... arg) throws Throwable {
               Foo test = new Foo();
               test.test("Bar.other");
          }
      }
      </code>
      <Bar.other>
      class Bar {
      }
      </Bar.other>
      After running the code, Bar.class file will be created.

      When I run Bar.other in the command line, compiler will throw the following error:
      error: Class names, 'Bar.other', are only accepted if annotation processing is explicitly requested
      1 error

      I feel it will be good, if JavaCompilerTool also throws the same exception.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                ahe Peter Ahe
                Reporter:
                savadhansunw Seetharama Avadhanam (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: