Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 26E3D200D34 for ; Fri, 3 Nov 2017 23:27:33 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 22CE6160BFB; Fri, 3 Nov 2017 22:27:33 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 44902160BDE for ; Fri, 3 Nov 2017 23:27:29 +0100 (CET) Received: (qmail 56264 invoked by uid 500); 3 Nov 2017 22:27:28 -0000 Mailing-List: contact commits-help@bval.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@bval.apache.org Delivered-To: mailing list commits@bval.apache.org Received: (qmail 55196 invoked by uid 99); 3 Nov 2017 22:27:27 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 03 Nov 2017 22:27:27 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 3931EE0A2A; Fri, 3 Nov 2017 22:27:27 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: mbenson@apache.org To: commits@bval.apache.org Date: Fri, 03 Nov 2017 22:27:51 -0000 Message-Id: <1e2157cd437c4831b3af809ed56518ab@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [26/51] [abbrv] bval git commit: BVAL-151 trim down external dependencies archived-at: Fri, 03 Nov 2017 22:27:33 -0000 BVAL-151 trim down external dependencies Project: http://git-wip-us.apache.org/repos/asf/bval/repo Commit: http://git-wip-us.apache.org/repos/asf/bval/commit/8dabb264 Tree: http://git-wip-us.apache.org/repos/asf/bval/tree/8dabb264 Diff: http://git-wip-us.apache.org/repos/asf/bval/diff/8dabb264 Branch: refs/heads/master Commit: 8dabb2641b284caa7233bff0d9e9927f42ac3347 Parents: aad8498 Author: Mark Struberg Authored: Mon Oct 24 21:02:02 2016 +0000 Committer: Mark Struberg Committed: Mon Oct 24 21:02:02 2016 +0000 ---------------------------------------------------------------------- bundle/pom.xml | 2 +- bval-core/pom.xml | 107 +- .../java/org/apache/bval/MetaBeanBuilder.java | 11 +- .../org/apache/bval/model/DynaTypeEnum.java | 3 +- .../org/apache/bval/model/FeaturesCapable.java | 8 +- .../org/apache/bval/model/MetaAnnotated.java | 16 +- .../java/org/apache/bval/model/MetaBean.java | 6 +- .../org/apache/bval/model/MetaProperty.java | 4 +- .../java/org/apache/bval/util/BValVersion.java | 1 - .../org/apache/bval/util/IndexedAccess.java | 7 +- .../java/org/apache/bval/util/KeyedAccess.java | 3 +- .../java/org/apache/bval/util/ObjectUtils.java | 96 ++ .../org/apache/bval/util/PropertyAccess.java | 4 +- .../java/org/apache/bval/util/StringUtils.java | 177 ++ .../java/org/apache/bval/util/Validate.java | 46 + .../apache/bval/util/reflection/Reflection.java | 139 +- .../apache/bval/util/reflection/TypeUtils.java | 1603 ++++++++++++++++++ .../org/apache/bval/util/StringUtilsTest.java | 32 + bval-jsr/pom.xml | 36 - .../java/org/apache/bval/cdi/BValExtension.java | 3 +- .../bval/jsr/AnnotationConstraintBuilder.java | 5 +- .../apache/bval/jsr/AnnotationProcessor.java | 6 +- .../bval/jsr/ApacheValidationProvider.java | 5 +- .../apache/bval/jsr/ApacheValidatorFactory.java | 15 +- .../org/apache/bval/jsr/BeanDescriptorImpl.java | 3 +- .../org/apache/bval/jsr/ClassValidator.java | 12 +- .../jsr/ConstraintAnnotationAttributes.java | 2 +- .../org/apache/bval/jsr/ConstraintDefaults.java | 4 +- .../bval/jsr/ConstraintDescriptorImpl.java | 68 +- .../apache/bval/jsr/ConstraintValidation.java | 14 +- .../bval/jsr/DefaultMessageInterpolator.java | 4 +- .../java/org/apache/bval/jsr/groups/Group.java | 23 +- .../resolver/DefaultTraversableResolver.java | 8 +- .../apache/bval/jsr/util/PathNavigation.java | 23 +- .../jsr/util/ValidationContextTraversal.java | 6 +- .../bval/jsr/xml/ValidationMappingParser.java | 8 +- .../bval/constraints/HasStringValidator.java | 6 +- bval-tck11/work-tests-suite.xml | 4 +- .../org/apache/bval/xml/XMLMetaBeanInfos.java | 4 +- .../org/apache/bval/xml/XMLMetaElement.java | 4 +- pom.xml | 1 + 41 files changed, 2267 insertions(+), 262 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/bval/blob/8dabb264/bundle/pom.xml ---------------------------------------------------------------------- diff --git a/bundle/pom.xml b/bundle/pom.xml index 5757114..ee6526e 100644 --- a/bundle/pom.xml +++ b/bundle/pom.xml @@ -74,7 +74,7 @@ org.apache.bval.* - org.apache.commons.lang3.*;resolution:=optional,javax.annotation.*;resolution:=optional,javax.interceptor.*;resolution:=optional, + javax.annotation.*;resolution:=optional,javax.interceptor.*;resolution:=optional, javax.inject.*;resolution:=optional,javax.enterprise.*;resolution:=optional,javax.persistence.*;resolution:=optional, org.apache.bval.xml.*;resolution:=optional,javax.validation.*;resolution:=optional, !org.apache.commons.weaver.*, http://git-wip-us.apache.org/repos/asf/bval/blob/8dabb264/bval-core/pom.xml ---------------------------------------------------------------------- diff --git a/bval-core/pom.xml b/bval-core/pom.xml index f56ee89..6288272 100644 --- a/bval-core/pom.xml +++ b/bval-core/pom.xml @@ -127,81 +127,44 @@ build.timestamp=${timestamp} - org.apache.maven.plugins - maven-shade-plugin - - - - org.apache.commons:commons-lang3 - org.apache.commons:commons-lang3 - - - - - org.apache.commons:commons-lang3 - - META-INF/maven/** - - - - - - org.apache.commons - org.apache.bval.core._oac - - - true - true - true - true - - - - - shade - - - - - - org.apache.commons - commons-weaver-maven-plugin + org.apache.commons + commons-weaver-maven-plugin - - - - org.eclipse.m2e - lifecycle-mapping - 1.0.0 - - - - - - - org.apache.maven.plugins - - - maven-antrun-plugin - - - [1.3,) - - - run - - - - - - - - - - - + + + + org.eclipse.m2e + lifecycle-mapping + 1.0.0 + + + + + + + org.apache.maven.plugins + + + maven-antrun-plugin + + + [1.3,) + + + run + + + + + + + + + + + http://git-wip-us.apache.org/repos/asf/bval/blob/8dabb264/bval-core/src/main/java/org/apache/bval/MetaBeanBuilder.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/MetaBeanBuilder.java b/bval-core/src/main/java/org/apache/bval/MetaBeanBuilder.java index 35f455c..ae4cca5 100644 --- a/bval-core/src/main/java/org/apache/bval/MetaBeanBuilder.java +++ b/bval-core/src/main/java/org/apache/bval/MetaBeanBuilder.java @@ -17,8 +17,7 @@ package org.apache.bval; import org.apache.bval.model.MetaBean; -import org.apache.commons.lang3.ArrayUtils; -import org.apache.commons.lang3.ClassUtils; +import org.apache.bval.util.reflection.Reflection; import java.util.HashMap; import java.util.Map; @@ -61,7 +60,7 @@ public class MetaBeanBuilder { * @return {@link MetaBeanFactory} array */ public MetaBeanFactory[] getFactories() { - return ArrayUtils.clone(factories); + return factories != null ? factories.clone() : null; } /** @@ -71,7 +70,7 @@ public class MetaBeanBuilder { * @param factories */ public void setFactories(MetaBeanFactory[] factories) { - this.factories = ArrayUtils.clone(factories); + this.factories = factories != null ? factories.clone() : null; } /** @@ -105,9 +104,9 @@ public class MetaBeanBuilder { protected Class findLocalClass(String className) { if (className != null) { try { - return ClassUtils.getClass(className); + return Reflection.toClass(className); } catch (ClassNotFoundException e) { - log.log(Level.FINE, String.format("Class not found: %s", className), e); + log.log(Level.FINE, String.format("Class not found: %s", className), e); } } return null; http://git-wip-us.apache.org/repos/asf/bval/blob/8dabb264/bval-core/src/main/java/org/apache/bval/model/DynaTypeEnum.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/model/DynaTypeEnum.java b/bval-core/src/main/java/org/apache/bval/model/DynaTypeEnum.java index 17e04d5..e4c506c 100644 --- a/bval-core/src/main/java/org/apache/bval/model/DynaTypeEnum.java +++ b/bval-core/src/main/java/org/apache/bval/model/DynaTypeEnum.java @@ -16,7 +16,6 @@ */ package org.apache.bval.model; -import org.apache.commons.lang3.ArrayUtils; /** * Description: ("artificial" enum with custom values).
@@ -85,7 +84,7 @@ public class DynaTypeEnum implements DynaType { * @return Value[] */ public Value[] getEnumConstants() { - return ArrayUtils.clone(enumConstants); + return enumConstants != null ? enumConstants.clone() : null; } /** http://git-wip-us.apache.org/repos/asf/bval/blob/8dabb264/bval-core/src/main/java/org/apache/bval/model/FeaturesCapable.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/model/FeaturesCapable.java b/bval-core/src/main/java/org/apache/bval/model/FeaturesCapable.java index 49ef65f..bd7464b 100644 --- a/bval-core/src/main/java/org/apache/bval/model/FeaturesCapable.java +++ b/bval-core/src/main/java/org/apache/bval/model/FeaturesCapable.java @@ -16,8 +16,6 @@ */ package org.apache.bval.model; -import org.apache.commons.lang3.ArrayUtils; - import java.io.Serializable; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -122,7 +120,7 @@ public abstract class FeaturesCapable implements Serializable { protected void copyInto(FeaturesCapable target) { target.features = target.createFeaturesMap(); target.features.putAll(features); - target.validations = ArrayUtils.clone(validations); + target.validations = validations != null ? validations.clone() : null; } /** @@ -131,7 +129,7 @@ public abstract class FeaturesCapable implements Serializable { * @return Validation array */ public Validation[] getValidations() { - return ArrayUtils.clone(validations); + return validations != null ? validations.clone() : null; } /** @@ -140,7 +138,7 @@ public abstract class FeaturesCapable implements Serializable { * @param validations */ public void setValidations(Validation[] validations) { - this.validations = ArrayUtils.clone(validations); + this.validations = validations != null ? validations.clone() : null; } /** http://git-wip-us.apache.org/repos/asf/bval/blob/8dabb264/bval-core/src/main/java/org/apache/bval/model/MetaAnnotated.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/model/MetaAnnotated.java b/bval-core/src/main/java/org/apache/bval/model/MetaAnnotated.java index 50e24a7..af9a331 100755 --- a/bval-core/src/main/java/org/apache/bval/model/MetaAnnotated.java +++ b/bval-core/src/main/java/org/apache/bval/model/MetaAnnotated.java @@ -17,20 +17,28 @@ package org.apache.bval.model; import java.lang.annotation.Annotation; +import java.util.HashSet; +import java.util.Set; + +import org.apache.bval.util.ObjectUtils; -import org.apache.commons.lang3.ArrayUtils; public abstract class MetaAnnotated extends Meta { private static final long serialVersionUID = 1L; - private Annotation[] annotations = new Annotation[0]; + private Set annotations = new HashSet(); + private Annotation[] annArray = null; public Annotation[] getAnnotations() { - return annotations; + if (annArray == null) { + annArray = annotations.isEmpty() ? ObjectUtils.EMPTY_ANNOTATION_ARRAY : annotations.toArray(new Annotation[annotations.size()]); + } + return annArray; } public void addAnnotation(final Annotation annotation) { - this.annotations = ArrayUtils.add(annotations, annotation); + this.annotations.add(annotation); + annArray = null; } } http://git-wip-us.apache.org/repos/asf/bval/blob/8dabb264/bval-core/src/main/java/org/apache/bval/model/MetaBean.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/model/MetaBean.java b/bval-core/src/main/java/org/apache/bval/model/MetaBean.java index 19d484b..8d2cb58 100644 --- a/bval-core/src/main/java/org/apache/bval/model/MetaBean.java +++ b/bval-core/src/main/java/org/apache/bval/model/MetaBean.java @@ -27,7 +27,6 @@ import java.util.Map; import java.util.TreeMap; import org.apache.bval.util.reflection.Reflection; -import org.apache.commons.lang3.StringUtils; import org.apache.commons.weaver.privilizer.Privilizing; import org.apache.commons.weaver.privilizer.Privilizing.CallTo; @@ -287,7 +286,7 @@ public class MetaBean extends FeaturesCapable implements Cloneable, Features.Bea } for (final Method m : clazz.getDeclaredMethods()) { final String name = getPropertyName(m); - if (StringUtils.isNotEmpty(name)) { + if (name != null && !name.isEmpty()) { if (!fields.containsKey(name)) { fields.put(name, Integer.valueOf(++i)); } @@ -320,7 +319,8 @@ public class MetaBean extends FeaturesCapable implements Cloneable, Features.Bea if (i2 == null) { // java.util.TreeMap requires that the comparator be consistent with #equals(), // therefore we must not incorrectly report 0 comparison for different property names - return StringUtils.compare(o1, o2); + // Both o1 and o2 cannot be null as they would have blown up with a NPE in fields.get already + return o1.compareTo(o2); } return -1; } http://git-wip-us.apache.org/repos/asf/bval/blob/8dabb264/bval-core/src/main/java/org/apache/bval/model/MetaProperty.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/model/MetaProperty.java b/bval-core/src/main/java/org/apache/bval/model/MetaProperty.java index 60ff9a8..690416f 100644 --- a/bval-core/src/main/java/org/apache/bval/model/MetaProperty.java +++ b/bval-core/src/main/java/org/apache/bval/model/MetaProperty.java @@ -16,10 +16,10 @@ */ package org.apache.bval.model; -import org.apache.commons.lang3.reflect.TypeUtils; - import java.lang.reflect.Type; +import org.apache.bval.util.reflection.TypeUtils; + /** * Description: the meta description of a property of a bean. It supports a map * of features and multiple validations.
http://git-wip-us.apache.org/repos/asf/bval/blob/8dabb264/bval-core/src/main/java/org/apache/bval/util/BValVersion.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/util/BValVersion.java b/bval-core/src/main/java/org/apache/bval/util/BValVersion.java index 74e2714..06ba3b3 100644 --- a/bval-core/src/main/java/org/apache/bval/util/BValVersion.java +++ b/bval-core/src/main/java/org/apache/bval/util/BValVersion.java @@ -24,7 +24,6 @@ import java.util.Properties; import java.util.StringTokenizer; import org.apache.bval.util.reflection.Reflection; -import org.apache.commons.lang3.StringUtils; import org.apache.commons.weaver.privilizer.Privilizing; import org.apache.commons.weaver.privilizer.Privilizing.CallTo; http://git-wip-us.apache.org/repos/asf/bval/blob/8dabb264/bval-core/src/main/java/org/apache/bval/util/IndexedAccess.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/util/IndexedAccess.java b/bval-core/src/main/java/org/apache/bval/util/IndexedAccess.java index 0cfc651..8f33466 100644 --- a/bval-core/src/main/java/org/apache/bval/util/IndexedAccess.java +++ b/bval-core/src/main/java/org/apache/bval/util/IndexedAccess.java @@ -16,8 +16,6 @@ */ package org.apache.bval.util; -import org.apache.commons.lang3.ObjectUtils; -import org.apache.commons.lang3.reflect.TypeUtils; import java.lang.annotation.ElementType; import java.lang.reflect.Array; @@ -26,6 +24,8 @@ import java.lang.reflect.TypeVariable; import java.util.List; import java.util.Map; +import org.apache.bval.util.reflection.TypeUtils; + /** * {@link AccessStrategy} to get an indexed member of an {@link Iterable} or * array object. @@ -46,7 +46,8 @@ public class IndexedAccess extends AccessStrategy { } if (TypeUtils.isAssignable(containerType, Iterable.class)) { Map, Type> typeArguments = TypeUtils.getTypeArguments(containerType, Iterable.class); - return ObjectUtils.defaultIfNull(TypeUtils.unrollVariables(typeArguments, ITERABLE_TYPE), Object.class); + Type type = TypeUtils.unrollVariables(typeArguments, ITERABLE_TYPE); + return type != null ? type : Object.class; } return null; } http://git-wip-us.apache.org/repos/asf/bval/blob/8dabb264/bval-core/src/main/java/org/apache/bval/util/KeyedAccess.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/util/KeyedAccess.java b/bval-core/src/main/java/org/apache/bval/util/KeyedAccess.java index b0e5e71..700287d 100644 --- a/bval-core/src/main/java/org/apache/bval/util/KeyedAccess.java +++ b/bval-core/src/main/java/org/apache/bval/util/KeyedAccess.java @@ -16,8 +16,7 @@ */ package org.apache.bval.util; -import org.apache.commons.lang3.ObjectUtils; -import org.apache.commons.lang3.reflect.TypeUtils; +import org.apache.bval.util.reflection.TypeUtils; import java.lang.annotation.ElementType; import java.lang.reflect.Type; http://git-wip-us.apache.org/repos/asf/bval/blob/8dabb264/bval-core/src/main/java/org/apache/bval/util/ObjectUtils.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/util/ObjectUtils.java b/bval-core/src/main/java/org/apache/bval/util/ObjectUtils.java new file mode 100644 index 0000000..0464eeb --- /dev/null +++ b/bval-core/src/main/java/org/apache/bval/util/ObjectUtils.java @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.bval.util; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Array; + +public final class ObjectUtils { + public static final Class[] EMPTY_CLASS_ARRAY = new Class[0]; + public static final String[] EMPTY_STRING_ARRAY = new String[0]; + public static final Annotation[] EMPTY_ANNOTATION_ARRAY = new Annotation[0]; + + private ObjectUtils() { + } + + /** + *

Returns a default value if the object passed is {@code null}.

+ * + *
+     * ObjectUtils.defaultIfNull(null, null)      = null
+     * ObjectUtils.defaultIfNull(null, "")        = ""
+     * ObjectUtils.defaultIfNull(null, "zz")      = "zz"
+     * ObjectUtils.defaultIfNull("abc", *)        = "abc"
+     * ObjectUtils.defaultIfNull(Boolean.TRUE, *) = Boolean.TRUE
+     * 
+ * + * @param the type of the object + * @param object the {@code Object} to test, may be {@code null} + * @param defaultValue the default value to return, may be {@code null} + * @return {@code object} if it is not {@code null}, defaultValue otherwise + */ + public static T defaultIfNull(final T object, final T defaultValue) { + return object != null ? object : defaultValue; + } + + public static boolean isNotEmpty(final T[] array) { + return !isEmpty(array); + } + + public static boolean isEmpty(final Object[] array) { + return array == null || array.length == 0; + } + + /** + *

Checks if the object is in the given array. + * + *

The method returns {@code false} if a {@code null} array is passed in. + * + * @param array the array to search through + * @param objectToFind the object to find + * @return {@code true} if the array contains the object + */ + public static boolean arrayContains(final Object[] array, final Object objectToFind) { + if (array == null) { + return false; + } + for (Object o : array) { + if (o.equals(objectToFind)) { + return true; + } + } + return false; + } + + public static T[] arrayAdd(T[] array, T objectToAdd) { + Class type; + if (array != null) { + type = array.getClass().getComponentType(); + } else if (objectToAdd != null) { + type = objectToAdd.getClass(); + } else { + throw new IllegalArgumentException("Arguments cannot both be null"); + } + final int arrayLength = Array.getLength(array); + T[] newArray = (T[]) Array.newInstance(array.getClass().getComponentType(), arrayLength + 1); + System.arraycopy(array, 0, newArray, 0, arrayLength); + newArray[newArray.length - 1] = objectToAdd; + + return newArray; + + } +} http://git-wip-us.apache.org/repos/asf/bval/blob/8dabb264/bval-core/src/main/java/org/apache/bval/util/PropertyAccess.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/util/PropertyAccess.java b/bval-core/src/main/java/org/apache/bval/util/PropertyAccess.java index 4a2061e..fe690ea 100644 --- a/bval-core/src/main/java/org/apache/bval/util/PropertyAccess.java +++ b/bval-core/src/main/java/org/apache/bval/util/PropertyAccess.java @@ -59,7 +59,7 @@ public class PropertyAccess extends AccessStrategy { final ClassLoader cl = Reflection.getClassLoader(PropertyAccess.class); boolean useBeanUtils; try { - Reflection.getClass(cl, BEANUTILS); + Reflection.toClass(BEANUTILS, cl); useBeanUtils = true; } catch (Exception e) { useBeanUtils = false; @@ -68,7 +68,7 @@ public class PropertyAccess extends AccessStrategy { if (useBeanUtils) { try { final Class beanUtilsPropertyAccess = - Reflection.getClass(cl, BEANUTILS_PROPERTY_ACCESS); + Reflection.toClass(BEANUTILS_PROPERTY_ACCESS, cl); ctor = Reflection.getDeclaredConstructor( beanUtilsPropertyAccess.asSubclass(PropertyAccess.class), http://git-wip-us.apache.org/repos/asf/bval/blob/8dabb264/bval-core/src/main/java/org/apache/bval/util/StringUtils.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/util/StringUtils.java b/bval-core/src/main/java/org/apache/bval/util/StringUtils.java new file mode 100644 index 0000000..3ac7ed9 --- /dev/null +++ b/bval-core/src/main/java/org/apache/bval/util/StringUtils.java @@ -0,0 +1,177 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.bval.util; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +public final class StringUtils { + private StringUtils() { + } + + /** + * Taken from commons-lang3. + *

+ *

Capitalizes a String changing the first character to title case as + * per {@link Character#toTitleCase(char)}. No other characters are changed.

+ *

+ *

For a word based algorithm, see {@link org.apache.commons.lang3.text.WordUtils#capitalize(String)}. + * A {@code null} input String returns {@code null}.

+ *

+ *

+     * StringUtils.capitalize(null)  = null
+     * StringUtils.capitalize("")    = ""
+     * StringUtils.capitalize("cat") = "Cat"
+     * StringUtils.capitalize("cAt") = "CAt"
+     * StringUtils.capitalize("'cat'") = "'cat'"
+     * 
+ * + * @param str the String to capitalize, may be null + * @return the capitalized String, {@code null} if null String input + * @see org.apache.commons.lang3.text.WordUtils#capitalize(String) + */ + public static String capitalize(final String str) { + int strLen; + if (str == null || (strLen = str.length()) == 0) { + return str; + } + + final char firstChar = str.charAt(0); + final char newChar = Character.toTitleCase(firstChar); + if (firstChar == newChar) { + // already capitalized + return str; + } + + char[] newChars = new char[strLen]; + newChars[0] = newChar; + str.getChars(1, strLen, newChars, 1); + return String.valueOf(newChars); + } + + /** + * Taken from commons-lang3. + *

Checks if a CharSequence is whitespace, empty ("") or null.

+ *

+ *

+     * StringUtils.isBlank(null)      = true
+     * StringUtils.isBlank("")        = true
+     * StringUtils.isBlank(" ")       = true
+     * StringUtils.isBlank("bob")     = false
+     * StringUtils.isBlank("  bob  ") = false
+     * 
+ * + * @param cs the CharSequence to check, may be null + * @return {@code true} if the CharSequence is null, empty or whitespace + */ + public static boolean isBlank(final CharSequence cs) { + int strLen; + if (cs == null || (strLen = cs.length()) == 0) { + return true; + } + for (int i = 0; i < strLen; i++) { + if (Character.isWhitespace(cs.charAt(i)) == false) { + return false; + } + } + return true; + } + + public static String join(Collection values, String joinToken) { + if (values == null) { + return null; + } + if (values.size() == 0) { + return ""; + } + if (values.size() == 1) { + return values.iterator().next().toString(); + } + if (joinToken == null) { + joinToken = "null"; // backward compat with commons-lang StringUtils... + } + + StringBuilder sb = new StringBuilder(values.size() * (16 + joinToken.length())); + Iterator it = values.iterator(); + sb.append(it.next()); + while (it.hasNext()) { + sb.append(joinToken).append(it.next()); + } + return sb.toString(); + } + + public static String joinArray(Object[] values, String joinToken) { + if (values == null) { + return null; + } + if (values.length == 0) { + return ""; + } + if (values.length == 1) { + return values[0].toString(); + } + if (joinToken == null) { + joinToken = "null"; // backward compat with commons-lang StringUtils... + } + + StringBuilder sb = new StringBuilder(values.length * (16 + joinToken.length())); + sb.append(values[0]); + for (int i = 1; i < values.length; i++) { + sb.append(joinToken).append(values[i]); + } + return sb.toString(); + } + + /** + *

Splits the provided text into an array, separator is whitespace. + */ + public static String[] split(String str) { + return split(str, null); + } + + /** + *

Splits the provided text into an array, separator is whitespace. + */ + public static String[] split(String str, Character token) { + if (str == null || str.length() == 0) { + return ObjectUtils.EMPTY_STRING_ARRAY; + } + + // split on token + List ret = new ArrayList(); + StringBuilder sb = new StringBuilder(str.length()); + for (int pos = 0; pos < str.length(); pos++) { + char c = str.charAt(pos); + if ((token == null && Character.isWhitespace(c)) || (token != null && token.equals(c))) { + if (sb.length() > 0) { + ret.add(sb.toString()); + sb.setLength(0); // reset the string + } + } + else { + sb.append(c); + } + } + if (sb.length() > 0) { + ret.add(sb.toString()); + } + return ret.toArray(new String[ret.size()]); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/bval/blob/8dabb264/bval-core/src/main/java/org/apache/bval/util/Validate.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/util/Validate.java b/bval-core/src/main/java/org/apache/bval/util/Validate.java new file mode 100644 index 0000000..12aca54 --- /dev/null +++ b/bval-core/src/main/java/org/apache/bval/util/Validate.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.bval.util; + + +/** + * Some used Validate from commons. + * + */ +public final class Validate { + private Validate() { + } + + public static T notNull(final T object) { + return notNull(object, "The validated object is null"); + } + + public static T notNull(final T object, final String message, final Object... values) { + if (object == null) { + throw new NullPointerException(String.format(message, values)); + } + return object; + } + + + public static void isTrue(final boolean expression, final String message, final Object... values) { + if (expression == false) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + +} http://git-wip-us.apache.org/repos/asf/bval/blob/8dabb264/bval-core/src/main/java/org/apache/bval/util/reflection/Reflection.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/util/reflection/Reflection.java b/bval-core/src/main/java/org/apache/bval/util/reflection/Reflection.java index e35f6df..674cf94 100644 --- a/bval-core/src/main/java/org/apache/bval/util/reflection/Reflection.java +++ b/bval-core/src/main/java/org/apache/bval/util/reflection/Reflection.java @@ -24,23 +24,72 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Member; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.util.HashMap; +import java.util.Map; -import org.apache.commons.lang3.ClassUtils; import org.apache.commons.weaver.privilizer.Privilizing; /** * Security-agnostic "blueprint" class for reflection-related operations. Intended for use by Apache BVal code. */ public class Reflection { + private static final Object[][] NATIVE_CODES = new Object[][]{ + {byte.class, "byte", "B"}, + {char.class, "char", "C"}, + {double.class, "double", "D"}, + {float.class, "float", "F"}, + {int.class, "int", "I"}, + {long.class, "long", "J"}, + {short.class, "short", "S"}, + {boolean.class, "boolean", "Z"}, + {void.class, "void", "V"} + }; + /** - * Get the named {@link Class} from the specified {@link ClassLoader}. - * @param classLoader - * @param className - * @return Class - * @throws Exception + * Maps primitive {@code Class}es to their corresponding wrapper {@code Class}. */ - public static Class getClass(final ClassLoader classLoader, final String className) throws Exception { - return ClassUtils.getClass(classLoader, className, true); + private static final Map, Class> PRIMITIVE_WRAPPER_MAP = new HashMap, Class>(); + static { + PRIMITIVE_WRAPPER_MAP.put(Boolean.TYPE, Boolean.class); + PRIMITIVE_WRAPPER_MAP.put(Byte.TYPE, Byte.class); + PRIMITIVE_WRAPPER_MAP.put(Character.TYPE, Character.class); + PRIMITIVE_WRAPPER_MAP.put(Short.TYPE, Short.class); + PRIMITIVE_WRAPPER_MAP.put(Integer.TYPE, Integer.class); + PRIMITIVE_WRAPPER_MAP.put(Long.TYPE, Long.class); + PRIMITIVE_WRAPPER_MAP.put(Double.TYPE, Double.class); + PRIMITIVE_WRAPPER_MAP.put(Float.TYPE, Float.class); + PRIMITIVE_WRAPPER_MAP.put(Void.TYPE, Void.TYPE); + } + + + + /** + *

Converts the specified primitive Class object to its corresponding + * wrapper Class object.

+ * + *

NOTE: From v2.2, this method handles {@code Void.TYPE}, + * returning {@code Void.TYPE}.

+ * + * @param cls the class to convert, may be null + * @return the wrapper class for {@code cls} or {@code cls} if + * {@code cls} is not a primitive. {@code null} if null input. + * @since 2.1 + */ + public static Class primitiveToWrapper(final Class cls) { + Class convertedClass = cls; + if (cls != null && cls.isPrimitive()) { + convertedClass = PRIMITIVE_WRAPPER_MAP.get(cls); + } + return convertedClass; + } + + public static Class wrapperToPrimitive(final Class cls) { + for (Map.Entry, Class> primitiveEntry : PRIMITIVE_WRAPPER_MAP.entrySet()) { + if (primitiveEntry.getValue().equals(cls)) { + return primitiveEntry.getKey(); + } + } + return null; } /** @@ -80,6 +129,79 @@ public class Reflection { return cl == null ? clazz.getClassLoader() : cl; } + public static Class toClass(String className) throws ClassNotFoundException + { + ClassLoader cl = getClassLoader(Reflection.class); + return toClass(className, cl); + } + + /** + * Return the class for the given string, correctly handling + * primitive types. If the given class loader is null, the context + * loader of the current thread will be used. + * + * @throws RuntimeException on load error + */ + public static Class toClass(String className, ClassLoader loader) throws ClassNotFoundException { + return toClass(className, false, loader); + } + + /** + * Return the class for the given string, correctly handling + * primitive types. If the given class loader is null, the context + * loader of the current thread will be used. + * + * @throws RuntimeException on load error + */ + public static Class toClass(String className, boolean resolve, ClassLoader loader) throws ClassNotFoundException { + if (className == null) { + throw new NullPointerException("className == null"); + } + + // array handling + int dims = 0; + while (className.endsWith("[]")) { + dims++; + className = className.substring(0, className.length() - 2); + } + + // check against primitive types + boolean primitive = false; + if (className.indexOf('.') == -1) { + for (int i = 0; !primitive && (i < NATIVE_CODES.length); i++) { + if (NATIVE_CODES[i][1].equals(className)) { + if (dims == 0) { + return (Class) NATIVE_CODES[i][0]; + } + className = (String) NATIVE_CODES[i][2]; + primitive = true; + } + } + } + + if (dims > 0) { + StringBuilder buf = new StringBuilder(className.length() + dims + 2); + for (int i = 0; i < dims; i++) { + buf.append('['); + } + if (!primitive) { + buf.append('L'); + } + buf.append(className); + if (!primitive) { + buf.append(';'); + } + className = buf.toString(); + } + + if (loader == null) { + loader = Thread.currentThread().getContextClassLoader(); + } + + return Class.forName(className, resolve, loader); + } + + /** * Convenient point for {@link Privilizing} {@link System#getProperty(String)}. * @param name @@ -114,7 +236,6 @@ public class Reflection { /** * Get the declared constructor from {@code clazz}. - * @param T generic type * @param clazz * @param parameters * @return {@link Constructor} or {@code null}