changeset 13183:0266ac3b26c0

JDK8: added support for default methods as well as interfaces containing static and private methods
author Doug Simon <doug.simon@oracle.com>
date Wed, 27 Nov 2013 18:53:07 +0100
parents 599eaf3bc4b2
children 6531170a229f
files graal/com.oracle.graal.api.meta.jdk8.test/src/com/oracle/graal/api/meta/jdk8/test/TestResolvedJavaMethodJDK8.java graal/com.oracle.graal.api.meta.test/src/com/oracle/graal/api/meta/test/TestResolvedJavaMethod.java graal/com.oracle.graal.api.meta.test/src/com/oracle/graal/api/meta/test/TestResolvedJavaType.java graal/com.oracle.graal.api.meta.test/src/com/oracle/graal/api/meta/test/TypeUniverse.java graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/ResolvedJavaMethod.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedJavaMethod.java mx/projects
diffstat 7 files changed, 96 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.api.meta.jdk8.test/src/com/oracle/graal/api/meta/jdk8/test/TestResolvedJavaMethodJDK8.java	Wed Nov 27 18:53:07 2013 +0100
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.graal.api.meta.jdk8.test;
+
+import static org.junit.Assert.*;
+import static org.junit.Assume.*;
+
+import java.lang.reflect.*;
+import java.util.*;
+
+import org.junit.*;
+
+import com.oracle.graal.api.meta.*;
+import com.oracle.graal.api.meta.test.*;
+
+/**
+ * Tests for {@link ResolvedJavaMethod} that require JDK >= 8.
+ */
+public class TestResolvedJavaMethodJDK8 extends MethodUniverse {
+
+    public TestResolvedJavaMethodJDK8() {
+    }
+
+    @Test
+    public void isDefaultTest() {
+        assumeTrue(JAVA_VERSION >= 1.8D);
+        for (Map.Entry<Method, ResolvedJavaMethod> e : methods.entrySet()) {
+            ResolvedJavaMethod m = e.getValue();
+            assertEquals(e.getKey().isDefault(), m.isDefault());
+        }
+        for (Map.Entry<Constructor, ResolvedJavaMethod> e : constructors.entrySet()) {
+            ResolvedJavaMethod m = e.getValue();
+            assertFalse(m.isDefault());
+        }
+    }
+}
--- a/graal/com.oracle.graal.api.meta.test/src/com/oracle/graal/api/meta/test/TestResolvedJavaMethod.java	Tue Nov 26 11:41:47 2013 -0800
+++ b/graal/com.oracle.graal.api.meta.test/src/com/oracle/graal/api/meta/test/TestResolvedJavaMethod.java	Wed Nov 27 18:53:07 2013 +0100
@@ -292,7 +292,8 @@
         "canBeInlined",
         "getLineNumberTable",
         "getLocalVariableTable",
-        "isInVirtualMethodTable"
+        "isInVirtualMethodTable",
+        "isDefault" // tested in TestResolvedJavaMethodJDK8
     };
     // @formatter:on
 
