# HG changeset patch # User Doug Simon # Date 1354051479 -3600 # Node ID e4d9f153934f53375f738256f95a49ca07928f78 # Parent 413f9352cdfd56142719c10de7ba2fb3282fd3de removed ResolvedJavaType.toJava() and introduced ResolvedJavaType.isPrimitive() diff -r 413f9352cdfd -r e4d9f153934f graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/MetaUtil.java --- a/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/MetaUtil.java Tue Nov 27 20:36:46 2012 +0100 +++ b/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/MetaUtil.java Tue Nov 27 22:24:39 2012 +0100 @@ -46,10 +46,63 @@ } /** - * Determines if a given type represents a primitive type. + * Gets the {@link Class} mirror for a given resolved type. + * + * @param type the type for which the Java mirror is requested + * @param loader class loader from which the class must be loaded (null means use the class loader of the {@link MetaUtil} class) + * @return the mirror for {@code type} + * @throws NoClassDefFoundError if the mirror is not available */ - public static boolean isPrimitive(ResolvedJavaType type) { - return type.getSuperclass() == null && !type.isInstanceClass(); + public static Class getMirrorOrFail(ResolvedJavaType type, ClassLoader loader) throws NoClassDefFoundError { + ResolvedJavaType elementalType = getElementalType(type); + Class elementalClass; + if (elementalType.isPrimitive()) { + elementalClass = type.getKind().toJavaClass(); + } else { + try { + elementalClass = Class.forName(toJavaName(elementalType), true, loader); + } catch (ClassNotFoundException e) { + throw (NoClassDefFoundError) new NoClassDefFoundError().initCause(e); + } + } + if (type.isArrayClass()) { + ResolvedJavaType t = type; + while (t.getComponentType() != null) { + elementalClass = Array.newInstance(elementalClass, 0).getClass(); + t = t.getComponentType(); + } + } + assert elementalClass != null : toJavaName(type); + return elementalClass; + } + + /** + * Gets the {@link Class} mirror for a given resolved type. + * + * @param type the type for which the Java mirror is requested + * @param loader class loader from which the class must be loaded (null means use the class loader of the {@link MetaUtil} class) + * @return the mirror for {@code type} or null if it is not available + */ + public static Class getMirror(ResolvedJavaType type, ClassLoader loader) { + try { + return getMirrorOrFail(type, loader); + } catch (NoClassDefFoundError e) { + return null; + } + } + + /** + * Gets the elemental type for a given type. + * The elemental type of an array type is the corresponding zero dimensional (e.g., + * the elemental type of {@code int[][][]} is {@code int}). A non-array type is its + * own elemental type. + */ + public static ResolvedJavaType getElementalType(ResolvedJavaType type) { + ResolvedJavaType t = type; + while (t.getComponentType() != null) { + t = t.getComponentType(); + } + return t; } /** diff -r 413f9352cdfd -r e4d9f153934f graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/ResolvedJavaType.java --- a/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/ResolvedJavaType.java Tue Nov 27 20:36:46 2012 +0100 +++ b/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/ResolvedJavaType.java Tue Nov 27 22:24:39 2012 +0100 @@ -104,6 +104,13 @@ boolean isArrayClass(); /** + * Checks whether this type is primitive. + * + * @return {@code true} if this type is primitive + */ + boolean isPrimitive(); + + /** * Returns the Java language modifiers for this type, as an integer. The {@link Modifier} class should be used to * decode the modifiers. Only the flags specified in the JVM specification will be included in the returned mask. * This method is identical to {@link Class#getModifiers()} in terms of the value return for this type. @@ -239,11 +246,6 @@ boolean isClass(Class c); /** - * Returns the {@link java.lang.Class} object representing this type. - */ - Class< ? > toJava(); - - /** * Returns the instance field of this class (or one of its super classes) at the given * offset, or {@code null} if there is no such field. * diff -r 413f9352cdfd -r e4d9f153934f graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedJavaType.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedJavaType.java Tue Nov 27 20:36:46 2012 +0100 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedJavaType.java Tue Nov 27 22:24:39 2012 +0100 @@ -160,17 +160,9 @@ return javaComponentType == null ? null : fromClass(javaComponentType); } - public ResolvedJavaType getElementType() { - ResolvedJavaType type = this; - while (type.getComponentType() != null) { - type = type.getComponentType(); - } - return type; - } - private static boolean hasSubtype(ResolvedJavaType type) { assert !type.isArrayClass() : type; - if (isPrimitive(type)) { + if (type.isPrimitive()) { return false; } HotSpotVMConfig config = HotSpotGraalRuntime.getInstance().getConfig(); @@ -184,8 +176,7 @@ public ResolvedJavaType findUniqueConcreteSubtype() { HotSpotVMConfig config = HotSpotGraalRuntime.getInstance().getConfig(); if (isArrayClass()) { - ResolvedJavaType elementType = getElementType(); - if (hasSubtype(elementType)) { + if (hasSubtype(getElementalType(this))) { return null; } return this; @@ -227,7 +218,7 @@ public HotSpotResolvedJavaType getSupertype() { if (isArrayClass()) { ResolvedJavaType componentType = getComponentType(); - if (javaMirror == Object[].class || componentType instanceof HotSpotTypePrimitive) { + if (javaMirror == Object[].class || componentType.isPrimitive()) { return (HotSpotResolvedJavaType) fromClass(Object.class); } return (HotSpotResolvedJavaType) ((HotSpotResolvedJavaType) componentType).getSupertype().getArrayClass(); @@ -240,7 +231,7 @@ @Override public ResolvedJavaType findLeastCommonAncestor(ResolvedJavaType otherType) { - if (otherType instanceof HotSpotTypePrimitive) { + if (otherType.isPrimitive()) { return null; } else { HotSpotResolvedJavaType t1 = this; @@ -294,6 +285,11 @@ } @Override + public boolean isPrimitive() { + return false; + } + + @Override public boolean isArrayClass() { return sizeOrSpecies == ARRAY_SPECIES_VALUE; } @@ -452,11 +448,6 @@ } @Override - public Class< ? > toJava() { - return javaMirror; - } - - @Override public Class mirror() { return javaMirror; } diff -r 413f9352cdfd -r e4d9f153934f graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotRuntime.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotRuntime.java Tue Nov 27 20:36:46 2012 +0100 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotRuntime.java Tue Nov 27 22:24:39 2012 +0100 @@ -606,7 +606,8 @@ ObjectStamp stamp = (ObjectStamp) obj.stamp(); if (stamp.nonNull() && stamp.isExactType()) { StructuredGraph graph = new StructuredGraph(); - ValueNode result = ConstantNode.forObject(stamp.type().toJava(), this, graph); + HotSpotJavaType type = (HotSpotJavaType) stamp.type(); + ValueNode result = ConstantNode.forObject(type.mirror(), this, graph); ReturnNode ret = graph.add(new ReturnNode(result)); graph.start().setNext(ret); return graph; diff -r 413f9352cdfd -r e4d9f153934f graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotTypePrimitive.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotTypePrimitive.java Tue Nov 27 20:36:46 2012 +0100 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotTypePrimitive.java Tue Nov 27 22:24:39 2012 +0100 @@ -102,6 +102,11 @@ } @Override + public boolean isPrimitive() { + return true; + } + + @Override public boolean isInitialized() { return true; } @@ -123,10 +128,7 @@ @Override public boolean isAssignableTo(ResolvedJavaType other) { - if (other instanceof HotSpotTypePrimitive) { - return other == this; - } - return false; + return other == this; } @Override @@ -165,11 +167,6 @@ } @Override - public Class< ? > toJava() { - return javaMirror; - } - - @Override public boolean isClass(Class c) { return c == javaMirror; } diff -r 413f9352cdfd -r e4d9f153934f graal/com.oracle.graal.snippets.test/src/com/oracle/graal/snippets/NewMultiArrayTest.java --- a/graal/com.oracle.graal.snippets.test/src/com/oracle/graal/snippets/NewMultiArrayTest.java Tue Nov 27 20:36:46 2012 +0100 +++ b/graal/com.oracle.graal.snippets.test/src/com/oracle/graal/snippets/NewMultiArrayTest.java Tue Nov 27 22:24:39 2012 +0100 @@ -99,9 +99,8 @@ @Override protected Object referenceInvoke(Method method, Object receiver, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { if (bottomType != null) { - Class< ? > componentType = bottomType.toJava(); try { - return Array.newInstance(componentType, dimensions); + return Array.newInstance(bottomClass, dimensions); } catch (Exception e) { throw new InvocationTargetException(e); } @@ -111,11 +110,13 @@ ResolvedJavaType arrayType; ResolvedJavaType bottomType; + Class bottomClass; int[] dimensions; @Test public void test1() { for (Class clazz : new Class[] {byte.class, char.class, short.class, int.class, float.class, long.class, double.class, String.class}) { + bottomClass = clazz; bottomType = runtime.lookupJavaType(clazz); arrayType = bottomType; for (int rank : new int[] {1, 2, 10, 50, 100, 200, 254, 255}) { diff -r 413f9352cdfd -r e4d9f153934f graal/com.oracle.graal.snippets/src/com/oracle/graal/snippets/Snippet.java --- a/graal/com.oracle.graal.snippets/src/com/oracle/graal/snippets/Snippet.java Tue Nov 27 20:36:46 2012 +0100 +++ b/graal/com.oracle.graal.snippets/src/com/oracle/graal/snippets/Snippet.java Tue Nov 27 22:24:39 2012 +0100 @@ -22,6 +22,8 @@ */ package com.oracle.graal.snippets; +import static com.oracle.graal.api.meta.MetaUtil.*; + import java.lang.annotation.*; import java.lang.reflect.*; @@ -80,7 +82,7 @@ if (method.getAnnotation(NodeIntrinsic.class) != null) { return false; } - if (Throwable.class.isAssignableFrom(method.getDeclaringClass().toJava())) { + if (Throwable.class.isAssignableFrom(getMirrorOrFail(method.getDeclaringClass(), null))) { if (method.getName().equals("")) { return false; } diff -r 413f9352cdfd -r e4d9f153934f graal/com.oracle.graal.snippets/src/com/oracle/graal/snippets/SnippetIntrinsificationPhase.java --- a/graal/com.oracle.graal.snippets/src/com/oracle/graal/snippets/SnippetIntrinsificationPhase.java Tue Nov 27 20:36:46 2012 +0100 +++ b/graal/com.oracle.graal.snippets/src/com/oracle/graal/snippets/SnippetIntrinsificationPhase.java Tue Nov 27 22:24:39 2012 +0100 @@ -22,6 +22,8 @@ */ package com.oracle.graal.snippets; +import static com.oracle.graal.api.meta.MetaUtil.*; + import java.lang.reflect.*; import java.util.*; @@ -79,11 +81,11 @@ } } - public static Class< ? >[] signatureToTypes(Signature signature, ResolvedJavaType accessingClass) { + public static Class[] signatureToTypes(Signature signature, ResolvedJavaType accessingClass) { int count = signature.getParameterCount(false); - Class< ? >[] result = new Class< ? >[count]; + Class[] result = new Class< ? >[count]; for (int i = 0; i < result.length; ++i) { - result[i] = signature.getParameterType(i, accessingClass).resolve(accessingClass).toJava(); + result[i] = getMirrorOrFail(signature.getParameterType(i, accessingClass).resolve(accessingClass), null); } return result; } @@ -124,7 +126,7 @@ } // Call the method - Constant constant = callMethod(target.getSignature().getReturnKind(), declaringClass.toJava(), target.getName(), parameterTypes, receiver, arguments); + Constant constant = callMethod(target.getSignature().getReturnKind(), getMirrorOrFail(declaringClass, null), target.getName(), parameterTypes, receiver, arguments); if (constant != null) { // Replace the invoke with the result of the call @@ -189,7 +191,7 @@ private static Class< ? > getNodeClass(ResolvedJavaMethod target, NodeIntrinsic intrinsic) { Class< ? > result = intrinsic.value(); if (result == NodeIntrinsic.class) { - result = target.getDeclaringClass().toJava(); + return getMirrorOrFail(target.getDeclaringClass(), null); } assert Node.class.isAssignableFrom(result); return result;