Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014-2015 the original author or authors.
   *
   * 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.dbflute.util;
 
 import java.util.Map;

Author(s):
modified by jflute (originated in Seasar2)
 
 public class DfReflectionUtil {
 
     // ===================================================================================
     //                                                                          Definition
     //                                                                          ==========
     protected static final Type[] EMPTY_TYPES = new Type[0];
     protected static Map<Class<?>, Class<?>> wrapperToPrimitiveMap = new HashMap<Class<?>, Class<?>>();
     protected static Map<Class<?>, Class<?>> primitiveToWrapperMap = new HashMap<Class<?>, Class<?>>();
     protected static Map<StringClass<?>> primitiveClsssNameMap = new HashMap<StringClass<?>>();
     static {
         .put(Character.class.);
         .put(Byte.class.);
         .put(Short.class.);
         .put(Integer.class.);
         .put(Long.class.);
         .put(Double.class.);
         .put(Float.class.);
         .put(Boolean.class.);
 
         .put(.Character.class);
         .put(.Byte.class);
         .put(.Short.class);
         .put(.Integer.class);
         .put(.Long.class);
         .put(.Double.class);
         .put(.Float.class);
         .put(.Boolean.class);
 
     }
 
     protected static final Method IS_BRIDGE_METHOD = getIsBridgeMethod();
     protected static final Method IS_SYNTHETIC_METHOD = getIsSyntheticMethod();
 
     protected static Method getIsBridgeMethod() {
         try {
             return Method.class.getMethod("isBridge", (Class[]) null);
         } catch (final NoSuchMethodException e) {
             return null;
         }
     }
 
     protected static Method getIsSyntheticMethod() {
         try {
             return Method.class.getMethod("isSynthetic", (Class[]) null);
         } catch (final NoSuchMethodException e) {
             return null;
         }
     }
 
     // ===================================================================================
     //                                                                               Class
     //                                                                               =====
     public static Class<?> forName(String className) {
         ClassLoader loader = Thread.currentThread().getContextClassLoader();
        try {
            return Class.forName(classNametrueloader);
        } catch (ClassNotFoundException e) {
            String msg = "The class was not found: class=" + className + " loader=" + loader;
            throw new ReflectionFailureException(msge);
        }
    }
    public static Object newInstance(Class<?> clazz) {
        assertObjectNotNull("clazz"clazz);
        try {
            return clazz.newInstance();
        } catch (InstantiationException e) {
            String msg = "Failed to instantiate the class: " + clazz;
            throw new ReflectionFailureException(msge);
        } catch (IllegalAccessException e) {
            String msg = "Illegal access to the class: " + clazz;
            throw new ReflectionFailureException(msge);
        }
    }
    public static Constructor<?> getConstructor(Class<?> clazzClass<?>[] argTypes) {
        try {
            return clazz.getConstructor(argTypes);
        } catch (NoSuchMethodException e) {
            String msg = "Such a method was not found:";
            msg = msg + " class=" + clazz + " argTypes=" + Arrays.asList(argTypes);
            throw new ReflectionFailureException(msge);
        }
    }
    public static Object newInstance(Constructor<?> constructorObject[] args) {
        try {
            return constructor.newInstance(args);
        } catch (InstantiationException e) {
            String msg = "Failed to instantiate the class: " + constructor;
            throw new ReflectionFailureException(msge);
        } catch (IllegalAccessException e) {
            String msg = "Illegal access to the constructor: " + constructor;
            throw new ReflectionFailureException(msge);
        } catch (InvocationTargetException e) {
            String msg = "The InvocationTargetException occurred: " + constructor;
            throw new ReflectionFailureException(msge.getTargetException());
        }
    }
    public static boolean isAssignableFrom(Class<?> toClassClass<?> fromClass) {
        if (toClass == Object.class && !fromClass.isPrimitive()) {
            return true;
        }
        if (toClass.isPrimitive()) {
            fromClass = getPrimitiveClassIfWrapper(fromClass);
        }
        return toClass.isAssignableFrom(fromClass);
    }
    public static Class<?> getPrimitiveClass(Class<?> clazz) {
        return (Class<?>) .get(clazz);
    }
    public static Class<?> getPrimitiveClassIfWrapper(Class<?> clazz) {
        Class<?> ret = getPrimitiveClass(clazz);
        if (ret != null) {
            return ret;
        }
        return clazz;
    }
    public static Class<?> getWrapperClass(Class<?> clazz) {
        return (Class<?>) .get(clazz);
    }
    // ===================================================================================
    //                                                                               Field
    //                                                                               =====
    public static Field getAccessibleField(Class<?> clazzString fieldName) {
        assertObjectNotNull("clazz"clazz);
        return findField(clazzfieldName.);
    }
    public static Field getPublicField(Class<?> clazzString fieldName) {
        assertObjectNotNull("clazz"clazz);
        return findField(clazzfieldName.);
    }
    public static Field getWholeField(Class<?> clazzString fieldName) {
        assertObjectNotNull("clazz"clazz);
        return findField(clazzfieldName.);
    }
    protected static Field findField(Class<?> clazzString fieldNameVisibilityType visibilityType) {
        assertObjectNotNull("clazz"clazz);
        for (Class<?> target = clazztarget != null && target != Object.classtarget = target.getSuperclass()) {
            final Field declaredField;
            try {
                declaredField = target.getDeclaredField(fieldName);
            } catch (SecurityException e) {
                String msg = "The security violation was found: " + fieldName;
                throw new IllegalStateException(msge);
            } catch (NoSuchFieldException continued) {
                continue;
            }
            final int modifier = declaredField.getModifiers();
            if (isOutOfTargetForPublic(visibilityTypemodifier)) {
                continue;
            }
            if (isOutOfTargetForAccessible(visibilityTypemodifierclazztarget)) {
                continue;
            }
            return declaredField;
        }
        return null;
    }
    public static Object getValue(Field fieldObject target) {
        assertObjectNotNull("field"field);
        try {
            return field.get(target);
        } catch (IllegalAccessException e) {
            String msg = "Illegal access to the field: field=" + field + " target=" + target;
            throw new ReflectionFailureException(msge);
        }
    }
    public static Object getValueForcedly(Field fieldObject target) {
        assertObjectNotNull("field"field);
        field.setAccessible(true);
        return getValue(fieldtarget);
    }
    public static void setValue(Field fieldObject targetObject value) {
        assertObjectNotNull("field"field);
        try {
            field.set(targetvalue);
        } catch (IllegalAccessException e) {
            String msg = "Illegal access to the field: field=" + field + " target=" + target + " value=" + value;
            throw new ReflectionFailureException(msge);
        }
    }
    public static void setValueForcedly(Field fieldObject targetObject value) {
        assertObjectNotNull("field"field);
        field.setAccessible(true);
        setValue(fieldtargetvalue);
    }
    public static boolean isStaticFinalField(Field field) {
        final int mod = field.getModifiers();
        return Modifier.isStatic(mod) && Modifier.isFinal(mod);
    }
    public static boolean isStaticVariableField(Field field) {
        final int mod = field.getModifiers();
        return Modifier.isStatic(mod) && !Modifier.isFinal(mod);
    }
    public static boolean isInstanceFinalField(Field field) {
        final int mod = field.getModifiers();
        return !Modifier.isStatic(mod) && Modifier.isFinal(mod);
    }
    public static boolean isInstanceVariableField(Field field) {
        final int mod = field.getModifiers();
        return !Modifier.isStatic(mod) && !Modifier.isFinal(mod);
    }
    public static boolean isPublicField(Field field) {
        final int mod = field.getModifiers();
        return Modifier.isPublic(mod);
    }
    // ===================================================================================
    //                                                                              Method
    //                                                                              ======
    
Get the accessible method that means as follows:
 o target class's methods = all
 o superclass's methods   = public or protected
 

Parameters:
clazz The type of class that defines the method. (NotNull)
methodName The name of method. (NotNull)
argTypes The type of argument. (NotNull)
Returns:
The instance of method. (NullAllowed: if null, not found)
    public static Method getAccessibleMethod(Class<?> clazzString methodNameClass<?>[] argTypes) {
        assertObjectNotNull("clazz"clazz);
        assertStringNotNullAndNotTrimmedEmpty("methodName"methodName);
        return findMethod(clazzmethodNameargTypes.false);
    }

    
Get the accessible method that means as follows:
 o target class's methods = all
 o superclass's methods   = public or protected
 
And it has the flexibly searching so you can specify types of sub-class to argTypes. But if overload methods exist, it returns the first-found method.

Parameters:
clazz The type of class that defines the method. (NotNull)
methodName The name of method. (NotNull)
argTypes The type of argument. (NotNull)
Returns:
The instance of method. (NullAllowed: if null, not found)
    public static Method getAccessibleMethodFlexibly(Class<?> clazzString methodNameClass<?>[] argTypes) {
        assertObjectNotNull("clazz"clazz);
        assertStringNotNullAndNotTrimmedEmpty("methodName"methodName);
        return findMethod(clazzmethodNameargTypes.true);
    }

    
Get the public method.

Parameters:
clazz The type of class that defines the method. (NotNull)
methodName The name of method. (NotNull)
argTypes The type of argument. (NotNull)
Returns:
The instance of method. (NullAllowed: if null, not found)
    public static Method getPublicMethod(Class<?> clazzString methodNameClass<?>[] argTypes) {
        assertObjectNotNull("clazz"clazz);
        assertStringNotNullAndNotTrimmedEmpty("methodName"methodName);
        return findMethod(clazzmethodNameargTypes.false);
    }

    
Get the public method.
And it has the flexibly searching so you can specify types of sub-class to argTypes.
But if overload methods exist, it returns the first-found method.
And no cache so you should cache it yourself if you call several times.

Parameters:
clazz The type of class that defines the method. (NotNull)
methodName The name of method. (NotNull)
argTypes The type of argument. (NotNull)
Returns:
The instance of method. (NullAllowed: if null, not found)
    public static Method getPublicMethodFlexibly(Class<?> clazzString methodNameClass<?>[] argTypes) {
        assertObjectNotNull("clazz"clazz);
        assertStringNotNullAndNotTrimmedEmpty("methodName"methodName);
        return findMethod(clazzmethodNameargTypes.true);
    }

    
Get the method in whole methods that means as follows:
 o target class's methods = all
 o superclass's methods   = all (also contains private)
 
And no cache so you should cache it yourself if you call several times.

Parameters:
clazz The type of class that defines the method. (NotNull)
methodName The name of method. (NotNull)
argTypes The type of argument. (NotNull)
Returns:
The instance of method. (NullAllowed: if null, not found)
    public static Method getWholeMethod(Class<?> clazzString methodNameClass<?>[] argTypes) {
        assertObjectNotNull("clazz"clazz);
        assertStringNotNullAndNotTrimmedEmpty("methodName"methodName);
        return findMethod(clazzmethodNameargTypes.false);
    }

    
Get the method in whole methods that means as follows:
 o target class's methods = all
 o superclass's methods   = all (also contains private)
 
And it has the flexibly searching so you can specify types of sub-class to argTypes.
But if overload methods exist, it returns the first-found method.
And no cache so you should cache it yourself if you call several times.

Parameters:
clazz The type of class that defines the method. (NotNull)
methodName The name of method. (NotNull)
argTypes The type of argument. (NotNull)
Returns:
The instance of method. (NullAllowed: if null, not found)
    public static Method getWholeMethodFlexibly(Class<?> clazzString methodNameClass<?>[] argTypes) {
        assertObjectNotNull("clazz"clazz);
        assertStringNotNullAndNotTrimmedEmpty("methodName"methodName);
        return findMethod(clazzmethodNameargTypes.true);
    }
    protected static Method findMethod(Class<?> clazzString methodNameClass<?>[] argTypesVisibilityType visibilityType,
            boolean flexibly) {
        final Method method = doFindMethodBasic(clazzmethodNameargTypesvisibilityType);
        if (method != null) {
            return method;
        } else {
            if (flexibly && argTypes.length >= 1) { // only when argument exists
                return doFindMethodFlexibly(clazzmethodNameargTypesvisibilityType);
            } else {
                return null;
            }
        }
    }
    protected static Method doFindMethodBasic(Class<?> clazzString methodNameClass<?>[] argTypesVisibilityType visibilityType) {
        for (Class<?> target = clazztarget != null && target != Object.classtarget = target.getSuperclass()) {
            final Method declaredMethod;
            try {
                declaredMethod = target.getDeclaredMethod(methodNameargTypes);
            } catch (SecurityException e) {
                String msg = "The security violation was found: " + methodName;
                throw new IllegalStateException(msge);
            } catch (NoSuchMethodException continued) {
                continue;
            } catch (NoClassDefFoundError e) {
                String msg = "No class definition: specified=" + clazz.getName() + "#" + methodName + "()";
                throw new IllegalStateException(msge);
            }
            final int modifier = declaredMethod.getModifiers();
            if (isOutOfTargetForPublic(visibilityTypemodifier)) {
                continue;
            }
            if (isOutOfTargetForAccessible(visibilityTypemodifierclazztarget)) {
                continue;
            }
            return declaredMethod;
        }
        return null;
    }
    protected static Method doFindMethodFlexibly(Class<?> clazzString methodNameClass<?>[] argTypesVisibilityType visibilityType) {
        for (Class<?> target = clazztarget != null && target != Object.classtarget = target.getSuperclass()) {
            final Method[] methods = target.getDeclaredMethods();
            for (int methodIndex = 0; methodIndex < methods.length; ++methodIndex) {
                final Method current = methods[methodIndex];
                final int modifier = current.getModifiers();
                if (isOutOfTargetForPublic(visibilityTypemodifier)) {
                    continue;
                }
                if (isOutOfTargetForAccessible(visibilityTypemodifierclazztarget)) {
                    continue;
                }
                if (methodName.equals(current.getName())) {
                    final Class<?>[] types = current.getParameterTypes();
                    if ((types == null || types.length == 0) && (argTypes == null || argTypes.length == 0)) {
                        return current;
                    }
                    if (types.length != argTypes.length) {
                        continue;
                    }
                    boolean diff = false;
                    for (int argIndex = 0; argIndex < types.lengthargIndex++) {
                        if (!types[argIndex].isAssignableFrom(argTypes[argIndex])) {
                            diff = true;
                            break;
                        }
                    }
                    if (!diff) {
                        return current;
                    }
                }
            }
        }
        return null;
    }
    protected static boolean isOutOfTargetForPublic(VisibilityType visibilityTypeint modifier) {
        return visibilityType == . && !Modifier.isPublic(modifier);
    }
    protected static boolean isOutOfTargetForAccessible(VisibilityType visibilityTypeint modifierClass<?> clazzClass<?> target) {
        return visibilityType == . && clazz != target && isDefaultOrPrivate(modifier);
    }

    
Invoke the method by reflection.

Parameters:
method The instance of method. (NotNull)
target The invocation target instance. (NullAllowed: if null, it means static method)
args The array of arguments. (NullAllowed)
Returns:
The return value of the method. (NullAllowed)
Throws:
DfReflectionUtil.ReflectionFailureException When invocation failure and illegal access
    public static Object invoke(Method methodObject targetObject[] args) {
        assertObjectNotNull("method"method);
        try {
            return method.invoke(targetargs);
        } catch (InvocationTargetException e) {
            Throwable t = e.getCause();
            if (t instanceof RuntimeException) {
                throw (RuntimeExceptiont;
            }
            if (t instanceof Error) {
                throw (Errort;
            }
            String msg = "The InvocationTargetException occurred: ";
            msg = msg + " method=" + method + " target=" + target;
            msg = msg + " args=" + (args != null ? Arrays.asList(args) : "");
            throw new ReflectionFailureException(msgt);
        } catch (IllegalArgumentException e) {
            String msg = "Illegal argument for the method:";
            msg = msg + " method=" + method + " target=" + target;
            msg = msg + " args=" + (args != null ? Arrays.asList(args) : "");
            throw new ReflectionFailureException(msge);
        } catch (IllegalAccessException e) {
            String msg = "Illegal access to the method:";
            msg = msg + " method=" + method + " target=" + target;
            msg = msg + " args=" + (args != null ? Arrays.asList(args) : "");
            throw new ReflectionFailureException(msge);
        }
    }
    public static Object invokeForcedly(Method methodObject targetObject[] args) {
        assertObjectNotNull("method"method);
        if (!isPublicMethod(method) && !method.isAccessible()) {
            method.setAccessible(true);
        }
        return invoke(methodtargetargs);
    }
    public static Object invokeStatic(Method methodObject[] args) {
        assertObjectNotNull("method"method);
        return invoke(methodnullargs);
    }
    public static boolean isPublicMethod(Method method) {
        final int mod = method.getModifiers();
        return Modifier.isPublic(mod);
    }
    public static boolean isBridgeMethod(final Method method) {
        if ( == null) {
            return false;
        }
        return ((Booleaninvoke(methodnull)).booleanValue();
    }
    public static boolean isSyntheticMethod(final Method method) {
        if ( == null) {
            return false;
        }
        return ((Booleaninvoke(methodnull)).booleanValue();
    }
    // ===================================================================================
    //                                                                            Modifier
    //                                                                            ========
    public static enum VisibilityType {
        ACCESSIBLE, PUBLIC, WHOLE
    }
    public static boolean isPublic(int modifier) {
        return Modifier.isPublic(modifier);
    }
    protected static boolean isDefaultOrPrivate(int modifier) {
        return !Modifier.isPublic(modifier) && !Modifier.isProtected(modifier);
    }
    public static boolean isStatic(int modifier) {
        return Modifier.isStatic(modifier);
    }
    // ===================================================================================
    //                                                                             Generic
    //                                                                             =======
    // -----------------------------------------------------
    //                                        Basic Handling
    //                                        --------------
    public static boolean isTypeOf(Type typeClass<?> clazz) {
        if (Class.class.isInstance(type)) {
            return clazz.isAssignableFrom(Class.class.cast(type));
        }
        if (ParameterizedType.class.isInstance(type)) {
            return isTypeOf(ParameterizedType.class.cast(type).getRawType(), clazz);
        }
        return false;
    }
    // -----------------------------------------------------
    //                                     Generic Parameter
    //                                     -----------------
    

Parameters:
type The type that has the generic type. (NotNull)
Returns:
The first generic type for the specified type. (NullAllowed: e.g. not found)
    public static Class<?> getGenericFirstClass(Type type) {
        return findGenericClass(type, 0);
    }

    

Parameters:
type The type that has the generic type. (NotNull)
Returns:
The second generic type for the specified type. (NullAllowed: e.g. not found)
    public static Class<?> getGenericSecondClass(Type type) {
        return findGenericClass(type, 1);
    }
    protected static Class<?> findGenericClass(Type typeint index) {
        return getRawClass(getGenericParameterType(typeindex));
    }
    public static Class<?> getRawClass(Type type) {
        if (type == null) {
            return null;
        }
        if (Class.class.isInstance(type)) {
            return Class.class.cast(type);
        }
        if (ParameterizedType.class.isInstance(type)) {
            return getRawClass(ParameterizedType.class.cast(type).getRawType());
        }
        if (WildcardType.class.isInstance(type)) {
            final WildcardType wildcardType = WildcardType.class.cast(type);
            final Type[] types = wildcardType.getUpperBounds();
            return getRawClass(types[0]);
        }
        if (GenericArrayType.class.isInstance(type)) {
            final GenericArrayType genericArrayType = GenericArrayType.class.cast(type);
            final Class<?> rawClass = getRawClass(genericArrayType.getGenericComponentType());
            return Array.newInstance(rawClass, 0).getClass();
        }
        return null;
    }
    public static Type getGenericParameterType(Type typeint index) {
        if (!ParameterizedType.class.isInstance(type)) {
            return null;
        }
        final Type[] genericParameterTypeList = getGenericParameterTypes(type);
        if (genericParameterTypeList.length == 0 || genericParameterTypeList.length < index) {
            return null;
        }
        return genericParameterTypeList[index];
    }
    public static Type[] getGenericParameterTypes(Type type) {
        if (ParameterizedType.class.isInstance(type)) {
            final ParameterizedType paramType = ParameterizedType.class.cast(type);
            return paramType.getActualTypeArguments();
        }
        if (GenericArrayType.class.isInstance(type)) {
            final GenericArrayType arrayType = GenericArrayType.class.cast(type);
            return getGenericParameterTypes(arrayType.getGenericComponentType());
        }
        return ;
    }
    // -----------------------------------------------------
    //                                         Type Variable
    //                                         -------------
    public static Map<TypeVariable<?>, TypegetTypeVariableMap(final Class<?> clazz) {
        final Map<TypeVariable<?>, Typemap = new LinkedHashMap<TypeVariable<?>, Type>(4);
        final TypeVariable<?>[] typeParameters = clazz.getTypeParameters();
        for (TypeVariable<?> typeParameter : typeParameters) {
            map.put(typeParametergetActualClass(typeParameter.getBounds()[0], map));
        }
        final Class<?> superClass = clazz.getSuperclass();
        final Type superClassType = clazz.getGenericSuperclass();
        if (superClass != null) {
            gatherTypeVariables(superClasssuperClassTypemap);
        }
        final Class<?>[] interfaces = clazz.getInterfaces();
        final Type[] interfaceTypes = clazz.getGenericInterfaces();
        for (int i = 0; i < interfaces.length; ++i) {
            gatherTypeVariables(interfaces[i], interfaceTypes[i], map);
        }
        return map;
    }
    protected static void gatherTypeVariables(final Class<?> clazzfinal Type typefinal Map<TypeVariable<?>, Typemap) {
        if (clazz == null) {
            return;
        }
        gatherTypeVariables(typemap);
        final Class<?> superClass = clazz.getSuperclass();
        final Type superClassType = clazz.getGenericSuperclass();
        if (superClass != null) {
            gatherTypeVariables(superClasssuperClassTypemap);
        }
        final Class<?>[] interfaces = clazz.getInterfaces();
        final Type[] interfaceTypes = clazz.getGenericInterfaces();
        for (int i = 0; i < interfaces.length; ++i) {
            gatherTypeVariables(interfaces[i], interfaceTypes[i], map);
        }
    }
    protected static void gatherTypeVariables(final Type typefinal Map<TypeVariable<?>, Typemap) {
        if (ParameterizedType.class.isInstance(type)) {
            final ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
            final TypeVariable<?>[] typeVariables = GenericDeclaration.class.cast(parameterizedType.getRawType()).getTypeParameters();
            final Type[] actualTypes = parameterizedType.getActualTypeArguments();
            for (int i = 0; i < actualTypes.length; ++i) {
                map.put(typeVariables[i], actualTypes[i]);
            }
        }
    }
    protected static Class<?> getActualClass(final Type typefinal Map<TypeVariable<?>, Typemap) {
        if (Class.class.isInstance(type)) {
            return Class.class.cast(type);
        }
        if (ParameterizedType.class.isInstance(type)) {
            return getActualClass(ParameterizedType.class.cast(type).getRawType(), map);
        }
        if (WildcardType.class.isInstance(type)) {
            return getActualClass(WildcardType.class.cast(type).getUpperBounds()[0], map);
        }
        if (TypeVariable.class.isInstance(type)) {
            final TypeVariable<?> typeVariable = TypeVariable.class.cast(type);
            if (map.containsKey(typeVariable)) {
                return getActualClass(map.get(typeVariable), map);
            }
            return getActualClass(typeVariable.getBounds()[0], map);
        }
        if (GenericArrayType.class.isInstance(type)) {
            final GenericArrayType genericArrayType = GenericArrayType.class.cast(type);
            final Class<?> componentClass = getActualClass(genericArrayType.getGenericComponentType(), map);
            return Array.newInstance(componentClass, 0).getClass();
        }
        return null;
    }
    // ===================================================================================
    //                                                                   Failure Exception
    //                                                                   =================
    public static class ReflectionFailureException extends RuntimeException {

        
The serial version UID for object serialization. (Default)
        private static final long serialVersionUID = 1L;

        
Constructor.

Parameters:
msg Exception message. (NotNull)
        public ReflectionFailureException(String msg) {
            super(msg);
        }

        
Constructor.

Parameters:
msg Exception message. (NotNull)
cause Throwable. (NotNull)
        public ReflectionFailureException(String msgThrowable cause) {
            super(msgcause);
        }
    }
    // ===================================================================================
    //                                                                       Assert Helper
    //                                                                       =============
    
Assert that the object is not null.

Parameters:
variableName The check name of variable for message. (NotNull)
value The checked value. (NotNull)
Throws:
java.lang.IllegalArgumentException When the argument is null.
    protected static void assertObjectNotNull(String variableNameObject value) {
        if (variableName == null) {
            String msg = "The value should not be null: variableName=null value=" + value;
            throw new IllegalArgumentException(msg);
        }
        if (value == null) {
            String msg = "The value should not be null: variableName=" + variableName;
            throw new IllegalArgumentException(msg);
        }
    }

    
Assert that the entity is not null and not trimmed empty.

Parameters:
variableName The check name of variable for message. (NotNull)
value The checked value. (NotNull)
    public static void assertStringNotNullAndNotTrimmedEmpty(String variableNameString value) {
        assertObjectNotNull("variableName"variableName);
        assertObjectNotNull("value"value);
        if (value.trim().length() == 0) {
            String msg = "The value should not be empty: variableName=" + variableName + " value=" + value;
            throw new IllegalArgumentException(msg);
        }
    }
New to GrepCode? Check out our FAQ X