public Class<?> generateInterface(String className, IOpenClass openClass, ClassLoader classLoader) throws Exception { boolean f1 = (includes == null) || (includes.length == 0); boolean f2 = (excludes == null) || (excludes.length == 0); if (f1 && f2) { return InterfaceGenerator.generateInterface(className, openClass, classLoader); } else { return InterfaceGenerator.generateInterface(className, openClass, classLoader, includes, excludes); } } }
private static boolean isMember(RuleInfo ruleInfo, String[] includes, String[] excludes) { boolean isMember = true; String methodSignature = getRuleInfoSignature(ruleInfo); if (includes != null && includes.length > 0) { isMember = false; for (String pattern : includes) { if (Pattern.matches(pattern, methodSignature)) { isMember = true; } } } if (excludes != null && excludes.length > 0 && isMember) { for (String pattern : excludes) { if (Pattern.matches(pattern, methodSignature)) { isMember = false; } } } return isMember; }
return generateInterface(className, EMPTY_RULES, classLoader); if (!isIgnoredMember(method)) { RuleInfo ruleInfo = getRuleInfoForMethod(method); boolean isMember = isMember(ruleInfo, includes, excludes); if (isMember) { rules.add(ruleInfo); if (!isIgnoredMember(field)) { if (field.isReadable()) { RuleInfo ruleInfo = getRuleInfoForField(field); boolean isMember = isMember(ruleInfo, includes, excludes); if (isMember) { MethodKey key = new MethodKey(ruleInfo.getName(), IOpenClass.EMPTY); return generateInterface(className, rules.toArray(new RuleInfo[0]), classLoader);
/** * Gets list of rules. * * @param methods array of methods what represents rule methods * @return list of rules meta-info */ private static List<RuleInfo> getRulesDecorated(Method[] methods) { List<RuleInfo> rules = new ArrayList<RuleInfo>(methods.length); for (Method method : methods) { // Check that method should be ignored or not. if (isIgnored(method)) { continue; } String methodName = method.getName(); Class<?>[] paramTypes = method.getParameterTypes(); Class<?> returnType = VariationsResult.class; Class<?>[] newParams = new Class<?>[]{VariationsPack.class}; Class<?>[] extendedParamTypes = ArrayUtils.addAll(paramTypes, newParams); RuleInfo ruleInfoEnhanced = InterfaceGenerator.createRuleInfo(methodName, extendedParamTypes, returnType); RuleInfo ruleInfoOriginal = InterfaceGenerator.createRuleInfo(methodName, paramTypes, method.getReturnType()); rules.add(ruleInfoEnhanced); rules.add(ruleInfoOriginal); } return rules; }
/** * Gets rule information of IOpenMethod instance. * * @param method IOpenMethod instance * @return rule info */ private static RuleInfo getRuleInfoForMethod(IOpenMethod method) { String methodName = method.getName(); IOpenClass[] paramClasses = method.getSignature().getParameterTypes(); Class<?> returnType = method.getType().getInstanceClass(); Class<?>[] paramTypes = getInstanceClasses(paramClasses); RuleInfo ruleInfo = createRuleInfo(methodName, paramTypes, returnType); return ruleInfo; }
/** * Generates interface class using collection of rules. * * @param className name of result class * @param rules collection of rules what will be used as interface methods * @param classLoader class loader what will be used to load generated * interface * @return generated interface * @throws Exception if an error has occurred */ public static Class<?> generateInterface(String className, RuleInfo[] rules, ClassLoader classLoader) throws Exception { ClassWriter classWriter = new ClassWriter(0); String name = className.replace('.', '/'); classWriter.visit(Opcodes.V1_7, PUBLIC_ABSTRACT_INTERFACE, name, null, JAVA_LANG_OBJECT, null); for (RuleInfo ruleInfo : rules) { String ruleName = ruleInfo.getName(); classWriter.visitMethod(PUBLIC_ABSTRACT, ruleName, getMethodTypes(ruleInfo), null, null); } classWriter.visitEnd(); // Create class object. // ClassUtils.defineClass(className, classWriter.toByteArray(), classLoader); // Return loaded to classpath class object. // return Class.forName(className, true, classLoader); }
/** * Gets list of rules. * * @param methods array of methods what represents rule methods * @return list of rules meta-info */ private static List<RuleInfo> getRulesDecorated(Method[] methods) { List<RuleInfo> rules = new ArrayList<RuleInfo>(methods.length); for (Method method : methods) { // Check that method should be ignored or not. if (isIgnored(method)) { continue; } String methodName = method.getName(); Class<?>[] paramTypes = method.getParameterTypes(); Class<?> returnType = VariationsResult.class; Class<?>[] newParams = new Class<?>[]{VariationsPack.class}; Class<?>[] extendedParamTypes = ArrayUtils.addAll(paramTypes, newParams); RuleInfo ruleInfoEnhanced = InterfaceGenerator.createRuleInfo(methodName, extendedParamTypes, returnType); RuleInfo ruleInfoOriginal = InterfaceGenerator.createRuleInfo(methodName, paramTypes, method.getReturnType()); rules.add(ruleInfoEnhanced); rules.add(ruleInfoOriginal); } return rules; }
/** * Generates interface class using methods and fields of given IOpenClass * instance. * * @param className name of result class * @param openClass IOpenClass instance * @param classLoader class loader what will be used to load generated * interface * @return generated interface * @throws Exception if an error has occurred */ public static Class<?> generateInterface(String className, IOpenClass openClass, ClassLoader classLoader) throws Exception { return generateInterface(className, openClass, classLoader, null, null); }
/** * Gets list of rules. * * @param methods array of methods what represents rule methods * @return list of rules meta-info */ private static List<RuleInfo> getRulesDecorated(Method[] methods) { List<RuleInfo> rules = new ArrayList<RuleInfo>(methods.length); for (Method method : methods) { // Check that method should be ignored or not. if (isIgnored(method)) { continue; } String methodName = method.getName(); Class<?>[] paramTypes = method.getParameterTypes(); Class<?> returnType = method.getReturnType(); Class<?>[] newParams = new Class<?>[]{IRulesRuntimeContext.class}; Class<?>[] extendedParamTypes = ArrayUtils.addAll(newParams, paramTypes); RuleInfo ruleInfo = InterfaceGenerator.createRuleInfo(methodName, extendedParamTypes, returnType); rules.add(ruleInfo); } return rules; }
/** * Decorates methods signatures of given clazz. * * @param clazz class to decorate * @param classLoader The classloader where generated class should be * placed. * @return new class with decorated methods signatures: added * {@link IRulesRuntimeContext} as the first parameter for each * method. * @throws Exception */ public static Class<?> decorateClass(Class<?> clazz, ClassLoader classLoader) throws Exception { final Logger log = LoggerFactory.getLogger(RuntimeContextInstantiationStrategyEnhancerHelper.class); Method[] methods = clazz.getMethods(); List<RuleInfo> rules = getRulesDecorated(methods); String className = clazz.getName() + DECORATED_CLASS_NAME_SUFFIX; RuleInfo[] rulesArray = rules.toArray(new RuleInfo[rules.size()]); log.debug("Generating interface for '{}' class", clazz.getName()); return InterfaceGenerator.generateInterface(className, rulesArray, classLoader); }
/** * Gets list of rules. * * @param methods array of methods what represents rule methods * @return list of rules meta-info */ private static List<RuleInfo> getRulesDecorated(Method[] methods) { List<RuleInfo> rules = new ArrayList<RuleInfo>(methods.length); for (Method method : methods) { // Check that method should be ignored or not. if (isIgnored(method)) { continue; } String methodName = method.getName(); Class<?>[] paramTypes = method.getParameterTypes(); Class<?> returnType = method.getReturnType(); Class<?>[] newParams = new Class<?>[]{IRulesRuntimeContext.class}; Class<?>[] extendedParamTypes = ArrayUtils.addAll(newParams, paramTypes); RuleInfo ruleInfo = InterfaceGenerator.createRuleInfo(methodName, extendedParamTypes, returnType); rules.add(ruleInfo); } return rules; }
/** * Decorates methods signatures of given clazz. * * @param clazz class to decorate * @param classLoader The classloader where generated class should be * placed. * @return new class with decorated methods signatures: added * {@link IRulesRuntimeContext} as the first parameter for each * method. * @throws Exception */ public static Class<?> decorateClass(Class<?> clazz, ClassLoader classLoader) throws Exception { final Logger log = LoggerFactory.getLogger(RuntimeContextInstantiationStrategyEnhancerHelper.class); Method[] methods = clazz.getMethods(); List<RuleInfo> rules = getRulesDecorated(methods); String className = clazz.getName() + DECORATED_CLASS_NAME_SUFFIX; RuleInfo[] rulesArray = rules.toArray(new RuleInfo[rules.size()]); log.debug("Generating interface for '{}' class", clazz.getName()); return InterfaceGenerator.generateInterface(className, rulesArray, classLoader); }
/** * Gets rule information of IOpenField instance. * * @param field IOpenField instance * @return rule info */ private static RuleInfo getRuleInfoForField(IOpenField field) { String methodName = ClassUtils.getter(field.getName()); Class<?>[] paramTypes = new Class<?>[0]; Class<?> returnType = field.getType().getInstanceClass(); RuleInfo ruleInfo = createRuleInfo(methodName, paramTypes, returnType); return ruleInfo; }
/** * Decorates methods signatures of given clazz. New decorated class will * have both original methods and decorated methods with * {@link VariationsPack} as the last parameter and {@link VariationsResult} * as the return type. * * @param clazz class to decorate * @param classLoader The classloader where generated class should be * placed. * @return new class with decorated methods signatures * @throws Exception */ public static Class<?> decorateClass(Class<?> clazz, ClassLoader classLoader) throws Exception { final Logger log = LoggerFactory.getLogger(VariationInstantiationStrategyEnhancerHelper.class); Method[] methods = clazz.getMethods(); List<RuleInfo> rules = getRulesDecorated(methods); String className = clazz.getName() + DECORATED_CLASS_NAME_SUFFIX; RuleInfo[] rulesArray = rules.toArray(new RuleInfo[rules.size()]); log.debug("Generating interface for '{}' class", clazz.getName()); return InterfaceGenerator.generateInterface(className, rulesArray, classLoader); }
/** * Decorates methods signatures of given clazz. New decorated class will * have both original methods and decorated methods with * {@link VariationsPack} as the last parameter and {@link VariationsResult} * as the return type. * * @param clazz class to decorate * @param classLoader The classloader where generated class should be * placed. * @return new class with decorated methods signatures * @throws Exception */ public static Class<?> decorateClass(Class<?> clazz, ClassLoader classLoader) throws Exception { final Logger log = LoggerFactory.getLogger(VariationInstantiationStrategyEnhancerHelper.class); Method[] methods = clazz.getMethods(); List<RuleInfo> rules = getRulesDecorated(methods); String className = clazz.getName() + DECORATED_CLASS_NAME_SUFFIX; RuleInfo[] rulesArray = rules.toArray(new RuleInfo[rules.size()]); log.debug("Generating interface for '{}' class", clazz.getName()); return InterfaceGenerator.generateInterface(className, rulesArray, classLoader); }