Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Commit

Permalink
am a7f5cee: Merge "Add type name conversion methods for dexgen"
Browse files Browse the repository at this point in the history
Merge commit 'a7f5cee6539fbfd6fd145614a5970f05439a648f' into dalvik-dev

* commit 'a7f5cee6539fbfd6fd145614a5970f05439a648f':
  Add type name conversion methods for dexgen
  • Loading branch information
Piotr Gurgul authored and Android Git Automerger committed Sep 17, 2010
2 parents 6ce970f a7f5cee commit 9a88aa7
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
13 changes: 12 additions & 1 deletion dexgen/src/com/android/dexgen/rop/cst/CstType.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 133,17 @@ public static CstType intern(Type type) {
return cst;
}

/**
* Returns an interned instance of this class for the given
* {@code Class} instance.
*
* @param clazz {@code non-null;} the underlying {@code Class} object
* @return {@code non-null;} an appropriately-constructed instance
*/
public static CstType intern(Class clazz) {
return intern(Type.intern(clazz));
}

/**
* Constructs an instance.
*
Expand Down Expand Up @@ -227,4 238,4 @@ public CstUtf8 getDescriptor() {

return descriptor;
}
}
}
76 changes: 74 additions & 2 deletions dexgen/src/com/android/dexgen/rop/type/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 31,10 @@ public final class Type implements TypeBearer, Comparable<Type> {
private static final HashMap<String, Type> internTable =
new HashMap<String, Type>(500);

/** {@code non-null;} table mapping types as {@code Class} objects to internal form */
private static final HashMap<Class, Type> CLASS_TYPE_MAP =
new HashMap<Class, Type>();

/** basic type constant for {@code void} */
public static final int BT_VOID = 0;

Expand Down Expand Up @@ -117,6 121,20 @@ public final class Type implements TypeBearer, Comparable<Type> {
* Note: VOID isn't put in the intern table, since it's special and
* shouldn't be found by a normal call to intern().
*/

/*
* Create a mapping between types as Java Class objects
* and types in dx internal format.
*/
CLASS_TYPE_MAP.put(boolean.class, BOOLEAN);
CLASS_TYPE_MAP.put(short.class, SHORT);
CLASS_TYPE_MAP.put(int.class, INT);
CLASS_TYPE_MAP.put(long.class, LONG);
CLASS_TYPE_MAP.put(char.class, CHAR);
CLASS_TYPE_MAP.put(byte.class, BYTE);
CLASS_TYPE_MAP.put(float.class, FLOAT);
CLASS_TYPE_MAP.put(double.class, DOUBLE);
CLASS_TYPE_MAP.put(void.class, VOID);
}

/**
Expand Down Expand Up @@ -276,6 294,21 @@ public final class Type implements TypeBearer, Comparable<Type> {
*/
private Type initializedType;

/**
* Returns the unique instance corresponding to the type represented by
* given {@code Class} object. See vmspec-2 sec4.3.2 for details on the
* field descriptor syntax. This method does <i>not</i> allow
* {@code "V"} (that is, type {@code void}) as a valid
* descriptor.
*
* @param clazz {@code non-null;} class whose descriptor
* will be internalized
* @return {@code non-null;} the corresponding instance
*/
public static Type intern(Class clazz) {
return intern(getInternalTypeName(clazz));
}

/**
* Returns the unique instance corresponding to the type with the
* given descriptor. See vmspec-2 sec4.3.2 for details on the
Expand All @@ -289,7 322,7 @@ public final class Type implements TypeBearer, Comparable<Type> {
* invalid syntax
*/
public static Type intern(String descriptor) {

Type result = internTable.get(descriptor);
if (result != null) {
return result;
Expand Down Expand Up @@ -361,6 394,20 @@ public static Type intern(String descriptor) {
return putIntern(result);
}

/**
* Returns the unique instance corresponding to the type represented by
* given {@code Class} object, allowing {@code "V"} to return the type
* for {@code void}. Other than that one caveat, this method
* is identical to {@link #intern}.
*
* @param clazz {@code non-null;} class which descriptor
* will be internalized
* @return {@code non-null;} the corresponding instance
*/
public static Type internReturnType(Class clazz) {
return internReturnType(getInternalTypeName(clazz));
}

/**
* Returns the unique instance corresponding to the type with the
* given descriptor, allowing {@code "V"} to return the type
Expand Down Expand Up @@ -410,6 457,31 @@ public static Type internClassName(String name) {
return intern('L' name ';');
}

/**
* Converts type name in the format as returned by reflection
* into dex internal form.
*
* @param clazz {@code non-null;} class whose name will be internalized
* @return string with the type name in dex internal format
*/
public static String getInternalTypeName(Class clazz) {
if (clazz == null) {
throw new NullPointerException("clazz == null");
}

if (clazz.isPrimitive()) {
return CLASS_TYPE_MAP.get(clazz).getDescriptor();
}

String slashed = clazz.getName().replace('.', '/');

if (clazz.isArray()) {
return slashed;
}

return 'L' slashed ';';
}

/**
* Constructs an instance corresponding to an "uninitialized type."
* This is a private constructor; use one of the public static
Expand Down Expand Up @@ -853,4 925,4 @@ private static Type putIntern(Type type) {
return type;
}
}
}
}

0 comments on commit 9a88aa7

Please sign in to comment.