8015073: c.s.t.javac.api.JavacTool.getTask() - more informative exception

Fri, 27 Sep 2013 16:05:56 -0700

author
ksrini
date
Fri, 27 Sep 2013 16:05:56 -0700
changeset 2075
82044fe8c7f7
parent 2074
dee28dd47e12
child 2076
34223fc58c1a

8015073: c.s.t.javac.api.JavacTool.getTask() - more informative exception
Reviewed-by: jjg

src/share/classes/com/sun/tools/javac/api/JavacTool.java file | annotate | diff | comparison | revisions
test/tools/javac/api/TestJavacTask.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/api/JavacTool.java	Fri Sep 27 13:06:38 2013 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/api/JavacTool.java	Fri Sep 27 16:05:56 2013 -0700
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -120,7 +120,6 @@
    1.11          try {
    1.12              ClientCodeWrapper ccw = ClientCodeWrapper.instance(context);
    1.13  
    1.14 -            final String kindMsg = "All compilation units must be of SOURCE kind";
    1.15              if (options != null)
    1.16                  for (String option : options)
    1.17                      option.getClass(); // null check
    1.18 @@ -132,8 +131,11 @@
    1.19              if (compilationUnits != null) {
    1.20                  compilationUnits = ccw.wrapJavaFileObjects(compilationUnits); // implicit null check
    1.21                  for (JavaFileObject cu : compilationUnits) {
    1.22 -                    if (cu.getKind() != JavaFileObject.Kind.SOURCE)
    1.23 +                    if (cu.getKind() != JavaFileObject.Kind.SOURCE) {
    1.24 +                        String kindMsg = "Compilation unit is not of SOURCE kind: "
    1.25 +                                + "\"" + cu.getName() + "\"";
    1.26                          throw new IllegalArgumentException(kindMsg);
    1.27 +                    }
    1.28                  }
    1.29              }
    1.30  
     2.1 --- a/test/tools/javac/api/TestJavacTask.java	Fri Sep 27 13:06:38 2013 -0700
     2.2 +++ b/test/tools/javac/api/TestJavacTask.java	Fri Sep 27 16:05:56 2013 -0700
     2.3 @@ -1,5 +1,5 @@
     2.4  /*
     2.5 - * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
     2.6 + * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
     2.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.8   *
     2.9   * This code is free software; you can redistribute it and/or modify it
    2.10 @@ -23,8 +23,8 @@
    2.11  
    2.12  /*
    2.13   * @test
    2.14 - * @bug     4813736
    2.15 - * @summary Provide a basic test of access to the Java Model from javac
    2.16 + * @bug     4813736 8015073
    2.17 + * @summary Provide a basic test of access to the Java Model from javac, and error messages
    2.18   * @author  Peter von der Ah\u00e9
    2.19   * @run main TestJavacTask TestJavacTask.java
    2.20   */
    2.21 @@ -40,21 +40,36 @@
    2.22  import javax.tools.ToolProvider;
    2.23  
    2.24  public class TestJavacTask {
    2.25 -
    2.26 -    static JavacTaskImpl getTask(JavaCompiler compiler, File... file) {
    2.27 +    static final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    2.28 +    static JavacTaskImpl getTask(File... file) {
    2.29          StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
    2.30          Iterable<? extends JavaFileObject> files =
    2.31              fm.getJavaFileObjectsFromFiles(Arrays.asList(file));
    2.32          return (JavacTaskImpl)compiler.getTask(null, fm, null, null, null, files);
    2.33      }
    2.34  
    2.35 -    public static void main(String... args) throws IOException {
    2.36 -        JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    2.37 +    static void basicTest(String... args) throws IOException {
    2.38          String srcdir = System.getProperty("test.src");
    2.39          File file = new File(srcdir, args[0]);
    2.40 -        JavacTaskImpl task = getTask(tool, file);
    2.41 +        JavacTaskImpl task = getTask(file);
    2.42          for (TypeElement clazz : task.enter(task.parse()))
    2.43              System.out.println(clazz.getSimpleName());
    2.44      }
    2.45  
    2.46 +    static void checkKindError() {
    2.47 +        final File testFile = new File("Test.java "); // <-note trailing space!
    2.48 +        try {
    2.49 +            getTask(testFile);
    2.50 +        } catch (IllegalArgumentException iae) {
    2.51 +            if (!iae.getMessage().contains("\"" + testFile.getName() + "\"")) {
    2.52 +                System.err.println("Got message: " + iae.getMessage());
    2.53 +                throw new RuntimeException("Error: expected string not found");
    2.54 +            }
    2.55 +        }
    2.56 +    }
    2.57 +
    2.58 +    public static void main(String... args) throws IOException {
    2.59 +        basicTest(args);
    2.60 +        checkKindError();
    2.61 +    }
    2.62  }

mercurial