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 17 package libcore.reflect; 18 19 import java.lang.reflect.Array; 20 21 /** 22 * Work with a type's internal name like "V" or "Ljava/lang/String;". 23 */ 24 public final class InternalNames { InternalNames()25 private InternalNames() { 26 } 27 getClass(ClassLoader classLoader, String internalName)28 public static Class<?> getClass(ClassLoader classLoader, String internalName) { 29 if (internalName.startsWith("[")) { 30 Class<?> componentClass = getClass(classLoader, internalName.substring(1)); 31 return Array.newInstance(componentClass, 0).getClass(); 32 } else if (internalName.equals("Z")) { 33 return boolean.class; 34 } else if (internalName.equals("B")) { 35 return byte.class; 36 } else if (internalName.equals("S")) { 37 return short.class; 38 } else if (internalName.equals("I")) { 39 return int.class; 40 } else if (internalName.equals("J")) { 41 return long.class; 42 } else if (internalName.equals("F")) { 43 return float.class; 44 } else if (internalName.equals("D")) { 45 return double.class; 46 } else if (internalName.equals("C")) { 47 return char.class; 48 } else if (internalName.equals("V")) { 49 return void.class; 50 } else { 51 String name = internalName.substring(1, internalName.length() - 1).replace('/', '.'); 52 try { 53 return classLoader.loadClass(name); 54 } catch (ClassNotFoundException e) { 55 NoClassDefFoundError error = new NoClassDefFoundError(name); 56 error.initCause(e); 57 throw error; 58 } 59 } 60 } 61 getInternalName(Class<?> c)62 public static String getInternalName(Class<?> c) { 63 if (c.isArray()) { 64 return '[' + getInternalName(c.getComponentType()); 65 } else if (c == boolean.class) { 66 return "Z"; 67 } else if (c == byte.class) { 68 return "B"; 69 } else if (c == short.class) { 70 return "S"; 71 } else if (c == int.class) { 72 return "I"; 73 } else if (c == long.class) { 74 return "J"; 75 } else if (c == float.class) { 76 return "F"; 77 } else if (c == double.class) { 78 return "D"; 79 } else if (c == char.class) { 80 return "C"; 81 } else if (c == void.class) { 82 return "V"; 83 } else { 84 return 'L' + c.getName().replace('.', '/') + ';'; 85 } 86 } 87 } 88