static String noConverter(Class fromType, Class desiredType) { return _formatter.format("no-converter-from", ClassFabUtils.getJavaClassName(desiredType), ClassFabUtils.getJavaClassName(fromType)); }
/** * Returns a class name derived from the provided interfaceClass. The package part of the * interface name is stripped out, and the result passed to {@link #generateClassName(String)}. * * @since 1.1 */ public static synchronized String generateClassName(Class interfaceClass) { String name = interfaceClass.getName(); int dotx = name.lastIndexOf('.'); return generateClassName(name.substring(dotx + 1)); }
/** * Creates a toString() method that identify the interceptor service id, * the intercepted service id, and the service interface class name). */ protected void addToStringMethod(InterceptorStack stack, ClassFab fab) { ClassFabUtils.addToStringMethod( fab, "<LoggingInterceptor for " + stack.getServiceExtensionPointId() + "(" + stack.getServiceInterface().getName() + ")>"); }
private void createInfrastructure(InterceptorStack stack, ClassFab classFab) { Class topClass = ClassFabUtils.getInstanceClass(stack.peek(), stack.getServiceInterface()); classFab.addField("_log", Log.class); // This is very important: since we know the instance of the top object (the next // object in the pipeline for this service), we can build the instance variable // and constructor to use the exact class rather than the service interface. // That's more efficient at runtime, lowering the cost of using interceptors. // One of the reasons I prefer Javassist over JDK Proxies. classFab.addField("_delegate", topClass); classFab.addConstructor( new Class[] { Log.class, topClass }, null, "{ _log = $1; _delegate = $2; }"); }
/** @since 1.1 */ private void processMethod(Method m, Map map) { _toString |= ClassFabUtils.isToString(m); MethodSignature sig = new MethodSignature(m); String uid = sig.getUniqueId(); MethodSignature existing = (MethodSignature) map.get(uid); if (existing == null || sig.isOverridingSignatureOf(existing)) map.put(uid, sig); }
/** @since 1.1 */ private static void addDelegateAccessor(ClassFab classFab, ServicePoint servicePoint, Object delegate) { classFab.addField("_shutdown", boolean.class); Class delegateClass = ClassFabUtils.getInstanceClass(delegate, servicePoint .getServiceInterface()); classFab.addField("_delegate", delegateClass); classFab.addConstructor(new Class[] { delegateClass }, null, "{ super(); _delegate = $1; }"); classFab.addInterface(RegistryShutdownListener.class); if( RegistryShutdownListener.class.isAssignableFrom( delegateClass ) ) { classFab.addMethod(Modifier.PUBLIC | Modifier.FINAL, new MethodSignature(void.class, "registryDidShutdown", null, null), "{ _delegate.registryDidShutdown(); _delegate = null; _shutdown = true; }"); } else { classFab.addMethod(Modifier.PUBLIC | Modifier.FINAL, new MethodSignature(void.class, "registryDidShutdown", null, null), "{ _delegate = null; _shutdown = true; }"); } BodyBuilder builder = new BodyBuilder(); builder.begin(); builder.addln("if (_shutdown)"); builder.addln(" throw org.apache.hivemind.HiveMind#createRegistryShutdownException();"); builder.add("return _delegate;"); builder.end(); classFab.addMethod(Modifier.FINAL | Modifier.PRIVATE, new MethodSignature(delegateClass, DELEGATE_ACCESSOR_METHOD_NAME, null, null), builder.toString()); }
static String wrongTypeForProperty(String propertyName, Class propertyType, Class requiredType) { return _formatter.format("wrong-type-for-property", propertyName, ClassFabUtils .getJavaClassName(propertyType), ClassFabUtils.getJavaClassName(requiredType)); }
Class createInterface(Class beanClass, Operation op) { String name = ClassFabUtils.generateClassName(beanClass); return createInterface(name, op); }
ClassFabUtils.addToStringMethod(_classFab, "<" + _type + " for " + _point.getExtensionPointId() + "(" + _serviceInterface.getName() + ")>");
static String noConverter(Class fromType, Class desiredType) { return _formatter.format("no-converter-from", ClassFabUtils.getJavaClassName(desiredType), ClassFabUtils.getJavaClassName(fromType)); }
ClassFab generateClassFab(CompiledExpression compiled) throws Exception { if (compiled.getGeneratedClass() != null) return compiled.getGeneratedClass(); ClassFab classFab = _classFactory.newClass(ClassFabUtils.generateClassName(compiled.getExpression().getClass()), Object.class); classFab.addInterface(ExpressionAccessor.class); compiled.setGeneratedClass(classFab); return classFab; }
static String incompatiblePropertyType(String propertyName, Class propertyType, Class expectedType) { return _formatter.format("incompatible-property-type", propertyName, ClassFabUtils .getJavaClassName(propertyType), ClassFabUtils.getJavaClassName(expectedType)); }
ClassFab generateClassFab(CompiledExpression compiled) throws Exception { if (compiled.getGeneratedClass() != null) return compiled.getGeneratedClass(); ClassFab classFab = _classFactory.newClass(ClassFabUtils.generateClassName(compiled.getExpression().getClass()), Object.class); classFab.addInterface(ExpressionAccessor.class); compiled.setGeneratedClass(classFab); return classFab; }
static String wrongTypeForProperty(String propertyName, Class propertyType, Class requiredType) { return _formatter.format("wrong-type-for-property", propertyName, ClassFabUtils .getJavaClassName(propertyType), ClassFabUtils.getJavaClassName(requiredType)); }
private Class constructInterceptorClass(InterceptorStack stack, List parameters) { Class serviceInterfaceClass = stack.getServiceInterface(); String name = ClassFabUtils.generateClassName(serviceInterfaceClass); ClassFab classFab = _factory.newClass(name, Object.class); classFab.addInterface(serviceInterfaceClass); createInfrastructure(stack, classFab); addServiceMethods(stack, classFab, parameters); return classFab.createClass(); }
/** * Javassist needs the class name to be as it appears in source code, even for arrays. Invoking * getName() on a Class instance representing an array returns the internal format (i.e, "[...;" * or something). This returns it as it would appear in Java code. */ public static String getJavaClassName(Class inputClass) { if (inputClass.isArray()) return getJavaClassName(inputClass.getComponentType()) + "[]"; return inputClass.getName(); }
Class baseClass = extendBeanClass ? declaredInterface : Object.class; _classFab = factory.newClass(ClassFabUtils.generateClassName(_serviceInterface), baseClass);
static String incompatiblePropertyType(String propertyName, Class propertyType, Class expectedType) { return _formatter.format("incompatible-property-type", propertyName, ClassFabUtils .getJavaClassName(propertyType), ClassFabUtils.getJavaClassName(expectedType)); }
static String propertyTypeMismatch(Class componentClass, String propertyName, Class actualPropertyType, Class expectedPropertyType) { return _formatter.format("property-type-mismatch", new Object[] { componentClass.getName(), propertyName, ClassFabUtils.getJavaClassName(actualPropertyType), ClassFabUtils.getJavaClassName(expectedPropertyType) }); }
/** @since 1.1 */ public static String paramWrongType(String parameterName, Object parameter, Class expectedType) { return _formatter.format("param-wrong-type", parameterName, ClassFabUtils .getJavaClassName(parameter.getClass()), ClassFabUtils .getJavaClassName(expectedType)); } }