http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/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 deleted file mode 100644 index bd7464b..0000000 --- a/bval-core/src/main/java/org/apache/bval/model/FeaturesCapable.java +++ /dev/null @@ -1,186 +0,0 @@ -/* - * 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.model; - -import java.io.Serializable; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - -/** - * Description: abstract superclass of meta objects that support a map of - * features.<br/> - */ -public abstract class FeaturesCapable implements Serializable { - private static final long serialVersionUID = -4045110242904814218L; - - private ConcurrentMap<String, Object> features = createFeaturesMap(); - - /** key = validation id, value = the validation */ - private Validation[] validations = new Validation[0]; - - /** - * Create a new FeaturesCapable instance. - */ - public FeaturesCapable() { - super(); - } - - /** - * Get the (live) map of features. - * - * @return Map<String, Object> - */ - public Map<String, Object> getFeatures() { - return features; - } - - /** - * Get the specified feature. - * - * @param <T> - * @param key - * @return T - */ - public <T> T getFeature(String key) { - return getFeature(key, (T) null); - } - - /** - * Get the specified feature, returning <code>defaultValue</code> if - * undeclared. - * - * @param <T> - * @param key - * @param defaultValue - * @return T - */ - @SuppressWarnings("unchecked") - public <T> T getFeature(String key, T defaultValue) { - final T value = (T) features.get(key); - if (value == null) { - return defaultValue; - } - return value; - } - - /** - * Convenience method to set a particular feature value. - * - * @param key - * @param value - */ - public <T> void putFeature(final String key, final T value) { - features.put(key, value); - } - - public <T> T initFeature(final String key, final T value) { - @SuppressWarnings("unchecked") - final T faster = (T) features.putIfAbsent(key, value); - return faster == null ? value : faster; - } - - /** - * Create a deep copy (copy receiver and copy properties). - * - * @param <T> - * @return new T instance - */ - public <T extends FeaturesCapable> T copy() { - try { - @SuppressWarnings("unchecked") - final T self = (T) clone(); - copyInto(self); - return self; - } catch (CloneNotSupportedException e) { - throw new IllegalStateException("cannot clone() " + this, e); - } - } - - /** - * Copy this {@link FeaturesCapable} into another {@link FeaturesCapable} - * instance. - * - * @param target - */ - protected void copyInto(FeaturesCapable target) { - target.features = target.createFeaturesMap(); - target.features.putAll(features); - target.validations = validations != null ? validations.clone() : null; - } - - /** - * Get any validations set for this {@link FeaturesCapable}. - * - * @return Validation array - */ - public Validation[] getValidations() { - return validations != null ? validations.clone() : null; - } - - /** - * Set the validations for this {@link FeaturesCapable}. - * - * @param validations - */ - public void setValidations(Validation[] validations) { - this.validations = validations != null ? validations.clone() : null; - } - - /** - * Add a validation to this {@link FeaturesCapable}. - * - * @param validation - * to add - */ - public void addValidation(Validation validation) { - if (this.validations == null) { - this.validations = new Validation[] { validation }; - } else { - Validation[] newValidations = new Validation[this.validations.length + 1]; - System.arraycopy(this.validations, 0, newValidations, 0, this.validations.length); - newValidations[validations.length] = validation; - this.validations = newValidations; - } - } - - /** - * Search for an equivalent validation among those configured. - * - * @param aValidation - * @return true if found - */ - public boolean hasValidation(Validation aValidation) { - if (validations == null) { - return false; - } - for (Validation validation : validations) { - if (validation.equals(aValidation)) { - return true; - } - } - return false; - } - - /** - * Create a features map for this {@link FeaturesCapable} object. - * @return ConcurrentMap - */ - protected ConcurrentMap<String, Object> createFeaturesMap() { - return new ConcurrentHashMap<String, Object>(); - } -}
http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/bval-core/src/main/java/org/apache/bval/model/Meta.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/model/Meta.java b/bval-core/src/main/java/org/apache/bval/model/Meta.java deleted file mode 100644 index e539b1d..0000000 --- a/bval-core/src/main/java/org/apache/bval/model/Meta.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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.model; - -public abstract class Meta extends FeaturesCapable { - private static final long serialVersionUID = 1L; - - protected MetaBean parentMetaBean; - - public MetaBean getParentMetaBean() { - return parentMetaBean; - } -} http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/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 deleted file mode 100755 index e2ab2f9..0000000 --- a/bval-core/src/main/java/org/apache/bval/model/MetaAnnotated.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.model; - -import java.lang.annotation.Annotation; -import java.util.HashSet; -import java.util.Set; - -import org.apache.bval.util.ObjectUtils; - -public abstract class MetaAnnotated extends Meta { - private static final long serialVersionUID = 1L; - - private Set<Annotation> annotations = new HashSet<Annotation>(); - private Annotation[] annArray = null; - - public Annotation[] getAnnotations() { - 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.add(annotation); - annArray = null; - } - -} http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/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 deleted file mode 100644 index 9738f23..0000000 --- a/bval-core/src/main/java/org/apache/bval/model/MetaBean.java +++ /dev/null @@ -1,378 +0,0 @@ -/* - * 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.model; - -import java.beans.Introspector; -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.Comparator; -import java.util.HashMap; -import java.util.Map; -import java.util.TreeMap; - -import org.apache.bval.util.reflection.Reflection; -import org.apache.commons.weaver.privilizer.Privilizing; -import org.apache.commons.weaver.privilizer.Privilizing.CallTo; - -/** - * Description: the meta description of a bean or class. the class/bean itself can have a map of features and an array - * of metaproperties.<br/> - * - * @see MetaProperty - */ -@Privilizing(@CallTo(Reflection.class)) -public class MetaBean extends FeaturesCapable implements Cloneable, Features.Bean { - private static final long serialVersionUID = 2L; - - private String id; - private String name; - private Class<?> beanClass; - - private Map<String, MetaProperty> properties = null; - private Map<Method, MetaMethod> methods = null; - private Map<Constructor<?>, MetaConstructor> constructors = null; - - /** - * Get the id. - * - * @return String - */ - public String getId() { - return id; - } - - /** - * Set the id. - * - * @param id - * the String to set - */ - public void setId(String id) { - this.id = id; - } - - /** - * Get the name. - * - * @return String - */ - public String getName() { - return name; - } - - /** - * Set the name. - * - * @param name - * the String to set - */ - public void setName(String name) { - this.name = name; - } - - /** - * Get the beanClass. - * - * @return Class - */ - public Class<?> getBeanClass() { - return beanClass; - } - - /** - * Set the beanClass. - * - * @param beanClass - * the Class<?> to set - */ - public void setBeanClass(Class<?> beanClass) { - this.beanClass = beanClass; - if (beanClass != null) { - // order of fields to ensure correct failling order - final Map<String, MetaProperty> oldProperties = properties; - final Map<Method, MetaMethod> oldMethods = methods; - final Map<Constructor<?>, MetaConstructor> oldConstructors = constructors; - - properties = new TreeMap<String, MetaProperty>(new FieldComparator(beanClass)); - if (oldProperties != null) { - properties.putAll(oldProperties); - } - methods = new TreeMap<Method, MetaMethod>(new MethodComparator(beanClass)); - if (oldMethods != null) { - methods.putAll(oldMethods); - } - constructors = new TreeMap<Constructor<?>, MetaConstructor>(new ConstructorComparator(beanClass)); - if (oldConstructors != null) { - constructors.putAll(oldConstructors); - } - } - } - - /** - * Get the properties. - * - * @return MetaProperty[] - */ - public MetaProperty[] getProperties() { - if (properties == null) { - return new MetaProperty[0]; - } - return properties.values().toArray(new MetaProperty[this.properties.size()]); - } - - public MetaMethod[] getMethods() { - if (methods == null) { - return new MetaMethod[0]; - } - return methods.values().toArray(new MetaMethod[this.methods.size()]); - } - - public void addMethod(final Method method, final MetaMethod meta) { - if (methods == null) { - methods = new HashMap<Method, MetaMethod>(); - } - methods.put(method, meta); - } - - public void addConstructor(final Constructor<?> constructor, final MetaConstructor meta) { - if (constructors == null) { - constructors = new HashMap<Constructor<?>, MetaConstructor>(); - } - constructors.put(constructor, meta); - } - - /** - * Set the properties. - * - * @param properties - * the MetaProperty[] to set - */ - public void setProperties(MetaProperty[] properties) { - this.properties = new HashMap<String, MetaProperty>(); - for (final MetaProperty property : properties) { - this.properties.put(property.getName(), property); - } - } - - /** - * Get the specified {@link MetaProperty}. - * - * @param name property name - * @return MetaProperty found or <code>null</code> - */ - public MetaProperty getProperty(String name) { - if (properties == null) { - return null; - } - return this.properties.get(name); - } - - /** - * Learn whether any known property is a relationship. - * - * @see MetaProperty#isRelationship() - * @return true when at least one of the properties is a relationship - */ - public boolean hasRelationships() { - if (properties == null) { - return false; - } - for (MetaProperty property : this.properties.values()) { - if (property.isRelationship()) { - return true; - } - } - return false; - } - - /** - * bidirectional - set the relationship between a MetaProperty and its parentMetaBean - * - * @param name property name - * @param property - * if <code>null</code>, remove - */ - public void putProperty(String name, MetaProperty property) { - if (properties == null) { - properties = new HashMap<String, MetaProperty>(); - } - if (property == null) { - this.properties.remove(name); - } else { - property.setParentMetaBean(this); - this.properties.put(name, property); - } - } - - /** - * {@inheritDoc} - */ - @Override - public String toString() { - return "MetaBean{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", beanClass=" + beanClass + '}'; - } - - /** - * {@inheritDoc} - */ - @Override - protected void copyInto(FeaturesCapable target) { - super.copyInto(target); - final MetaBean copy = (MetaBean) target; - if (properties != null) { - copy.properties = new TreeMap<String, MetaProperty>(); - for (Map.Entry<String, MetaProperty> entry : properties.entrySet()) { - copy.properties.put(entry.getKey(), (MetaProperty) entry.getValue().copy()); - } - } - } - - /** - * <p> - * If this {@link MetaBean} is compatible with <code>bean</code>, return <code>this</code>, else <code>null</code>. - * </p> - * <p> - * Compatibility is satisfied in one of the following ways: - * <ul> - * <li><code>bean</code> is null</li> - * <li><code>bean</code> is an instance of our <code>beanClass</code></li> - * <li><code>bean</code> <em>is</em> our <code>beanClass</code> itself</li> - * </ul> - * </p> - * - * @param bean instance - * @return <code>this</code> or <code>null</code> - */ - public MetaBean resolveMetaBean(Object bean) { - return bean == null || bean == beanClass || beanClass.isInstance(bean) ? this : null; - } - - public MetaMethod getMethod(final Method method) { - return methods == null ? null : methods.get(method); - } - - public MetaConstructor getConstructor(final Constructor<?> constructor) { - return constructors == null ? null : constructors.get(constructor); - } - - protected static class FieldComparator implements Comparator<String> { - private final Map<String, Integer> fields = new HashMap<String, Integer>(); - - protected FieldComparator(final Class<?> beanClass) { - int i = 0; - Class<?> clazz = beanClass; - while (clazz != null && clazz != Object.class) { - for (final Field f : Reflection.getDeclaredFields(clazz)) { - final String name = f.getName(); - if (!fields.containsKey(name)) { - fields.put(name, Integer.valueOf(++i)); - } - } - for (final Method m : clazz.getDeclaredMethods()) { - final String name = getPropertyName(m); - if (name != null && !name.isEmpty()) { - if (!fields.containsKey(name)) { - fields.put(name, Integer.valueOf(++i)); - } - } - } - clazz = clazz.getSuperclass(); - } - } - - private String getPropertyName(Method potentialAccessor) { - if (potentialAccessor.getParameterTypes().length == 0) { - final String name = potentialAccessor.getName(); - if (Boolean.TYPE.equals(potentialAccessor.getReturnType()) - && potentialAccessor.getName().startsWith("is")) { - return Introspector.decapitalize(name.substring(2)); - } - if (!Void.TYPE.equals(potentialAccessor.getReturnType()) - && potentialAccessor.getName().startsWith("get")) { - return Introspector.decapitalize(name.substring(3)); - } - } - return null; - } - - @Override - public int compare(final String o1, final String o2) { - final Integer i1 = fields.get(o1); - final Integer i2 = fields.get(o2); - if (i1 == null) { - 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 - // 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; - } - if (i2 == null) { - return 1; - } - return i1.intValue() - i2.intValue(); - } - - } - - protected static class MethodComparator implements Comparator<Method> { - private final Map<Method, Integer> methods = new HashMap<Method, Integer>(); - - protected MethodComparator(final Class<?> beanClass) { - Class<?> clazz = beanClass; - while (clazz != null && clazz != Object.class) { - for (final Method m : Reflection.getDeclaredMethods(clazz)) { - methods.put(m, Arrays.hashCode(m.getParameterTypes())); - } - clazz = clazz.getSuperclass(); - } - } - - @Override - public int compare(final Method o1, final Method o2) { - if (o1 == o2) { - return 0; - } - - final int i = o1.getName().compareTo(o2.getName()); - return i == 0 ? methods.get(o1) - methods.get(o2) : i; - } - } - - protected static class ConstructorComparator implements Comparator<Constructor<?>> { - private final Map<Constructor<?>, Integer> constructors = new HashMap<Constructor<?>, Integer>(); - - protected ConstructorComparator(final Class<?> beanClass) { - for (final Constructor<?> c : Reflection.getDeclaredConstructors(beanClass)) { - constructors.put(c, Arrays.hashCode(c.getParameterTypes())); - } - } - - @Override - public int compare(final Constructor<?> o1, final Constructor<?> o2) { - if (o1 == o2) { - return 0; - } - - final int i = o1.getName().compareTo(o2.getName()); - return i == 0 ? constructors.get(o1) - constructors.get(o2) : i; - } - } -} http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/bval-core/src/main/java/org/apache/bval/model/MetaConstructor.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/model/MetaConstructor.java b/bval-core/src/main/java/org/apache/bval/model/MetaConstructor.java deleted file mode 100644 index 4a6436b..0000000 --- a/bval-core/src/main/java/org/apache/bval/model/MetaConstructor.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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.model; - -import java.lang.reflect.Constructor; - -public class MetaConstructor extends MetaInvocable { - private static final long serialVersionUID = 1L; - - private final Constructor<?> constructor; - - public MetaConstructor(final MetaBean metabean, final Constructor<?> constructor) { - this.parentMetaBean = metabean; - this.constructor = constructor; - } - - public Constructor<?> getConstructor() { - return constructor; - } -} http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/bval-core/src/main/java/org/apache/bval/model/MetaInvocable.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/model/MetaInvocable.java b/bval-core/src/main/java/org/apache/bval/model/MetaInvocable.java deleted file mode 100644 index b5fd8ab..0000000 --- a/bval-core/src/main/java/org/apache/bval/model/MetaInvocable.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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.model; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -public abstract class MetaInvocable extends MetaAnnotated { - private static final long serialVersionUID = 1L; - - private Map<Integer, MetaParameter> parameters = new HashMap<Integer, MetaParameter>(); - - public Collection<MetaParameter> getParameters() { - return new ArrayList<MetaParameter>(parameters.values()); - } - - public void addParameter(final int idx, final MetaParameter param) { - parameters.put(idx, param); - } - - public MetaParameter getParameter(final Integer index) { - return parameters.get(index); - } -} http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/bval-core/src/main/java/org/apache/bval/model/MetaMethod.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/model/MetaMethod.java b/bval-core/src/main/java/org/apache/bval/model/MetaMethod.java deleted file mode 100644 index 952dc5c..0000000 --- a/bval-core/src/main/java/org/apache/bval/model/MetaMethod.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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.model; - -import java.lang.reflect.Method; - -public class MetaMethod extends MetaInvocable { - private static final long serialVersionUID = 1L; - - private final Method method; - - public MetaMethod(final MetaBean parent, final Method method) { - this.parentMetaBean = parent; - this.method = method; - } - - public Method getMethod() { - return method; - } -} http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/bval-core/src/main/java/org/apache/bval/model/MetaParameter.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/model/MetaParameter.java b/bval-core/src/main/java/org/apache/bval/model/MetaParameter.java deleted file mode 100644 index f46ef16..0000000 --- a/bval-core/src/main/java/org/apache/bval/model/MetaParameter.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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.model; - -public class MetaParameter extends MetaAnnotated { - private static final long serialVersionUID = 1L; - - private final MetaInvocable invocable; - private final Integer index; - - public MetaParameter(final MetaInvocable metaMethod, final Integer index) { - this.invocable = metaMethod; - this.index = index; - } - - public MetaInvocable getMethod() { - return invocable; - } - - public Integer getIndex() { - return index; - } - - @Override - public MetaBean getParentMetaBean() { - return invocable.getParentMetaBean(); - } -} http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/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 deleted file mode 100644 index 690416f..0000000 --- a/bval-core/src/main/java/org/apache/bval/model/MetaProperty.java +++ /dev/null @@ -1,164 +0,0 @@ -/* - * 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.model; - -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.<br/> - * - * @see Validation - * @see MetaBean - */ -public class MetaProperty extends Meta implements Cloneable, Features.Property { - private static final long serialVersionUID = 1L; - - private String name; - - private Type type; - private MetaBean metaBean; - - /** - * Create a new MetaProperty instance. - */ - public MetaProperty() { - } - - /** - * Get the metabean of the target bean (mainly for relationships). - * @return MetaBean (may be null). - */ - public MetaBean getMetaBean() { - return metaBean; - } - - /** - * Set the MetaBean of this {@link MetaProperty}. - * @param metaBean to set - */ - public void setMetaBean(MetaBean metaBean) { - this.metaBean = metaBean; - } - - /** - * Set the metabean that owns this property (usually called by MetaBean.putProperty()) - * @param parentMetaBean - */ - void setParentMetaBean(MetaBean parentMetaBean) { - this.parentMetaBean = parentMetaBean; - } - - /** - * Learn whether this property is considered a relationship. - * @return <code>true</code> if it has a MetaBean of its own - */ - public boolean isRelationship() { - return metaBean != null; - } - - /** - * Set the type of this property. - * @param type to set - */ - public void setType(Type type) { - this.type = type; - } - - /** - * Get the type of this property. - * @return - */ - public Type getType() { - return type; - } - - /** - * Resolve the type of this property to a class. - * @return Class, <code>null</code> if cannot be determined - */ - public Class<?> getTypeClass() { - Type targetType = type instanceof DynaType ? ((DynaType) type).getRawType() : type; - if (targetType == null) { - return null; - } - Type assigningType = getParentMetaBean() == null ? null : getParentMetaBean().getBeanClass(); - return TypeUtils.getRawType(targetType, assigningType); - } - - /** - * Get the name of this property. - * @return String - */ - public String getName() { - return name; - } - - /** - * Learn whether this property is considered mandatory. - * @return <code>true</code> if the <code>MANDATORY</code> feature is set to <code>true</code>. - * @see {@link Features.Property#MANDATORY} - */ - public boolean isMandatory() { - return getFeature(MANDATORY, Boolean.FALSE).booleanValue(); - } - - /** - * Set this property as being mandatory (or not). - * @param mandatory - * @see {@link Features.Property#MANDATORY} - */ - public void setMandatory(boolean mandatory) { - putFeature(MANDATORY, Boolean.valueOf(mandatory)); - } - - /** - * Get javascript validations of this property. - * @return String[] - * @deprecated - */ - @Deprecated // remove this method? - public String[] getJavaScriptValidations() { - return getFeature(JAVASCRIPT_VALIDATION_FUNCTIONS); - } - - /** - * Set the name of this property. - * @param name to set - */ - public void setName(String name) { - this.name = name; - } - - /** - * {@inheritDoc} - */ - @Override - public MetaProperty clone() throws CloneNotSupportedException { - return (MetaProperty) super.clone(); - } - - /** - * {@inheritDoc} - */ - @Override - public String toString() { - return "MetaProperty{" + "name='" + name + '\'' + ", type=" + type + '}'; - } -} http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/bval-core/src/main/java/org/apache/bval/model/Validation.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/model/Validation.java b/bval-core/src/main/java/org/apache/bval/model/Validation.java deleted file mode 100644 index a81e731..0000000 --- a/bval-core/src/main/java/org/apache/bval/model/Validation.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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.model; - -/** - * Description: Interface for a single validation <br/> - */ -public interface Validation { - /** - * Perform a single validation routine. - * Validate the object or property according to the current ValidationContext. - * - * @param context - to access the property, value, constraints - */ - <T extends ValidationListener> void validate(ValidationContext<T> context); -} http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/bval-core/src/main/java/org/apache/bval/model/ValidationContext.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/model/ValidationContext.java b/bval-core/src/main/java/org/apache/bval/model/ValidationContext.java deleted file mode 100644 index c26d2b7..0000000 --- a/bval-core/src/main/java/org/apache/bval/model/ValidationContext.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * 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.model; - -import org.apache.bval.util.AccessStrategy; - -/** - * Description: Interface of the context that holds all state information - * during the validation process<br/> - */ -public interface ValidationContext<T extends ValidationListener> { - /** - * Get the property value. - * @return {@link Object} - */ - Object getPropertyValue(); - - /** - * Get the value by using the given access strategy. - * @param access - * @return {@link Object} - */ - Object getPropertyValue(AccessStrategy access); - - /** - * Get the property name. - * @return {@link String} - */ - String getPropertyName(); - - /** - * Get the {@link ValidationListener}. - * @return T - */ - T getListener(); - - /** - * Get the bean. - * @return {@link Object} - */ - Object getBean(); - - /** - * Get the model meta-bean. - * @return {@link MetaBean} - */ - MetaBean getMetaBean(); - - /** - * Set the model meta-bean. - * @param metaBean - */ - void setMetaBean(MetaBean metaBean); - - /** - * Get the model meta-property. - * @return {@link MetaProperty} - */ - MetaProperty getMetaProperty(); - - /** - * Set the bean. - * @param bean - */ - void setBean(Object bean); - - /** - * Avoid recursion by recording the current state of this context as having been validated. - * <p/> - * - * @return true when this state had not already been recorded - */ - boolean collectValidated(); - - /** - * Set the current bean/metabean. - * @param aBean - * @param aMetaBean - */ - void setBean(Object aBean, MetaBean aMetaBean); - - /** - * Set the current meta-property. - * @param metaProperty - */ - void setMetaProperty(MetaProperty metaProperty); - - /** - * Step deeper into association at 'prop' - * @param prop - * @param access - */ - void moveDown(MetaProperty prop, AccessStrategy access); - - void moveDown(String prop); - - /** - * Step out from a validation of associated objects. - * @param bean - * @param metaBean - */ - void moveUp(Object bean, MetaBean metaBean); - - /** - * Set the index of the object currently validated into the context. - * used to create the propertyPath with [index] information for collections. - * @param index - */ - void setCurrentIndex(Integer index); - - /** - * set the key of the object in a map currently validated into the context. - * used to create the propertyPath with [key] information for maps. - * @param key - */ - void setCurrentKey(Object key); - - /** - * Get the current access strategy. - * @return {@link AccessStrategy} - */ - AccessStrategy getAccess(); -} http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/bval-core/src/main/java/org/apache/bval/model/ValidationListener.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/model/ValidationListener.java b/bval-core/src/main/java/org/apache/bval/model/ValidationListener.java deleted file mode 100644 index 76e4fab..0000000 --- a/bval-core/src/main/java/org/apache/bval/model/ValidationListener.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * 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.model; - -import java.io.Serializable; - -/** - * Description: The interface to collect errors found during validation<br/> - */ -public interface ValidationListener { - /** - * Simple API to add an error reason during validation. - * Error notification added from a {@link org.apache.bval.model.Validation} with context information - * taken from the given {@link org.apache.bval.model.ValidationContext}. - * - * @param reason a constant describing the reason. This is normally the key of the - * feature that was violated in the object 'owner' for property 'propertyName' - * @param context - contains - * bean = the object that contains the error (owner) - * propertyName = the Name of the attribute that caused the error - */ - <T extends ValidationListener> void addError(String reason, ValidationContext<T> context); - - /** Alternative method to add a fully initialized {@link ValidationListener.Error} object. */ - <T extends ValidationListener> void addError(Error error, ValidationContext<T> context); - - /** - * An object holding a single validation constraint violation - * found during the validation process. - */ - public class Error implements Serializable { - private static final long serialVersionUID = 1L; - - /** Reason */ - final String reason; - /** Owner */ - final Object owner; - /** Property name*/ - final String propertyName; - - /** - * Create a new Error instance. - * @param aReason - * @param aOwner - * @param aPropertyName - */ - public Error(String aReason, Object aOwner, String aPropertyName) { - this.reason = aReason; - this.owner = aOwner; - this.propertyName = aPropertyName; - } - - /** - * Get the reason. - * @return String - */ - public String getReason() { - return reason; - } - - /** - * Get the owner. - * @return Object - */ - public Object getOwner() { - return owner; - } - - /** - * Get the propertyName. - * @return String - */ - public String getPropertyName() { - return propertyName; - } - - /** - * {@inheritDoc} - */ - @Override - public String toString() { - return "Error{" + "reason='" + reason + '\'' + ", propertyName='" + propertyName + '\'' + '}'; - } - } -} http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/bval-core/src/main/java/org/apache/bval/routines/EMailValidationUtils.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/routines/EMailValidationUtils.java b/bval-core/src/main/java/org/apache/bval/routines/EMailValidationUtils.java deleted file mode 100644 index 0835bae..0000000 --- a/bval-core/src/main/java/org/apache/bval/routines/EMailValidationUtils.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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.routines; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * Description: holds the regexp to validate an email address<br> - * User: roman.stumm<br> - * Date: 17.06.2010<br> - * Time: 10:40:59<br> - */ -public class EMailValidationUtils { - private static String ATOM = "[^\\x00-\\x1F\\(\\)\\<\\>\\@\\,\\;\\:\\\\\\\"\\.\\[\\]\\s]"; - private static String DOMAIN = "(" + ATOM + "+(\\." + ATOM + "+)*"; - private static String IP_DOMAIN = "\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\]"; - public static final Pattern DEFAULT_EMAIL_PATTERN; - - static { - DEFAULT_EMAIL_PATTERN = Pattern.compile("^" + ATOM + "+(\\." + ATOM + "+)*@" + DOMAIN + "|" + IP_DOMAIN + ")$", - Pattern.CASE_INSENSITIVE); - } - - /** - * Learn whether a given object is a valid email address. - * - * @param value - * to check - * @return <code>true</code> if the validation passes - */ - public static boolean isValid(Object value) { - return isValid(value, DEFAULT_EMAIL_PATTERN); - } - - /** - * Learn whether a particular value matches a given pattern per - * {@link Matcher#matches()}. - * - * @param value - * @param aPattern - * @return <code>true</code> if <code>value</code> was a <code>String</code> - * matching <code>aPattern</code> - */ - // TODO it would seem to make sense to move or reduce the visibility of this - // method as it is more general than email. - public static boolean isValid(Object value, Pattern aPattern) { - if (value == null) { - return true; - } - if (!(value instanceof CharSequence)) { - return false; - } - CharSequence seq = (CharSequence) value; - if (seq.length() == 0) { - return true; - } - return aPattern.matcher(seq).matches(); - } - -} http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/bval-core/src/main/java/org/apache/bval/util/AccessStrategy.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/util/AccessStrategy.java b/bval-core/src/main/java/org/apache/bval/util/AccessStrategy.java deleted file mode 100644 index 5118a26..0000000 --- a/bval-core/src/main/java/org/apache/bval/util/AccessStrategy.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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.ElementType; -import java.lang.reflect.Type; - -/** - * Description: abstract class to encapsulate different strategies - * to get the value of a Property. This class is designed such that - * subclasses are intended to know internally to which property they refer, - * with only the particular target instance being externally required - * to calculate the property's value. One intent of this design is - * that the notion of the very definition of a property is abstracted - * along with the mechanism for accessing that property.<br/> - */ -public abstract class AccessStrategy { - /** - * Get the value from the given instance. - * @param instance - * @return the value - * @throws IllegalArgumentException in case of an error - */ - public abstract Object get(Object instance); - - /** - * Get the Java program {@link ElementType} used by this {@link AccessStrategy} - * to determine property values. - * @return ElementType - */ - public abstract ElementType getElementType(); - - /** - * Get the type of the property - * @return Type - */ - public abstract Type getJavaType(); - - /** - * Get a name representative of this property. - * @return String - */ - public abstract String getPropertyName(); - -} http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/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 deleted file mode 100644 index 13d1fa3..0000000 --- a/bval-core/src/main/java/org/apache/bval/util/BValVersion.java +++ /dev/null @@ -1,195 +0,0 @@ -/* - * 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.io.IOException; -import java.io.InputStream; -import java.util.Properties; -import java.util.StringTokenizer; - -import org.apache.bval.util.reflection.Reflection; -import org.apache.commons.weaver.privilizer.Privilizing; -import org.apache.commons.weaver.privilizer.Privilizing.CallTo; - -/** - * This class contains version information for BVal. - * It uses Ant's filter tokens to convert the template into a java - * file with current information. - */ -@Privilizing(@CallTo(Reflection.class)) -public class BValVersion { - - /** Project name */ - public static final String PROJECT_NAME = "Apache BVal"; - /** Unique id of the current project/version/revision */ - public static final String PROJECT_ID; - /** Version number */ - public static final String VERSION_NUMBER; - /** Major release number */ - public static final int MAJOR_RELEASE; - /** Minor release number */ - public static final int MINOR_RELEASE; - /** Patch/point release number */ - public static final int PATCH_RELEASE; - /** Release status */ - public static final String RELEASE_STATUS; - /** Version control revision number */ - public static final String REVISION_NUMBER; - - static { - Properties revisionProps = new Properties(); - try (InputStream in = BValVersion.class.getResourceAsStream("/META-INF/org.apache.bval.revision.properties")) { - if (in != null) { - revisionProps.load(in); - } - } catch (IOException ioe) { - } - - String vers = revisionProps.getProperty("project.version"); - if (vers == null || "".equals(vers.trim())) { - vers = "0.0.0"; - } - VERSION_NUMBER = vers; - - StringTokenizer tok = new StringTokenizer(VERSION_NUMBER, ".-"); - int major, minor, patch; - try { - major = tok.hasMoreTokens() ? Integer.parseInt(tok.nextToken()) : 0; - } catch (Exception e) { - major = 0; - } - - try { - minor = tok.hasMoreTokens() ? Integer.parseInt(tok.nextToken()) : 0; - } catch (Exception e) { - minor = 0; - } - - try { - patch = tok.hasMoreTokens() ? Integer.parseInt(tok.nextToken()) : 0; - } catch (Exception e) { - patch = 0; - } - - String revision = revisionProps.getProperty("svn.revision"); - if (StringUtils.isBlank(revision)) { - revision = "unknown"; - } else { - tok = new StringTokenizer(revision, ":"); - String strTok = null; - while (tok.hasMoreTokens()) { - try { - strTok = tok.nextToken(); - } catch (Exception e) { - } - } - if (strTok != null) { - revision = strTok; - } - } - - MAJOR_RELEASE = major; - MINOR_RELEASE = minor; - PATCH_RELEASE = patch; - RELEASE_STATUS = tok.hasMoreTokens() ? tok.nextToken("!") : ""; - REVISION_NUMBER = revision; - PROJECT_ID = PROJECT_NAME + " " + VERSION_NUMBER + "-r" + REVISION_NUMBER; - } - - /** - * Get the project version number. - * @return String - */ - public static String getVersion() { - return VERSION_NUMBER; - } - - /** - * Get the version control revision number. - * @return String - */ - public static String getRevision() { - return REVISION_NUMBER; - } - - /** - * Get the project name. - * @return String - */ - public static String getName() { - return PROJECT_NAME; - } - - /** - * Get the fully-qualified project id. - * @return String - */ - public static String getID() { - return PROJECT_ID; - } - - /** - * Main method of this class that prints the {@link #toString()} to <code>System.out</code>. - * @param args ignored - */ - public static void main(String[] args) { - System.out.println(new BValVersion().toString()); - } - - /** - * {@inheritDoc} - */ - @Override - public String toString() { - final StringBuilder buf = new StringBuilder(80 * 40); - appendBanner(buf); - buf.append("\n"); - - appendProperty("os.name", buf).append("\n"); - appendProperty("os.version", buf).append("\n"); - appendProperty("os.arch", buf).append("\n\n"); - - appendProperty("java.version", buf).append("\n"); - appendProperty("java.vendor", buf).append("\n\n"); - - buf.append("java.class.path:\n"); - final StringTokenizer tok = new StringTokenizer(Reflection.getProperty("java.class.path")); - while (tok.hasMoreTokens()) { - buf.append("\t").append(tok.nextToken()); - buf.append("\n"); - } - buf.append("\n"); - - appendProperty("user.dir", buf).append("\n"); - return buf.toString(); - } - - private void appendBanner(StringBuilder buf) { - buf.append("Project").append(": ").append(getName()); - buf.append("\n"); - buf.append("Version").append(": ").append(getVersion()); - buf.append("\n"); - buf.append("Revision").append(": ").append(getRevision()); - buf.append("\n"); - } - - private StringBuilder appendProperty(String prop, StringBuilder buf) { - return buf.append(prop).append(": ").append(Reflection.getProperty(prop)); - } -} http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/bval-core/src/main/java/org/apache/bval/util/BeanUtilsPropertyAccess.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/util/BeanUtilsPropertyAccess.java b/bval-core/src/main/java/org/apache/bval/util/BeanUtilsPropertyAccess.java deleted file mode 100755 index c189e72..0000000 --- a/bval-core/src/main/java/org/apache/bval/util/BeanUtilsPropertyAccess.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.reflect.InvocationTargetException; - -import org.apache.commons.beanutils.DynaBean; -import org.apache.commons.beanutils.PropertyUtils; - -/** - * Commons BeanUtils-specific {@link PropertyAccess} subclass. - * - * @since 1.1.2 - */ -class BeanUtilsPropertyAccess extends PropertyAccess { - - public BeanUtilsPropertyAccess(Class<?> clazz, String propertyName) { - super(clazz, propertyName); - } - - @Override - protected Object getPublicProperty(Object bean) - throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { - if (bean instanceof DynaBean) { - return PropertyUtils.getSimpleProperty(bean, getPropertyName()); - } - return super.getPublicProperty(bean); - } - -} http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/bval-core/src/main/java/org/apache/bval/util/Exceptions.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/util/Exceptions.java b/bval-core/src/main/java/org/apache/bval/util/Exceptions.java deleted file mode 100644 index 9487cde..0000000 --- a/bval-core/src/main/java/org/apache/bval/util/Exceptions.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * 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.function.BiFunction; -import java.util.function.Function; -import java.util.function.Supplier; -import java.util.stream.Stream; - -/** - * Utility class for the creation and throwing of Exceptions. - */ -public class Exceptions { - - public static <E extends Exception> E create(Function<? super String, ? extends E> fn, String format, - Object... args) { - return create(fn, () -> String.format(format, args)); - } - - public static <E extends Exception, C extends Throwable> E create( - BiFunction<? super String, ? super C, ? extends E> fn, C cause, String format, Object... args) { - return create(fn, cause, () -> String.format(format, args)); - } - - public static <E extends Exception> E create(Function<? super String, ? extends E> fn, Supplier<String> message) { - return elideStackTrace(fn.apply(message.get())); - } - - public static <E extends Exception, C extends Throwable> E create( - BiFunction<? super String, ? super C, ? extends E> fn, C cause, Supplier<String> message) { - return elideStackTrace(fn.apply(message.get(), cause)); - } - - public static <E extends Exception, R> R raise(Function<? super String, ? extends E> fn, String format, - Object... args) throws E { - throw create(fn, format, args); - } - - public static <E extends Exception> void raiseIf(boolean condition, Function<? super String, ? extends E> fn, - String format, Object... args) throws E { - if (condition) { - raise(fn, format, args); - } - } - - public static <E extends Exception> void raiseUnless(boolean condition, Function<? super String, ? extends E> fn, - String format, Object... args) throws E { - raiseIf(!condition, fn, format, args); - } - - public static <E extends Exception, R> R raise(Function<? super String, ? extends E> fn, Supplier<String> message) - throws E { - throw create(fn, message); - } - - public static <E extends Exception> void raiseIf(boolean condition, Function<? super String, ? extends E> fn, - Supplier<String> message) throws E { - if (condition) { - raise(fn, message); - } - } - - public static <E extends Exception> void raiseUnless(boolean condition, Function<? super String, ? extends E> fn, - Supplier<String> message) throws E { - raiseIf(!condition, fn, message); - } - - public static <E extends Exception, C extends Throwable, R> R raise( - BiFunction<? super String, ? super C, ? extends E> fn, C cause, String format, Object... args) throws E { - throw create(fn, cause, format, args); - } - - public static <E extends Exception, C extends Throwable> void raiseIf(boolean condition, - BiFunction<? super String, ? super C, ? extends E> fn, C cause, String format, Object... args) throws E { - if (condition) { - raise(fn, cause, format, args); - } - } - - public static <E extends Exception, C extends Throwable> void raiseUnless(boolean condition, - BiFunction<? super String, ? super C, ? extends E> fn, C cause, String format, Object... args) throws E { - raiseIf(!condition, fn, cause, format, args); - } - - public static <E extends Exception, C extends Throwable, R> R raise( - BiFunction<? super String, ? super C, ? extends E> fn, C cause, Supplier<String> message) throws E { - throw create(fn, cause, message); - } - - public static <E extends Exception, C extends Throwable> void raiseIf(boolean condition, - BiFunction<? super String, ? super C, ? extends E> fn, C cause, Supplier<String> message) throws E { - if (condition) { - raise(fn, cause, message); - } - } - - public static <E extends Exception, C extends Throwable> void raiseUnless(boolean condition, - BiFunction<? super String, ? super C, ? extends E> fn, C cause, Supplier<String> message) throws E { - raiseIf(!condition, fn, cause, message); - } - - private static <T extends Throwable> T elideStackTrace(T t) { - final StackTraceElement[] stackTrace = t.fillInStackTrace().getStackTrace(); - t.setStackTrace(Stream.of(stackTrace).filter(e -> !Exceptions.class.getName().equals(e.getClassName())) - .toArray(StackTraceElement[]::new)); - return t; - } - - private Exceptions() { - } -} http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/bval-core/src/main/java/org/apache/bval/util/FieldAccess.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/util/FieldAccess.java b/bval-core/src/main/java/org/apache/bval/util/FieldAccess.java deleted file mode 100644 index 720d1db..0000000 --- a/bval-core/src/main/java/org/apache/bval/util/FieldAccess.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * 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.ElementType; -import java.lang.reflect.Field; -import java.lang.reflect.Type; - -import org.apache.bval.util.reflection.Reflection; -import org.apache.commons.weaver.privilizer.Privilizing; -import org.apache.commons.weaver.privilizer.Privilizing.CallTo; - -/** - * Description: direct field access strategy.<br/> - */ -@Privilizing(@CallTo(Reflection.class)) -public class FieldAccess extends AccessStrategy { - - private final Field field; - - /** - * Create a new FieldAccess instance. - * @param field - */ - public FieldAccess(final Field field) { - this.field = field; - } - - /** - * {@inheritDoc} - */ - @Override - public Object get(final Object instance) { - final boolean mustUnset = Reflection.setAccessible(field, true); - try { - return field.get(instance); - } catch (IllegalAccessException e) { - throw new IllegalArgumentException(e); - } finally { - if (mustUnset) { - Reflection.setAccessible(field, false); - } - } - } - - /** - * {@inheritDoc} - */ - @Override - public ElementType getElementType() { - return ElementType.FIELD; - } - - /** - * {@inheritDoc} - */ - @Override - public Type getJavaType() { - return field.getGenericType(); - } - - /** - * {@inheritDoc} - */ - @Override - public String getPropertyName() { - return field.getName(); - } - - /** - * {@inheritDoc} - */ - @Override - public String toString() { - return field.toString(); - } - - /** - * {@inheritDoc} - */ - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - FieldAccess that = (FieldAccess) o; - - return field.equals(that.field); - } - - /** - * {@inheritDoc} - */ - @Override - public int hashCode() { - return field.hashCode(); - } -} http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/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 deleted file mode 100644 index ab6a937..0000000 --- a/bval-core/src/main/java/org/apache/bval/util/IndexedAccess.java +++ /dev/null @@ -1,121 +0,0 @@ -/** - * 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.ElementType; -import java.lang.reflect.Array; -import java.lang.reflect.Type; -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. - */ -public class IndexedAccess extends AccessStrategy { - private static final TypeVariable<?> ITERABLE_TYPE = Iterable.class.getTypeParameters()[0]; - - /** - * Get the Java element type of a particular container type. - * - * @param containerType - * @return Type or <code>null</code> if <code>containerType</code> is not - * some type of {@link Iterable} or array - */ - public static Type getJavaElementType(Type containerType) { - if (TypeUtils.isArrayType(containerType)) { - return TypeUtils.getArrayComponentType(containerType); - } - if (TypeUtils.isAssignable(containerType, Iterable.class)) { - Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(containerType, Iterable.class); - Type type = TypeUtils.unrollVariables(typeArguments, ITERABLE_TYPE); - return type != null ? type : Object.class; - } - return null; - } - - private final Type containerType; - private final Integer index; - - /** - * Create a new IndexedAccessStrategy instance. - * - * @param containerType - * @param index - */ - public IndexedAccess(Type containerType, Integer index) { - super(); - this.containerType = containerType; - this.index = index; - } - - /** - * {@inheritDoc} - */ - @Override - public Object get(Object instance) { - if (index == null) { - throw new UnsupportedOperationException("Cannot read null index"); - } - if (instance != null && instance.getClass().isArray()) { - if (Array.getLength(instance) - index > 0) { - return Array.get(instance, index); - } - } else if (instance instanceof List<?>) { - List<?> list = (List<?>) instance; - if (list.size() - index > 0) { - return list.get(index); - } - } else if (instance instanceof Iterable<?>) { - int i = 0; - for (Object o : (Iterable<?>) instance) { - if (++i == index) { - return o; - } - } - } - return null; - } - - /** - * {@inheritDoc} - */ - @Override - public ElementType getElementType() { - return ElementType.METHOD; - } - - /** - * {@inheritDoc} - */ - @Override - public Type getJavaType() { - return getJavaElementType(containerType); - } - - /** - * {@inheritDoc} - */ - @Override - public String getPropertyName() { - return String.format("[%d]", index); - } - -} http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/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 deleted file mode 100644 index 700287d..0000000 --- a/bval-core/src/main/java/org/apache/bval/util/KeyedAccess.java +++ /dev/null @@ -1,121 +0,0 @@ -/** - * 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 org.apache.bval.util.reflection.TypeUtils; - -import java.lang.annotation.ElementType; -import java.lang.reflect.Type; -import java.lang.reflect.TypeVariable; -import java.util.Map; - -/** - * {@link AccessStrategy} to get a keyed value from a {@link Map}. Contains - * special handling when a string key is used against a container type whose key - * parameter is not assignable from {@link String}: against a map whose key type - * is an enum class, it will be interpreted as a named enum constant; other key - * types will be compared via {@link Object#toString()}. - */ -public class KeyedAccess extends AccessStrategy { - private static final TypeVariable<?>[] MAP_TYPEVARS = Map.class.getTypeParameters(); - - /** - * Get the Java element type of a particular container type. - * - * @param containerType - * @return Type or <code>null</code> if <code>containerType</code> is not - * some kind of {@link Map} - */ - public static Type getJavaElementType(Type containerType) { - if (TypeUtils.isAssignable(containerType, Map.class)) { - Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(containerType, Map.class); - return ObjectUtils.defaultIfNull(TypeUtils.unrollVariables(typeArguments, MAP_TYPEVARS[1]), Object.class); - } - return null; - } - - private final Type containerType; - private final Object key; - - /** - * Create a new KeyedAccess instance. - * - * @param containerType - * @param key - */ - public KeyedAccess(Type containerType, Object key) { - super(); - this.containerType = containerType; - this.key = key; - } - - /** - * {@inheritDoc} - */ - @Override - public Object get(Object instance) { - if (instance instanceof Map<?, ?>) { - Map<?, ?> map = (Map<?, ?>) instance; - Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(containerType, Map.class); - Type keyType = TypeUtils.unrollVariables(typeArguments, MAP_TYPEVARS[0]); - if (key == null || keyType == null || TypeUtils.isInstance(key, keyType)) { - return map.get(key); - } - if (key instanceof String) { - String name = (String) key; - Class<?> rawKeyType = TypeUtils.getRawType(keyType, containerType); - if (rawKeyType.isEnum()) { - @SuppressWarnings({ "unchecked", "rawtypes" }) - final Object result = map.get(Enum.valueOf((Class<? extends Enum>) rawKeyType, name)); - return result; - } - for (Map.Entry<?, ?> e : map.entrySet()) { - if (name.equals(e.getKey())) { - return e.getValue(); - } - } - } - } - return null; - } - - /** - * {@inheritDoc} - */ - @Override - public ElementType getElementType() { - return ElementType.METHOD; - } - - /** - * {@inheritDoc} - */ - @Override - public Type getJavaType() { - final Type result = getJavaElementType(containerType); - return result == null ? Object.class : result; - } - - /** - * {@inheritDoc} - */ - @Override - public String getPropertyName() { - return String.format("[%s]", key); - } - -} http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/bval-core/src/main/java/org/apache/bval/util/Lazy.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/util/Lazy.java b/bval-core/src/main/java/org/apache/bval/util/Lazy.java deleted file mode 100644 index 4796de3..0000000 --- a/bval-core/src/main/java/org/apache/bval/util/Lazy.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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.Optional; -import java.util.function.BiConsumer; -import java.util.function.Consumer; -import java.util.function.Supplier; - -/** - * @since 2.0 - * - * @param <T> - */ -public class Lazy<T> implements Supplier<T> { - private T value; - private volatile Supplier<T> init; - - public Lazy(Supplier<T> init) { - reset(init); - } - - public Lazy<T> reset(Supplier<T> init) { - this.init = Validate.notNull(init); - return this; - } - - @Override - public T get() { - if (init != null) { - synchronized (this) { - if (init != null) { - value = init.get(); - init = null; - } - } - } - return value; - } - - public Optional<T> optional() { - return Optional.ofNullable(value); - } - - public <U> Consumer<U> consumer(BiConsumer<? super T, ? super U> delegate) { - return u -> delegate.accept(get(), u); - } -} http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/bval-core/src/main/java/org/apache/bval/util/LazyInt.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/util/LazyInt.java b/bval-core/src/main/java/org/apache/bval/util/LazyInt.java deleted file mode 100644 index b866226..0000000 --- a/bval-core/src/main/java/org/apache/bval/util/LazyInt.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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.OptionalInt; -import java.util.function.IntSupplier; - -/** - * @since 2.0 - */ -public class LazyInt implements IntSupplier { - private int value; - private volatile IntSupplier init; - - public LazyInt(IntSupplier init) { - this.init = Validate.notNull(init); - } - - @Override - public int getAsInt() { - if (init != null) { - synchronized (this) { - if (init != null) { - value = init.getAsInt(); - init = null; - } - } - } - return value; - } - - public synchronized OptionalInt optional() { - return init == null ? OptionalInt.of(value) : OptionalInt.empty(); - } -} http://git-wip-us.apache.org/repos/asf/bval/blob/ed299e4f/bval-core/src/main/java/org/apache/bval/util/MethodAccess.java ---------------------------------------------------------------------- diff --git a/bval-core/src/main/java/org/apache/bval/util/MethodAccess.java b/bval-core/src/main/java/org/apache/bval/util/MethodAccess.java deleted file mode 100644 index 298272e..0000000 --- a/bval-core/src/main/java/org/apache/bval/util/MethodAccess.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * 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.beans.Introspector; -import java.lang.annotation.ElementType; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Type; - -import org.apache.bval.util.reflection.Reflection; -import org.apache.commons.weaver.privilizer.Privilizing; -import org.apache.commons.weaver.privilizer.Privilizing.CallTo; - -/** - * Description: invoke a zero-argument method (getter)<br/> - */ -@Privilizing(@CallTo(Reflection.class)) -public class MethodAccess extends AccessStrategy { - private final Method method; - private final String propertyName; - - /** - * Create a new MethodAccess instance. - * @param method - */ - public MethodAccess(Method method) { - this(getPropertyName(method), method); - } - - /** - * Create a new MethodAccess instance. - * @param propertyName - * @param method - */ - public MethodAccess(String propertyName, final Method method) { - this.method = method; - this.propertyName = propertyName; - } - - /** - * Process bean properties getter by applying the JavaBean naming conventions. - * - * @param member the member for which to get the property name. - * @return The bean method name with the "is" or "get" prefix stripped off, <code>null</code> - * the method name id not according to the JavaBeans standard. - */ - public static String getPropertyName(Method member) { - final String methodName = member.getName(); - if (methodName.startsWith("is")) { - return Introspector.decapitalize(methodName.substring(2)); - } - if (methodName.startsWith("get")) { - return Introspector.decapitalize(methodName.substring(3)); - } - return null; - } - - /** - * {@inheritDoc} - * normally the propertyName of the getter method, e.g.<br> - * method: getName() -> propertyName: name<br> - * method: isValid() -> propertyName: valid<br> - */ - @Override - public String getPropertyName() { - return propertyName; - } - - /** - * {@inheritDoc} - */ - @Override - public Object get(final Object instance) { - final boolean mustUnset = Reflection.setAccessible(method, true); - try { - return method.invoke(instance); - } catch (IllegalAccessException e) { - throw new IllegalArgumentException(e); - } catch (InvocationTargetException e) { - throw new IllegalArgumentException(e); - } finally { - if (mustUnset) { - Reflection.setAccessible(method, false); - } - } - } - - /** - * {@inheritDoc} - */ - @Override - public ElementType getElementType() { - return ElementType.METHOD; - } - - /** - * {@inheritDoc} - */ - @Override - public Type getJavaType() { - return method.getGenericReturnType(); - } - - /** - * {@inheritDoc} - */ - @Override - public String toString() { - return method.toString(); - } - - /** - * {@inheritDoc} - */ - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - MethodAccess that = (MethodAccess) o; - - return method.equals(that.method); - } - - /** - * {@inheritDoc} - */ - @Override - public int hashCode() { - return method.hashCode(); - } -}
