@Override protected String convert(Class type) { return PlasticUtils.toTypeName(type); } };
/** Creates a MethodDescription from a Java Method. The generic signature will be null. */ public MethodDescription(Method method) { this(method.getModifiers(), PlasticUtils.toTypeName(method.getReturnType()), method.getName(), PlasticUtils .toTypeNames(method.getParameterTypes()), null, PlasticUtils.toTypeNames(method.getExceptionTypes())); }
private static Method toMethod(Class declaringClass, String methodName, Class... parameterTypes) { return PlasticUtils.getMethod(declaringClass, methodName, parameterTypes); }
private static String toJavaClassNames(List<Class> classes) { Class[] asArray = classes.toArray(new Class[classes.size()]); String[] namesArray = PlasticUtils.toTypeNames(asArray); List<String> names = CollectionFactory.newList(namesArray); return InternalUtils.joinSorted(names); }
String makeUnique(Set<String> values, String input) { return values.contains(input) ? input + "$" + PlasticUtils.nextUID() : input; }
@Override @SuppressWarnings("unchecked") public <S, T> Coercion<S, T> getCoercion(Class<S> sourceType, Class<T> targetType) { assert sourceType != null; assert targetType != null; Class effectiveSourceType = PlasticUtils.toWrapperType(sourceType); Class effectiveTargetType = PlasticUtils.toWrapperType(targetType); if (effectiveTargetType.isAssignableFrom(effectiveSourceType)) { return NO_COERCION; } return getTargetCoercion(effectiveTargetType).getCoercion(effectiveSourceType); }
private static String toJavaClassNames(List<Class> classes) { Class[] asArray = classes.toArray(new Class[classes.size()]); String[] namesArray = PlasticUtils.toTypeNames(asArray); List<String> names = CollectionFactory.newList(namesArray); return InternalUtils.joinSorted(names); }
String makeUnique(Set<String> values, String input) { return values.contains(input) ? input + "$" + PlasticUtils.nextUID() : input; }
/** * Internal-use constructor. * * @param sourceType * the source (or input) type of the coercion, may be Void.class to indicate a coercion from null * @param targetType * the target (or output) type of the coercion * @param coercion * the object that performs the coercion * @param wrap * if true, the coercion is wrapped to provide a useful toString() */ @SuppressWarnings("unchecked") public CoercionTuple(Class<S> sourceType, Class<T> targetType, Coercion<S, T> coercion, boolean wrap) { assert sourceType != null; assert targetType != null; assert coercion != null; this.sourceType = PlasticUtils.toWrapperType(sourceType); this.targetType = PlasticUtils.toWrapperType(targetType); this.coercion = wrap ? new CoercionWrapper<S, T>(coercion) : coercion; }
/** Converts a number of types (usually, arguments to a method or constructor) into their type names. */ public static String[] toTypeNames(Class[] types) { String[] result = new String[types.length]; for (int i = 0; i < result.length; i++) result[i] = toTypeName(types[i]); return result; }
/** Creates a MethodDescription from a Java Method. The generic signature will be null. */ public MethodDescription(Method method) { this(method.getModifiers(), PlasticUtils.toTypeName(method.getReturnType()), method.getName(), PlasticUtils .toTypeNames(method.getParameterTypes()), null, PlasticUtils.toTypeNames(method.getExceptionTypes())); }
@Override public InstructionBuilder invokeConstructor(Class clazz, Class... argumentTypes) { check(); return invokeConstructor(clazz.getName(), PlasticUtils.toTypeNames(argumentTypes)); }
/** * Creates the underlying {@link PlasticClassTransformation} for an interface proxy. This should only be * used in the cases where encapsulating the PlasticClass construction into a {@linkplain PlasticClassTransformer * callback} is not feasible (which is the case for some of the older APIs inside Tapestry IoC). * * @param interfaceType * class proxy will extend from * @param implementationType * class that implements interfaceType. It can be null. * @return transformation from which an instantiator may be created */ public <T> PlasticClassTransformation<T> createProxyTransformation(Class interfaceType, Class implementationType) { assert interfaceType != null; if (!interfaceType.isInterface()) throw new IllegalArgumentException(String.format( "Class %s is not an interface; proxies may only be created for interfaces.", interfaceType.getName())); String name = String.format("$%s_%s", interfaceType.getSimpleName(), PlasticUtils.nextUID()); final String implementationClassName = implementationType != null ? implementationType.getName() : null; PlasticClassTransformation<T> result = pool.createTransformation("java.lang.Object", name, implementationClassName); result.getPlasticClass().introduceInterface(interfaceType); return result; }
/** * Returns the parent of the given class. Tweaks inheritance for object arrays. Returns null instead of * Object.class. */ private Class parentOf(Class clazz) { if (clazz != void.class && clazz.isPrimitive()) return PlasticUtils.toWrapperType(clazz); if (clazz.isArray() && clazz != Object[].class) { Class componentType = clazz.getComponentType(); while (componentType.isArray()) componentType = componentType.getComponentType(); if (!componentType.isPrimitive()) return Object[].class; } Class parent = clazz.getSuperclass(); return parent != Object.class ? parent : null; }
private static Method toMethod(Class declaringClass, String methodName, Class... parameterTypes) { return PlasticUtils.getMethod(declaringClass, methodName, parameterTypes); }
@Override protected String convert(Class type) { return PlasticUtils.toTypeName(type); } };
private void doInvoke(int opcode, Class clazz, Class returnType, String methodName, Class... argumentTypes) { doInvoke(opcode, clazz.getName(), cache.toTypeName(returnType), methodName, PlasticUtils.toTypeNames(argumentTypes)); }
/** * Creates the underlying {@link PlasticClassTransformation} for an interface proxy. This should only be * used in the cases where encapsulating the PlasticClass construction into a {@linkplain PlasticClassTransformer * callback} is not feasible (which is the case for some of the older APIs inside Tapestry IoC). * * @param interfaceType * class proxy will extend from * @param implementationType * class that implements interfaceType. It can be null. * @return transformation from which an instantiator may be created */ public <T> PlasticClassTransformation<T> createProxyTransformation(Class interfaceType, Class implementationType) { assert interfaceType != null; if (!interfaceType.isInterface()) throw new IllegalArgumentException(String.format( "Class %s is not an interface; proxies may only be created for interfaces.", interfaceType.getName())); String name = String.format("$%s_%s", interfaceType.getSimpleName(), PlasticUtils.nextUID()); final String implementationClassName = implementationType != null ? implementationType.getName() : null; PlasticClassTransformation<T> result = pool.createTransformation("java.lang.Object", name, implementationClassName); result.getPlasticClass().introduceInterface(interfaceType); return result; }
@Override @SuppressWarnings("unchecked") public <S, T> String explain(Class<S> sourceType, Class<T> targetType) { assert sourceType != null; assert targetType != null; Class effectiveTargetType = PlasticUtils.toWrapperType(targetType); Class effectiveSourceType = PlasticUtils.toWrapperType(sourceType); // Is a coercion even necessary? Not if the target type is assignable from the // input value. if (effectiveTargetType.isAssignableFrom(effectiveSourceType)) { return ""; } return getTargetCoercion(effectiveTargetType).explain(effectiveSourceType); }
/** * Uses {@link #getMethod(Class, String, Class...)} and wraps the result as a {@link MethodDescription}. * * @param declaringClass * containing class * @param name * name of method * @param parameterTypes * types of parameters * @return description for method * @throws RuntimeException * if any error (such as method not found) */ public static MethodDescription getMethodDescription(Class declaringClass, String name, Class... parameterTypes) { return new MethodDescription(getMethod(declaringClass, name, parameterTypes)); }