/** * Creates an entirely new class. The class extends from Object and implements the provided interface. * * @param interfaceType * class to extend from, which must be a class, not an interface * @param implementationType * class that implements interfaceType. It can be null. * @param callback * used to configure the new class * @return the instantiator, which allows instances of the new class to be created * @see #createProxyTransformation(Class, Class) * @since 5.4 */ public <T> ClassInstantiator<T> createProxy(Class<T> interfaceType, Class<? extends T> implementationType, PlasticClassTransformer callback) { assert callback != null; PlasticClassTransformation<T> transformation = createProxyTransformation(interfaceType, implementationType); callback.transform(transformation.getPlasticClass()); return transformation.createInstantiator(); }
/** * 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 public T build() { return (T) transformation.createInstantiator().newInstance(); } }
/** * 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 public T build() { return (T) transformation.createInstantiator().newInstance(); } }
/** * Creates an entirely new class. The class extends from Object and implements the provided interface. * * @param interfaceType * class to extend from, which must be a class, not an interface * @param implementationType * class that implements interfaceType. It can be null. * @param callback * used to configure the new class * @return the instantiator, which allows instances of the new class to be created * @see #createProxyTransformation(Class, Class) * @since 5.4 */ public <T> ClassInstantiator<T> createProxy(Class<T> interfaceType, Class<? extends T> implementationType, PlasticClassTransformer callback) { assert callback != null; PlasticClassTransformation<T> transformation = createProxyTransformation(interfaceType, implementationType); callback.transform(transformation.getPlasticClass()); return transformation.createInstantiator(); }
public AspectInterceptorBuilderImpl(AnnotationAccess annotationAccess, PlasticProxyFactory plasticProxyFactory, Class<T> serviceInterface, T delegate, String description) { super(annotationAccess); this.serviceInterface = serviceInterface; final Class<? extends Object> delegateType = delegate.getClass(); transformation = plasticProxyFactory.createProxyTransformation(serviceInterface, (Class<? extends T>) delegateType); plasticClass = transformation.getPlasticClass(); plasticClass.addToString(description); allMethods.addAll(Arrays.asList(serviceInterface.getMethods())); final PlasticField delegateField = plasticClass.introduceField(serviceInterface, "delegate").inject(delegate); for (Method method : allMethods) { plasticClass.introduceMethod(method).delegateTo(delegateField); } // TAP5-2235 final String delegateTypeName = delegateType.getName(); MethodDescription getDelegateMethodDescription = new MethodDescription(delegateTypeName, PlasticProxyFactoryImpl.INTERNAL_GET_DELEGATE); plasticClass.introduceMethod(getDelegateMethodDescription, new InstructionBuilderCallback() { @Override public void doBuild(InstructionBuilder builder) { builder.loadThis().getField(delegateField); builder.checkcast(delegateType).returnResult(); } }); }
/** * Creates an entirely new class, extending from the provided base class. * * @param baseClass * class to extend from, which must be a class, not an interface * @param callback * used to configure the new class * @return the instantiator, which allows instances of the new class to be created */ public <T> ClassInstantiator<T> createClass(Class<T> baseClass, PlasticClassTransformer callback) { assert baseClass != null; assert callback != null; if (baseClass.isInterface()) throw new IllegalArgumentException(String.format("Class %s defines an interface, not a base class.", baseClass.getName())); String name = String.format("$%s_%s", baseClass.getSimpleName(), PlasticUtils.nextUID()); PlasticClassTransformation<T> transformation = pool.createTransformation(baseClass.getName(), name); callback.transform(transformation.getPlasticClass()); return transformation.createInstantiator(); }
public AspectInterceptorBuilderImpl(AnnotationAccess annotationAccess, PlasticProxyFactory plasticProxyFactory, Class<T> serviceInterface, T delegate, String description) { super(annotationAccess); this.serviceInterface = serviceInterface; final Class<? extends Object> delegateType = delegate.getClass(); transformation = plasticProxyFactory.createProxyTransformation(serviceInterface, (Class<? extends T>) delegateType); plasticClass = transformation.getPlasticClass(); plasticClass.addToString(description); allMethods.addAll(Arrays.asList(serviceInterface.getMethods())); final PlasticField delegateField = plasticClass.introduceField(serviceInterface, "delegate").inject(delegate); for (Method method : allMethods) { plasticClass.introduceMethod(method).delegateTo(delegateField); } // TAP5-2235 final String delegateTypeName = delegateType.getName(); MethodDescription getDelegateMethodDescription = new MethodDescription(delegateTypeName, PlasticProxyFactoryImpl.INTERNAL_GET_DELEGATE); plasticClass.introduceMethod(getDelegateMethodDescription, new InstructionBuilderCallback() { @Override public void doBuild(InstructionBuilder builder) { builder.loadThis().getField(delegateField); builder.checkcast(delegateType).returnResult(); } }); }
/** * Creates an entirely new class, extending from the provided base class. * * @param baseClass * class to extend from, which must be a class, not an interface * @param callback * used to configure the new class * @return the instantiator, which allows instances of the new class to be created */ public <T> ClassInstantiator<T> createClass(Class<T> baseClass, PlasticClassTransformer callback) { assert baseClass != null; assert callback != null; if (baseClass.isInterface()) throw new IllegalArgumentException(String.format("Class %s defines an interface, not a base class.", baseClass.getName())); String name = String.format("$%s_%s", baseClass.getSimpleName(), PlasticUtils.nextUID()); PlasticClassTransformation<T> transformation = pool.createTransformation(baseClass.getName(), name); callback.transform(transformation.getPlasticClass()); return transformation.createInstantiator(); }