changeset 9984:f3330a4487eb

added ResolvedJava[Field|Method].isSynthetic()
author Doug Simon <doug.simon@oracle.com>
date Tue, 11 Jun 2013 01:17:10 +0200
parents 13384d19fec0
children e6bd1004082a
files graal/com.oracle.graal.api.meta.test/src/com/oracle/graal/api/meta/test/TestResolvedJavaMethod.java graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/ResolvedJavaField.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/HotSpotResolvedJavaField.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedJavaMethod.java
diffstat 5 files changed, 67 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.api.meta.test/src/com/oracle/graal/api/meta/test/TestResolvedJavaMethod.java	Tue Jun 11 00:00:40 2013 +0200
+++ b/graal/com.oracle.graal.api.meta.test/src/com/oracle/graal/api/meta/test/TestResolvedJavaMethod.java	Tue Jun 11 01:17:10 2013 +0200
@@ -132,6 +132,18 @@
     }
 
     @Test
+    public void isSyntheticTest() {
+        for (Map.Entry<Method, ResolvedJavaMethod> e : methods.entrySet()) {
+            ResolvedJavaMethod m = e.getValue();
+            assertEquals(e.getKey().isSynthetic(), m.isSynthetic());
+        }
+        for (Map.Entry<Constructor, ResolvedJavaMethod> e : constructors.entrySet()) {
+            ResolvedJavaMethod m = e.getValue();
+            assertEquals(e.getKey().isSynthetic(), m.isSynthetic());
+        }
+    }
+
+    @Test
     public void canBeStaticallyBoundTest() {
         for (Map.Entry<Method, ResolvedJavaMethod> e : methods.entrySet()) {
             ResolvedJavaMethod m = e.getValue();
--- a/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/ResolvedJavaField.java	Tue Jun 11 00:00:40 2013 +0200
+++ b/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/ResolvedJavaField.java	Tue Jun 11 01:17:10 2013 +0200
@@ -45,6 +45,11 @@
     boolean isInternal();
 
     /**
+     * Determines if this field is a synthetic field as defined by the Java Language Specification.
+     */
+    boolean isSynthetic();
+
+    /**
      * Gets the constant value of this field for a given object, if available.
      * 
      * @param receiver object from which this field's value is to be read. This value is ignored if
--- a/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/ResolvedJavaMethod.java	Tue Jun 11 00:00:40 2013 +0200
+++ b/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/ResolvedJavaMethod.java	Tue Jun 11 01:17:10 2013 +0200
@@ -80,6 +80,12 @@
     int getModifiers();
 
     /**
+     * Determines if this method is a synthetic method as defined by the Java Language
+     * Specification.
+     */
+    boolean isSynthetic();
+
+    /**
      * 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/HotSpotResolvedJavaField.java	Tue Jun 11 00:00:40 2013 +0200
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedJavaField.java	Tue Jun 11 01:17:10 2013 +0200
@@ -160,6 +160,15 @@
     }
 
     @Override
+    public boolean isSynthetic() {
+        Field javaField = toJava();
+        if (javaField != null) {
+            return javaField.isSynthetic();
+        }
+        return false;
+    }
+
+    @Override
     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
         Field javaField = toJava();
         if (javaField != null) {
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedJavaMethod.java	Tue Jun 11 00:00:40 2013 +0200
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedJavaMethod.java	Tue Jun 11 01:17:10 2013 +0200
@@ -332,6 +332,29 @@
     }
 
     @Override
+    public boolean isSynthetic() {
+        if (isConstructor()) {
+            Constructor<?> javaConstructor = toJavaConstructor();
+            return javaConstructor == null ? false : javaConstructor.isSynthetic();
+        }
+
+        // Cannot use toJava() as it ignores the return type
+        HotSpotSignature sig = getSignature();
+        JavaType[] sigTypes = MetaUtil.signatureToTypes(sig, null);
+        HotSpotRuntime runtime = graalRuntime().getRuntime();
+        for (Method method : holder.mirror().getDeclaredMethods()) {
+            if (method.getName().equals(name)) {
+                if (runtime.lookupJavaType(method.getReturnType()).equals(sig.getReturnType(holder))) {
+                    if (matches(runtime, sigTypes, method.getParameterTypes())) {
+                        return method.isSynthetic();
+                    }
+                }
+            }
+        }
+        return false;
+    }
+
+    @Override
     public Type[] getGenericParameterTypes() {
         if (isConstructor()) {
             Constructor javaConstructor = toJavaConstructor();
@@ -351,6 +374,18 @@
         return result;
     }
 
+    private static boolean matches(HotSpotRuntime runtime, JavaType[] sigTypes, Class<?>[] parameterTypes) {
+        if (parameterTypes.length == sigTypes.length) {
+            for (int i = 0; i < parameterTypes.length; i++) {
+                if (!runtime.lookupJavaType(parameterTypes[i]).equals(sigTypes[i])) {
+                    return false;
+                }
+            }
+            return true;
+        }
+        return false;
+    }
+
     private Method toJava() {
         try {
             return holder.mirror().getDeclaredMethod(name, signatureToTypes());