You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2017/02/13 11:01:08 UTC
[1/2] struts git commit: Reformats code
Repository: struts
Updated Branches:
refs/heads/master 534dfc6bd -> f22873e12
Reformats code
Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/3dafd2a3
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/3dafd2a3
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/3dafd2a3
Branch: refs/heads/master
Commit: 3dafd2a35665eb90e6d89256057dd8e1fabae1f4
Parents: 534dfc6
Author: Lukasz Lenart <lu...@apache.org>
Authored: Mon Feb 13 11:58:57 2017 +0100
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Mon Feb 13 11:58:57 2017 +0100
----------------------------------------------------------------------
.../xwork2/util/AnnotationUtils.java | 253 +++++++++----------
1 file changed, 125 insertions(+), 128 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/struts/blob/3dafd2a3/core/src/main/java/com/opensymphony/xwork2/util/AnnotationUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/opensymphony/xwork2/util/AnnotationUtils.java b/core/src/main/java/com/opensymphony/xwork2/util/AnnotationUtils.java
index 1c37918..36166dd 100644
--- a/core/src/main/java/com/opensymphony/xwork2/util/AnnotationUtils.java
+++ b/core/src/main/java/com/opensymphony/xwork2/util/AnnotationUtils.java
@@ -48,8 +48,8 @@ public class AnnotationUtils {
* Adds all fields with the specified Annotation of class clazz and its superclasses to allFields
*
* @param annotationClass the {@link Annotation}s to find
- * @param clazz The {@link Class} to inspect
- * @param allFields list of all fields
+ * @param clazz The {@link Class} to inspect
+ * @param allFields list of all fields
*/
public static void addAllFields(Class<? extends Annotation> annotationClass, Class clazz, List<Field> allFields) {
@@ -61,7 +61,7 @@ public class AnnotationUtils {
for (Field field : fields) {
Annotation ann = field.getAnnotation(annotationClass);
- if (ann!=null) {
+ if (ann != null) {
allFields.add(field);
}
}
@@ -72,8 +72,8 @@ public class AnnotationUtils {
* Adds all methods with the specified Annotation of class clazz and its superclasses to allFields
*
* @param annotationClass the {@link Annotation}s to find
- * @param clazz The {@link Class} to inspect
- * @param allMethods list of all methods
+ * @param clazz The {@link Class} to inspect
+ * @param allMethods list of all methods
*/
public static void addAllMethods(Class<? extends Annotation> annotationClass, Class clazz, List<Method> allMethods) {
@@ -85,7 +85,7 @@ public class AnnotationUtils {
for (Method method : methods) {
Annotation ann = method.getAnnotation(annotationClass);
- if (ann!=null) {
+ if (ann != null) {
allMethods.add(method);
}
}
@@ -93,8 +93,7 @@ public class AnnotationUtils {
}
/**
- *
- * @param clazz The {@link Class} to inspect
+ * @param clazz The {@link Class} to inspect
* @param allInterfaces list of all interfaces
*/
public static void addAllInterfaces(Class clazz, List<Class> allInterfaces) {
@@ -107,134 +106,132 @@ public class AnnotationUtils {
addAllInterfaces(clazz.getSuperclass(), allInterfaces);
}
- /**
- * For the given <code>Class</code> get a collection of the the {@link AnnotatedElement}s
- * that match the given <code>annotation</code>s or if no <code>annotation</code>s are
- * specified then return all of the annotated elements of the given <code>Class</code>.
- * Includes only the method level annotations.
- *
- * @param clazz The {@link Class} to inspect
- * @param annotation the {@link Annotation}s to find
- * @return A {@link Collection}<{@link AnnotatedElement}> containing all of the
- * method {@link AnnotatedElement}s matching the specified {@link Annotation}s
- */
- public static Collection<Method> getAnnotatedMethods(Class clazz, Class<? extends Annotation>... annotation){
+ /**
+ * For the given <code>Class</code> get a collection of the the {@link AnnotatedElement}s
+ * that match the given <code>annotation</code>s or if no <code>annotation</code>s are
+ * specified then return all of the annotated elements of the given <code>Class</code>.
+ * Includes only the method level annotations.
+ *
+ * @param clazz The {@link Class} to inspect
+ * @param annotation the {@link Annotation}s to find
+ * @return A {@link Collection}<{@link AnnotatedElement}> containing all of the
+ * method {@link AnnotatedElement}s matching the specified {@link Annotation}s
+ */
+ public static Collection<Method> getAnnotatedMethods(Class clazz, Class<? extends Annotation>... annotation) {
Collection<Method> toReturn = new HashSet<>();
for (Method m : clazz.getMethods()) {
- boolean found = false;
- for( Class<? extends Annotation> c : annotation ){
- if( null != findAnnotation(m, c) ){
- found = true;
- break;
- }
- }
+ boolean found = false;
+ for (Class<? extends Annotation> c : annotation) {
+ if (null != findAnnotation(m, c)) {
+ found = true;
+ break;
+ }
+ }
if (found) {
toReturn.add(m);
} else if (ArrayUtils.isEmpty(annotation) && ArrayUtils.isNotEmpty(m.getAnnotations())) {
toReturn.add(m);
}
- }
-
- return toReturn;
- }
+ }
- /**
- * Find a single {@link Annotation} of {@code annotationType} from the supplied
- * {@link Method}, traversing its super methods (i.e., from superclasses and
- * interfaces) if no annotation can be found on the given method itself.
- * <p>Annotations on methods are not inherited by default, so we need to handle
- * this explicitly.
- * @param method the method to look for annotations on
- * @param annotationType the annotation type to look for
- * @return the annotation found, or {@code null} if none
- */
- public static <A extends Annotation> A findAnnotation(Method method, Class<A> annotationType) {
- A result = getAnnotation(method, annotationType);
- Class<?> clazz = method.getDeclaringClass();
- if (result == null) {
- result = searchOnInterfaces(method, annotationType, clazz.getInterfaces());
- }
- while (result == null) {
- clazz = clazz.getSuperclass();
- if (clazz == null || clazz.equals(Object.class)) {
- break;
- }
- try {
- Method equivalentMethod = clazz.getDeclaredMethod(method.getName(), method.getParameterTypes());
- result = getAnnotation(equivalentMethod, annotationType);
- }
- catch (NoSuchMethodException ex) {
- // No equivalent method found
- }
- if (result == null) {
- result = searchOnInterfaces(method, annotationType, clazz.getInterfaces());
- }
- }
- return result;
- }
+ return toReturn;
+ }
- /**
- * Get a single {@link Annotation} of {@code annotationType} from the supplied
- * Method, Constructor or Field. Meta-annotations will be searched if the annotation
- * is not declared locally on the supplied element.
- * @param annotatedElement the Method, Constructor or Field from which to get the annotation
- * @param annotationType the annotation type to look for, both locally and as a meta-annotation
- * @return the matching annotation, or {@code null} if none found
- */
- public static <T extends Annotation> T getAnnotation(AnnotatedElement annotatedElement, Class<T> annotationType) {
- try {
- T ann = annotatedElement.getAnnotation(annotationType);
- if (ann == null) {
- for (Annotation metaAnn : annotatedElement.getAnnotations()) {
- ann = metaAnn.annotationType().getAnnotation(annotationType);
- if (ann != null) {
- break;
- }
- }
- }
- return ann;
- }
- catch (Exception ex) {
- // Assuming nested Class values not resolvable within annotation attributes...
- return null;
- }
- }
+ /**
+ * Find a single {@link Annotation} of {@code annotationType} from the supplied
+ * {@link Method}, traversing its super methods (i.e., from superclasses and
+ * interfaces) if no annotation can be found on the given method itself.
+ * <p>Annotations on methods are not inherited by default, so we need to handle
+ * this explicitly.
+ *
+ * @param method the method to look for annotations on
+ * @param annotationType the annotation type to look for
+ * @return the annotation found, or {@code null} if none
+ */
+ public static <A extends Annotation> A findAnnotation(Method method, Class<A> annotationType) {
+ A result = getAnnotation(method, annotationType);
+ Class<?> clazz = method.getDeclaringClass();
+ if (result == null) {
+ result = searchOnInterfaces(method, annotationType, clazz.getInterfaces());
+ }
+ while (result == null) {
+ clazz = clazz.getSuperclass();
+ if (clazz == null || clazz.equals(Object.class)) {
+ break;
+ }
+ try {
+ Method equivalentMethod = clazz.getDeclaredMethod(method.getName(), method.getParameterTypes());
+ result = getAnnotation(equivalentMethod, annotationType);
+ } catch (NoSuchMethodException ex) {
+ // No equivalent method found
+ }
+ if (result == null) {
+ result = searchOnInterfaces(method, annotationType, clazz.getInterfaces());
+ }
+ }
+ return result;
+ }
- private static <A extends Annotation> A searchOnInterfaces(Method method, Class<A> annotationType, Class<?>... ifcs) {
- A annotation = null;
- for (Class<?> iface : ifcs) {
- if (isInterfaceWithAnnotatedMethods(iface)) {
- try {
- Method equivalentMethod = iface.getMethod(method.getName(), method.getParameterTypes());
- annotation = getAnnotation(equivalentMethod, annotationType);
- }
- catch (NoSuchMethodException ex) {
- // Skip this interface - it doesn't have the method...
- }
- if (annotation != null) {
- break;
- }
- }
- }
- return annotation;
- }
+ /**
+ * Get a single {@link Annotation} of {@code annotationType} from the supplied
+ * Method, Constructor or Field. Meta-annotations will be searched if the annotation
+ * is not declared locally on the supplied element.
+ *
+ * @param annotatedElement the Method, Constructor or Field from which to get the annotation
+ * @param annotationType the annotation type to look for, both locally and as a meta-annotation
+ * @return the matching annotation, or {@code null} if none found
+ */
+ public static <T extends Annotation> T getAnnotation(AnnotatedElement annotatedElement, Class<T> annotationType) {
+ try {
+ T ann = annotatedElement.getAnnotation(annotationType);
+ if (ann == null) {
+ for (Annotation metaAnn : annotatedElement.getAnnotations()) {
+ ann = metaAnn.annotationType().getAnnotation(annotationType);
+ if (ann != null) {
+ break;
+ }
+ }
+ }
+ return ann;
+ } catch (Exception ex) {
+ // Assuming nested Class values not resolvable within annotation attributes...
+ return null;
+ }
+ }
- private static boolean isInterfaceWithAnnotatedMethods(Class<?> iface) {
- boolean found = false;
- for (Method ifcMethod : iface.getMethods()) {
- try {
- if (ifcMethod.getAnnotations().length > 0) {
- found = true;
- break;
- }
- }
- catch (Exception ex) {
- // Assuming nested Class values not resolvable within annotation attributes...
- }
- }
- return found;
- }
+ private static <A extends Annotation> A searchOnInterfaces(Method method, Class<A> annotationType, Class<?>... ifcs) {
+ A annotation = null;
+ for (Class<?> iface : ifcs) {
+ if (isInterfaceWithAnnotatedMethods(iface)) {
+ try {
+ Method equivalentMethod = iface.getMethod(method.getName(), method.getParameterTypes());
+ annotation = getAnnotation(equivalentMethod, annotationType);
+ } catch (NoSuchMethodException ex) {
+ // Skip this interface - it doesn't have the method...
+ }
+ if (annotation != null) {
+ break;
+ }
+ }
+ }
+ return annotation;
+ }
+
+ private static boolean isInterfaceWithAnnotatedMethods(Class<?> iface) {
+ boolean found = false;
+ for (Method ifcMethod : iface.getMethods()) {
+ try {
+ if (ifcMethod.getAnnotations().length > 0) {
+ found = true;
+ break;
+ }
+ } catch (Exception ex) {
+ // Assuming nested Class values not resolvable within annotation attributes...
+ }
+ }
+ return found;
+ }
/**
* Returns the property name for a method.
@@ -264,10 +261,10 @@ public class AnnotationUtils {
* Returns the annotation on the given class or the package of the class. This searchs up the
* class hierarchy and the package hierarchy for the closest match.
*
- * @param <T> class type
- * @param clazz The class to search for the annotation.
- * @param annotationClass The Class of the annotation.
- * @return The annotation or null.
+ * @param <T> class type
+ * @param clazz The class to search for the annotation.
+ * @param annotationClass The Class of the annotation.
+ * @return The annotation or null.
*/
public static <T extends Annotation> T findAnnotation(Class<?> clazz, Class<T> annotationClass) {
T ann = clazz.getAnnotation(annotationClass);
[2/2] struts git commit: Drops code duplication
Posted by lu...@apache.org.
Drops code duplication
Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/f22873e1
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/f22873e1
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/f22873e1
Branch: refs/heads/master
Commit: f22873e127ca8c4de8b636415eb8097db77d79c6
Parents: 3dafd2a
Author: Lukasz Lenart <lu...@apache.org>
Authored: Mon Feb 13 12:00:59 2017 +0100
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Mon Feb 13 12:00:59 2017 +0100
----------------------------------------------------------------------
...nnotationValidationConfigurationBuilder.java | 32 ++------------------
1 file changed, 2 insertions(+), 30 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/struts/blob/f22873e1/core/src/main/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilder.java b/core/src/main/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilder.java
index dc8d856..b587bf3 100644
--- a/core/src/main/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilder.java
+++ b/core/src/main/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilder.java
@@ -15,6 +15,7 @@
*/
package com.opensymphony.xwork2.validator;
+import com.opensymphony.xwork2.util.AnnotationUtils;
import com.opensymphony.xwork2.validator.annotations.*;
import org.apache.commons.lang3.StringUtils;
@@ -24,8 +25,6 @@ import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
/**
* <code>AnnotationValidationConfigurationBuilder</code>
@@ -36,9 +35,6 @@ import java.util.regex.Pattern;
*/
public class AnnotationValidationConfigurationBuilder {
- private static final Pattern SETTER_PATTERN = Pattern.compile("set([A-Z][A-Za-z0-9]*)$");
- private static final Pattern GETTER_PATTERN = Pattern.compile("(get|is|has)([A-Z][A-Za-z0-9]*)$");
-
private ValidatorFactory validatorFactory;
public AnnotationValidationConfigurationBuilder(ValidatorFactory fac) {
@@ -61,7 +57,7 @@ public class AnnotationValidationConfigurationBuilder {
if (o instanceof Method) {
Method method = (Method) o;
- fieldName = resolvePropertyName(method);
+ fieldName = AnnotationUtils.resolvePropertyName(method);
methodName = method.getName();
annotations = method.getAnnotations();
@@ -874,28 +870,4 @@ public class AnnotationValidationConfigurationBuilder {
}
- /**
- * Returns the property name for a method.
- * This method is independant from property fields.
- *
- * @param method The method to get the property name for.
- * @return the property name for given method; null if non could be resolved.
- */
- public String resolvePropertyName(Method method) {
-
- Matcher matcher = SETTER_PATTERN.matcher(method.getName());
- if (matcher.matches() && method.getParameterTypes().length == 1) {
- String raw = matcher.group(1);
- return raw.substring(0, 1).toLowerCase() + raw.substring(1);
- }
-
- matcher = GETTER_PATTERN.matcher(method.getName());
- if (matcher.matches() && method.getParameterTypes().length == 0) {
- String raw = matcher.group(2);
- return raw.substring(0, 1).toLowerCase() + raw.substring(1);
- }
-
- return null;
- }
-
}