use of java.lang.reflect.Parameter in project Network-depr by Mas281.
the class CommandManager method handleMethod.
/**
* Handles the registering of a command via its method
*
* @param object The object containing the method
* @param method The method to handle
*/
private void handleMethod(Object object, Method method) {
if (method.isAnnotationPresent(Command.class)) {
if (method.getParameterCount() < 1) {
// No parameters, invalid command
return;
}
int paramIndex = -1;
int minArgs = 0;
int maxArgs = 0;
boolean optional = false;
for (Parameter parameter : method.getParameters()) {
// There's already been an optional argument
if (optional) {
return;
}
paramIndex++;
Class<?> paramClass = parameter.getType();
if (paramIndex == 0) {
if (paramClass != User.class) {
// First parameter is not a User, invalid command
return;
}
continue;
}
if (!isSupportedParameter(paramClass)) {
// Parameter with no context found or not String
return;
}
if (parameter.isAnnotationPresent(Optional.class)) {
optional = true;
} else {
minArgs++;
}
maxArgs++;
}
if (method.getParameterCount() == 1) {
maxArgs = Integer.MAX_VALUE;
}
String[] aliases = method.getDeclaredAnnotation(Command.class).value().split("\\|");
String usage = getUsage(method);
Rank requiredRank = getRequiredRank(method);
Rank[] specificRanks = getSpecificRanks(method);
CommandData data = new CommandData(object, method, aliases, usage, requiredRank, specificRanks, minArgs, maxArgs);
commands.add(data);
}
}
use of java.lang.reflect.Parameter in project cxf by apache.
the class Validator method checkMethodsForInvalidURITemplates.
private static void checkMethodsForInvalidURITemplates(Class<?> userType, Method[] methods) throws RestClientDefinitionException {
Path classPathAnno = userType.getAnnotation(Path.class);
final Set<String> classLevelVariables = new HashSet<>();
URITemplate classTemplate = null;
if (classPathAnno != null) {
classTemplate = new URITemplate(classPathAnno.value());
classLevelVariables.addAll(classTemplate.getVariables());
}
URITemplate template;
for (Method method : methods) {
Path methodPathAnno = method.getAnnotation(Path.class);
if (methodPathAnno != null) {
template = classPathAnno == null ? new URITemplate(methodPathAnno.value()) : new URITemplate(classPathAnno.value() + "/" + methodPathAnno.value());
} else {
template = classTemplate;
}
if (template == null) {
continue;
}
Set<String> allVariables = new HashSet<>(template.getVariables());
if (!allVariables.isEmpty()) {
Map<String, String> paramMap = new HashMap<>();
for (Parameter p : method.getParameters()) {
PathParam pathParam = p.getAnnotation(PathParam.class);
if (pathParam != null) {
paramMap.put(pathParam.value(), "x");
}
}
try {
template.substitute(paramMap, Collections.<String>emptySet(), false);
} catch (IllegalArgumentException ex) {
throwException("VALIDATION_UNRESOLVED_PATH_PARAMS", userType, method);
}
} else {
List<String> foundParams = new ArrayList<>();
for (Parameter p : method.getParameters()) {
PathParam pathParam = p.getAnnotation(PathParam.class);
if (pathParam != null) {
foundParams.add(pathParam.value());
}
}
if (!foundParams.isEmpty()) {
throwException("VALIDATION_EXTRA_PATH_PARAMS", userType, method);
}
}
}
}
use of java.lang.reflect.Parameter in project atlasmap by atlasmap.
the class DefaultAtlasFieldActionService method detectFieldActionParameters.
protected Properties detectFieldActionParameters(String actionClassName) throws ClassNotFoundException {
Class<?> actionClazz = Class.forName(actionClassName);
Properties props = null;
for (Method method : actionClazz.getMethods()) {
// Find setters to avoid the get / is confusion
if (method.getParameterCount() == 1 && method.getName().startsWith("set")) {
// We have a parameter
if (props == null) {
props = new Properties();
}
Property prop = null;
for (Parameter param : method.getParameters()) {
prop = new Property();
prop.setName(camelize(method.getName().substring("set".length())));
prop.setFieldType(getConversionService().fieldTypeFromClass(param.getType()));
props.getProperty().add(prop);
}
}
}
return props;
}
use of java.lang.reflect.Parameter in project Summer by yale8848.
the class MethodsProcessor method get.
public static void get(List<ClassInfo> classInfos, Class clazz) {
Path path = (Path) clazz.getAnnotation(Path.class);
if (path == null || path.value() == null) {
return;
}
ClassInfo classInfo = new ClassInfo();
classInfo.setClassPath(path.value());
classInfo.setClazzObj(newClass(clazz));
classInfo.setClazz(clazz);
Interceptor[] interceptorsClazz = getBefores((Before) clazz.getAnnotation(Before.class));
if (interceptorsClazz != null) {
classInfo.setBefores(interceptorsClazz);
}
interceptorsClazz = getAfters((After) clazz.getAnnotation(After.class));
if (interceptorsClazz != null) {
classInfo.setAfters(interceptorsClazz);
}
for (Method method : clazz.getMethods()) {
Class mt = method.getDeclaringClass();
if (mt == Object.class) {
continue;
}
MethodInfo methodInfo = new MethodInfo();
Interceptor[] interceptorsMethod = getBefores((Before) method.getAnnotation(Before.class));
if (interceptorsMethod != null) {
methodInfo.setBefores(interceptorsMethod);
}
interceptorsMethod = getAfters((After) method.getAnnotation(After.class));
if (interceptorsMethod != null) {
methodInfo.setAfters(interceptorsMethod);
}
Blocking blocking = method.getAnnotation(Blocking.class);
if (blocking != null) {
methodInfo.setBlocking(true);
}
Path pathMthod = (Path) method.getAnnotation(Path.class);
Produces produces = (Produces) method.getAnnotation(Produces.class);
methodInfo.setMethodPath(getPathValue(pathMthod));
methodInfo.setProducesType(getProducesValue(produces));
methodInfo.setHttpMethod(getHttpMethod(method));
methodInfo.setMethod(method);
Parameter[] parameters = method.getParameters();
Class<?>[] parameterTypes = method.getParameterTypes();
Annotation[][] annotations = method.getParameterAnnotations();
int i = 0;
for (Annotation[] an : annotations) {
ArgInfo argInfo = new ArgInfo();
argInfo.setAnnotation(an);
argInfo.setClazz(parameterTypes[i]);
argInfo.setParameter(parameters[i]);
for (Annotation ant : an) {
if (ant instanceof Context) {
argInfo.setContext(true);
} else if (ant instanceof DefaultValue) {
argInfo.setDefaultValue(((DefaultValue) ant).value());
} else if (ant instanceof PathParam) {
argInfo.setPathParam(true);
argInfo.setPathParam(((PathParam) ant).value());
} else if (ant instanceof QueryParam) {
argInfo.setQueryParam(true);
argInfo.setQueryParam(((QueryParam) ant).value());
} else if (ant instanceof FormParam) {
argInfo.setFormParam(true);
argInfo.setFormParam(((FormParam) ant).value());
}
}
i++;
methodInfo.addArgInfo(argInfo);
}
classInfo.addMethodInfo(methodInfo);
}
classInfos.add(classInfo);
}
use of java.lang.reflect.Parameter in project flow by vaadin.
the class ComponentEventBusUtil method findEventDataExpressions.
/**
* Scans the event type and forms a map of event data expression (for
* {@link Element#addEventListener(String, com.vaadin.flow.dom.DomEventListener, String...)}
* ) to Java type, with the same order as the parameters for the event
* constructor (as returned by {@link #getEventConstructor(Class)}).
*
* @return a map of event data expressions, in the order defined by the
* component event constructor parameters
*/
private static LinkedHashMap<String, Class<?>> findEventDataExpressions(Constructor<? extends ComponentEvent<?>> eventConstructor) {
LinkedHashMap<String, Class<?>> eventDataExpressions = new LinkedHashMap<>();
// Parameter 1 is always "boolean fromClient"
for (int i = 2; i < eventConstructor.getParameterCount(); i++) {
Parameter p = eventConstructor.getParameters()[i];
EventData eventData = p.getAnnotation(EventData.class);
if (eventData == null || eventData.value().isEmpty()) {
// @DomEvent, or its value is empty."
throw new IllegalArgumentException(String.format("The parameter %s of the constructor %s has no @%s, or the annotation value is empty", p.getName(), eventConstructor.toString(), EventData.class.getSimpleName()));
}
eventDataExpressions.put(eventData.value(), ReflectTools.convertPrimitiveType(p.getType()));
}
return eventDataExpressions;
}
Aggregations