--- a/graal/com.oracle.graal.api.meta.test/src/com/oracle/graal/api/meta/test/TestResolvedJavaType.java	Tue Nov 26 11:41:47 2013 -0800
+++ b/graal/com.oracle.graal.api.meta.test/src/com/oracle/graal/api/meta/test/TestResolvedJavaType.java	Wed Nov 27 18:53:07 2013 +0100
@@ -443,8 +443,14 @@
             if (c.isInterface() || c.isPrimitive()) {
                 ResolvedJavaType type = metaAccess.lookupJavaType(c);
                 for (Method m : c.getDeclaredMethods()) {
-                    ResolvedJavaMethod impl = type.resolveMethod(metaAccess.lookupJavaMethod(m));
-                    assertEquals(m.toString(), null, impl);
+                    if (JAVA_VERSION <= 1.7D || (!isStatic(m.getModifiers()) && !isPrivate(m.getModifiers()))) {
+                        ResolvedJavaMethod resolved = metaAccess.lookupJavaMethod(m);
+                        ResolvedJavaMethod impl = type.resolveMethod(resolved);
+                        ResolvedJavaMethod expected = resolved.isDefault() ? resolved : null;
+                        assertEquals(m.toString(), expected, impl);
+                    } else {
+                        // As of JDK 8, interfaces can have static and private methods
+                    }
                 }
             } else {
                 ResolvedJavaType type = metaAccess.lookupJavaType(c);
--- a/graal/com.oracle.graal.api.meta.test/src/com/oracle/graal/api/meta/test/TypeUniverse.java	Tue Nov 26 11:41:47 2013 -0800
+++ b/graal/com.oracle.graal.api.meta.test/src/com/oracle/graal/api/meta/test/TypeUniverse.java	Wed Nov 27 18:53:07 2013 +0100
@@ -42,6 +42,7 @@
 public class TypeUniverse {
 
     public final Unsafe unsafe;
+    public static final double JAVA_VERSION = Double.valueOf(System.getProperty("java.specification.version"));
 
     public final MetaAccessProvider metaAccess;
     public final ConstantReflectionProvider constantReflection;
--- a/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/ResolvedJavaMethod.java	Tue Nov 26 11:41:47 2013 -0800
+++ b/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/ResolvedJavaMethod.java	Wed Nov 27 18:53:07 2013 +0100
@@ -91,6 +91,17 @@
     boolean isSynthetic();
 
     /**
+     * Returns {@code true} if this method is a default method; returns {@code false} otherwise.
+     * 
+     * A default method is a public non-abstract instance method, that is, a non-static method with
+     * a body, declared in an interface type.
+     * 
+     * @return true if and only if this method is a default method as defined by the Java Language
+     *         Specification.
+     */
+    boolean isDefault();
+
+    /**
      * Checks whether this method is a class initializer.
      * 
      * @return {@code true} if the method is a class initializer
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedJavaMethod.java	Tue Nov 26 11:41:47 2013 -0800
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedJavaMethod.java	Wed Nov 27 18:53:07 2013 +0100
@@ -371,6 +371,15 @@
         return false;
     }
 
+    public boolean isDefault() {
+        if (isConstructor()) {
+            return false;
+        }
+        // Copied from java.lang.Method.isDefault()
+        int mask = Modifier.ABSTRACT | Modifier.PUBLIC | Modifier.STATIC;
+        return ((getModifiers() & mask) == Modifier.PUBLIC) && getDeclaringClass().isInterface();
+    }
+
     @Override
     public Type[] getGenericParameterTypes() {
         if (isConstructor()) {
--- a/mx/projects	Tue Nov 26 11:41:47 2013 -0800
+++ b/mx/projects	Wed Nov 27 18:53:07 2013 +0100
@@ -67,6 +67,14 @@
 project@com.oracle.graal.api.meta.test@javaCompliance=1.7
 project@com.oracle.graal.api.meta.test@workingSets=API,Graal,Test
 
+# graal.api.meta.jdk8.test
+project@com.oracle.graal.api.meta.jdk8.test@subDir=graal
+project@com.oracle.graal.api.meta.jdk8.test@sourceDirs=src
+project@com.oracle.graal.api.meta.jdk8.test@dependencies=com.oracle.graal.api.meta.test
+project@com.oracle.graal.api.meta.jdk8.test@checkstyle=com.oracle.graal.graph
+project@com.oracle.graal.api.meta.jdk8.test@javaCompliance=1.8
+project@com.oracle.graal.api.meta.jdk8.test@workingSets=API,Graal,Test
+
 # graal.api.code
 project@com.oracle.graal.api.code@subDir=graal
 project@com.oracle.graal.api.code@sourceDirs=src
@@ -176,7 +184,7 @@
 project@com.oracle.graal.hotspot.test@javaCompliance=1.7
 project@com.oracle.graal.hotspot.test@workingSets=Graal,HotSpot,Test
 
-# graal.hotspot.test
+# graal.hotspot.jdk8.test
 project@com.oracle.graal.hotspot.jdk8.test@subDir=graal
 project@com.oracle.graal.hotspot.jdk8.test@sourceDirs=src
 project@com.oracle.graal.hotspot.jdk8.test@dependencies=com.oracle.graal.compiler.test