TypeId.java revision 0e49fb9243b7463835ab80ef7cc62435f55846ce
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.google.dexmaker; 18 19import com.android.dx.rop.cst.CstType; 20import java.util.HashMap; 21import java.util.Map; 22 23/** 24 * A primitive type, interface or class. 25 */ 26public final class TypeId<T> { 27 /** The {@code boolean} primitive type. */ 28 public static final TypeId<Boolean> BOOLEAN 29 = new TypeId<Boolean>(com.android.dx.rop.type.Type.BOOLEAN); 30 31 /** The {@code byte} primitive type. */ 32 public static final TypeId<Byte> BYTE = new TypeId<Byte>(com.android.dx.rop.type.Type.BYTE); 33 34 /** The {@code char} primitive type. */ 35 public static final TypeId<Character> CHAR 36 = new TypeId<Character>(com.android.dx.rop.type.Type.CHAR); 37 38 /** The {@code double} primitive type. */ 39 public static final TypeId<Double> DOUBLE = new TypeId<Double>(com.android.dx.rop.type.Type.DOUBLE); 40 41 /** The {@code float} primitive type. */ 42 public static final TypeId<Float> FLOAT = new TypeId<Float>(com.android.dx.rop.type.Type.FLOAT); 43 44 /** The {@code int} primitive type. */ 45 public static final TypeId<Integer> INT = new TypeId<Integer>(com.android.dx.rop.type.Type.INT); 46 47 /** The {@code long} primitive type. */ 48 public static final TypeId<Long> LONG = new TypeId<Long>(com.android.dx.rop.type.Type.LONG); 49 50 /** The {@code short} primitive type. */ 51 public static final TypeId<Short> SHORT = new TypeId<Short>(com.android.dx.rop.type.Type.SHORT); 52 53 /** The {@code void} primitive type. Only used as a return type. */ 54 public static final TypeId<Void> VOID = new TypeId<Void>(com.android.dx.rop.type.Type.VOID); 55 56 /** The {@code Object} type. */ 57 public static final TypeId<Object> OBJECT = new TypeId<Object>(com.android.dx.rop.type.Type.OBJECT); 58 59 /** The {@code String} type. */ 60 public static final TypeId<String> STRING = new TypeId<String>(com.android.dx.rop.type.Type.STRING); 61 62 private static final Map<Class<?>, TypeId<?>> PRIMITIVE_TO_TYPE 63 = new HashMap<Class<?>, TypeId<?>>(); 64 static { 65 PRIMITIVE_TO_TYPE.put(boolean.class, BOOLEAN); 66 PRIMITIVE_TO_TYPE.put(byte.class, BYTE); 67 PRIMITIVE_TO_TYPE.put(char.class, CHAR); 68 PRIMITIVE_TO_TYPE.put(double.class, DOUBLE); 69 PRIMITIVE_TO_TYPE.put(float.class, FLOAT); 70 PRIMITIVE_TO_TYPE.put(int.class, INT); 71 PRIMITIVE_TO_TYPE.put(long.class, LONG); 72 PRIMITIVE_TO_TYPE.put(short.class, SHORT); 73 PRIMITIVE_TO_TYPE.put(void.class, VOID); 74 } 75 76 final String name; 77 78 /** cached converted values */ 79 final com.android.dx.rop.type.Type ropType; 80 final CstType constant; 81 82 TypeId(com.android.dx.rop.type.Type ropType) { 83 this(ropType.getDescriptor(), ropType); 84 } 85 86 TypeId(String name, com.android.dx.rop.type.Type ropType) { 87 if (name == null || ropType == null) { 88 throw new NullPointerException(); 89 } 90 this.name = name; 91 this.ropType = ropType; 92 this.constant = CstType.intern(ropType); 93 } 94 95 /** 96 * @param name a descriptor like "Ljava/lang/Class;". 97 */ 98 public static <T> TypeId<T> get(String name) { 99 return new TypeId<T>(name, com.android.dx.rop.type.Type.internReturnType(name)); 100 } 101 102 public static <T> TypeId<T> get(Class<T> type) { 103 if (type.isPrimitive()) { 104 @SuppressWarnings("unchecked") // guarded by equals 105 TypeId<T> result = (TypeId<T>) PRIMITIVE_TO_TYPE.get(type); 106 return result; 107 } 108 String name = type.getName().replace('.', '/'); 109 return get(type.isArray() ? name : 'L' + name + ';'); 110 } 111 112 public <V> FieldId<T, V> getField(TypeId<V> type, String name) { 113 return new FieldId<T, V>(this, type, name); 114 } 115 116 public MethodId<T, Void> getConstructor(TypeId<?>... parameters) { 117 return new MethodId<T, Void>(this, VOID, "<init>", new TypeList(parameters)); 118 } 119 120 public <R> MethodId<T, R> getMethod(TypeId<R> returnType, String name, TypeId<?>... parameters) { 121 return new MethodId<T, R>(this, returnType, name, new TypeList(parameters)); 122 } 123 124 public String getName() { 125 return name; 126 } 127 128 @Override public boolean equals(Object o) { 129 return o instanceof TypeId 130 && ((TypeId) o).name.equals(name); 131 } 132 133 @Override public int hashCode() { 134 return name.hashCode(); 135 } 136 137 @Override public String toString() { 138 return name; 139 } 140} 141