@Override public Object coerceArgument(Object argument) { if (argument instanceof Closure) { Class clazz = getTheClass(); return coerceToSAM((Closure) argument, method, clazz); } else { return argument; } }
public Object coerceArgument(Object argument) { if (argument instanceof Number) { return coerceNumber(argument); } return argument; }
/** * returns the abstract method from a SAM type, if it is a SAM type. * @param c the SAM class * @return null if nothing was found, the method otherwise */ public static Method getSAMMethod(Class<?> c) { try { return getSAMMethodImpl(c); } catch (NoClassDefFoundError ignore) { return null; } }
private static CachedClass createCachedClass(Class klazz, ClassInfo classInfo) { if (klazz == Object.class) return new ObjectCachedClass(classInfo); return new StringCachedClass(classInfo); cachedClass = new NumberCachedClass(klazz, classInfo); } else if (klazz == Integer.class || klazz == Integer.TYPE) { cachedClass = new IntegerCachedClass(klazz, classInfo, klazz==Integer.class); } else if (klazz == Double.class || klazz == Double.TYPE) { cachedClass = new DoubleCachedClass(klazz, classInfo, klazz==Double.class); } else if (klazz == BigDecimal.class) { cachedClass = new BigDecimalCachedClass(klazz, classInfo); } else if (klazz == Long.class || klazz == Long.TYPE) { cachedClass = new LongCachedClass(klazz, classInfo, klazz==Long.class); } else if (klazz == Float.class || klazz == Float.TYPE) { cachedClass = new FloatCachedClass(klazz, classInfo, klazz==Float.class); } else if (klazz == Short.class || klazz == Short.TYPE) { cachedClass = new ShortCachedClass(klazz, classInfo, klazz==Short.class); } else if (klazz == Boolean.TYPE) { cachedClass = new BooleanCachedClass(klazz, classInfo, false); } else if (klazz == Character.TYPE) { cachedClass = new CharacterCachedClass(klazz, classInfo, false); } else if (klazz == BigInteger.class) { cachedClass = new BigIntegerCachedClass(klazz, classInfo); } else if (klazz == Byte.class || klazz == Byte.TYPE) { cachedClass = new ByteCachedClass(klazz, classInfo, klazz==Byte.class); } else { cachedClass = new CachedClass(klazz, classInfo);
public Closure(Object owner, Object thisObject) { this.owner = owner; this.delegate = owner; this.thisObject = thisObject; final CachedClosureClass cachedClass = (CachedClosureClass) ReflectionCache.getCachedClass(getClass()); parameterTypes = cachedClass.getParameterTypes(); maximumNumberOfParameters = cachedClass.getMaximumNumberOfParameters(); }
private Object coerceNumber(Object argument) { Class param = getTheClass(); if (param == Byte.class /*|| param == Byte.TYPE*/) { argument = Byte.valueOf(((Number) argument).byteValue()); } else if (param == BigInteger.class) { argument = new BigInteger(String.valueOf((Number) argument)); } return argument; } }
public CachedClosureClass(Class klazz, ClassInfo classInfo) { super(klazz, classInfo); CachedMethod methods [] = getMethods(); // set it to -1 for starters so parameterTypes will always get a type int maximumNumberOfParameters = -1; Class[] parameterTypes = null; for (CachedMethod method : methods) { if ("doCall".equals(method.getName())) { final Class[] pt = method.getNativeParameterTypes(); if (pt.length > maximumNumberOfParameters) { parameterTypes = pt; maximumNumberOfParameters = parameterTypes.length; } } } // this line should be useless, but well, just in case maximumNumberOfParameters = Math.max(maximumNumberOfParameters,0); this.maximumNumberOfParameters = maximumNumberOfParameters; this.parameterTypes = parameterTypes; }
public Object coerceArgument(Object argument) { Class argumentClass = argument.getClass(); if (argumentClass.getName().charAt(0) != '[') return argument; Class argumentComponent = argumentClass.getComponentType(); Class paramComponent = getTheClass().getComponentType(); if (paramComponent.isPrimitive()) { argument = DefaultTypeTransformation.convertToPrimitiveArray(argument, paramComponent); } else if (paramComponent == String.class && argument instanceof GString[]) { GString[] strings = (GString[]) argument; String[] ret = new String[strings.length]; for (int i = 0; i < strings.length; i++) { ret[i] = strings[i].toString(); } argument = ret; } else if (paramComponent==Object.class && argumentComponent.isPrimitive()){ argument = DefaultTypeTransformation.primitiveArrayBox(argument); } return argument; }
getAbstractMethods(c, methods); if (methods.isEmpty()) return null; ListIterator<Method> it = methods.listIterator(); while (it.hasNext()) { Method m = it.next(); if (hasUsableImplementation(c, m)) it.remove(); return getSingleNonDuplicateMethod(methods);
private static void getAbstractMethods(Class c, List<Method> current) { if (c==null || !Modifier.isAbstract(c.getModifiers())) return; getAbstractMethods(c.getSuperclass(), current); for (Class ci : c.getInterfaces()) { getAbstractMethods(ci, current); } for (Method m : getDeclaredMethods(c)) { if (Modifier.isPrivate(m.getModifiers())) continue; if (Modifier.isAbstract(m.getModifiers())) current.add(m); } }
private static boolean isSAM(Class<?> c) { return CachedSAMClass.getSAMMethod(c) !=null; }
private static Object continueCastOnSAM(Object object, Class type) { if (object instanceof Closure) { Method m = CachedSAMClass.getSAMMethod(type); if (m != null) { return CachedSAMClass.coerceToSAM((Closure) object, m, type);
public static Object coerceToSAM(Closure argument, Method method, Class clazz) { return coerceToSAM(argument, method, clazz, clazz.isInterface()); }
private static boolean hasUsableImplementation(Class c, Method m) { if (c==m.getDeclaringClass()) return false; Method found; try { found = c.getMethod(m.getName(), m.getParameterTypes()); int asp = found.getModifiers() & ABSTRACT_STATIC_PRIVATE; int visible = found.getModifiers() & VISIBILITY; if (visible !=0 && asp == 0) return true; } catch (NoSuchMethodException e) {/*ignore*/} if (c==Object.class) return false; return hasUsableImplementation(c.getSuperclass(), m); }
@Override public boolean isAssignableFrom(Class argument) { return argument == null || Closure.class.isAssignableFrom(argument) || ReflectionCache.isAssignableFrom(getTheClass(), argument); }
public Object coerceArgument(Object argument) { if (argument instanceof Number) { return coerceNumber(argument); } return argument; }
private void handleSAM() { if (handle!=null) return; if (!(args[0] instanceof Closure)) return; Method m = CachedSAMClass.getSAMMethod(staticTargetType); if (m==null) return; //TODO: optimize: add guard based on type Closure handle = MethodHandles.insertArguments(SAM_CONVERSION, 1, m, staticTargetType); }
public CachedSAMClass(Class klazz, ClassInfo classInfo) { super(klazz, classInfo); method = getSAMMethod(klazz); if (method==null) throw new GroovyBugError("assigned method should not have been null!"); }
Method method = CachedSAMClass.getSAMMethod(parameter); if (method == null) return null;
if (clazz.isInterface() && !(clazz.isInstance(cl))) { if (Traits.isTrait(clazz)) { Method samMethod = CachedSAMClass.getSAMMethod(clazz); if (samMethod!=null) { Map impl = Collections.singletonMap(samMethod.getName(),cl);