org.springframework.util.ReflectionUtils.MethodCallback Java Examples

The following examples show how to use org.springframework.util.ReflectionUtils.MethodCallback. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReflectionUtil.java    From stategen with GNU Affero General Public License v3.0 6 votes vote down vote up
/***不获取到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);
        }
    }
}
 
Example #2
Source File: ControllerHelpers.java    From stategen with GNU Affero General Public License v3.0 6 votes vote down vote up
/***
 * 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);
}
 
Example #3
Source File: DynamoDBHashAndRangeKeyExtractingEntityMetadataImpl.java    From spring-data-dynamodb with Apache License 2.0 6 votes vote down vote up
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 + "!");

}
 
Example #4
Source File: ApiWrap.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
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);
    }
 
Example #5
Source File: DynamoDBHashAndRangeKeyExtractingEntityMetadataImpl.java    From spring-data-dynamodb with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #6
Source File: FieldAndGetterReflectionEntityInformation.java    From spring-data-dynamodb with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
	}
}
 
Example #7
Source File: AnnotatedElementAccessor.java    From jdal with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}