changeset 16973:d02b53cdb79c

augment error message with location of problematic element if annotation processing environment might not be able to show the source location of the message
author Doug Simon <doug.simon@oracle.com>
date Wed, 27 Aug 2014 14:09:13 +0200
parents 3bf348ff55e5
children 841119d17b02
files graal/com.oracle.graal.nodeinfo.processor/src/com/oracle/graal/nodeinfo/processor/GraphNodeProcessor.java
diffstat 1 files changed, 35 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.nodeinfo.processor/src/com/oracle/graal/nodeinfo/processor/GraphNodeProcessor.java	Wed Aug 27 12:58:44 2014 +0200
+++ b/graal/com.oracle.graal.nodeinfo.processor/src/com/oracle/graal/nodeinfo/processor/GraphNodeProcessor.java	Wed Aug 27 14:09:13 2014 +0200
@@ -22,8 +22,11 @@
  */
 package com.oracle.graal.nodeinfo.processor;
 
+import static com.oracle.truffle.dsl.processor.java.ElementUtils.*;
+
 import java.io.*;
 import java.util.*;
+import java.util.stream.*;
 
 import javax.annotation.processing.*;
 import javax.lang.model.*;
@@ -46,23 +49,39 @@
         return SourceVersion.latest();
     }
 
+    /**
+     * Node class currently being processed.
+     */
+    private Element scope;
+
+    public static boolean isEnclosedIn(Element e, Element scopeElement) {
+        List<Element> elementHierarchy = getElementHierarchy(e);
+        return elementHierarchy.contains(scopeElement);
+    }
+
     void errorMessage(Element element, String format, Object... args) {
         message(Kind.ERROR, element, format, args);
     }
 
     void message(Kind kind, Element element, String format, Object... args) {
-        processingEnv.getMessager().printMessage(kind, String.format(format, args), element);
+        if (scope != null && !isEnclosedIn(element, scope)) {
+            // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=428357#c1
+            String loc = getElementHierarchy(element).stream().map(Object::toString).collect(Collectors.joining("."));
+            processingEnv.getMessager().printMessage(kind, String.format(loc + ": " + format, args), scope);
+        } else {
+            processingEnv.getMessager().printMessage(kind, String.format(format, args), element);
+        }
     }
 
     /**
      * Bugs in an annotation processor can cause silent failure so try to report any exception
      * throws as errors.
      */
-    private void reportException(Element element, Throwable t) {
+    private void reportException(Kind kind, Element element, Throwable t) {
         StringWriter buf = new StringWriter();
         t.printStackTrace(new PrintWriter(buf));
         buf.toString();
-        errorMessage(element, "Exception thrown during processing: %s", buf.toString());
+        message(kind, element, "Exception thrown during processing: %s", buf.toString());
     }
 
     ProcessingEnvironment getProcessingEnv() {
@@ -94,6 +113,7 @@
         GraphNodeGenerator gen = new GraphNodeGenerator(this);
 
         for (Element element : roundEnv.getElementsAnnotatedWith(NodeInfo.class)) {
+            scope = element;
             try {
                 if (!isNodeType(element)) {
                     errorMessage(element, "%s can only be applied to Node subclasses", NodeInfo.class.getSimpleName());
@@ -126,11 +146,9 @@
             } catch (ElementException ee) {
                 errorMessage(ee.element, ee.getMessage());
             } catch (Throwable t) {
-                if (!isBug367599(t)) {
-                    reportException(element, t);
-                } else {
-                    message(Kind.NOTE, element, t.toString());
-                }
+                reportException(isBug367599(t) ? Kind.NOTE : Kind.ERROR, element, t);
+            } finally {
+                scope = null;
             }
         }
         return false;
@@ -141,14 +159,16 @@
      * href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=367599">Bug 367599</a>.
      */
     public static boolean isBug367599(Throwable t) {
-        for (StackTraceElement ste : t.getStackTrace()) {
-            if (ste.toString().contains("org.eclipse.jdt.internal.apt.pluggable.core.filer.IdeFilerImpl.create")) {
-                // See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=367599
-                return true;
+        if (t instanceof FilerException) {
+            for (StackTraceElement ste : t.getStackTrace()) {
+                if (ste.toString().contains("org.eclipse.jdt.internal.apt.pluggable.core.filer.IdeFilerImpl.create")) {
+                    // See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=367599
+                    return true;
+                }
             }
-        }
-        if (t.getCause() != null) {
-            return isBug367599(t.getCause());
+            if (t.getCause() != null) {
+                return isBug367599(t.getCause());
+            }
         }
         return false;
     }