📄 metaclasshelper.java
字号:
/* * Copyright 2005 John G. Wilson * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.codehaus.groovy.runtime;import groovy.lang.Closure;import groovy.lang.GString;import groovy.lang.GroovyRuntimeException;import groovy.lang.MetaMethod;import java.lang.reflect.Array;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationHandler;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import java.lang.reflect.Proxy;import java.math.BigDecimal;import java.math.BigInteger;import java.util.Iterator;import java.util.List;import java.util.logging.Level;import java.util.logging.Logger;import org.codehaus.groovy.GroovyBugError;/** * @author John Wilson * @author Jochen Theodorou */public class MetaClassHelper { public static final Object[] EMPTY_ARRAY = {}; public static Class[] EMPTY_TYPE_ARRAY = {}; protected static final Object[] ARRAY_WITH_NULL = { null }; protected static final Logger log = Logger.getLogger(MetaClassHelper.class.getName()); private static final int MAX_ARG_LEN = 12; public static boolean accessibleToConstructor(final Class at, final Constructor constructor) { boolean accessible = false; if (Modifier.isPublic(constructor.getModifiers())) { accessible = true; } else if (Modifier.isPrivate(constructor.getModifiers())) { accessible = at.getName().equals(constructor.getName()); } else if ( Modifier.isProtected(constructor.getModifiers()) ) { if ( at.getPackage() == null && constructor.getDeclaringClass().getPackage() == null ) { accessible = true; } else if ( at.getPackage() == null && constructor.getDeclaringClass().getPackage() != null ) { accessible = false; } else if ( at.getPackage() != null && constructor.getDeclaringClass().getPackage() == null ) { accessible = false; } else if ( at.getPackage().equals(constructor.getDeclaringClass().getPackage()) ) { accessible = true; } else { boolean flag = false; Class clazz = at; while ( !flag && clazz != null ) { if (clazz.equals(constructor.getDeclaringClass()) ) { flag = true; break; } if (clazz.equals(Object.class) ) { break; } clazz = clazz.getSuperclass(); } accessible = flag; } } else { if ( at.getPackage() == null && constructor.getDeclaringClass().getPackage() == null ) { accessible = true; } else if ( at.getPackage() == null && constructor.getDeclaringClass().getPackage() != null ) { accessible = false; } else if ( at.getPackage() != null && constructor.getDeclaringClass().getPackage() == null ) { accessible = false; } else if ( at.getPackage().equals(constructor.getDeclaringClass().getPackage()) ) { accessible = true; } } return accessible; } public static Object[] asWrapperArray(Object parameters, Class componentType) { Object[] ret=null; if (componentType == boolean.class) { boolean[] array = (boolean[]) parameters; ret = new Object[array.length]; for (int i=0; i<array.length; i++) { ret[i] = new Boolean(array[i]); } } else if (componentType == char.class) { char[] array = (char[]) parameters; ret = new Object[array.length]; for (int i=0; i<array.length; i++) { ret[i] = new Character(array[i]); } } else if (componentType == byte.class) { byte[] array = (byte[]) parameters; ret = new Object[array.length]; for (int i=0; i<array.length; i++) { ret[i] = new Byte(array[i]); } } else if (componentType == int.class) { int[] array = (int[]) parameters; ret = new Object[array.length]; for (int i=0; i<array.length; i++) { ret[i] = new Integer(array[i]); } } else if (componentType == short.class) { short[] array = (short[]) parameters; ret = new Object[array.length]; for (int i=0; i<array.length; i++) { ret[i] = new Short(array[i]); } } else if (componentType == long.class) { long[] array = (long[]) parameters; ret = new Object[array.length]; for (int i=0; i<array.length; i++) { ret[i] = new Long(array[i]); } } else if (componentType == double.class) { double[] array = (double[]) parameters; ret = new Object[array.length]; for (int i=0; i<array.length; i++) { ret[i] = new Double(array[i]); } } else if (componentType == float.class) { float[] array = (float[]) parameters; ret = new Object[array.length]; for (int i=0; i<array.length; i++) { ret[i] = new Float(array[i]); } } return ret; } /** * @param list * @param parameterType * @return */ public static Object asPrimitiveArray(List list, Class parameterType) { Class arrayType = parameterType.getComponentType(); Object objArray = Array.newInstance(arrayType, list.size()); for (int i = 0; i < list.size(); i++) { Object obj = list.get(i); if (arrayType.isPrimitive()) { if (obj instanceof Integer) { Array.setInt(objArray, i, ((Integer) obj).intValue()); } else if (obj instanceof Double) { Array.setDouble(objArray, i, ((Double) obj).doubleValue()); } else if (obj instanceof Boolean) { Array.setBoolean(objArray, i, ((Boolean) obj).booleanValue()); } else if (obj instanceof Long) { Array.setLong(objArray, i, ((Long) obj).longValue()); } else if (obj instanceof Float) { Array.setFloat(objArray, i, ((Float) obj).floatValue()); } else if (obj instanceof Character) { Array.setChar(objArray, i, ((Character) obj).charValue()); } else if (obj instanceof Byte) { Array.setByte(objArray, i, ((Byte) obj).byteValue()); } else if (obj instanceof Short) { Array.setShort(objArray, i, ((Short) obj).shortValue()); } } else { Array.set(objArray, i, obj); } } return objArray; } protected static Class autoboxType(Class type) { if (type.isPrimitive()) { if (type == int.class) { return Integer.class; } else if (type == double.class) { return Double.class; } else if (type == long.class) { return Long.class; } else if (type == boolean.class) { return Boolean.class; } else if (type == float.class) { return Float.class; } else if (type == char.class) { return Character.class; } else if (type == byte.class) { return Byte.class; } else if (type == short.class) { return Short.class; } } return type; } public static int calculateParameterDistance(Class[] arguments, Class[] parameters) { int dist=0; for (int i=0; i<arguments.length; i++) { if (parameters[i]==arguments[i]) continue; if (parameters[i].isInterface()) { dist+=3; continue; } if (arguments[i]!=null) { if (autoboxType(parameters[i]) == autoboxType(arguments[i])){ // type is not equal, but boxed types are. Increase distance // by 1 to reflect the change in type dist +=1; continue; } if (arguments[i].isPrimitive() || parameters[i].isPrimitive()) { // type is not equal, increase distance by 2 to reflect // the change in type dist+=2; continue; } // add one to dist to be sure interfaces are prefered dist++; Class clazz = arguments[i]; while (clazz!=null) { if (clazz==parameters[i]) break; if (clazz==GString.class && parameters[i]==String.class) { dist+=2; break; } clazz = clazz.getSuperclass(); dist+=3; } } else { // choose the distance to Object if a parameter is null // this will mean that Object is prefered over a more // specific type // remove one to dist to be sure Object is prefered dist--; Class clazz = parameters[i]; if (clazz.isPrimitive()) { dist+=2; } else { while (clazz!=Object.class) { clazz = clazz.getSuperclass(); dist+=2; } } } } return dist; } public static String capitalize(String property) { return property.substring(0, 1).toUpperCase() + property.substring(1, property.length()); } /** * @return the method with 1 parameter which takes the most general type of * object (e.g. Object) */ public static Object chooseEmptyMethodParams(List methods) { for (Iterator iter = methods.iterator(); iter.hasNext();) { Object method = iter.next(); Class[] paramTypes = getParameterTypes(method); int paramLength = paramTypes.length; if (paramLength == 0) { return method; } } return null; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -