changeset 23259:47e45579e9ce

Support generic types in generated plugins
author Christian Wimmer <christian.wimmer@oracle.com>
date Tue, 05 Jan 2016 16:32:42 -0800
parents 375597985a1f
children 206bf428d745
files graal/com.oracle.graal.replacements.verifier/src/com/oracle/graal/replacements/verifier/GeneratedFoldPlugin.java graal/com.oracle.graal.replacements.verifier/src/com/oracle/graal/replacements/verifier/GeneratedPlugin.java
diffstat 2 files changed, 64 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.replacements.verifier/src/com/oracle/graal/replacements/verifier/GeneratedFoldPlugin.java	Wed Dec 16 16:17:23 2015 -0800
+++ b/graal/com.oracle.graal.replacements.verifier/src/com/oracle/graal/replacements/verifier/GeneratedFoldPlugin.java	Tue Jan 05 16:32:42 2016 -0800
@@ -25,6 +25,7 @@
 import java.io.PrintWriter;
 import java.util.List;
 import java.util.Set;
+import java.util.TreeSet;
 
 import javax.annotation.processing.ProcessingEnvironment;
 import javax.lang.model.element.ExecutableElement;
@@ -78,10 +79,29 @@
             argCount++;
         }
 
+        Set<String> suppressWarnings = new TreeSet<>();
         if (intrinsicMethod.getAnnotation(Deprecated.class) != null) {
-            out.printf("            @SuppressWarnings(\"deprecation\")\n");
+            suppressWarnings.add("deprecation");
         }
-        out.printf("            %s result = %s.%s(", intrinsicMethod.getReturnType(), receiver, intrinsicMethod.getSimpleName());
+        if (hasRawtypeWarning(intrinsicMethod.getReturnType())) {
+            suppressWarnings.add("rawtypes");
+        }
+        for (VariableElement param : params) {
+            if (hasUncheckedWarning(param.asType())) {
+                suppressWarnings.add("unchecked");
+            }
+        }
+        if (suppressWarnings.size() > 0) {
+            out.printf("            @SuppressWarnings({");
+            String sep = "";
+            for (String suppressWarning : suppressWarnings) {
+                out.printf("%s\"%s\"", sep, suppressWarning);
+                sep = ", ";
+            }
+            out.printf("})\n");
+        }
+
+        out.printf("            %s result = %s.%s(", getErasedType(intrinsicMethod.getReturnType()), receiver, intrinsicMethod.getSimpleName());
         if (argCount > firstArg) {
             out.printf("arg%d", firstArg);
             for (int i = firstArg + 1; i < argCount; i++) {
--- a/graal/com.oracle.graal.replacements.verifier/src/com/oracle/graal/replacements/verifier/GeneratedPlugin.java	Wed Dec 16 16:17:23 2015 -0800
+++ b/graal/com.oracle.graal.replacements.verifier/src/com/oracle/graal/replacements/verifier/GeneratedPlugin.java	Tue Jan 05 16:32:42 2016 -0800
@@ -117,6 +117,43 @@
         }
     }
 
+    static boolean hasRawtypeWarning(TypeMirror type) {
+        switch (type.getKind()) {
+            case DECLARED:
+                DeclaredType declared = (DeclaredType) type;
+                return declared.getTypeArguments().size() > 0;
+            case TYPEVAR:
+                return false;
+            case WILDCARD:
+                return false;
+            case ARRAY:
+                return hasRawtypeWarning(((ArrayType) type).getComponentType());
+            default:
+                return false;
+        }
+    }
+
+    static boolean hasUncheckedWarning(TypeMirror type) {
+        switch (type.getKind()) {
+            case DECLARED:
+                DeclaredType declared = (DeclaredType) type;
+                for (TypeMirror typeParam : declared.getTypeArguments()) {
+                    if (hasUncheckedWarning(typeParam)) {
+                        return true;
+                    }
+                }
+                return false;
+            case TYPEVAR:
+                return true;
+            case WILDCARD:
+                return ((WildcardType) type).getExtendsBound() != null;
+            case ARRAY:
+                return hasUncheckedWarning(((ArrayType) type).getComponentType());
+            default:
+                return false;
+        }
+    }
+
     private void createPrivateMembers(PrintWriter out, InjectedDependencies deps) {
         if (!deps.isEmpty()) {
             out.printf("\n");
@@ -161,7 +198,10 @@
     }
 
     protected static void constantArgument(ProcessingEnvironment env, PrintWriter out, InjectedDependencies deps, int argIdx, TypeMirror type, int nodeIdx) {
-        out.printf("            %s arg%d;\n", type, argIdx);
+        if (hasRawtypeWarning(type)) {
+            out.printf("            @SuppressWarnings({\"rawtypes\"})\n");
+        }
+        out.printf("            %s arg%d;\n", getErasedType(type), argIdx);
         out.printf("            if (args[%d].isConstant()) {\n", nodeIdx);
         if (type.equals(resolvedJavaTypeType(env))) {
             out.printf("                arg%d = %s.asJavaType(args[%d].asConstant());\n", argIdx, deps.use(WellKnownDependency.CONSTANT_REFLECTION), nodeIdx);
@@ -192,7 +232,7 @@
                     out.printf("                arg%d = args[%d].asJavaConstant().asDouble();\n", argIdx, nodeIdx);
                     break;
                 case DECLARED:
-                    out.printf("                arg%d = %s.asObject(%s.class, args[%d].asJavaConstant());\n", argIdx, deps.use(WellKnownDependency.SNIPPET_REFLECTION), type, nodeIdx);
+                    out.printf("                arg%d = %s.asObject(%s.class, args[%d].asJavaConstant());\n", argIdx, deps.use(WellKnownDependency.SNIPPET_REFLECTION), getErasedType(type), nodeIdx);
                     break;
                 default:
                     throw new IllegalArgumentException();