diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/util/ReflectionUtil.java b/hibernate-testing/src/main/java/org/hibernate/testing/util/ReflectionUtil.java index b99c9802e3..c75fb8270f 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/util/ReflectionUtil.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/util/ReflectionUtil.java @@ -1,3 +1,9 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ package org.hibernate.testing.util; import java.lang.reflect.Constructor; @@ -13,8 +19,10 @@ public class ReflectionUtil { /** * Get a field from a given class + * * @param clazz clazz * @param name field name + * * @return field object */ public static Field getField(Class clazz, String name) { @@ -23,19 +31,21 @@ public class ReflectionUtil { field.setAccessible( true ); return field; } - catch ( NoSuchFieldException e ) { + catch (NoSuchFieldException e) { Class superClass = clazz.getSuperclass(); if ( !clazz.equals( superClass ) ) { return getField( superClass, name ); } - throw new IllegalArgumentException( "Class " + clazz + " does not contain a " + name + " field", e); + throw new IllegalArgumentException( "Class " + clazz + " does not contain a " + name + " field", e ); } } /** * Get a field value from a given object + * * @param target Object whose field is being read * @param name field name + * * @return field object */ public static T getFieldValue(Object target, String name) { @@ -44,32 +54,38 @@ public class ReflectionUtil { field.setAccessible( true ); return (T) field.get( target ); } - catch ( NoSuchFieldException e ) { - throw new IllegalArgumentException( "Class " + target.getClass() + " does not contain a " + name + " field", e); + catch (NoSuchFieldException e) { + throw new IllegalArgumentException( + "Class " + target.getClass() + " does not contain a " + name + " field", + e + ); } - catch ( IllegalAccessException e ) { - throw new IllegalArgumentException( "Cannot set field " + name, e); + catch (IllegalAccessException e) { + throw new IllegalArgumentException( "Cannot set field " + name, e ); } } /** * Get a field value from a given class + * * @param target Class whose field is being read * @param name field name + * * @return field value */ public static T getStaticFieldValue(Class target, String name) { try { - Field field = getField(target, name); + Field field = getField( target, name ); return (T) field.get( null ); } - catch ( IllegalAccessException e ) { - throw new IllegalArgumentException( "Cannot set field " + name, e); + catch (IllegalAccessException e) { + throw new IllegalArgumentException( "Cannot set field " + name, e ); } } /** * Set target Object field to a certain value + * * @param target Object whose field is being set * @param field Object field to set * @param value the new value for the given field @@ -78,118 +94,128 @@ public class ReflectionUtil { try { field.set( target, value ); } - catch ( IllegalAccessException e ) { - throw new IllegalArgumentException("Field " + field + " could not be set", e ); + catch (IllegalAccessException e) { + throw new IllegalArgumentException( "Field " + field + " could not be set", e ); } } /** * Set target Object field to a certain value + * * @param target Object whose field is being set * @param fieldName Object field naem to set * @param value the new value for the given field */ public static void setField(Object target, String fieldName, Object value) { try { - Field field = getField(target.getClass(), fieldName); + Field field = getField( target.getClass(), fieldName ); field.set( target, value ); } - catch ( IllegalAccessException e ) { - throw new IllegalArgumentException("Field " + fieldName + " could not be set", e ); + catch (IllegalAccessException e) { + throw new IllegalArgumentException( "Field " + fieldName + " could not be set", e ); } } /** * Set target Class field to a certain value + * * @param target Class whose field is being set * @param fieldName Class field name to set * @param value the new value for the given field */ public static void setStaticField(Class target, String fieldName, Object value) { try { - Field field = getField(target, fieldName); + Field field = getField( target, fieldName ); field.set( null, value ); } - catch ( IllegalAccessException e ) { - throw new IllegalArgumentException("Field " + fieldName + " could not be set", e ); + catch (IllegalAccessException e) { + throw new IllegalArgumentException( "Field " + fieldName + " could not be set", e ); } } /** * New target Object instance using the given arguments + * * @param constructorSupplier constructor supplier * @param args Constructor arguments + * * @return new Object instance */ public static T newInstance(Supplier> constructorSupplier, Object... args) { try { - Constructor constructor = constructorSupplier.get(); + Constructor constructor = constructorSupplier.get(); constructor.setAccessible( true ); return (T) constructor.newInstance( args ); } - catch ( IllegalAccessException | InstantiationException | InvocationTargetException e ) { - throw new IllegalArgumentException("Constructor could not be called", e ); + catch (IllegalAccessException | InstantiationException | InvocationTargetException e) { + throw new IllegalArgumentException( "Constructor could not be called", e ); } } /** * New target Object instance using the given Class name + * * @param className class name + * * @return new Object instance */ public static T newInstance(String className) { try { return (T) Class.forName( className ).newInstance(); } - catch ( ClassNotFoundException | IllegalAccessException | InstantiationException e ) { - throw new IllegalArgumentException("Constructor could not be called", e ); + catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) { + throw new IllegalArgumentException( "Constructor could not be called", e ); } } /** * Get setter method * - * @param target target object - * @param property property + * @param target target object + * @param property property * @param parameterType setter parameter type + * * @return setter method */ public static Method getSetter(Object target, String property, Class parameterType) { - String setterMethodName = "set" + property.substring(0, 1).toUpperCase() + property.substring(1); - Method setter = getMethod(target, setterMethodName, parameterType); - setter.setAccessible(true); + String setterMethodName = "set" + property.substring( 0, 1 ).toUpperCase() + property.substring( 1 ); + Method setter = getMethod( target, setterMethodName, parameterType ); + setter.setAccessible( true ); return setter; } /** * Get target method * - * @param target target object - * @param methodName method name + * @param target target object + * @param methodName method name * @param parameterTypes method parameter types + * * @return return value */ public static Method getMethod(Object target, String methodName, Class... parameterTypes) { try { - return target.getClass().getMethod(methodName, parameterTypes); - } catch (NoSuchMethodException e) { - throw new IllegalArgumentException(e); + return target.getClass().getMethod( methodName, parameterTypes ); + } + catch (NoSuchMethodException e) { + throw new IllegalArgumentException( e ); } } /** * Invoke setter method with the given parameter * - * @param target target object - * @param property property + * @param target target object + * @param property property * @param parameter setter parameter */ public static void setProperty(Object target, String property, Object parameter) { - Method setter = getSetter( target, property, parameter.getClass()); + Method setter = getSetter( target, property, parameter.getClass() ); try { - setter.invoke(target, parameter); - } catch (IllegalAccessException | InvocationTargetException e) { - throw new IllegalArgumentException(e); + setter.invoke( target, parameter ); + } + catch (IllegalAccessException | InvocationTargetException e) { + throw new IllegalArgumentException( e ); } } }