/** * Creates a plugin for caching method return values. */ public CachedReturnPlugin() { super(declaresMethod(isAnnotatedWith(Enhance.class))); randomString = new RandomString(); classFileLocator = ClassFileLocator.ForClassLoader.of(CachedReturnPlugin.class.getClassLoader()); TypePool typePool = TypePool.Default.of(classFileLocator); adviceByType = new HashMap<TypeDescription, TypeDescription>(); for (Class<?> type : new Class<?>[]{ boolean.class, byte.class, short.class, char.class, int.class, long.class, float.class, double.class, Object.class }) { adviceByType.put(TypeDescription.ForLoadedType.ForLoadedType.of(type), typePool.describe(CachedReturnPlugin.class.getName() + ADVICE_INFIX + type.getSimpleName()).resolve()); } }
@Override public boolean matches(TypeDescription targetInterface) { if (ElementMatchers.declaresMethod(named(targetMethod.getName()) .and(returns(targetMethod.getReturnType().asErasure())) .and(takesArguments(targetMethod.getParameters().asTypeList().asErasures()))) .matches(targetInterface)) { return true; } else { for (TypeDescription typeDescription : targetInterface.getInterfaces().asErasures()) { if (matches(typeDescription)) { return true; } } } return false; } }
@Override public boolean matches(MethodDescription targetMethod) { TypeDescription superClass = targetMethod.getDeclaringType().asErasure(); do { superClass = superClass.getSuperClass().asErasure(); if (declaresMethod(named(targetMethod.getName()) .and(returns(targetMethod.getReturnType().asErasure())) .and(takesArguments(targetMethod.getParameters().asTypeList().asErasures())) .and(extraMethodMatcher)) .matches(superClass)) { return true; } } while (superClassMatcher.matches(superClass)); return false; } }
@Override public boolean matches(TypeDescription targetInterface) { if (ElementMatchers.declaresMethod(named(targetMethod.getName()) .and(returns(targetMethod.getReturnType().asErasure())) .and(takesArguments(targetMethod.getParameters().asTypeList().asErasures()))) .matches(targetInterface)) { return true; } else { for (TypeDescription typeDescription : targetInterface.getInterfaces().asErasures()) { if (matches(typeDescription)) { return true; } } } return false; } }
@Override public ElementMatcher<? super TypeDescription> getTypeMatcher() { return matches(methodMatcher.getClassMatcher()) .and(not(nameContains("$JaxbAccessor"))) .and(not(nameContains("$$"))) .and(not(nameContains("CGLIB"))) .and(not(nameContains("EnhancerBy"))) .and(not(nameContains("$Proxy"))) .and(declaresMethod(matches(methodMatcher.getMethodMatcher()))); }
private boolean declaresInHierarchy(MethodDescription targetMethod, TypeDescription type) { if (declaresMethod(named(targetMethod.getName()) .and(returns(targetMethod.getReturnType().asErasure())) .and(takesArguments(targetMethod.getParameters().asTypeList().asErasures())) .and(extraMethodMatcher)) .matches(type)) { return true; } for (TypeDescription interfaze : type.getInterfaces().asErasures()) { if (superClassMatcher.matches(interfaze)) { if (declaresInHierarchy(targetMethod, interfaze)) { return true; } } } final TypeDescription.Generic superClass = type.getSuperClass(); if (superClass != null && superClassMatcher.matches(superClass.asErasure())) { return declaresInHierarchy(targetMethod, superClass.asErasure()); } return false; }