下面列出了怎么用org.springframework.util.ReflectionUtils.MethodCallback的API类实例代码及写法,或者点击链接到github查看源代码。
/***不获取到Object中的method*/
public static void doWithMethods(Class<?> clazz, MethodCallback mc, MethodFilter mf) {
// Keep backing up the inheritance hierarchy.
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
if (mf != null && !mf.matches(method)) {
continue;
}
try {
mc.doWith(method);
}
catch (IllegalAccessException ex) {
throw new IllegalStateException("Not allowed to access method '" + method.getName() + "': " + ex);
}
}
Class<?> superclass = clazz.getSuperclass();
if (superclass != null && superclass!=Object.class) {
doWithMethods(clazz.getSuperclass(), mc, mf);
}
else if (clazz.isInterface()) {
for (Class<?> superIfc : clazz.getInterfaces()) {
doWithMethods(superIfc, mc, mf);
}
}
}
/***
* controller中的method扫描
*
* @param menuClz
* @param controllerClass
* @param controllerName
* @param controllerMenu
* @param controllerUrl
*/
private static <M extends IMenu<M>> void scanMethodMenus(Class<M> menuClz, Class<?> controllerClass, final String controllerName,
M controllerMenu, String controllerUrl) {
ReflectionUtil.doWithMethods(controllerClass, new MethodCallback() {
@Override
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
VisitCheck visitCheckAnno = AnnotatedElementUtils.findMergedAnnotation(method, VisitCheck.class);
String methodName = method.getName();
MenuType menuType = MenuType.API_PATH;
VisitCheckType visitCheckType = VisitCheckType.getCheckType(visitCheckAnno != null);
ApiOperation apiOperation = AnnotatedElementUtils.findMergedAnnotation(method, ApiOperation.class);
String methodApiName = apiOperation == null ? methodName : apiOperation.value();
methodApiName = StringUtil.isNotBlank(methodApiName) ? methodApiName : methodName;
String url = ControllerHelpers.getMethodUrlAndMethod(controllerUrl, method, true).getUrl();
M methodMenu = IMenu.createMenu(menuClz, controllerName, methodName, url, methodApiName, null, menuType, visitCheckType);
controllerMenu.addChild(methodMenu);
}
}, ApiMethodFilter.filter);
}
public DynamoDBHashAndRangeKeyExtractingEntityMetadataImpl(final Class<T> domainType) {
super(domainType);
this.hashAndRangeKeyMethodExtractor = new DynamoDBHashAndRangeKeyMethodExtractorImpl<T>(getJavaType());
ReflectionUtils.doWithMethods(domainType, new MethodCallback() {
public void doWith(Method method) {
if (method.getAnnotation(DynamoDBHashKey.class) != null) {
String setterMethodName = toSetterMethodNameFromAccessorMethod(method);
if (setterMethodName != null) {
hashKeySetterMethod = ReflectionUtils.findMethod(domainType, setterMethodName, method.getReturnType());
}
}
}
});
ReflectionUtils.doWithFields(domainType, new FieldCallback() {
public void doWith(Field field) {
if (field.getAnnotation(DynamoDBHashKey.class) != null) {
hashKeyField = ReflectionUtils.findField(domainType, field.getName());
}
}
});
Assert.isTrue(hashKeySetterMethod != null || hashKeyField != null, "Unable to find hash key field or setter method on " + domainType + "!");
Assert.isTrue(hashKeySetterMethod == null || hashKeyField == null, "Found both hash key field and setter method on " + domainType + "!");
}
protected void scanMethods() {
final ApiWrap _this = this;
ReflectionUtil.doWithMethods(getClazz(), new MethodCallback() {
@Override
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
MethodWrap methodWrap = new MethodWrap(_this, method);
String methodName = method.getName();
AssertUtil.mustFalse(functions.containsKey(methodName), "代码生成终止:" + getClazz() + " 中超过1个相同的api:" + methodName);
functions.put(methodName, methodWrap);
}
}, ApiMethodFilter.filter);
}
@Override
public Set<String> getIndexRangeKeyPropertyNames() {
final Set<String> propertyNames = new HashSet<String>();
ReflectionUtils.doWithMethods(getJavaType(), new MethodCallback() {
public void doWith(Method method) {
if (method.getAnnotation(DynamoDBIndexRangeKey.class) != null) {
if ((method.getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexName() != null && method
.getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexName().trim().length() > 0)
|| (method.getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexNames() != null && method
.getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexNames().length > 0)) {
propertyNames.add(getPropertyNameForAccessorMethod(method));
}
}
}
});
ReflectionUtils.doWithFields(getJavaType(), new FieldCallback() {
public void doWith(Field field) {
if (field.getAnnotation(DynamoDBIndexRangeKey.class) != null) {
if ((field.getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexName() != null && field
.getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexName().trim().length() > 0)
|| (field.getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexNames() != null && field
.getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexNames().length > 0)) {
propertyNames.add(getPropertyNameForField(field));
}
}
}
});
return propertyNames;
}
/**
* Creates a new {@link FieldAndGetterReflectionEntityInformation} inspecting the
* given domain class for a getter carrying the given annotation.
*
* @param domainClass
* must not be {@literal null}.
* @param annotation
* must not be {@literal null}.
*/
public FieldAndGetterReflectionEntityInformation(Class<T> domainClass, final Class<? extends Annotation> annotation) {
super(domainClass);
Assert.notNull(annotation);
ReflectionUtils.doWithMethods(domainClass, new MethodCallback() {
public void doWith(Method method) {
if (method.getAnnotation(annotation) != null) {
FieldAndGetterReflectionEntityInformation.this.method = method;
return;
}
}
});
if (method == null)
{
ReflectionUtils.doWithFields(domainClass, new FieldCallback() {
public void doWith(Field field) {
if (field.getAnnotation(annotation) != null) {
FieldAndGetterReflectionEntityInformation.this.field = field;
return;
}
}
});
}
Assert.isTrue(this.method != null || this.field != null, String.format("No field or method annotated with %s found!", annotation.toString()));
Assert.isTrue(this.method == null || this.field == null, String.format("Both field and method annotated with %s found!", annotation.toString()));
if (method != null)
{
ReflectionUtils.makeAccessible(method);
}
}
/**
* Find annotated elements on types
* @param ann annotation to search
* @param clazz class to search on.
* @return List with annotated elements
*/
public static List<AnnotatedElement> findAnnotatedElements(final Class<? extends Annotation> annotationType, Class<?> clazz) {
final ArrayList<AnnotatedElement> elements = new ArrayList<AnnotatedElement>();
// Lookup fields
ReflectionUtils.doWithFields(clazz, new FieldCallback() {
@Override
public void doWith(Field field) throws IllegalArgumentException,
IllegalAccessException {
if (field.getAnnotation(annotationType) != null) {
elements.add(field);
}
}
});
// Lookup methods
ReflectionUtils.doWithMethods(clazz, new MethodCallback() {
@Override
public void doWith(Method method) throws IllegalArgumentException,
IllegalAccessException {
if (org.springframework.core.annotation.AnnotationUtils.getAnnotation(method, annotationType) != null)
elements.add(method);
}
});
return elements;
}