Hibernate SVN: r19568 - search/trunk/hibernate-search/src/main/java/org/hibernate/search/engine.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2010-05-20 13:56:27 -0400 (Thu, 20 May 2010)
New Revision: 19568
Modified:
search/trunk/hibernate-search/src/main/java/org/hibernate/search/engine/DocumentBuilderIndexedEntity.java
Log:
Fix typo in exception
Modified: search/trunk/hibernate-search/src/main/java/org/hibernate/search/engine/DocumentBuilderIndexedEntity.java
===================================================================
--- search/trunk/hibernate-search/src/main/java/org/hibernate/search/engine/DocumentBuilderIndexedEntity.java 2010-05-20 17:55:52 UTC (rev 19567)
+++ search/trunk/hibernate-search/src/main/java/org/hibernate/search/engine/DocumentBuilderIndexedEntity.java 2010-05-20 17:56:27 UTC (rev 19568)
@@ -219,7 +219,7 @@
analyzer = propertiesMetadata.analyzer;
}
if ( analyzer == null ) {
- throw new AssertionFailure( "Analizer should not be undefined" );
+ throw new AssertionFailure( "Analyzer should not be undefined" );
}
this.analyzer.addScopedAnalyzer( fieldName, analyzer );
}
14 years, 7 months
Hibernate SVN: r19567 - search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/query/dsl.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2010-05-20 13:55:52 -0400 (Thu, 20 May 2010)
New Revision: 19567
Modified:
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/query/dsl/DSLTest.java
Log:
Minor clean on test
Modified: search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/query/dsl/DSLTest.java
===================================================================
--- search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/query/dsl/DSLTest.java 2010-05-20 11:58:01 UTC (rev 19566)
+++ search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/query/dsl/DSLTest.java 2010-05-20 17:55:52 UTC (rev 19567)
@@ -32,7 +32,7 @@
Transaction transaction = fts.beginTransaction();
final QueryBuilder monthQb = fts.getSearchFactory()
.buildQueryBuilder().forEntity( Month.class ).get();
- Query
+ Query
//regular term query
query = monthQb.exact().onField( "mythology" ).matching( "cold" ).createQuery();
@@ -177,8 +177,6 @@
assertEquals( 2, results.size() );
assertEquals( "February", results.get( 0 ).getName() );
- //FIXME add other method tests besides boostedTo
-
transaction.commit();
cleanData( fts );
14 years, 7 months
Hibernate SVN: r19566 - in validator/trunk/hibernate-validator/src: main/java/org/hibernate/validator/engine and 10 other directories.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2010-05-20 07:58:01 -0400 (Thu, 20 May 2010)
New Revision: 19566
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/FutureDefinition.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/ContainsField.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/ContainsMethod.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetAnnotationParameter.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetClassLoader.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetConstructor.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetDeclaredField.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetDeclaredFields.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetDeclaredMethods.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetMethod.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetMethodFromPropertyName.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetMethods.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/LoadClass.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/NewInstance.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/SetAccessibility.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Tournament.java
Removed:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ContainsField.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ContainsMethod.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetAnnotationParameter.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetClassLoader.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetConstructor.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetDeclaredField.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetDeclaredFields.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetDeclaredMethods.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetMethod.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetMethodFromPropertyName.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetMethods.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/LoadClass.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/NewInstance.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/SetAccessibility.java
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefinition.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintsForType.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ConstraintValidatorFactoryImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ValidatorFactoryImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/resolver/DefaultTraversableResolver.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/BeanMetaDataImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/ConstraintDescriptorImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/ConstraintHelper.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/resourceloading/PlatformResourceBundleLocator.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ReflectionHelper.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/annotationfactory/AnnotationFactory.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/annotationfactory/AnnotationProxy.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/xml/ValidationXmlParser.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/xml/XmlMappingParser.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Marathon.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/metadata/ConstraintHelperTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/util/ReflectionHelperTest.java
Log:
HV-274 Moved privileged actions into own package
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefinition.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefinition.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefinition.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -24,7 +24,7 @@
import java.util.Map;
import javax.validation.ValidationException;
-import org.hibernate.validator.util.NewInstance;
+import org.hibernate.validator.util.privilegedactions.NewInstance;
import org.hibernate.validator.util.ReflectionHelper;
/**
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintsForType.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintsForType.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintsForType.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
@@ -21,7 +21,7 @@
import java.lang.annotation.ElementType;
import java.security.AccessController;
-import org.hibernate.validator.util.NewInstance;
+import org.hibernate.validator.util.privilegedactions.NewInstance;
import static java.lang.annotation.ElementType.TYPE;
Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/FutureDefinition.java (from rev 19559, validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/NotNullDefinition.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/FutureDefinition.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/FutureDefinition.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -0,0 +1,46 @@
+// $Id$
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.hibernate.validator.cfg;
+
+import java.lang.annotation.ElementType;
+import javax.validation.Payload;
+import javax.validation.constraints.Future;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class FutureDefinition extends ConstraintDefinition<Future> {
+ public FutureDefinition(Class<?> beanType, String property, ElementType elementType, ConstraintMapping mapping) {
+ super( beanType, Future.class, property, elementType, mapping );
+ }
+
+ public FutureDefinition message(String message) {
+ addParameter( "message", message );
+ return this;
+ }
+
+ public FutureDefinition groups(Class<?>... groups) {
+ addParameter( "groups", groups );
+ return this;
+ }
+
+ public FutureDefinition payload(Class<? extends Payload>... payload) {
+ addParameter( "payload", payload );
+ return this;
+ }
+}
\ No newline at end of file
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ConstraintValidatorFactoryImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ConstraintValidatorFactoryImpl.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ConstraintValidatorFactoryImpl.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -21,7 +21,7 @@
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorFactory;
-import org.hibernate.validator.util.NewInstance;
+import org.hibernate.validator.util.privilegedactions.NewInstance;
/**
* Default <code>ConstraintValidatorFactory</code> using a no-arg constructor.
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ValidatorFactoryImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ValidatorFactoryImpl.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ValidatorFactoryImpl.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -20,7 +20,6 @@
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Member;
-import java.security.AccessController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -44,7 +43,6 @@
import org.hibernate.validator.metadata.ConstraintHelper;
import org.hibernate.validator.metadata.ConstraintOrigin;
import org.hibernate.validator.metadata.MetaConstraint;
-import org.hibernate.validator.util.GetDeclaredField;
import org.hibernate.validator.util.ReflectionHelper;
import org.hibernate.validator.util.annotationfactory.AnnotationDescriptor;
import org.hibernate.validator.util.annotationfactory.AnnotationFactory;
@@ -120,6 +118,11 @@
);
}
+ /**
+ * Reads the configuration from {@code mapping} and creates the appropriate meta-data structures.
+ *
+ * @param mapping The constraint configuration created via the programmatic API.
+ */
private <A extends Annotation, T> void initProgrammaticConfiguration(ConstraintMapping mapping) {
Map<Class<?>, List<ConstraintDefinition<?>>> configData = mapping.getConfigData();
for ( Map.Entry<Class<?>, List<ConstraintDefinition<?>>> entry : configData.entrySet() ) {
@@ -146,14 +149,10 @@
annotation, constraintHelper, config.getElementType(), ConstraintOrigin.DEFINED_LOCALLY
);
- final Member member;
- GetDeclaredField action = GetDeclaredField.action( config.getBeanType(), config.getProperty() );
- if ( System.getSecurityManager() != null ) {
- member = AccessController.doPrivileged( action );
- }
- else {
- member = action.run();
- }
+ final Member member = ReflectionHelper.getMember(
+ config.getBeanType(), config.getProperty(), config.getElementType()
+ );
+
MetaConstraint<T, ?> metaConstraint = new MetaConstraint(
config.getBeanType(), member, constraintDescriptor
);
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/resolver/DefaultTraversableResolver.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/resolver/DefaultTraversableResolver.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/resolver/DefaultTraversableResolver.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -26,8 +26,8 @@
import org.slf4j.Logger;
import org.hibernate.validator.util.LoggerFactory;
-import org.hibernate.validator.util.NewInstance;
-import org.hibernate.validator.util.LoadClass;
+import org.hibernate.validator.util.privilegedactions.NewInstance;
+import org.hibernate.validator.util.privilegedactions.LoadClass;
/**
* A JPA 2 aware <code>TraversableResolver</code>.
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/BeanMetaDataImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/BeanMetaDataImpl.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/BeanMetaDataImpl.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -42,11 +42,11 @@
import org.slf4j.Logger;
-import org.hibernate.validator.util.GetDeclaredFields;
-import org.hibernate.validator.util.GetDeclaredMethods;
+import org.hibernate.validator.util.privilegedactions.GetDeclaredFields;
+import org.hibernate.validator.util.privilegedactions.GetDeclaredMethods;
import org.hibernate.validator.util.LoggerFactory;
import org.hibernate.validator.util.ReflectionHelper;
-import org.hibernate.validator.util.SetAccessibility;
+import org.hibernate.validator.util.privilegedactions.SetAccessibility;
/**
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/ConstraintDescriptorImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/ConstraintDescriptorImpl.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/ConstraintDescriptorImpl.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -43,10 +43,10 @@
import org.slf4j.Logger;
-import org.hibernate.validator.util.GetAnnotationParameter;
-import org.hibernate.validator.util.GetDeclaredMethods;
-import org.hibernate.validator.util.GetMethod;
-import org.hibernate.validator.util.GetMethods;
+import org.hibernate.validator.util.privilegedactions.GetAnnotationParameter;
+import org.hibernate.validator.util.privilegedactions.GetDeclaredMethods;
+import org.hibernate.validator.util.privilegedactions.GetMethod;
+import org.hibernate.validator.util.privilegedactions.GetMethods;
import org.hibernate.validator.util.LoggerFactory;
import org.hibernate.validator.util.annotationfactory.AnnotationDescriptor;
import org.hibernate.validator.util.annotationfactory.AnnotationFactory;
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/ConstraintHelper.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/ConstraintHelper.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/ConstraintHelper.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -72,9 +72,9 @@
import org.hibernate.validator.constraints.impl.SizeValidatorForCollection;
import org.hibernate.validator.constraints.impl.SizeValidatorForMap;
import org.hibernate.validator.constraints.impl.SizeValidatorForString;
-import org.hibernate.validator.util.GetMethods;
-import org.hibernate.validator.util.GetMethod;
-import org.hibernate.validator.util.GetAnnotationParameter;
+import org.hibernate.validator.util.privilegedactions.GetMethods;
+import org.hibernate.validator.util.privilegedactions.GetMethod;
+import org.hibernate.validator.util.privilegedactions.GetAnnotationParameter;
/**
* Keeps track of builtin constraints and their validator implementations, as well as already resolved validator definitions.
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/resourceloading/PlatformResourceBundleLocator.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/resourceloading/PlatformResourceBundleLocator.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/resourceloading/PlatformResourceBundleLocator.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -24,7 +24,7 @@
import org.slf4j.Logger;
-import org.hibernate.validator.util.GetClassLoader;
+import org.hibernate.validator.util.privilegedactions.GetClassLoader;
import org.hibernate.validator.util.LoggerFactory;
/**
Deleted: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ContainsField.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ContainsField.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ContainsField.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -1,47 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validator.util;
-
-import java.security.PrivilegedAction;
-
-/**
- * @author Emmanuel Bernard
- */
-public class ContainsField implements PrivilegedAction<Boolean> {
- private final Class<?> clazz;
- private final String property;
-
- public static ContainsField action(Class<?> clazz, String property) {
- return new ContainsField( clazz, property );
- }
-
- private ContainsField(Class<?> clazz, String property) {
- this.clazz = clazz;
- this.property = property;
- }
-
- public Boolean run() {
- try {
- clazz.getDeclaredField( property );
- return true;
- }
- catch ( NoSuchFieldException e ) {
- return false;
- }
- }
-}
Deleted: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ContainsMethod.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ContainsMethod.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ContainsMethod.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -1,41 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validator.util;
-
-import java.security.PrivilegedAction;
-
-/**
- * @author Emmanuel Bernard
- */
-public class ContainsMethod implements PrivilegedAction<Boolean> {
- private final Class<?> clazz;
- private final String property;
-
- public static ContainsMethod action(Class<?> clazz, String property) {
- return new ContainsMethod( clazz, property );
- }
-
- private ContainsMethod(Class<?> clazz, String property) {
- this.clazz = clazz;
- this.property = property;
- }
-
- public Boolean run() {
- return ReflectionHelper.getMethod( clazz, property ) != null;
- }
-}
Deleted: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetAnnotationParameter.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetAnnotationParameter.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetAnnotationParameter.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -1,45 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validator.util;
-
-import java.lang.annotation.Annotation;
-import java.security.PrivilegedAction;
-
-/**
- * @author Emmanuel Bernard
- */
-public class GetAnnotationParameter<T> implements PrivilegedAction<T> {
- private final Annotation annotation;
- private final String parameterName;
- private final Class<T> type;
-
-
- public static <T> GetAnnotationParameter<T> action(Annotation annotation, String parameterName, Class<T> type) {
- return new GetAnnotationParameter<T>( annotation, parameterName, type );
- }
-
- private GetAnnotationParameter(Annotation annotation, String parameterName, Class<T> type) {
- this.annotation = annotation;
- this.parameterName = parameterName;
- this.type = type;
- }
-
- public T run() {
- return ReflectionHelper.getAnnotationParameter( annotation, parameterName, type );
- }
-}
Deleted: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetClassLoader.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetClassLoader.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetClassLoader.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -1,51 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validator.util;
-
-import java.security.PrivilegedAction;
-
-/**
- * @author Emmanuel Bernard
- */
-public final class GetClassLoader implements PrivilegedAction<ClassLoader> {
- private final Class<?> clazz;
-
- public static GetClassLoader fromContext() {
- return new GetClassLoader( null );
- }
-
- public static GetClassLoader fromClass(Class<?> clazz) {
- if ( clazz == null ) {
- throw new IllegalArgumentException( "Class is null" );
- }
- return new GetClassLoader( clazz );
- }
-
- private GetClassLoader(Class<?> clazz) {
- this.clazz = clazz;
- }
-
- public ClassLoader run() {
- if ( clazz != null ) {
- return clazz.getClassLoader();
- }
- else {
- return Thread.currentThread().getContextClassLoader();
- }
- }
-}
Deleted: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetConstructor.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetConstructor.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetConstructor.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -1,47 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validator.util;
-
-import java.lang.reflect.Constructor;
-import java.security.PrivilegedAction;
-
-/**
- * @author Emmanuel Bernard
- */
-public class GetConstructor<T> implements PrivilegedAction<Constructor<T>> {
- private final Class<T> clazz;
- private final Class<?>[] params;
-
- public static <T> GetConstructor<T> action(Class<T> clazz, Class<?>... params) {
- return new GetConstructor<T>( clazz, params );
- }
-
- private GetConstructor(Class<T> clazz, Class<?>... params) {
- this.clazz = clazz;
- this.params = params;
- }
-
- public Constructor<T> run() {
- try {
- return clazz.getConstructor(params);
- }
- catch ( NoSuchMethodException e ) {
- return null;
- }
- }
-}
Deleted: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetDeclaredField.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetDeclaredField.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetDeclaredField.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -1,49 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validator.util;
-
-import java.lang.reflect.Field;
-import java.security.PrivilegedAction;
-
-/**
- * @author Emmanuel Bernard
- */
-public class GetDeclaredField implements PrivilegedAction<Field> {
- private final Class<?> clazz;
- private final String fieldName;
-
- public static GetDeclaredField action(Class<?> clazz, String fieldName) {
- return new GetDeclaredField( clazz, fieldName );
- }
-
- private GetDeclaredField(Class<?> clazz, String fieldName) {
- this.clazz = clazz;
- this.fieldName = fieldName;
- }
-
- public Field run() {
- try {
- final Field field = clazz.getDeclaredField( fieldName );
- ReflectionHelper.setAccessibility( field );
- return field;
- }
- catch ( NoSuchFieldException e ) {
- return null;
- }
- }
-}
Deleted: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetDeclaredFields.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetDeclaredFields.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetDeclaredFields.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -1,40 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validator.util;
-
-import java.security.PrivilegedAction;
-import java.lang.reflect.Field;
-
-/**
- * @author Emmanuel Bernard
- */
-public class GetDeclaredFields implements PrivilegedAction<Field[]> {
- private final Class<?> clazz;
-
- public static GetDeclaredFields action(Class<?> clazz) {
- return new GetDeclaredFields( clazz );
- }
-
- private GetDeclaredFields(Class<?> clazz) {
- this.clazz = clazz;
- }
-
- public Field[] run() {
- return clazz.getDeclaredFields();
- }
-}
Deleted: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetDeclaredMethods.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetDeclaredMethods.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetDeclaredMethods.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -1,40 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validator.util;
-
-import java.security.PrivilegedAction;
-import java.lang.reflect.Method;
-
-/**
- * @author Emmanuel Bernard
- */
-public class GetDeclaredMethods implements PrivilegedAction<Method[]> {
- private final Class<?> clazz;
-
- public static GetDeclaredMethods action(Class<?> clazz) {
- return new GetDeclaredMethods( clazz );
- }
-
- private GetDeclaredMethods(Class<?> clazz) {
- this.clazz = clazz;
- }
-
- public Method[] run() {
- return clazz.getDeclaredMethods();
- }
-}
Deleted: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetMethod.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetMethod.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetMethod.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -1,47 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validator.util;
-
-import java.lang.reflect.Method;
-import java.security.PrivilegedAction;
-
-/**
- * @author Emmanuel Bernard
- */
-public class GetMethod implements PrivilegedAction<Method> {
- private final Class<?> clazz;
- private final String methodName;
-
- public static GetMethod action(Class<?> clazz, String methodName) {
- return new GetMethod( clazz, methodName );
- }
-
- private GetMethod(Class<?> clazz, String methodName) {
- this.clazz = clazz;
- this.methodName = methodName;
- }
-
- public Method run() {
- try {
- return clazz.getMethod(methodName);
- }
- catch ( NoSuchMethodException e ) {
- return null;
- }
- }
-}
Deleted: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetMethodFromPropertyName.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetMethodFromPropertyName.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetMethodFromPropertyName.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -1,42 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validator.util;
-
-import java.lang.reflect.Method;
-import java.security.PrivilegedAction;
-
-/**
- * @author Emmanuel Bernard
- */
-public class GetMethodFromPropertyName implements PrivilegedAction<Method> {
- private final Class<?> clazz;
- private final String property;
-
- public static GetMethodFromPropertyName action(Class<?> clazz, String property) {
- return new GetMethodFromPropertyName( clazz, property );
- }
-
- private GetMethodFromPropertyName(Class<?> clazz, String property) {
- this.clazz = clazz;
- this.property = property;
- }
-
- public Method run() {
- return ReflectionHelper.getMethod( clazz, property );
- }
-}
Deleted: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetMethods.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetMethods.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetMethods.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -1,40 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validator.util;
-
-import java.security.PrivilegedAction;
-import java.lang.reflect.Method;
-
-/**
- * @author Emmanuel Bernard
- */
-public class GetMethods implements PrivilegedAction<Method[]> {
- private final Class<?> clazz;
-
- public static GetMethods action(Class<?> clazz) {
- return new GetMethods( clazz );
- }
-
- private GetMethods(Class<?> clazz) {
- this.clazz = clazz;
- }
-
- public Method[] run() {
- return clazz.getMethods();
- }
-}
Deleted: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/LoadClass.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/LoadClass.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/LoadClass.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -1,56 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validator.util;
-
-import java.security.PrivilegedAction;
-import javax.validation.ValidationException;
-
-/**
- * @author Emmanuel Bernard
- */
-public class LoadClass implements PrivilegedAction<Class<?>> {
- private final String className;
- private final Class<?> caller;
-
- public static LoadClass action(String className, Class<?> caller) {
- return new LoadClass( className, caller );
- }
-
- private LoadClass(String className, Class<?> caller) {
- this.className = className;
- this.caller = caller;
- }
-
- public Class<?> run() {
- try {
- ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
- if ( contextClassLoader != null ) {
- return contextClassLoader.loadClass( className );
- }
- }
- catch ( Throwable e ) {
- // ignore
- }
- try {
- return Class.forName( className, true, caller.getClassLoader() );
- }
- catch ( ClassNotFoundException e ) {
- throw new ValidationException("Unable to load class: " + className, e);
- }
- }
-}
Deleted: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/NewInstance.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/NewInstance.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/NewInstance.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -1,72 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validator.util;
-
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
-import java.security.PrivilegedAction;
-import javax.validation.ValidationException;
-
-/**
- * Execute instance creation as privileged action.
- *
- * @author Emmanuel Bernard
- * @author Hardy Ferentschik
- */
-public class NewInstance<T> implements PrivilegedAction<T> {
- private final Class<T> clazz;
- private final String message;
- private final Class<?>[] parameterTypes;
- private final Object[] initArgs;
-
- public static <T> NewInstance<T> action(Class<T> clazz, String message, Object... initArgs) {
- return new NewInstance<T>( clazz, message, initArgs );
- }
-
- private NewInstance(Class<T> clazz, String message, Object... initArgs) {
- this.clazz = clazz;
- this.message = message;
- this.initArgs = initArgs;
- this.parameterTypes = new Class<?>[initArgs.length];
- for ( int i = 0; i < initArgs.length; i++ ) {
- this.parameterTypes[i] = initArgs[i].getClass();
- }
- }
-
- public T run() {
- try {
- Constructor<T> constructor = clazz.getConstructor( parameterTypes );
- return constructor.newInstance( initArgs );
- }
- catch ( InstantiationException e ) {
- throw new ValidationException( "Unable to instantiate " + message + ": " + clazz, e );
- }
- catch ( IllegalAccessException e ) {
- throw new ValidationException( "Unable to instantiate " + clazz, e );
- }
- catch ( NoSuchMethodException e ) {
- throw new ValidationException( "Unable to instantiate " + clazz, e );
- }
- catch ( InvocationTargetException e ) {
- throw new ValidationException( "Unable to instantiate " + clazz, e );
- }
- catch ( RuntimeException e ) {
- throw new ValidationException( "Unable to instantiate " + clazz, e );
- }
- }
-}
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ReflectionHelper.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ReflectionHelper.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ReflectionHelper.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -30,6 +30,7 @@
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
+import java.security.AccessController;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
@@ -39,6 +40,9 @@
import com.googlecode.jtype.TypeUtils;
+import org.hibernate.validator.util.privilegedactions.GetDeclaredField;
+import org.hibernate.validator.util.privilegedactions.GetMethod;
+
/**
* Some reflection utility methods.
*
@@ -120,6 +124,19 @@
* @return {@code true} is the property and can be access via the specified type, {@code false} otherwise.
*/
public static boolean propertyExists(Class<?> clazz, String property, ElementType elementType) {
+ return getMember( clazz, property, elementType ) != null;
+ }
+
+ /**
+ * Returns the member with the given name and type.
+ *
+ * @param clazz The class from which to retrieve the member. Cannot be {@code null}.
+ * @param property The property name without 'is', 'get' or 'has'. Cannot be {@code null} or empty.
+ * @param elementType The element type. Either {@code ElementType.FIELD} or {@code ElementType METHOD}.
+ *
+ * @return the member which matching the name and type or {@code null} if no such member exists.
+ */
+ public static Member getMember(Class<?> clazz, String property, ElementType elementType) {
if ( clazz == null ) {
throw new IllegalArgumentException( "The class cannot be null" );
}
@@ -132,31 +149,33 @@
throw new IllegalArgumentException( "Element type has to be FIELD or METHOD" );
}
- boolean exists = false;
+ Member member = null;
if ( ElementType.FIELD.equals( elementType ) ) {
- try {
- clazz.getDeclaredField( property );
- exists = true;
+ GetDeclaredField action = GetDeclaredField.action( clazz, property );
+ if ( System.getSecurityManager() != null ) {
+ member = AccessController.doPrivileged( action );
}
- catch ( NoSuchFieldException e ) {
- exists = false;
+ else {
+ member = action.run();
}
}
else {
String methodName = property.substring( 0, 1 ).toUpperCase() + property.substring( 1 );
- String[] prefixes = {"is", "get", "has"};
- for(String prefix : prefixes) {
- try {
- clazz.getDeclaredMethod( prefix + methodName );
- exists = true;
+ String[] prefixes = { "is", "get", "has" };
+ for ( String prefix : prefixes ) {
+ GetMethod action = GetMethod.action( clazz, prefix + methodName );
+ if ( System.getSecurityManager() != null ) {
+ member = AccessController.doPrivileged( action );
+ }
+ else {
+ member = action.run();
+ }
+ if ( member != null ) {
break;
- } catch (NoSuchMethodException e) {
-
}
}
-
}
- return exists;
+ return member;
}
@@ -232,7 +251,7 @@
return value;
}
- static void setAccessibility(Member member) {
+ public static void setAccessibility(Member member) {
// HV-257
// Also set accessibility in case of public abstract members. If you proxy an interface using java.lang.reflect.Proxy
// per default you will get a IllegalAccessException since you are not allowed to access public abstract methods.
@@ -394,7 +413,7 @@
* @return Returns the method with the specified name or <code>null</code> if it does not exist.
*/
//run client in privileged block
- static Method getMethod(Class<?> clazz, String methodName) {
+ public static Method getMethod(Class<?> clazz, String methodName) {
try {
char string[] = methodName.toCharArray();
string[0] = Character.toUpperCase( string[0] );
Deleted: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/SetAccessibility.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/SetAccessibility.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/SetAccessibility.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -1,41 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validator.util;
-
-import java.lang.reflect.Member;
-import java.security.PrivilegedAction;
-
-/**
- * @author Emmanuel Bernard
- */
-public class SetAccessibility implements PrivilegedAction<Object> {
- private final Member member;
-
- public static SetAccessibility action(Member member) {
- return new SetAccessibility( member );
- }
-
- private SetAccessibility(Member member) {
- this.member = member;
- }
-
- public Object run() {
- ReflectionHelper.setAccessibility( member );
- return member;
- }
-}
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/annotationfactory/AnnotationFactory.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/annotationfactory/AnnotationFactory.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/annotationfactory/AnnotationFactory.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -24,8 +24,8 @@
import java.lang.reflect.Proxy;
import java.security.AccessController;
-import org.hibernate.validator.util.GetConstructor;
-import org.hibernate.validator.util.GetClassLoader;
+import org.hibernate.validator.util.privilegedactions.GetConstructor;
+import org.hibernate.validator.util.privilegedactions.GetClassLoader;
/**
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/annotationfactory/AnnotationProxy.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/annotationfactory/AnnotationProxy.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/annotationfactory/AnnotationProxy.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -27,7 +27,7 @@
import java.util.SortedSet;
import java.util.TreeSet;
-import org.hibernate.validator.util.GetDeclaredMethods;
+import org.hibernate.validator.util.privilegedactions.GetDeclaredMethods;
/**
* A concrete implementation of <code>Annotation</code> that pretends it is a
Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/ContainsField.java (from rev 19558, validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ContainsField.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/ContainsField.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/ContainsField.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -0,0 +1,47 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validator.util.privilegedactions;
+
+import java.security.PrivilegedAction;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class ContainsField implements PrivilegedAction<Boolean> {
+ private final Class<?> clazz;
+ private final String property;
+
+ public static ContainsField action(Class<?> clazz, String property) {
+ return new ContainsField( clazz, property );
+ }
+
+ private ContainsField(Class<?> clazz, String property) {
+ this.clazz = clazz;
+ this.property = property;
+ }
+
+ public Boolean run() {
+ try {
+ clazz.getDeclaredField( property );
+ return true;
+ }
+ catch ( NoSuchFieldException e ) {
+ return false;
+ }
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/ContainsField.java
___________________________________________________________________
Name: svn:keywords
+ Id
Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/ContainsMethod.java (from rev 19558, validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ContainsMethod.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/ContainsMethod.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/ContainsMethod.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -0,0 +1,43 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validator.util.privilegedactions;
+
+import java.security.PrivilegedAction;
+
+import org.hibernate.validator.util.ReflectionHelper;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class ContainsMethod implements PrivilegedAction<Boolean> {
+ private final Class<?> clazz;
+ private final String property;
+
+ public static ContainsMethod action(Class<?> clazz, String property) {
+ return new ContainsMethod( clazz, property );
+ }
+
+ private ContainsMethod(Class<?> clazz, String property) {
+ this.clazz = clazz;
+ this.property = property;
+ }
+
+ public Boolean run() {
+ return ReflectionHelper.getMethod( clazz, property ) != null;
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/ContainsMethod.java
___________________________________________________________________
Name: svn:keywords
+ Id
Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetAnnotationParameter.java (from rev 19558, validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetAnnotationParameter.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetAnnotationParameter.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetAnnotationParameter.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -0,0 +1,47 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validator.util.privilegedactions;
+
+import java.lang.annotation.Annotation;
+import java.security.PrivilegedAction;
+
+import org.hibernate.validator.util.ReflectionHelper;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class GetAnnotationParameter<T> implements PrivilegedAction<T> {
+ private final Annotation annotation;
+ private final String parameterName;
+ private final Class<T> type;
+
+
+ public static <T> GetAnnotationParameter<T> action(Annotation annotation, String parameterName, Class<T> type) {
+ return new GetAnnotationParameter<T>( annotation, parameterName, type );
+ }
+
+ private GetAnnotationParameter(Annotation annotation, String parameterName, Class<T> type) {
+ this.annotation = annotation;
+ this.parameterName = parameterName;
+ this.type = type;
+ }
+
+ public T run() {
+ return ReflectionHelper.getAnnotationParameter( annotation, parameterName, type );
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetAnnotationParameter.java
___________________________________________________________________
Name: svn:keywords
+ Id
Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetClassLoader.java (from rev 19558, validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetClassLoader.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetClassLoader.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetClassLoader.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -0,0 +1,51 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validator.util.privilegedactions;
+
+import java.security.PrivilegedAction;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public final class GetClassLoader implements PrivilegedAction<ClassLoader> {
+ private final Class<?> clazz;
+
+ public static GetClassLoader fromContext() {
+ return new GetClassLoader( null );
+ }
+
+ public static GetClassLoader fromClass(Class<?> clazz) {
+ if ( clazz == null ) {
+ throw new IllegalArgumentException( "Class is null" );
+ }
+ return new GetClassLoader( clazz );
+ }
+
+ private GetClassLoader(Class<?> clazz) {
+ this.clazz = clazz;
+ }
+
+ public ClassLoader run() {
+ if ( clazz != null ) {
+ return clazz.getClassLoader();
+ }
+ else {
+ return Thread.currentThread().getContextClassLoader();
+ }
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetClassLoader.java
___________________________________________________________________
Name: svn:keywords
+ Id
Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetConstructor.java (from rev 19558, validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetConstructor.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetConstructor.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetConstructor.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -0,0 +1,47 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validator.util.privilegedactions;
+
+import java.lang.reflect.Constructor;
+import java.security.PrivilegedAction;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class GetConstructor<T> implements PrivilegedAction<Constructor<T>> {
+ private final Class<T> clazz;
+ private final Class<?>[] params;
+
+ public static <T> GetConstructor<T> action(Class<T> clazz, Class<?>... params) {
+ return new GetConstructor<T>( clazz, params );
+ }
+
+ private GetConstructor(Class<T> clazz, Class<?>... params) {
+ this.clazz = clazz;
+ this.params = params;
+ }
+
+ public Constructor<T> run() {
+ try {
+ return clazz.getConstructor(params);
+ }
+ catch ( NoSuchMethodException e ) {
+ return null;
+ }
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetConstructor.java
___________________________________________________________________
Name: svn:keywords
+ Id
Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetDeclaredField.java (from rev 19558, validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetDeclaredField.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetDeclaredField.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetDeclaredField.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -0,0 +1,51 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validator.util.privilegedactions;
+
+import java.lang.reflect.Field;
+import java.security.PrivilegedAction;
+
+import org.hibernate.validator.util.ReflectionHelper;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class GetDeclaredField implements PrivilegedAction<Field> {
+ private final Class<?> clazz;
+ private final String fieldName;
+
+ public static GetDeclaredField action(Class<?> clazz, String fieldName) {
+ return new GetDeclaredField( clazz, fieldName );
+ }
+
+ private GetDeclaredField(Class<?> clazz, String fieldName) {
+ this.clazz = clazz;
+ this.fieldName = fieldName;
+ }
+
+ public Field run() {
+ try {
+ final Field field = clazz.getDeclaredField( fieldName );
+ ReflectionHelper.setAccessibility( field );
+ return field;
+ }
+ catch ( NoSuchFieldException e ) {
+ return null;
+ }
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetDeclaredField.java
___________________________________________________________________
Name: svn:keywords
+ Id
Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetDeclaredFields.java (from rev 19558, validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetDeclaredFields.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetDeclaredFields.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetDeclaredFields.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -0,0 +1,40 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validator.util.privilegedactions;
+
+import java.security.PrivilegedAction;
+import java.lang.reflect.Field;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class GetDeclaredFields implements PrivilegedAction<Field[]> {
+ private final Class<?> clazz;
+
+ public static GetDeclaredFields action(Class<?> clazz) {
+ return new GetDeclaredFields( clazz );
+ }
+
+ private GetDeclaredFields(Class<?> clazz) {
+ this.clazz = clazz;
+ }
+
+ public Field[] run() {
+ return clazz.getDeclaredFields();
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetDeclaredFields.java
___________________________________________________________________
Name: svn:keywords
+ Id
Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetDeclaredMethods.java (from rev 19558, validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetDeclaredMethods.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetDeclaredMethods.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetDeclaredMethods.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -0,0 +1,40 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validator.util.privilegedactions;
+
+import java.security.PrivilegedAction;
+import java.lang.reflect.Method;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class GetDeclaredMethods implements PrivilegedAction<Method[]> {
+ private final Class<?> clazz;
+
+ public static GetDeclaredMethods action(Class<?> clazz) {
+ return new GetDeclaredMethods( clazz );
+ }
+
+ private GetDeclaredMethods(Class<?> clazz) {
+ this.clazz = clazz;
+ }
+
+ public Method[] run() {
+ return clazz.getDeclaredMethods();
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetDeclaredMethods.java
___________________________________________________________________
Name: svn:keywords
+ Id
Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetMethod.java (from rev 19558, validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetMethod.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetMethod.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetMethod.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -0,0 +1,47 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validator.util.privilegedactions;
+
+import java.lang.reflect.Method;
+import java.security.PrivilegedAction;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class GetMethod implements PrivilegedAction<Method> {
+ private final Class<?> clazz;
+ private final String methodName;
+
+ public static GetMethod action(Class<?> clazz, String methodName) {
+ return new GetMethod( clazz, methodName );
+ }
+
+ private GetMethod(Class<?> clazz, String methodName) {
+ this.clazz = clazz;
+ this.methodName = methodName;
+ }
+
+ public Method run() {
+ try {
+ return clazz.getMethod(methodName);
+ }
+ catch ( NoSuchMethodException e ) {
+ return null;
+ }
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetMethod.java
___________________________________________________________________
Name: svn:keywords
+ Id
Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetMethodFromPropertyName.java (from rev 19558, validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetMethodFromPropertyName.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetMethodFromPropertyName.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetMethodFromPropertyName.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -0,0 +1,44 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validator.util.privilegedactions;
+
+import java.lang.reflect.Method;
+import java.security.PrivilegedAction;
+
+import org.hibernate.validator.util.ReflectionHelper;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class GetMethodFromPropertyName implements PrivilegedAction<Method> {
+ private final Class<?> clazz;
+ private final String property;
+
+ public static GetMethodFromPropertyName action(Class<?> clazz, String property) {
+ return new GetMethodFromPropertyName( clazz, property );
+ }
+
+ private GetMethodFromPropertyName(Class<?> clazz, String property) {
+ this.clazz = clazz;
+ this.property = property;
+ }
+
+ public Method run() {
+ return ReflectionHelper.getMethod( clazz, property );
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetMethodFromPropertyName.java
___________________________________________________________________
Name: svn:keywords
+ Id
Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetMethods.java (from rev 19558, validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/GetMethods.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetMethods.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetMethods.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -0,0 +1,40 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validator.util.privilegedactions;
+
+import java.security.PrivilegedAction;
+import java.lang.reflect.Method;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class GetMethods implements PrivilegedAction<Method[]> {
+ private final Class<?> clazz;
+
+ public static GetMethods action(Class<?> clazz) {
+ return new GetMethods( clazz );
+ }
+
+ private GetMethods(Class<?> clazz) {
+ this.clazz = clazz;
+ }
+
+ public Method[] run() {
+ return clazz.getMethods();
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/GetMethods.java
___________________________________________________________________
Name: svn:keywords
+ Id
Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/LoadClass.java (from rev 19558, validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/LoadClass.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/LoadClass.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/LoadClass.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -0,0 +1,56 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validator.util.privilegedactions;
+
+import java.security.PrivilegedAction;
+import javax.validation.ValidationException;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class LoadClass implements PrivilegedAction<Class<?>> {
+ private final String className;
+ private final Class<?> caller;
+
+ public static LoadClass action(String className, Class<?> caller) {
+ return new LoadClass( className, caller );
+ }
+
+ private LoadClass(String className, Class<?> caller) {
+ this.className = className;
+ this.caller = caller;
+ }
+
+ public Class<?> run() {
+ try {
+ ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
+ if ( contextClassLoader != null ) {
+ return contextClassLoader.loadClass( className );
+ }
+ }
+ catch ( Throwable e ) {
+ // ignore
+ }
+ try {
+ return Class.forName( className, true, caller.getClassLoader() );
+ }
+ catch ( ClassNotFoundException e ) {
+ throw new ValidationException("Unable to load class: " + className, e);
+ }
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/LoadClass.java
___________________________________________________________________
Name: svn:keywords
+ Id
Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/NewInstance.java (from rev 19558, validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/NewInstance.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/NewInstance.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/NewInstance.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -0,0 +1,72 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validator.util.privilegedactions;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.security.PrivilegedAction;
+import javax.validation.ValidationException;
+
+/**
+ * Execute instance creation as privileged action.
+ *
+ * @author Emmanuel Bernard
+ * @author Hardy Ferentschik
+ */
+public class NewInstance<T> implements PrivilegedAction<T> {
+ private final Class<T> clazz;
+ private final String message;
+ private final Class<?>[] parameterTypes;
+ private final Object[] initArgs;
+
+ public static <T> NewInstance<T> action(Class<T> clazz, String message, Object... initArgs) {
+ return new NewInstance<T>( clazz, message, initArgs );
+ }
+
+ private NewInstance(Class<T> clazz, String message, Object... initArgs) {
+ this.clazz = clazz;
+ this.message = message;
+ this.initArgs = initArgs;
+ this.parameterTypes = new Class<?>[initArgs.length];
+ for ( int i = 0; i < initArgs.length; i++ ) {
+ this.parameterTypes[i] = initArgs[i].getClass();
+ }
+ }
+
+ public T run() {
+ try {
+ Constructor<T> constructor = clazz.getConstructor( parameterTypes );
+ return constructor.newInstance( initArgs );
+ }
+ catch ( InstantiationException e ) {
+ throw new ValidationException( "Unable to instantiate " + message + ": " + clazz, e );
+ }
+ catch ( IllegalAccessException e ) {
+ throw new ValidationException( "Unable to instantiate " + clazz, e );
+ }
+ catch ( NoSuchMethodException e ) {
+ throw new ValidationException( "Unable to instantiate " + clazz, e );
+ }
+ catch ( InvocationTargetException e ) {
+ throw new ValidationException( "Unable to instantiate " + clazz, e );
+ }
+ catch ( RuntimeException e ) {
+ throw new ValidationException( "Unable to instantiate " + clazz, e );
+ }
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/NewInstance.java
___________________________________________________________________
Name: svn:keywords
+ Id
Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/SetAccessibility.java (from rev 19558, validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/SetAccessibility.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/SetAccessibility.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/SetAccessibility.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -0,0 +1,43 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validator.util.privilegedactions;
+
+import java.lang.reflect.Member;
+import java.security.PrivilegedAction;
+
+import org.hibernate.validator.util.ReflectionHelper;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class SetAccessibility implements PrivilegedAction<Object> {
+ private final Member member;
+
+ public static SetAccessibility action(Member member) {
+ return new SetAccessibility( member );
+ }
+
+ private SetAccessibility(Member member) {
+ this.member = member;
+ }
+
+ public Object run() {
+ ReflectionHelper.setAccessibility( member );
+ return member;
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/privilegedactions/SetAccessibility.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/xml/ValidationXmlParser.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/xml/ValidationXmlParser.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/xml/ValidationXmlParser.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -37,10 +37,10 @@
import org.slf4j.Logger;
import org.xml.sax.SAXException;
-import org.hibernate.validator.util.GetClassLoader;
-import org.hibernate.validator.util.LoadClass;
+import org.hibernate.validator.util.privilegedactions.GetClassLoader;
+import org.hibernate.validator.util.privilegedactions.LoadClass;
import org.hibernate.validator.util.LoggerFactory;
-import org.hibernate.validator.util.NewInstance;
+import org.hibernate.validator.util.privilegedactions.NewInstance;
/**
* Parser for <i>validation.xml</i> using JAXB.
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/xml/XmlMappingParser.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/xml/XmlMappingParser.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/xml/XmlMappingParser.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -54,13 +54,13 @@
import org.hibernate.validator.metadata.ConstraintHelper;
import org.hibernate.validator.metadata.ConstraintOrigin;
import org.hibernate.validator.metadata.MetaConstraint;
-import org.hibernate.validator.util.ContainsField;
-import org.hibernate.validator.util.ContainsMethod;
-import org.hibernate.validator.util.GetClassLoader;
-import org.hibernate.validator.util.GetDeclaredField;
-import org.hibernate.validator.util.GetMethod;
-import org.hibernate.validator.util.GetMethodFromPropertyName;
-import org.hibernate.validator.util.LoadClass;
+import org.hibernate.validator.util.privilegedactions.ContainsField;
+import org.hibernate.validator.util.privilegedactions.ContainsMethod;
+import org.hibernate.validator.util.privilegedactions.GetClassLoader;
+import org.hibernate.validator.util.privilegedactions.GetDeclaredField;
+import org.hibernate.validator.util.privilegedactions.GetMethod;
+import org.hibernate.validator.util.privilegedactions.GetMethodFromPropertyName;
+import org.hibernate.validator.util.privilegedactions.LoadClass;
import org.hibernate.validator.util.LoggerFactory;
import org.hibernate.validator.util.annotationfactory.AnnotationDescriptor;
import org.hibernate.validator.util.annotationfactory.AnnotationFactory;
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -17,6 +17,8 @@
*/
package org.hibernate.validator.test.cfg;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.ValidationException;
@@ -29,6 +31,7 @@
import org.hibernate.validator.HibernateValidator;
import org.hibernate.validator.HibernateValidatorConfiguration;
import org.hibernate.validator.cfg.ConstraintMapping;
+import org.hibernate.validator.cfg.FutureDefinition;
import org.hibernate.validator.cfg.MinDefinition;
import org.hibernate.validator.cfg.NotNullDefinition;
import org.hibernate.validator.test.util.TestUtil;
@@ -89,11 +92,37 @@
assertConstraintViolation( violations.iterator().next(), "may not be null" );
}
- @Test
- public void testSingleConstraintWrongAccessType() {
+ @Test(enabled = false)
+ public void testInheritedConstraint() {
HibernateValidatorConfiguration config = TestUtil.getConfiguration( HibernateValidator.class );
ConstraintMapping mapping = new ConstraintMapping();
+ mapping.type( Marathon.class )
+ .property( "name", METHOD )
+ .constraint( NotNullDefinition.class )
+ .type( Tournament.class )
+ .property( "tournamentDate", METHOD )
+ .constraint( FutureDefinition.class );
+
+ config.addMapping( mapping );
+
+ ValidatorFactory factory = config.buildValidatorFactory();
+ Validator validator = factory.getValidator();
+
+ Marathon marathon = new Marathon();
+ marathon.setName( "New York Marathon" );
+ Calendar cal = GregorianCalendar.getInstance();
+ cal.set( Calendar.YEAR, -1 );
+ marathon.setTournamentDate( cal.getTime() );
+
+ Set<ConstraintViolation<Marathon>> violations = validator.validate( marathon );
+ assertNumberOfViolations( violations, 1 );
+ assertConstraintViolation( violations.iterator().next(), "may not be null" );
+ }
+
+ @Test
+ public void testSingleConstraintWrongAccessType() {
+ ConstraintMapping mapping = new ConstraintMapping();
try {
mapping.type( Marathon.class )
.property( "numberOfRunners", METHOD )
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Marathon.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Marathon.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Marathon.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
@@ -17,17 +17,18 @@
*/
package org.hibernate.validator.test.cfg;
-import javax.validation.constraints.Min;
-import javax.validation.constraints.NotNull;
+import java.util.Date;
/**
* @author Hardy Ferentschik
*/
-public class Marathon {
+public class Marathon implements Tournament {
private String name;
private long numberOfRunners;
+ private Date tournamentDate;
+
public String getName() {
return name;
}
@@ -35,6 +36,14 @@
public void setName(String name) {
this.name = name;
}
+
+ public Date getTournamentDate() {
+ return tournamentDate;
+ }
+
+ public void setTournamentDate(Date tournamentDate) {
+ this.tournamentDate = tournamentDate;
+ }
}
Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Tournament.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Tournament.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Tournament.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -0,0 +1,29 @@
+// $Id:$
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.hibernate.validator.test.cfg;
+
+import java.util.Date;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public interface Tournament {
+ Date getTournamentDate();
+}
+
+
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/metadata/ConstraintHelperTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/metadata/ConstraintHelperTest.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/metadata/ConstraintHelperTest.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -30,7 +30,7 @@
import org.testng.annotations.Test;
import org.hibernate.validator.metadata.ConstraintHelper;
-import org.hibernate.validator.util.SetAccessibility;
+import org.hibernate.validator.util.privilegedactions.SetAccessibility;
/**
* @author Hardy Ferentschik
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/util/ReflectionHelperTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/util/ReflectionHelperTest.java 2010-05-20 10:29:39 UTC (rev 19565)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/util/ReflectionHelperTest.java 2010-05-20 11:58:01 UTC (rev 19566)
@@ -46,7 +46,7 @@
import static org.testng.Assert.fail;
/**
- * Tests for the <code>ReflectionHelper</code>.
+ * Tests for the {@code ReflectionHelper}.
*
* @author Hardy Ferentschik
*/
14 years, 7 months
Hibernate SVN: r19565 - in validator/trunk/hibernate-validator/src: main/java/org/hibernate/validator/util and 2 other directories.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2010-05-20 06:29:39 -0400 (Thu, 20 May 2010)
New Revision: 19565
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefinition.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ReflectionHelper.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/util/ReflectionHelperTest.java
Log:
HV-274 Some error handling
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefinition.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefinition.java 2010-05-20 05:45:33 UTC (rev 19564)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefinition.java 2010-05-20 10:29:39 UTC (rev 19565)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
@@ -25,6 +25,7 @@
import javax.validation.ValidationException;
import org.hibernate.validator.util.NewInstance;
+import org.hibernate.validator.util.ReflectionHelper;
/**
* @author Hardy Ferentschik
@@ -50,9 +51,17 @@
throw new ValidationException( "ConstraintMapping cannot be null" );
}
- if ( ( ElementType.FIELD.equals( elementType ) || ElementType.METHOD.equals( elementType ) )
- && ( property == null || property.length() == 0 ) ) {
- throw new ValidationException( "A property level constraint cannot have a null or empty property name" );
+ if ( ElementType.FIELD.equals( elementType ) || ElementType.METHOD.equals( elementType ) ) {
+ if ( property == null || property.length() == 0 ) {
+ throw new ValidationException( "A property level constraint cannot have a null or empty property name" );
+ }
+
+ if ( !ReflectionHelper.propertyExists( beanType, property, elementType ) ) {
+ throw new ValidationException(
+ "The class " + beanType + " does not have a property '"
+ + property + "' with access " + elementType
+ );
+ }
}
this.beanType = beanType;
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java 2010-05-20 05:45:33 UTC (rev 19564)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java 2010-05-20 10:29:39 UTC (rev 19565)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
@@ -22,6 +22,8 @@
import java.util.List;
import java.util.Map;
+import org.hibernate.validator.util.ReflectionHelper;
+
/**
* @author Hardy Ferentschik
*/
@@ -38,6 +40,7 @@
protected void addConstraintConfig(ConstraintDefinition<?> definition) {
Class<?> beanClass = definition.getBeanType();
+
if ( configData.containsKey( beanClass ) ) {
configData.get( beanClass ).add( definition );
}
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ReflectionHelper.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ReflectionHelper.java 2010-05-20 05:45:33 UTC (rev 19564)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ReflectionHelper.java 2010-05-20 10:29:39 UTC (rev 19565)
@@ -19,6 +19,7 @@
import java.beans.Introspector;
import java.lang.annotation.Annotation;
+import java.lang.annotation.ElementType;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
@@ -51,8 +52,6 @@
private ReflectionHelper() {
}
- //run client in privileged block
-
@SuppressWarnings("unchecked")
public static <T> T getAnnotationParameter(Annotation annotation, String parameterName, Class<T> type) {
try {
@@ -112,6 +111,56 @@
}
/**
+ * Checks whether the property with the specified name and type exists on the given class.
+ *
+ * @param clazz The class to check for the property. Cannot be {@code null}.
+ * @param property The property name without 'is', 'get' or 'has'. Cannot be {@code null} or empty.
+ * @param elementType The element type. Either {@code ElementType.FIELD} or {@code ElementType METHOD}.
+ *
+ * @return {@code true} is the property and can be access via the specified type, {@code false} otherwise.
+ */
+ public static boolean propertyExists(Class<?> clazz, String property, ElementType elementType) {
+ if ( clazz == null ) {
+ throw new IllegalArgumentException( "The class cannot be null" );
+ }
+
+ if ( property == null || property.length() == 0 ) {
+ throw new IllegalArgumentException( "Property name cannot be null or empty" );
+ }
+
+ if ( !( ElementType.FIELD.equals( elementType ) || ElementType.METHOD.equals( elementType ) ) ) {
+ throw new IllegalArgumentException( "Element type has to be FIELD or METHOD" );
+ }
+
+ boolean exists = false;
+ if ( ElementType.FIELD.equals( elementType ) ) {
+ try {
+ clazz.getDeclaredField( property );
+ exists = true;
+ }
+ catch ( NoSuchFieldException e ) {
+ exists = false;
+ }
+ }
+ else {
+ String methodName = property.substring( 0, 1 ).toUpperCase() + property.substring( 1 );
+ String[] prefixes = {"is", "get", "has"};
+ for(String prefix : prefixes) {
+ try {
+ clazz.getDeclaredMethod( prefix + methodName );
+ exists = true;
+ break;
+ } catch (NoSuchMethodException e) {
+
+ }
+ }
+
+ }
+ return exists;
+ }
+
+
+ /**
* Returns the type of the field of return type of a method.
*
* @param member the member for which to get the type.
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java 2010-05-20 05:45:33 UTC (rev 19564)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java 2010-05-20 10:29:39 UTC (rev 19565)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
@@ -19,9 +19,11 @@
import java.util.Set;
import javax.validation.ConstraintViolation;
+import javax.validation.ValidationException;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
+import org.slf4j.Logger;
import org.testng.annotations.Test;
import org.hibernate.validator.HibernateValidator;
@@ -30,17 +32,21 @@
import org.hibernate.validator.cfg.MinDefinition;
import org.hibernate.validator.cfg.NotNullDefinition;
import org.hibernate.validator.test.util.TestUtil;
+import org.hibernate.validator.util.LoggerFactory;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static org.hibernate.validator.test.util.TestUtil.assertConstraintViolation;
import static org.hibernate.validator.test.util.TestUtil.assertNumberOfViolations;
import static org.testng.Assert.assertTrue;
+import static org.testng.FileAssert.fail;
/**
* @author Hardy Ferentschik
*/
public class ConstraintMappingTest {
+ private static final Logger log = LoggerFactory.make();
+
@Test
public void testConstraintMapping() {
ConstraintMapping mapping = new ConstraintMapping();
@@ -83,24 +89,21 @@
assertConstraintViolation( violations.iterator().next(), "may not be null" );
}
-// @Test
-// public void testSingleConstraintWrongAccessType() {
-// HibernateValidatorConfiguration config = TestUtil.getConfiguration( HibernateValidator.class );
-//
-// ConstraintMapping mapping = new ConstraintMapping();
-// mapping.type( Marathon.class )
-// .property( "numberOfRunners", METHOD )
-// .constraint( NotNullDefinition.class );
-//
-// config.addMapping( mapping );
-//
-// ValidatorFactory factory = config.buildValidatorFactory();
-// Validator validator = factory.getValidator();
-//
-// Set<ConstraintViolation<Marathon>> violations = validator.validate( new Marathon() );
-// assertNumberOfViolations( violations, 1 );
-// assertConstraintViolation( violations.iterator().next(), "may not be null" );
-// }
+ @Test
+ public void testSingleConstraintWrongAccessType() {
+ HibernateValidatorConfiguration config = TestUtil.getConfiguration( HibernateValidator.class );
+
+ ConstraintMapping mapping = new ConstraintMapping();
+ try {
+ mapping.type( Marathon.class )
+ .property( "numberOfRunners", METHOD )
+ .constraint( NotNullDefinition.class );
+ fail();
+ }
+ catch ( ValidationException e ) {
+ log.debug( e.toString() );
+ }
+ }
}
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/util/ReflectionHelperTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/util/ReflectionHelperTest.java 2010-05-20 05:45:33 UTC (rev 19564)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/util/ReflectionHelperTest.java 2010-05-20 10:29:39 UTC (rev 19565)
@@ -36,6 +36,9 @@
import org.hibernate.validator.util.ReflectionHelper;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.TYPE;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
@@ -172,9 +175,33 @@
}
}
+ @Test
+ public void testPropertyExists() {
+ assertTrue( ReflectionHelper.propertyExists( Foo.class, "foo", FIELD ) );
+ assertFalse( ReflectionHelper.propertyExists( Foo.class, "foo", METHOD ) );
+ assertFalse( ReflectionHelper.propertyExists( Foo.class, "bar", FIELD ) );
+ assertTrue( ReflectionHelper.propertyExists( Foo.class, "bar", METHOD ) );
+
+ try {
+ assertTrue( ReflectionHelper.propertyExists( Foo.class, "bar", TYPE ) );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+ }
+
public class TestTypes {
public List<String> stringList;
public Map<String, Object> objectMap;
public String[] stringArray;
}
+
+ public class Foo {
+ String foo;
+
+ public String getBar() {
+ return "bar";
+ }
+ }
}
14 years, 7 months
Hibernate SVN: r19564 - core/trunk/testsuite/src/test/java/org/hibernate/test/keymanytoone/bidir/component.
by hibernate-commits@lists.jboss.org
Author: steve.ebersole(a)jboss.com
Date: 2010-05-20 01:45:33 -0400 (Thu, 20 May 2010)
New Revision: 19564
Modified:
core/trunk/testsuite/src/test/java/org/hibernate/test/keymanytoone/bidir/component/EagerKeyManyToOneTest.java
Log:
HHH-2277 - bidirectional <key-many-to-one> both lazy=false fetch=join lead to infinite loop
Modified: core/trunk/testsuite/src/test/java/org/hibernate/test/keymanytoone/bidir/component/EagerKeyManyToOneTest.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/keymanytoone/bidir/component/EagerKeyManyToOneTest.java 2010-05-20 05:37:43 UTC (rev 19563)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/keymanytoone/bidir/component/EagerKeyManyToOneTest.java 2010-05-20 05:45:33 UTC (rev 19564)
@@ -105,7 +105,7 @@
s.close();
}
- public void testLoadEntityWithEagerFetchingToKeyManyToOneReferenceBackToSelfFailureExpected() {
+ public void testLoadEntityWithEagerFetchingToKeyManyToOneReferenceBackToSelf() {
// long winded method name to say that this is a test specifically for HHH-2277 ;)
// essentially we have a bidirectional association where one side of the
// association is actually part of a composite PK.
14 years, 7 months
Hibernate SVN: r19563 - in core/trunk/core/src/main/java/org/hibernate: loader and 3 other directories.
by hibernate-commits@lists.jboss.org
Author: steve.ebersole(a)jboss.com
Date: 2010-05-20 01:37:43 -0400 (Thu, 20 May 2010)
New Revision: 19563
Added:
core/trunk/core/src/main/java/org/hibernate/loader/PropertyPath.java
Modified:
core/trunk/core/src/main/java/org/hibernate/impl/SessionImpl.java
core/trunk/core/src/main/java/org/hibernate/loader/AbstractEntityJoinWalker.java
core/trunk/core/src/main/java/org/hibernate/loader/JoinWalker.java
core/trunk/core/src/main/java/org/hibernate/loader/Loader.java
core/trunk/core/src/main/java/org/hibernate/loader/OuterJoinableAssociation.java
core/trunk/core/src/main/java/org/hibernate/loader/collection/BasicCollectionJoinWalker.java
core/trunk/core/src/main/java/org/hibernate/loader/collection/OneToManyJoinWalker.java
core/trunk/core/src/main/java/org/hibernate/loader/criteria/CriteriaJoinWalker.java
core/trunk/core/src/main/java/org/hibernate/loader/entity/EntityJoinWalker.java
core/trunk/core/src/main/java/org/hibernate/loader/entity/EntityLoader.java
Log:
HHH-2277 - bidirectional <key-many-to-one> both lazy=false fetch=join lead to infinite loop
Modified: core/trunk/core/src/main/java/org/hibernate/impl/SessionImpl.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/impl/SessionImpl.java 2010-05-19 18:29:45 UTC (rev 19562)
+++ core/trunk/core/src/main/java/org/hibernate/impl/SessionImpl.java 2010-05-20 05:37:43 UTC (rev 19563)
@@ -1022,9 +1022,11 @@
public Object internalLoad(String entityName, Serializable id, boolean eager, boolean nullable) throws HibernateException {
// todo : remove
- LoadEventListener.LoadType type = nullable ?
- LoadEventListener.INTERNAL_LOAD_NULLABLE :
- eager ? LoadEventListener.INTERNAL_LOAD_EAGER : LoadEventListener.INTERNAL_LOAD_LAZY;
+ LoadEventListener.LoadType type = nullable
+ ? LoadEventListener.INTERNAL_LOAD_NULLABLE
+ : eager
+ ? LoadEventListener.INTERNAL_LOAD_EAGER
+ : LoadEventListener.INTERNAL_LOAD_LAZY;
LoadEvent event = new LoadEvent(id, entityName, true, this);
fireLoad(event, type);
if ( !nullable ) {
Modified: core/trunk/core/src/main/java/org/hibernate/loader/AbstractEntityJoinWalker.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/loader/AbstractEntityJoinWalker.java 2010-05-19 18:29:45 UTC (rev 19562)
+++ core/trunk/core/src/main/java/org/hibernate/loader/AbstractEntityJoinWalker.java 2010-05-20 05:37:43 UTC (rev 19563)
@@ -29,7 +29,6 @@
import java.util.Iterator;
import org.hibernate.FetchMode;
-import org.hibernate.LockMode;
import org.hibernate.MappingException;
import org.hibernate.LockOptions;
import org.hibernate.engine.CascadeStyle;
@@ -42,7 +41,6 @@
import org.hibernate.sql.JoinFragment;
import org.hibernate.sql.Select;
import org.hibernate.type.AssociationType;
-import org.hibernate.util.CollectionHelper;
/**
* Abstract walker for walkers which begin at an entity (criteria
@@ -76,23 +74,20 @@
final String whereString,
final String orderByString,
final LockOptions lockOptions) throws MappingException {
+ initAll( whereString, orderByString, lockOptions, AssociationInitCallback.NO_CALLBACK );
+ }
+
+ protected final void initAll(
+ final String whereString,
+ final String orderByString,
+ final LockOptions lockOptions,
+ final AssociationInitCallback callback) throws MappingException {
walkEntityTree( persister, getAlias() );
List allAssociations = new ArrayList();
- allAssociations.addAll(associations);
- allAssociations.add(
- new OuterJoinableAssociation(
- persister.getEntityType(),
- null,
- null,
- alias,
- JoinFragment.LEFT_OUTER_JOIN,
- null,
- getFactory(),
- CollectionHelper.EMPTY_MAP
- )
- );
- initPersisters(allAssociations, lockOptions);
- initStatementString( whereString, orderByString, lockOptions);
+ allAssociations.addAll( associations );
+ allAssociations.add( OuterJoinableAssociation.createRoot( persister.getEntityType(), alias, getFactory() ) );
+ initPersisters( allAssociations, lockOptions, callback );
+ initStatementString( whereString, orderByString, lockOptions );
}
protected final void initProjection(
@@ -162,17 +157,18 @@
return isJoinedFetchEnabledInMapping( config, type );
}
- protected final boolean isJoinFetchEnabledByProfile(OuterJoinLoadable persister, String path, int propertyNumber) {
+ protected final boolean isJoinFetchEnabledByProfile(OuterJoinLoadable persister, PropertyPath path, int propertyNumber) {
if ( !getLoadQueryInfluencers().hasEnabledFetchProfiles() ) {
// perf optimization
return false;
}
// ugh, this stuff has to be made easier...
+ final String fullPath = path.getFullPath();
String rootPropertyName = persister.getSubclassPropertyName( propertyNumber );
- int pos = path.lastIndexOf( rootPropertyName );
+ int pos = fullPath.lastIndexOf( rootPropertyName );
String relativePropertyPath = pos >= 0
- ? path.substring( pos )
+ ? fullPath.substring( pos )
: rootPropertyName;
String fetchRole = persister.getEntityName() + "." + relativePropertyPath;
Modified: core/trunk/core/src/main/java/org/hibernate/loader/JoinWalker.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/loader/JoinWalker.java 2010-05-19 18:29:45 UTC (rev 19562)
+++ core/trunk/core/src/main/java/org/hibernate/loader/JoinWalker.java 2010-05-20 05:37:43 UTC (rev 19563)
@@ -40,8 +40,6 @@
import org.hibernate.engine.JoinHelper;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.LoadQueryInfluencers;
-import org.hibernate.engine.profile.FetchProfile;
-import org.hibernate.engine.profile.Fetch;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.collection.QueryableCollection;
import org.hibernate.persister.entity.EntityPersister;
@@ -93,7 +91,8 @@
this.loadQueryInfluencers = loadQueryInfluencers;
}
-
+
+
public String[] getCollectionSuffixes() {
return collectionSuffixes;
}
@@ -194,7 +193,7 @@
final AssociationType type,
final String[] aliasedLhsColumns,
final String alias,
- final String path,
+ final PropertyPath path,
int currentDepth,
final int joinType) throws MappingException {
if ( joinType >= 0 ) {
@@ -209,7 +208,7 @@
}
}
- protected String getWithClause(String path) {
+ protected String getWithClause(PropertyPath path) {
return "";
}
@@ -221,7 +220,7 @@
final AssociationType type,
final String[] aliasedLhsColumns,
final String alias,
- String path,
+ final PropertyPath path,
final int currentDepth,
final int joinType) throws MappingException {
@@ -236,6 +235,7 @@
// only need to worry about restrictions (and not say adding more
// joins)
OuterJoinableAssociation assoc = new OuterJoinableAssociation(
+ path,
type,
alias,
aliasedLhsColumns,
@@ -245,7 +245,7 @@
getFactory(),
loadQueryInfluencers.getEnabledFilters()
);
- assoc.validateJoin( path );
+ assoc.validateJoin( path.getFullPath() );
associations.add( assoc );
int nextDepth = currentDepth + 1;
@@ -277,7 +277,7 @@
* Walk the association tree for an entity, adding associations which should
* be join fetched to the {@link #associations} inst var. This form is the
* entry point into the walking for a given entity, starting the recursive
- * calls into {@link #walkEntityTree(OuterJoinLoadable, String, String, int)}.
+ * calls into {@link #walkEntityTree(org.hibernate.persister.entity.OuterJoinLoadable, String, PropertyPath ,int)}.
*
* @param persister The persister representing the entity to be walked.
* @param alias The (root) alias to use for this entity/persister.
@@ -286,15 +286,14 @@
protected final void walkEntityTree(
OuterJoinLoadable persister,
String alias) throws MappingException {
- walkEntityTree( persister, alias, "", 0 );
+ walkEntityTree( persister, alias, new PropertyPath(), 0 );
}
/**
* For a collection role, return a list of associations to be fetched by outerjoin
*/
- protected final void walkCollectionTree(QueryableCollection persister, String alias)
- throws MappingException {
- walkCollectionTree(persister, alias, "", 0);
+ protected final void walkCollectionTree(QueryableCollection persister, String alias) throws MappingException {
+ walkCollectionTree( persister, alias, new PropertyPath(), 0 );
//TODO: when this is the entry point, we should use an INNER_JOIN for fetching the many-to-many elements!
}
@@ -302,11 +301,10 @@
* For a collection role, return a list of associations to be fetched by outerjoin
*/
private void walkCollectionTree(
- final QueryableCollection persister,
- final String alias,
- final String path,
- final int currentDepth)
- throws MappingException {
+ final QueryableCollection persister,
+ final String alias,
+ final PropertyPath path,
+ final int currentDepth) throws MappingException {
if ( persister.isOneToMany() ) {
walkEntityTree(
@@ -338,7 +336,7 @@
!useInnerJoin,
currentDepth - 1,
null //operations which cascade as far as the collection also cascade to collection elements
- );
+ );
addAssociationToJoinTreeIfNecessary(
associationType,
aliasedLhsColumns,
@@ -382,7 +380,7 @@
final OuterJoinLoadable persister,
final int propertyNumber,
final String alias,
- final String path,
+ final PropertyPath path,
final boolean nullable,
final int currentDepth) throws MappingException {
String[] aliasedLhsColumns = JoinHelper.getAliasedLHSColumnNames(
@@ -393,10 +391,10 @@
);
String lhsTable = JoinHelper.getLHSTableName(associationType, propertyNumber, persister);
- String subpath = subPath( path, persister.getSubclassPropertyName(propertyNumber) );
+ PropertyPath subPath = path.append( persister.getSubclassPropertyName(propertyNumber) );
int joinType = getJoinType(
persister,
- subpath,
+ subPath,
propertyNumber,
associationType,
persister.getFetchMode( propertyNumber ),
@@ -410,7 +408,7 @@
associationType,
aliasedLhsColumns,
alias,
- subpath,
+ subPath,
currentDepth,
joinType
);
@@ -436,7 +434,7 @@
*/
protected int getJoinType(
OuterJoinLoadable persister,
- final String path,
+ final PropertyPath path,
int propertyNumber,
AssociationType associationType,
FetchMode metadataFetchMode,
@@ -476,7 +474,7 @@
protected int getJoinType(
AssociationType associationType,
FetchMode config,
- String path,
+ PropertyPath path,
String lhsTable,
String[] lhsColumns,
boolean nullable,
@@ -498,18 +496,18 @@
* Walk the association tree for an entity, adding associations which should
* be join fetched to the {@link #associations} inst var. This form is the
* entry point into the walking for a given entity, starting the recursive
- * calls into {@link #walkEntityTree(OuterJoinLoadable, String, String, int)}.
+ * calls into {@link #walkEntityTree(org.hibernate.persister.entity.OuterJoinLoadable, String, PropertyPath ,int)}.
*
* @param persister The persister representing the entity to be walked.
* @param alias The (root) alias to use for this entity/persister.
- * @param path todo this seems to be rooted at the *root* persister
+ * @param path The property path to the entity being walked
* @param currentDepth The current join depth
* @throws org.hibernate.MappingException ???
*/
private void walkEntityTree(
final OuterJoinLoadable persister,
final String alias,
- final String path,
+ final PropertyPath path,
final int currentDepth) throws MappingException {
int n = persister.countSubclassProperties();
for ( int i = 0; i < n; i++ ) {
@@ -527,13 +525,13 @@
}
else if ( type.isComponentType() ) {
walkComponentTree(
- ( AbstractComponentType ) type,
- i,
- 0,
- persister,
- alias,
- subPath( path, persister.getSubclassPropertyName(i) ),
- currentDepth
+ ( AbstractComponentType ) type,
+ i,
+ 0,
+ persister,
+ alias,
+ path.append( persister.getSubclassPropertyName(i) ),
+ currentDepth
);
}
}
@@ -559,7 +557,7 @@
int begin,
final OuterJoinLoadable persister,
final String alias,
- final String path,
+ final PropertyPath path,
final int currentDepth) throws MappingException {
Type[] types = componentType.getSubtypes();
String[] propertyNames = componentType.getPropertyNames();
@@ -574,11 +572,11 @@
);
String lhsTable = JoinHelper.getLHSTableName(associationType, propertyNumber, persister);
- String subpath = subPath( path, propertyNames[i] );
+ final PropertyPath subPath = path.append( propertyNames[i] );
final boolean[] propertyNullability = componentType.getPropertyNullability();
final int joinType = getJoinType(
persister,
- subpath,
+ subPath,
propertyNumber,
associationType,
componentType.getFetchMode(i),
@@ -592,21 +590,21 @@
associationType,
aliasedLhsColumns,
alias,
- subpath,
+ subPath,
currentDepth,
joinType
);
}
else if ( types[i].isComponentType() ) {
- String subpath = subPath( path, propertyNames[i] );
+ final PropertyPath subPath = path.append( propertyNames[i] );
walkComponentTree(
( AbstractComponentType ) types[i],
propertyNumber,
begin,
persister,
alias,
- subpath,
+ subPath,
currentDepth
);
}
@@ -623,7 +621,7 @@
final String[] cols,
final QueryableCollection persister,
final String alias,
- final String path,
+ final PropertyPath path,
final int currentDepth) throws MappingException {
Type[] types = compositeType.getSubtypes();
@@ -640,12 +638,12 @@
// (or even a property-ref) in a composite-element:
String[] aliasedLhsColumns = StringHelper.qualify(alias, lhsColumns);
- String subpath = subPath( path, propertyNames[i] );
+ final PropertyPath subPath = path.append( propertyNames[i] );
final boolean[] propertyNullability = compositeType.getPropertyNullability();
final int joinType = getJoinType(
associationType,
compositeType.getFetchMode(i),
- subpath,
+ subPath,
persister.getTableName(),
lhsColumns,
propertyNullability==null || propertyNullability[i],
@@ -656,19 +654,19 @@
associationType,
aliasedLhsColumns,
alias,
- subpath,
+ subPath,
currentDepth,
joinType
);
}
else if ( types[i].isComponentType() ) {
- String subpath = subPath( path, propertyNames[i] );
+ final PropertyPath subPath = path.append( propertyNames[i] );
walkCompositeElementTree(
(AbstractComponentType) types[i],
lhsColumns,
persister,
alias,
- subpath,
+ subPath,
currentDepth
);
}
@@ -678,18 +676,6 @@
}
/**
- * Extend the path by the given property name
- */
- private static String subPath(String path, String property) {
- if ( path==null || path.length()==0) {
- return property;
- }
- else {
- return StringHelper.qualify(path, property);
- }
- }
-
- /**
* Use an inner join if it is a non-null association and this
* is the "first" join in a series
*/
@@ -746,10 +732,7 @@
return type.isEntityType() && isJoinedFetchEnabledInMapping(config, type) ;
}
- protected String generateTableAlias(
- final int n,
- final String path,
- final Joinable joinable) {
+ protected String generateTableAlias(final int n, final PropertyPath path, final Joinable joinable) {
return StringHelper.generateAlias( joinable.getName(), n );
}
@@ -761,10 +744,7 @@
* Used to detect circularities in the joined graph, note that
* this method is side-effecty
*/
- protected boolean isDuplicateAssociation(
- final String foreignKeyTable,
- final String[] foreignKeyColumns
- ) {
+ protected boolean isDuplicateAssociation(final String foreignKeyTable, final String[] foreignKeyColumns) {
AssociationKey associationKey = new AssociationKey(foreignKeyColumns, foreignKeyTable);
return !visitedAssociationKeys.add( associationKey );
}
@@ -773,11 +753,7 @@
* Used to detect circularities in the joined graph, note that
* this method is side-effecty
*/
- protected boolean isDuplicateAssociation(
- final String lhsTable,
- final String[] lhsColumnNames,
- final AssociationType type
- ) {
+ protected boolean isDuplicateAssociation(final String lhsTable, final String[] lhsColumnNames, final AssociationType type) {
final String foreignKeyTable;
final String[] foreignKeyColumns;
if ( type.getForeignKeyDirection()==ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT ) {
@@ -815,20 +791,23 @@
* Should we join this association?
*/
protected boolean isJoinable(
- final int joinType,
- final Set visitedAssociationKeys,
- final String lhsTable,
- final String[] lhsColumnNames,
- final AssociationType type,
- final int depth
- ) {
- if (joinType<0) return false;
+ final int joinType,
+ final Set visitedAssociationKeys,
+ final String lhsTable,
+ final String[] lhsColumnNames,
+ final AssociationType type,
+ final int depth) {
+
+ if ( joinType < 0 ) {
+ return false;
+ }
- if (joinType==JoinFragment.INNER_JOIN) return true;
-
+ if ( joinType == JoinFragment.INNER_JOIN ) {
+ return true;
+ }
+
Integer maxFetchDepth = getFactory().getSettings().getMaximumFetchDepth();
- final boolean tooDeep = maxFetchDepth!=null &&
- depth >= maxFetchDepth.intValue();
+ final boolean tooDeep = maxFetchDepth!=null && depth >= maxFetchDepth.intValue();
return !tooDeep && !isDuplicateAssociation(lhsTable, lhsColumnNames, type);
}
@@ -985,8 +964,22 @@
initPersisters( associations, new LockOptions(lockMode));
}
+ protected static interface AssociationInitCallback {
+ public static final AssociationInitCallback NO_CALLBACK = new AssociationInitCallback() {
+ public void associationProcessed(OuterJoinableAssociation oja, int position) {
+ }
+ };
+
+ public void associationProcessed(OuterJoinableAssociation oja, int position);
+ }
protected void initPersisters(final List associations, final LockOptions lockOptions) throws MappingException {
-
+ initPersisters( associations, lockOptions, AssociationInitCallback.NO_CALLBACK );
+ }
+
+ protected void initPersisters(
+ final List associations,
+ final LockOptions lockOptions,
+ final AssociationInitCallback callback) throws MappingException {
final int joins = countEntityPersisters(associations);
final int collections = countCollectionPersisters(associations);
@@ -1013,6 +1006,7 @@
aliases[i] = oj.getRHSAlias();
owners[i] = oj.getOwner(associations);
ownerAssociationTypes[i] = (EntityType) oj.getJoinableType();
+ callback.associationProcessed( oj, i );
i++;
}
@@ -1029,11 +1023,12 @@
if ( collPersister.isOneToMany() ) {
persisters[i] = (Loadable) collPersister.getElementPersister();
aliases[i] = oj.getRHSAlias();
+ callback.associationProcessed( oj, i );
i++;
}
}
}
-
+
if ( ArrayHelper.isAllNegative(owners) ) owners = null;
if ( collectionOwners!=null && ArrayHelper.isAllNegative(collectionOwners) ) {
collectionOwners = null;
Modified: core/trunk/core/src/main/java/org/hibernate/loader/Loader.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/loader/Loader.java 2010-05-19 18:29:45 UTC (rev 19562)
+++ core/trunk/core/src/main/java/org/hibernate/loader/Loader.java 2010-05-20 05:37:43 UTC (rev 19563)
@@ -173,6 +173,10 @@
return null;
}
+ protected int[][] getCompositeKeyManyToOneTargetIndices() {
+ return null;
+ }
+
/**
* What lock options does this load entities with?
*
@@ -599,20 +603,8 @@
final Loadable[] persisters = getEntityPersisters();
final int entitySpan = persisters.length;
+ extractKeysFromResultSet( persisters, queryParameters, resultSet, session, keys, lockModesArray, hydratedObjects );
- for ( int i = 0; i < entitySpan; i++ ) {
- keys[i] = getKeyFromResultSet(
- i,
- persisters[i],
- i == entitySpan - 1 ?
- queryParameters.getOptionalId() :
- null,
- resultSet,
- session
- );
- //TODO: the i==entitySpan-1 bit depends upon subclass implementation (very bad)
- }
-
registerNonExists( keys, persisters, session );
// this call is side-effecty
@@ -648,6 +640,98 @@
}
+ protected void extractKeysFromResultSet(
+ Loadable[] persisters,
+ QueryParameters queryParameters,
+ ResultSet resultSet,
+ SessionImplementor session,
+ EntityKey[] keys,
+ LockMode[] lockModes,
+ List hydratedObjects) throws SQLException {
+ final int entitySpan = persisters.length;
+
+ final int numberOfPersistersToProcess;
+ final Serializable optionalId = queryParameters.getOptionalId();
+ if ( isSingleRowLoader() && optionalId != null ) {
+ keys[ entitySpan - 1 ] = new EntityKey( optionalId, persisters[ entitySpan - 1 ], session.getEntityMode() );
+ // skip the last persister below...
+ numberOfPersistersToProcess = entitySpan - 1;
+ }
+ else {
+ numberOfPersistersToProcess = entitySpan;
+ }
+
+ final Object[] hydratedKeyState = new Object[numberOfPersistersToProcess];
+
+ for ( int i = 0; i < numberOfPersistersToProcess; i++ ) {
+ final Type idType = persisters[i].getIdentifierType();
+ hydratedKeyState[i] = idType.hydrate( resultSet, getEntityAliases()[i].getSuffixedKeyAliases(), session, null );
+ }
+
+ for ( int i = 0; i < numberOfPersistersToProcess; i++ ) {
+ final Type idType = persisters[i].getIdentifierType();
+ if ( idType.isComponentType() && getCompositeKeyManyToOneTargetIndices() != null ) {
+ // we may need to force resolve any key-many-to-one(s)
+ int[] keyManyToOneTargetIndices = getCompositeKeyManyToOneTargetIndices()[i];
+ // todo : better solution is to order the index processing based on target indices
+ // that would account for multiple levels whereas this scheme does not
+ if ( keyManyToOneTargetIndices != null ) {
+ for ( int targetIndex : keyManyToOneTargetIndices ) {
+ if ( targetIndex < numberOfPersistersToProcess ) {
+ final Type targetIdType = persisters[targetIndex].getIdentifierType();
+ final Serializable targetId = (Serializable) targetIdType.resolve(
+ hydratedKeyState[targetIndex],
+ session,
+ null
+ );
+ // todo : need a way to signal that this key is resolved and its data resolved
+ keys[targetIndex] = new EntityKey( targetId, persisters[targetIndex], session.getEntityMode() );
+ }
+
+ // this part copied from #getRow, this section could be refactored out
+ Object object = session.getEntityUsingInterceptor( keys[targetIndex] );
+ if ( object != null ) {
+ //its already loaded so don't need to hydrate it
+ instanceAlreadyLoaded(
+ resultSet,
+ targetIndex,
+ persisters[targetIndex],
+ keys[targetIndex],
+ object,
+ lockModes[targetIndex],
+ session
+ );
+ }
+ else {
+ object = instanceNotYetLoaded(
+ resultSet,
+ targetIndex,
+ persisters[targetIndex],
+ getEntityAliases()[targetIndex].getRowIdAlias(),
+ keys[targetIndex],
+ lockModes[targetIndex],
+ getOptionalObjectKey( queryParameters, session ),
+ queryParameters.getOptionalObject(),
+ hydratedObjects,
+ session
+ );
+ }
+ }
+ }
+ }
+ final Serializable resolvedId = (Serializable) idType.resolve( hydratedKeyState[i], session, null );
+ keys[i] = resolvedId == null ? null : new EntityKey( resolvedId, persisters[i], session.getEntityMode() );
+ }
+ }
+
+ private Serializable determineResultId(SessionImplementor session, Serializable optionalId, Type idType, Serializable resolvedId) {
+ final boolean idIsResultId = optionalId != null
+ && resolvedId != null
+ && idType.isEqual( optionalId, resolvedId, session.getEntityMode(), factory );
+ final Serializable resultId = idIsResultId ? optionalId : resolvedId;
+ return resultId;
+ }
+
protected void applyPostLoadLocks(Object[] row, LockMode[] lockModesArray, SessionImplementor session) {
}
Modified: core/trunk/core/src/main/java/org/hibernate/loader/OuterJoinableAssociation.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/loader/OuterJoinableAssociation.java 2010-05-19 18:29:45 UTC (rev 19562)
+++ core/trunk/core/src/main/java/org/hibernate/loader/OuterJoinableAssociation.java 2010-05-20 05:37:43 UTC (rev 19563)
@@ -1,10 +1,10 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
- * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
- * distributed under license by Red Hat Middleware LLC.
+ * distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
@@ -20,7 +20,6 @@
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
- *
*/
package org.hibernate.loader;
@@ -35,6 +34,7 @@
import org.hibernate.sql.JoinFragment;
import org.hibernate.type.AssociationType;
import org.hibernate.type.EntityType;
+import org.hibernate.util.CollectionHelper;
/**
* Part of the Hibernate SQL rendering internals. This class represents
@@ -43,6 +43,7 @@
* @author Gavin King
*/
public final class OuterJoinableAssociation {
+ private final PropertyPath propertyPath;
private final AssociationType joinableType;
private final Joinable joinable;
private final String lhsAlias; // belong to other persister
@@ -53,7 +54,25 @@
private final String on;
private final Map enabledFilters;
+ public static OuterJoinableAssociation createRoot(
+ AssociationType joinableType,
+ String alias,
+ SessionFactoryImplementor factory) {
+ return new OuterJoinableAssociation(
+ new PropertyPath(),
+ joinableType,
+ null,
+ null,
+ alias,
+ JoinFragment.LEFT_OUTER_JOIN,
+ null,
+ factory,
+ CollectionHelper.EMPTY_MAP
+ );
+ }
+
public OuterJoinableAssociation(
+ PropertyPath propertyPath,
AssociationType joinableType,
String lhsAlias,
String[] lhsColumns,
@@ -62,6 +81,7 @@
String withClause,
SessionFactoryImplementor factory,
Map enabledFilters) throws MappingException {
+ this.propertyPath = propertyPath;
this.joinableType = joinableType;
this.lhsAlias = lhsAlias;
this.lhsColumns = lhsColumns;
@@ -74,14 +94,26 @@
this.enabledFilters = enabledFilters; // needed later for many-to-many/filter application
}
+ public PropertyPath getPropertyPath() {
+ return propertyPath;
+ }
+
public int getJoinType() {
return joinType;
}
+ public String getLhsAlias() {
+ return lhsAlias;
+ }
+
public String getRHSAlias() {
return rhsAlias;
}
+ public String getRhsAlias() {
+ return rhsAlias;
+ }
+
private boolean isOneToOne() {
if ( joinableType.isEntityType() ) {
EntityType etype = (EntityType) joinableType;
@@ -90,7 +122,6 @@
else {
return false;
}
-
}
public AssociationType getJoinableType() {
@@ -150,12 +181,8 @@
}
public void validateJoin(String path) throws MappingException {
- if (
- rhsColumns==null ||
- lhsColumns==null ||
- lhsColumns.length!=rhsColumns.length ||
- lhsColumns.length==0
- ) {
+ if ( rhsColumns==null || lhsColumns==null
+ || lhsColumns.length!=rhsColumns.length || lhsColumns.length==0 ) {
throw new MappingException("invalid join columns for association: " + path);
}
}
Added: core/trunk/core/src/main/java/org/hibernate/loader/PropertyPath.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/loader/PropertyPath.java (rev 0)
+++ core/trunk/core/src/main/java/org/hibernate/loader/PropertyPath.java 2010-05-20 05:37:43 UTC (rev 19563)
@@ -0,0 +1,90 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.loader;
+
+import org.hibernate.util.StringHelper;
+
+/**
+* TODO : javadoc
+*
+* @author Steve Ebersole
+*/
+public class PropertyPath {
+ private final PropertyPath parent;
+ private final String property;
+ private final String fullPath;
+
+ public PropertyPath(PropertyPath parent, String property) {
+ this.parent = parent;
+ this.property = property;
+
+ final String prefix;
+ if ( parent != null ) {
+ final String resolvedParent = parent.getFullPath();
+ if ( StringHelper.isEmpty( resolvedParent ) ) {
+ prefix = "";
+ }
+ else {
+ prefix = resolvedParent + '.';
+ }
+ }
+ else {
+ prefix = "";
+ }
+ this.fullPath = prefix + property;
+ }
+
+ public PropertyPath(String property) {
+ this( null, property );
+ }
+
+ public PropertyPath() {
+ this( "" );
+ }
+
+ public PropertyPath append(String property) {
+ return new PropertyPath( this, property );
+ }
+
+ public PropertyPath getParent() {
+ return parent;
+ }
+
+ public String getProperty() {
+ return property;
+ }
+
+ public String getFullPath() {
+ return fullPath;
+ }
+
+ public boolean isRoot() {
+ return parent == null && StringHelper.isEmpty( property );
+ }
+
+ @Override
+ public String toString() {
+ return getClass().getSimpleName() + '[' + fullPath + ']';
+ }
+}
Modified: core/trunk/core/src/main/java/org/hibernate/loader/collection/BasicCollectionJoinWalker.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/loader/collection/BasicCollectionJoinWalker.java 2010-05-19 18:29:45 UTC (rev 19562)
+++ core/trunk/core/src/main/java/org/hibernate/loader/collection/BasicCollectionJoinWalker.java 2010-05-20 05:37:43 UTC (rev 19563)
@@ -27,8 +27,6 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
-import java.util.Map;
-import java.util.Set;
import org.hibernate.FetchMode;
import org.hibernate.LockMode;
@@ -38,12 +36,12 @@
import org.hibernate.engine.CascadeStyle;
import org.hibernate.loader.BasicLoader;
import org.hibernate.loader.OuterJoinableAssociation;
+import org.hibernate.loader.PropertyPath;
import org.hibernate.persister.collection.QueryableCollection;
import org.hibernate.persister.entity.OuterJoinLoadable;
import org.hibernate.sql.JoinFragment;
import org.hibernate.sql.Select;
import org.hibernate.type.AssociationType;
-import org.hibernate.util.CollectionHelper;
import org.hibernate.util.StringHelper;
/**
@@ -73,18 +71,7 @@
List allAssociations = new ArrayList();
allAssociations.addAll(associations);
- allAssociations.add(
- new OuterJoinableAssociation(
- collectionPersister.getCollectionType(),
- null,
- null,
- alias,
- JoinFragment.LEFT_OUTER_JOIN,
- null,
- getFactory(),
- CollectionHelper.EMPTY_MAP
- )
- );
+ allAssociations.add( OuterJoinableAssociation.createRoot( collectionPersister.getCollectionType(), alias, getFactory() ) );
initPersisters(allAssociations, LockMode.NONE);
initStatementString(alias, batchSize, subquery);
}
@@ -155,7 +142,7 @@
protected int getJoinType(
OuterJoinLoadable persister,
- String path,
+ PropertyPath path,
int propertyNumber,
AssociationType associationType,
FetchMode metadataFetchMode,
@@ -177,7 +164,7 @@
currentDepth
);
//we can use an inner join for the many-to-many
- if ( joinType==JoinFragment.LEFT_OUTER_JOIN && "".equals(path) ) {
+ if ( joinType==JoinFragment.LEFT_OUTER_JOIN && path.isRoot() ) {
joinType=JoinFragment.INNER_JOIN;
}
return joinType;
Modified: core/trunk/core/src/main/java/org/hibernate/loader/collection/OneToManyJoinWalker.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/loader/collection/OneToManyJoinWalker.java 2010-05-19 18:29:45 UTC (rev 19562)
+++ core/trunk/core/src/main/java/org/hibernate/loader/collection/OneToManyJoinWalker.java 2010-05-20 05:37:43 UTC (rev 19563)
@@ -79,17 +79,7 @@
List allAssociations = new ArrayList();
allAssociations.addAll(associations);
- allAssociations.add( new OuterJoinableAssociation(
- oneToManyPersister.getCollectionType(),
- null,
- null,
- alias,
- JoinFragment.LEFT_OUTER_JOIN,
- null,
- getFactory(),
- CollectionHelper.EMPTY_MAP
- ) );
-
+ allAssociations.add( OuterJoinableAssociation.createRoot( oneToManyPersister.getCollectionType(), alias, getFactory() ) );
initPersisters(allAssociations, LockMode.NONE);
initStatementString(elementPersister, alias, batchSize, subquery);
}
Modified: core/trunk/core/src/main/java/org/hibernate/loader/criteria/CriteriaJoinWalker.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/loader/criteria/CriteriaJoinWalker.java 2010-05-19 18:29:45 UTC (rev 19562)
+++ core/trunk/core/src/main/java/org/hibernate/loader/criteria/CriteriaJoinWalker.java 2010-05-20 05:37:43 UTC (rev 19563)
@@ -30,7 +30,6 @@
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
-import org.hibernate.LockMode;
import org.hibernate.MappingException;
import org.hibernate.LockOptions;
import org.hibernate.engine.CascadeStyle;
@@ -38,12 +37,12 @@
import org.hibernate.engine.LoadQueryInfluencers;
import org.hibernate.impl.CriteriaImpl;
import org.hibernate.loader.AbstractEntityJoinWalker;
+import org.hibernate.loader.PropertyPath;
import org.hibernate.persister.entity.Joinable;
import org.hibernate.persister.entity.OuterJoinLoadable;
import org.hibernate.persister.entity.Queryable;
import org.hibernate.type.AssociationType;
import org.hibernate.type.Type;
-import org.hibernate.type.TypeFactory;
import org.hibernate.util.ArrayHelper;
/**
@@ -121,7 +120,7 @@
protected int getJoinType(
OuterJoinLoadable persister,
- final String path,
+ final PropertyPath path,
int propertyNumber,
AssociationType associationType,
FetchMode metadataFetchMode,
@@ -130,15 +129,15 @@
String[] lhsColumns,
final boolean nullable,
final int currentDepth) throws MappingException {
- if ( translator.isJoin( path ) ) {
- return translator.getJoinType( path );
+ if ( translator.isJoin( path.getFullPath() ) ) {
+ return translator.getJoinType( path.getFullPath() );
}
else {
if ( translator.hasProjection() ) {
return -1;
}
else {
- FetchMode fetchMode = translator.getRootCriteria().getFetchMode( path );
+ FetchMode fetchMode = translator.getRootCriteria().getFetchMode( path.getFullPath() );
if ( isDefaultFetchMode( fetchMode ) ) {
if ( isJoinFetchEnabledByProfile( persister, path, propertyNumber ) ) {
return getJoinType( nullable, currentDepth );
@@ -174,14 +173,14 @@
protected int getJoinType(
AssociationType associationType,
FetchMode config,
- String path,
+ PropertyPath path,
String lhsTable,
String[] lhsColumns,
boolean nullable,
int currentDepth,
CascadeStyle cascadeStyle) throws MappingException {
- return ( translator.isJoin( path ) ?
- translator.getJoinType( path ) :
+ return ( translator.isJoin( path.getFullPath() ) ?
+ translator.getJoinType( path.getFullPath() ) :
super.getJoinType(
associationType,
config,
@@ -208,9 +207,9 @@
( (Queryable) getPersister() ).filterFragment( getAlias(), getLoadQueryInfluencers().getEnabledFilters() );
}
- protected String generateTableAlias(int n, String path, Joinable joinable) {
+ protected String generateTableAlias(int n, PropertyPath path, Joinable joinable) {
if ( joinable.consumesEntityAlias() ) {
- final Criteria subcriteria = translator.getCriteria(path);
+ final Criteria subcriteria = translator.getCriteria( path.getFullPath() );
String sqlAlias = subcriteria==null ? null : translator.getSQLAlias(subcriteria);
if (sqlAlias!=null) {
userAliasList.add( subcriteria.getAlias() ); //alias may be null
@@ -235,8 +234,8 @@
return "criteria query";
}
- protected String getWithClause(String path) {
- return translator.getWithClause(path);
+ protected String getWithClause(PropertyPath path) {
+ return translator.getWithClause( path.getFullPath() );
}
}
Modified: core/trunk/core/src/main/java/org/hibernate/loader/entity/EntityJoinWalker.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/loader/entity/EntityJoinWalker.java 2010-05-19 18:29:45 UTC (rev 19562)
+++ core/trunk/core/src/main/java/org/hibernate/loader/entity/EntityJoinWalker.java 2010-05-20 05:37:43 UTC (rev 19563)
@@ -24,18 +24,27 @@
*/
package org.hibernate.loader.entity;
+import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashMap;
import org.hibernate.FetchMode;
import org.hibernate.LockMode;
+import org.hibernate.LockOptions;
import org.hibernate.MappingException;
-import org.hibernate.LockOptions;
import org.hibernate.engine.CascadeStyle;
+import org.hibernate.engine.LoadQueryInfluencers;
import org.hibernate.engine.SessionFactoryImplementor;
-import org.hibernate.engine.LoadQueryInfluencers;
import org.hibernate.loader.AbstractEntityJoinWalker;
+import org.hibernate.loader.OuterJoinableAssociation;
+import org.hibernate.loader.PropertyPath;
+import org.hibernate.persister.collection.QueryableCollection;
+import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.persister.entity.OuterJoinLoadable;
import org.hibernate.type.AssociationType;
+import org.hibernate.type.ComponentType;
+import org.hibernate.type.EntityType;
+import org.hibernate.type.Type;
/**
* A walker for loaders that fetch entities
@@ -46,13 +55,14 @@
public class EntityJoinWalker extends AbstractEntityJoinWalker {
private final LockOptions lockOptions = new LockOptions();
+ private final int[][] compositeKeyManyToOneTargetIndices;
public EntityJoinWalker(
OuterJoinLoadable persister,
String[] uniqueKey,
int batchSize,
LockMode lockMode,
- SessionFactoryImplementor factory,
+ final SessionFactoryImplementor factory,
LoadQueryInfluencers loadQueryInfluencers) throws MappingException {
super( persister, factory, loadQueryInfluencers );
@@ -62,7 +72,9 @@
//include the discriminator and class-level where, but not filters
.append( persister.filterFragment( getAlias(), Collections.EMPTY_MAP ) );
- initAll( whereCondition.toString(), "", lockOptions);
+ AssociationInitCallbackImpl callback = new AssociationInitCallbackImpl( factory );
+ initAll( whereCondition.toString(), "", lockOptions, callback );
+ this.compositeKeyManyToOneTargetIndices = callback.resolve();
}
public EntityJoinWalker(
@@ -79,12 +91,14 @@
//include the discriminator and class-level where, but not filters
.append( persister.filterFragment( getAlias(), Collections.EMPTY_MAP ) );
- initAll( whereCondition.toString(), "", lockOptions);
+ AssociationInitCallbackImpl callback = new AssociationInitCallbackImpl( factory );
+ initAll( whereCondition.toString(), "", lockOptions, callback );
+ this.compositeKeyManyToOneTargetIndices = callback.resolve();
}
protected int getJoinType(
OuterJoinLoadable persister,
- String path,
+ PropertyPath path,
int propertyNumber,
AssociationType associationType,
FetchMode metadataFetchMode,
@@ -116,5 +130,132 @@
public String getComment() {
return "load " + getPersister().getEntityName();
}
-
+
+ public int[][] getCompositeKeyManyToOneTargetIndices() {
+ return compositeKeyManyToOneTargetIndices;
+ }
+
+ private static class AssociationInitCallbackImpl implements AssociationInitCallback {
+ private final SessionFactoryImplementor factory;
+ private final HashMap<String,OuterJoinableAssociation> associationsByAlias
+ = new HashMap<String, OuterJoinableAssociation>();
+ private final HashMap<String,Integer> positionsByAlias = new HashMap<String, Integer>();
+ private final ArrayList<String> aliasesForAssociationsWithCompositesIds
+ = new ArrayList<String>();
+
+ public AssociationInitCallbackImpl(SessionFactoryImplementor factory) {
+ this.factory = factory;
+ }
+
+ public void associationProcessed(OuterJoinableAssociation oja, int position) {
+ associationsByAlias.put( oja.getRhsAlias(), oja );
+ positionsByAlias.put( oja.getRhsAlias(), position );
+ EntityPersister entityPersister = null;
+ if ( oja.getJoinableType().isCollectionType() ) {
+ entityPersister = ( ( QueryableCollection) oja.getJoinable() ).getElementPersister();
+ }
+ else if ( oja.getJoinableType().isEntityType() ) {
+ entityPersister = ( EntityPersister ) oja.getJoinable();
+ }
+ if ( entityPersister != null
+ && entityPersister.getIdentifierType().isComponentType()
+ && ! entityPersister.getEntityMetamodel().getIdentifierProperty().isEmbedded()
+ && hasAssociation( (ComponentType) entityPersister.getIdentifierType() ) ) {
+ aliasesForAssociationsWithCompositesIds.add( oja.getRhsAlias() );
+ }
+ }
+
+ private boolean hasAssociation(ComponentType componentType) {
+ int i = 0;
+ for ( Type subType : componentType.getSubtypes() ) {
+ if ( subType.isEntityType() ) {
+ return true;
+ }
+ else if ( subType.isComponentType() && hasAssociation( ( (ComponentType) subType ) ) ) {
+ return true;
+ }
+ i++;
+ }
+ return false;
+ }
+
+ public int[][] resolve() {
+ int[][] compositeKeyManyToOneTargetIndices = null;
+ for ( final String aliasWithCompositeId : aliasesForAssociationsWithCompositesIds ) {
+ final OuterJoinableAssociation joinWithCompositeId = associationsByAlias.get( aliasWithCompositeId );
+ final ArrayList<Integer> keyManyToOneTargetIndices = new ArrayList<Integer>();
+ // for each association with a composite id containing key-many-to-one(s), find the bidirectional side of
+ // each key-many-to-one (if exists) to see if it is eager as well. If so, we need to track the indices
+ EntityPersister entityPersister = null;
+ if ( joinWithCompositeId.getJoinableType().isCollectionType() ) {
+ entityPersister = ( ( QueryableCollection) joinWithCompositeId.getJoinable() ).getElementPersister();
+ }
+ else if ( joinWithCompositeId.getJoinableType().isEntityType() ) {
+ entityPersister = ( EntityPersister ) joinWithCompositeId.getJoinable();
+ }
+
+ findKeyManyToOneTargetIndices(
+ keyManyToOneTargetIndices,
+ joinWithCompositeId,
+ (ComponentType) entityPersister.getIdentifierType()
+ );
+
+ if ( ! keyManyToOneTargetIndices.isEmpty() ) {
+ if ( compositeKeyManyToOneTargetIndices == null ) {
+ compositeKeyManyToOneTargetIndices = new int[ associationsByAlias.size() ][];
+ }
+ int position = positionsByAlias.get( aliasWithCompositeId );
+ compositeKeyManyToOneTargetIndices[position] = new int[ keyManyToOneTargetIndices.size() ];
+ int i = 0;
+ for ( int index : keyManyToOneTargetIndices ) {
+ compositeKeyManyToOneTargetIndices[position][i] = index;
+ i++;
+ }
+ }
+ }
+ return compositeKeyManyToOneTargetIndices;
+ }
+
+ private void findKeyManyToOneTargetIndices(
+ ArrayList<Integer> keyManyToOneTargetIndices,
+ OuterJoinableAssociation joinWithCompositeId,
+ ComponentType componentType) {
+ for ( Type subType : componentType.getSubtypes() ) {
+ if ( subType.isEntityType() ) {
+ Integer index = locateKeyManyToOneTargetIndex( joinWithCompositeId, (EntityType) subType );
+ if ( index != null ) {
+ keyManyToOneTargetIndices.add( index );
+ }
+ }
+ else if ( subType.isComponentType() ) {
+ findKeyManyToOneTargetIndices(
+ keyManyToOneTargetIndices,
+ joinWithCompositeId,
+ (ComponentType) subType
+ );
+ }
+ }
+ }
+
+ private Integer locateKeyManyToOneTargetIndex(OuterJoinableAssociation joinWithCompositeId, EntityType keyManyToOneType) {
+ // the lhs (if one) is a likely candidate
+ if ( joinWithCompositeId.getLhsAlias() != null ) {
+ final OuterJoinableAssociation lhs = associationsByAlias.get( joinWithCompositeId.getLhsAlias() );
+ if ( keyManyToOneType.getAssociatedEntityName( factory ).equals( lhs.getJoinableType().getAssociatedEntityName( factory ) ) ) {
+ return positionsByAlias.get( lhs.getRhsAlias() );
+ }
+ }
+ // otherwise, seek out OuterJoinableAssociation which are RHS of given OuterJoinableAssociation
+ // (joinWithCompositeId)
+ for ( OuterJoinableAssociation oja : associationsByAlias.values() ) {
+ if ( oja.getLhsAlias() != null && oja.getLhsAlias().equals( joinWithCompositeId.getRhsAlias() ) ) {
+ if ( keyManyToOneType.equals( oja.getJoinableType() ) ) {
+ return positionsByAlias.get( oja.getLhsAlias() );
+ }
+ }
+ }
+ return null;
+ }
+ }
+
}
\ No newline at end of file
Modified: core/trunk/core/src/main/java/org/hibernate/loader/entity/EntityLoader.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/loader/entity/EntityLoader.java 2010-05-19 18:29:45 UTC (rev 19562)
+++ core/trunk/core/src/main/java/org/hibernate/loader/entity/EntityLoader.java 2010-05-20 05:37:43 UTC (rev 19563)
@@ -24,14 +24,12 @@
*/
package org.hibernate.loader.entity;
-import org.hibernate.HibernateException;
import org.hibernate.LockMode;
+import org.hibernate.LockOptions;
import org.hibernate.MappingException;
-import org.hibernate.LockOptions;
+import org.hibernate.engine.LoadQueryInfluencers;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.SessionImplementor;
-import org.hibernate.engine.LoadQueryInfluencers;
-import org.hibernate.loader.JoinWalker;
import org.hibernate.persister.entity.OuterJoinLoadable;
import org.hibernate.type.Type;
@@ -46,6 +44,7 @@
public class EntityLoader extends AbstractEntityLoader {
private final boolean batchLoader;
+ private final int[][] compositeKeyManyToOneTargetIndices;
public EntityLoader(
OuterJoinLoadable persister,
@@ -107,7 +106,7 @@
LoadQueryInfluencers loadQueryInfluencers) throws MappingException {
super( persister, uniqueKeyType, factory, loadQueryInfluencers );
- JoinWalker walker = new EntityJoinWalker(
+ EntityJoinWalker walker = new EntityJoinWalker(
persister,
uniqueKey,
batchSize,
@@ -116,7 +115,7 @@
loadQueryInfluencers
);
initFromWalker( walker );
-
+ this.compositeKeyManyToOneTargetIndices = walker.getCompositeKeyManyToOneTargetIndices();
postInstantiate();
batchLoader = batchSize > 1;
@@ -134,7 +133,7 @@
LoadQueryInfluencers loadQueryInfluencers) throws MappingException {
super( persister, uniqueKeyType, factory, loadQueryInfluencers );
- JoinWalker walker = new EntityJoinWalker(
+ EntityJoinWalker walker = new EntityJoinWalker(
persister,
uniqueKey,
batchSize,
@@ -143,7 +142,7 @@
loadQueryInfluencers
);
initFromWalker( walker );
-
+ this.compositeKeyManyToOneTargetIndices = walker.getCompositeKeyManyToOneTargetIndices();
postInstantiate();
batchLoader = batchSize > 1;
@@ -163,5 +162,8 @@
protected boolean isSingleRowLoader() {
return !batchLoader;
}
-
+
+ public int[][] getCompositeKeyManyToOneTargetIndices() {
+ return compositeKeyManyToOneTargetIndices;
+ }
}
\ No newline at end of file
14 years, 7 months
Hibernate SVN: r19562 - search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test.
by hibernate-commits@lists.jboss.org
Author: stliu
Date: 2010-05-19 14:29:45 -0400 (Wed, 19 May 2010)
New Revision: 19562
Added:
search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/RollbackWork.java
Modified:
search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/TestCase.java
search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/TransactionTest.java
Log:
JBPAPP-2289 HSEARCH unit test failure: org.hibernate.search.test.reader.functionality.TestableSharingBufferReaderProvider
Added: search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/RollbackWork.java
===================================================================
--- search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/RollbackWork.java (rev 0)
+++ search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/RollbackWork.java 2010-05-19 18:29:45 UTC (rev 19562)
@@ -0,0 +1,42 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ *
+ */
+package org.hibernate.search.test;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+import org.hibernate.jdbc.Work;
+
+/**
+ *
+ * @author stliu
+ */
+public class RollbackWork implements Work{
+
+ public void execute( Connection connection ) throws SQLException {
+ connection.rollback();
+ }
+
+}
\ No newline at end of file
Modified: search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/TestCase.java
===================================================================
--- search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/TestCase.java 2010-05-19 16:44:39 UTC (rev 19561)
+++ search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/TestCase.java 2010-05-19 18:29:45 UTC (rev 19562)
@@ -2,8 +2,6 @@
package org.hibernate.search.test;
import java.io.InputStream;
-import java.sql.Connection;
-import java.sql.SQLException;
import org.apache.lucene.analysis.StopAnalyzer;
@@ -17,7 +15,6 @@
import org.hibernate.dialect.Dialect;
import org.hibernate.event.FlushEventListener;
import org.hibernate.event.def.DefaultFlushEventListener;
-import org.hibernate.jdbc.Work;
import org.hibernate.search.event.FullTextIndexEventListener;
/**
@@ -79,13 +76,6 @@
}
}
- private class RollbackWork implements Work{
-
- public void execute( Connection connection ) throws SQLException {
- connection.rollback();
- }
-
- }
protected void runTest() throws Throwable {
try {
super.runTest();
Modified: search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/TransactionTest.java
===================================================================
--- search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/TransactionTest.java 2010-05-19 16:44:39 UTC (rev 19561)
+++ search/branches/v3_1_1_GA_CP/src/test/java/org/hibernate/search/test/TransactionTest.java 2010-05-19 18:29:45 UTC (rev 19562)
@@ -6,7 +6,6 @@
import org.apache.lucene.index.IndexReader;
import org.hibernate.Session;
-import org.hibernate.cfg.Configuration;
/**
* @author Emmanuel Bernard
14 years, 7 months
Hibernate SVN: r19561 - in validator/trunk/hibernate-validator/src: main/java/org/hibernate/validator/cfg and 2 other directories.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2010-05-19 12:44:39 -0400 (Wed, 19 May 2010)
New Revision: 19561
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Marathon.java
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/HibernateValidatorConfiguration.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ConfigurationImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ValidatorFactoryImpl.java
Log:
HV-274 Initial integration with ValidatorFactory (work in progress!)
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/HibernateValidatorConfiguration.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/HibernateValidatorConfiguration.java 2010-05-19 16:21:20 UTC (rev 19560)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/HibernateValidatorConfiguration.java 2010-05-19 16:44:39 UTC (rev 19561)
@@ -19,6 +19,7 @@
import javax.validation.Configuration;
+import org.hibernate.validator.cfg.ConstraintMapping;
import org.hibernate.validator.resourceloading.ResourceBundleLocator;
/**
@@ -62,4 +63,5 @@
*/
ResourceBundleLocator getDefaultResourceBundleLocator();
+ HibernateValidatorConfiguration addMapping(ConstraintMapping mapping);
}
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java 2010-05-19 16:21:20 UTC (rev 19560)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java 2010-05-19 16:44:39 UTC (rev 19561)
@@ -18,7 +18,6 @@
package org.hibernate.validator.cfg;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -39,12 +38,13 @@
protected void addConstraintConfig(ConstraintDefinition<?> definition) {
Class<?> beanClass = definition.getBeanType();
- if(configData.containsKey( beanClass )) {
+ if ( configData.containsKey( beanClass ) ) {
configData.get( beanClass ).add( definition );
- } else {
+ }
+ else {
List<ConstraintDefinition<?>> definitionList = new ArrayList<ConstraintDefinition<?>>();
definitionList.add( definition );
- configData.put(beanClass, definitionList);
+ configData.put( beanClass, definitionList );
}
}
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ConfigurationImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ConfigurationImpl.java 2010-05-19 16:21:20 UTC (rev 19560)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ConfigurationImpl.java 2010-05-19 16:44:39 UTC (rev 19561)
@@ -36,6 +36,7 @@
import org.slf4j.Logger;
import org.hibernate.validator.HibernateValidatorConfiguration;
+import org.hibernate.validator.cfg.ConstraintMapping;
import org.hibernate.validator.engine.resolver.DefaultTraversableResolver;
import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator;
import org.hibernate.validator.resourceloading.PlatformResourceBundleLocator;
@@ -46,7 +47,7 @@
import org.hibernate.validator.xml.ValidationXmlParser;
/**
- * Hibernate specific <code>Configuration</code> implementation.
+ * Hibernate specific {@code Configuration} implementation.
*
* @author Emmanuel Bernard
* @author Hardy Ferentschik
@@ -72,8 +73,8 @@
private ValidationBootstrapParameters validationBootstrapParameters;
private boolean ignoreXmlConfiguration = false;
-
private Set<InputStream> configurationStreams = new HashSet<InputStream>();
+ private ConstraintMapping mapping;
public ConfigurationImpl(BootstrapState state) {
if ( state.getValidationProviderResolver() == null ) {
@@ -126,6 +127,7 @@
return this;
}
+
public ValidatorFactory buildValidatorFactory() {
parseValidationXml();
ValidatorFactory factory = null;
@@ -210,6 +212,15 @@
return defaultResourceBundleLocator;
}
+ public HibernateValidatorConfiguration addMapping(ConstraintMapping mapping) {
+ this.mapping = mapping;
+ return this;
+ }
+
+ public ConstraintMapping getMapping() {
+ return mapping;
+ }
+
private boolean isSpecificProvider() {
return validationBootstrapParameters.provider != null;
}
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ValidatorFactoryImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ValidatorFactoryImpl.java 2010-05-19 16:21:20 UTC (rev 19560)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ValidatorFactoryImpl.java 2010-05-19 16:44:39 UTC (rev 19561)
@@ -20,6 +20,7 @@
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Member;
+import java.security.AccessController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -34,6 +35,8 @@
import javax.validation.ValidatorFactory;
import javax.validation.spi.ConfigurationState;
+import org.hibernate.validator.cfg.ConstraintDefinition;
+import org.hibernate.validator.cfg.ConstraintMapping;
import org.hibernate.validator.metadata.AnnotationIgnores;
import org.hibernate.validator.metadata.BeanMetaDataCache;
import org.hibernate.validator.metadata.BeanMetaDataImpl;
@@ -41,11 +44,15 @@
import org.hibernate.validator.metadata.ConstraintHelper;
import org.hibernate.validator.metadata.ConstraintOrigin;
import org.hibernate.validator.metadata.MetaConstraint;
+import org.hibernate.validator.util.GetDeclaredField;
import org.hibernate.validator.util.ReflectionHelper;
+import org.hibernate.validator.util.annotationfactory.AnnotationDescriptor;
+import org.hibernate.validator.util.annotationfactory.AnnotationFactory;
import org.hibernate.validator.xml.XmlMappingParser;
/**
- * Factory returning initialized <code>Validator</code> instances.
+ * Factory returning initialized <code>Validator</code> instances. This is Hibernate Validator's default
+ * implementation of the {@code ValidatorFactory} interface.
*
* @author Emmanuel Bernard
* @author Hardy Ferentschik
@@ -56,6 +63,10 @@
private final TraversableResolver traversableResolver;
private final ConstraintValidatorFactory constraintValidatorFactory;
private final ConstraintHelper constraintHelper;
+
+ /**
+ * Used to cache the constraint meta data for validated entities
+ */
private final BeanMetaDataCache beanMetaDataCache;
public ValidatorFactoryImpl(ConfigurationState configurationState) {
@@ -66,10 +77,17 @@
this.constraintHelper = new ConstraintHelper();
this.beanMetaDataCache = new BeanMetaDataCache();
- //HV-302; don't load XmlMappingParser if not necessary
+ // HV-302; don't load XmlMappingParser if not necessary
if ( !configurationState.getMappingStreams().isEmpty() ) {
- initBeanMetaData( configurationState.getMappingStreams() );
+ initXmlConfiguration( configurationState.getMappingStreams() );
}
+
+ if ( configurationState instanceof ConfigurationImpl ) {
+ ConfigurationImpl hibernateSpecificConfig = ( ConfigurationImpl ) configurationState;
+ if ( hibernateSpecificConfig.getMapping() != null ) {
+ initProgrammaticConfiguration( hibernateSpecificConfig.getMapping() );
+ }
+ }
}
public Validator getValidator() {
@@ -102,23 +120,83 @@
);
}
- private <T> void initBeanMetaData(Set<InputStream> mappingStreams) {
+ private <A extends Annotation, T> void initProgrammaticConfiguration(ConstraintMapping mapping) {
+ Map<Class<?>, List<ConstraintDefinition<?>>> configData = mapping.getConfigData();
+ for ( Map.Entry<Class<?>, List<ConstraintDefinition<?>>> entry : configData.entrySet() ) {
+ Map<Class<?>, List<MetaConstraint<T, ?>>> constraints = new HashMap<Class<?>, List<MetaConstraint<T, ?>>>();
+ for ( ConstraintDefinition<?> config : entry.getValue() ) {
+ AnnotationDescriptor<A> annotationDescriptor = new AnnotationDescriptor<A>(
+ ( Class<A> ) config.getConstraintType()
+ );
+ for ( Map.Entry<String, Object> parameter : config.getParameters().entrySet() ) {
+ annotationDescriptor.setValue( parameter.getKey(), parameter.getValue() );
+ }
+ A annotation;
+ try {
+ annotation = AnnotationFactory.create( annotationDescriptor );
+ }
+ catch ( RuntimeException e ) {
+ throw new ValidationException(
+ "Unable to create annotation for configured constraint: " + e.getMessage(), e
+ );
+ }
+
+ ConstraintDescriptorImpl<A> constraintDescriptor = new ConstraintDescriptorImpl<A>(
+ annotation, constraintHelper, config.getElementType(), ConstraintOrigin.DEFINED_LOCALLY
+ );
+
+ final Member member;
+ GetDeclaredField action = GetDeclaredField.action( config.getBeanType(), config.getProperty() );
+ if ( System.getSecurityManager() != null ) {
+ member = AccessController.doPrivileged( action );
+ }
+ else {
+ member = action.run();
+ }
+ MetaConstraint<T, ?> metaConstraint = new MetaConstraint(
+ config.getBeanType(), member, constraintDescriptor
+ );
+ List<MetaConstraint<T, ?>> constraintList = new ArrayList<MetaConstraint<T, ?>>();
+ constraintList.add( metaConstraint );
+ constraints.put( config.getBeanType(), constraintList );
+
+
+ BeanMetaDataImpl<T> metaData = new BeanMetaDataImpl<T>(
+ ( Class<T> ) config.getBeanType(),
+ constraintHelper,
+ new ArrayList<Class<?>>(),
+ constraints,
+ new ArrayList<Member>(),
+ new AnnotationIgnores(),
+ beanMetaDataCache
+ );
+
+ beanMetaDataCache.addBeanMetaData( ( Class<T> ) config.getBeanType(), metaData );
+ }
+ }
+ }
+
+ private <T> void initXmlConfiguration(Set<InputStream> mappingStreams) {
+
XmlMappingParser mappingParser = new XmlMappingParser( constraintHelper );
mappingParser.parse( mappingStreams );
- Set<Class<?>> processedClasses = mappingParser.getProcessedClasses();
+ Set<Class<?>> xmlConfiguredClasses = mappingParser.getXmlConfiguredClasses();
AnnotationIgnores annotationIgnores = mappingParser.getAnnotationIgnores();
- for ( Class<?> clazz : processedClasses ) {
+ for ( Class<?> clazz : xmlConfiguredClasses ) {
@SuppressWarnings("unchecked")
Class<T> beanClass = ( Class<T> ) clazz;
- List<Class<?>> classes = new ArrayList<Class<?>>();
- ReflectionHelper.computeClassHierarchy( beanClass, classes );
+ List<Class<?>> classes = ReflectionHelper.computeClassHierarchy( beanClass );
Map<Class<?>, List<MetaConstraint<T, ?>>> constraints = new HashMap<Class<?>, List<MetaConstraint<T, ?>>>();
List<Member> cascadedMembers = new ArrayList<Member>();
+ // we need to collect all constraints which apply for a single class. Due to constraint inheritance
+ // some constraints might be configured in super classes or interfaces. The xml configuration does not
+ // imply any order so we have to check whether any of the super classes or interfaces of a given bean has
+ // as well been configured via xml
for ( Class<?> classInHierarchy : classes ) {
- if ( processedClasses.contains( classInHierarchy ) ) {
+ if ( xmlConfiguredClasses.contains( classInHierarchy ) ) {
addXmlConfiguredConstraints( mappingParser, beanClass, classInHierarchy, constraints );
addXmlCascadedMember( mappingParser, classInHierarchy, cascadedMembers );
}
Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java 2010-05-19 16:44:39 UTC (rev 19561)
@@ -0,0 +1,106 @@
+// $Id:$
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.hibernate.validator.test.cfg;
+
+import java.util.Set;
+import javax.validation.ConstraintViolation;
+import javax.validation.Validator;
+import javax.validation.ValidatorFactory;
+
+import org.testng.annotations.Test;
+
+import org.hibernate.validator.HibernateValidator;
+import org.hibernate.validator.HibernateValidatorConfiguration;
+import org.hibernate.validator.cfg.ConstraintMapping;
+import org.hibernate.validator.cfg.MinDefinition;
+import org.hibernate.validator.cfg.NotNullDefinition;
+import org.hibernate.validator.test.util.TestUtil;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static org.hibernate.validator.test.util.TestUtil.assertConstraintViolation;
+import static org.hibernate.validator.test.util.TestUtil.assertNumberOfViolations;
+import static org.testng.Assert.assertTrue;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class ConstraintMappingTest {
+ @Test
+ public void testConstraintMapping() {
+ ConstraintMapping mapping = new ConstraintMapping();
+ mapping.type( Marathon.class )
+ .property( "name", METHOD )
+ .constraint( NotNullDefinition.class )
+ .property( "numberOfRunners", FIELD )
+ .constraint( MinDefinition.class ).value( 1 );
+
+ assertTrue( mapping.getConfigData().containsKey( Marathon.class ) );
+ assertTrue( mapping.getConfigData().get( Marathon.class ).size() == 2 );
+ }
+
+ @Test
+ public void testNoConstraintViolationForUnmappedEntity() {
+ HibernateValidatorConfiguration config = TestUtil.getConfiguration( HibernateValidator.class );
+ ValidatorFactory factory = config.buildValidatorFactory();
+ Validator validator = factory.getValidator();
+
+ Set<ConstraintViolation<Marathon>> violations = validator.validate( new Marathon() );
+ assertNumberOfViolations( violations, 0 );
+ }
+
+ @Test
+ public void testSingleConstraint() {
+ HibernateValidatorConfiguration config = TestUtil.getConfiguration( HibernateValidator.class );
+
+ ConstraintMapping mapping = new ConstraintMapping();
+ mapping.type( Marathon.class )
+ .property( "name", METHOD )
+ .constraint( NotNullDefinition.class );
+
+ config.addMapping( mapping );
+
+ ValidatorFactory factory = config.buildValidatorFactory();
+ Validator validator = factory.getValidator();
+
+ Set<ConstraintViolation<Marathon>> violations = validator.validate( new Marathon() );
+ assertNumberOfViolations( violations, 1 );
+ assertConstraintViolation( violations.iterator().next(), "may not be null" );
+ }
+
+// @Test
+// public void testSingleConstraintWrongAccessType() {
+// HibernateValidatorConfiguration config = TestUtil.getConfiguration( HibernateValidator.class );
+//
+// ConstraintMapping mapping = new ConstraintMapping();
+// mapping.type( Marathon.class )
+// .property( "numberOfRunners", METHOD )
+// .constraint( NotNullDefinition.class );
+//
+// config.addMapping( mapping );
+//
+// ValidatorFactory factory = config.buildValidatorFactory();
+// Validator validator = factory.getValidator();
+//
+// Set<ConstraintViolation<Marathon>> violations = validator.validate( new Marathon() );
+// assertNumberOfViolations( violations, 1 );
+// assertConstraintViolation( violations.iterator().next(), "may not be null" );
+// }
+}
+
+
Property changes on: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Marathon.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Marathon.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Marathon.java 2010-05-19 16:44:39 UTC (rev 19561)
@@ -0,0 +1,40 @@
+// $Id:$
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.hibernate.validator.test.cfg;
+
+import javax.validation.constraints.Min;
+import javax.validation.constraints.NotNull;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class Marathon {
+ private String name;
+
+ private long numberOfRunners;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
+
+
Property changes on: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/Marathon.java
___________________________________________________________________
Name: svn:keywords
+ Id
14 years, 7 months
Hibernate SVN: r19560 - validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/messageinterpolation.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2010-05-19 12:21:20 -0400 (Wed, 19 May 2010)
New Revision: 19560
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/messageinterpolation/ResourceBundleMessageInterpolator.java
Log:
HV-274 Made sure keyword substitution is enabled
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/messageinterpolation/ResourceBundleMessageInterpolator.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/messageinterpolation/ResourceBundleMessageInterpolator.java 2010-05-19 16:20:53 UTC (rev 19559)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/messageinterpolation/ResourceBundleMessageInterpolator.java 2010-05-19 16:21:20 UTC (rev 19560)
@@ -1,4 +1,4 @@
-// $Id: ResourceBundleMessageInterpolator.java 19081 2010-03-22 20:19:52Z hardy.ferentschik $
+// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/messageinterpolation/ResourceBundleMessageInterpolator.java
___________________________________________________________________
Name: svn:keywords
+ Id
14 years, 7 months