Home | History | Annotate | Download | only in client
      1 /*******************************************************************************
      2  * Copyright 2011 See AUTHORS file.
      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 com.badlogic.gwtref.client;
     18 
     19 import java.util.Collection;
     20 
     21 import com.google.gwt.core.client.GWT;
     22 
     23 public class ReflectionCache {
     24 	private static IReflectionCache instance = GWT.create(IReflectionCache.class);
     25 
     26 	public static Type forName (String name) throws ClassNotFoundException {
     27 		Type type = instance.forName(convert(name));
     28 		if (type == null) {
     29 			throw new RuntimeException("Couldn't find Type for class '" + name + "'");
     30 		}
     31 		return type;
     32 	}
     33 
     34 	public static Type getType (Class clazz) {
     35 		if (clazz == null) return null;
     36 		Type type = instance.forName(convert(clazz.getName()));
     37 		if (type == null) {
     38 			throw new RuntimeException("Couldn't find Type for class '" + clazz.getName() + "'");
     39 		}
     40 		return type;
     41 	}
     42 
     43 	private static String convert (String className) {
     44 		if (className.startsWith("[")) {
     45 			int dimensions = 0;
     46 			char c = className.charAt(0);
     47 			String suffix = "";
     48 			while (c == '[') {
     49 				dimensions++;
     50 				suffix += "[]";
     51 				c = className.charAt(dimensions);
     52 			}
     53 			char t = className.charAt(dimensions);
     54 			switch (t) {
     55 			case 'Z':
     56 				return "boolean" + suffix;
     57 			case 'B':
     58 				return "byte" + suffix;
     59 			case 'C':
     60 				return "char" + suffix;
     61 			case 'L':
     62 				return className.substring(dimensions + 1, className.length() - 1).replace('$', '.') + suffix;
     63 			case 'D':
     64 				return "double" + suffix;
     65 			case 'F':
     66 				return "float" + suffix;
     67 			case 'I':
     68 				return "int" + suffix;
     69 			case 'J':
     70 				return "long" + suffix;
     71 			case 'S':
     72 				return "short" + suffix;
     73 			default:
     74 				throw new IllegalArgumentException("Couldn't transform '" + className + "' to qualified source name");
     75 			}
     76 		} else {
     77 			return className.replace('$', '.');
     78 		}
     79 	}
     80 
     81 	public static Object newArray (Class componentType, int size) {
     82 		return instance.newArray(getType(componentType), size);
     83 	}
     84 
     85 	public static Object getFieldValue (Field field, Object obj) throws IllegalAccessException {
     86 		return instance.get(field, obj);
     87 	}
     88 
     89 	public static void setFieldValue (Field field, Object obj, Object value) throws IllegalAccessException {
     90 		instance.set(field, obj, value);
     91 	}
     92 
     93 	public static Object invoke (Method method, Object obj, Object[] params) {
     94 		return instance.invoke(method, obj, params);
     95 	}
     96 
     97 	public static int getArrayLength (Type type, Object obj) {
     98 		return instance.getArrayLength(type, obj);
     99 	}
    100 
    101 	public static Object getArrayElement (Type type, Object obj, int i) {
    102 		return instance.getArrayElement(type, obj, i);
    103 	}
    104 
    105 	public static void setArrayElement (Type type, Object obj, int i, Object value) {
    106 		instance.setArrayElement(type, obj, i, value);
    107 	}
    108 }
    109