From c4c5c8a1427e64ace1c35f7c99f39894e570b70d Mon Sep 17 00:00:00 2001 From: Jacques Le Roux Date: Sun, 20 Jan 2019 09:39:30 +0000 Subject: [PATCH] =?UTF-8?q?Improved:=20=E2=80=98ObjectType=E2=80=99=20cont?= =?UTF-8?q?ains=20unneeded=20code=20(OFBIZ-10771)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The ObjectType class contains utilities that extends the Java reflection API by defining methods which accepts string representation of Java types and classes. It appears that this class contains unneeded code which is either dead or is redundant with the services already defined in the reflection API Removes the useless isOrSubOf, instanceOf and interfaceOf methods. Additionally it removes the getInstance method which is not used anywhere in the framework or in the plugins. Also includes similar changes in AbstractConverter.java Converters.java DateTimeConverters.java GenericEntity.java IfInstanceOf.java Thanks: Mathieu git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1851701 13f79535-47bb-0310-9956-ffa450edef68 --- .../base/conversion/AbstractConverter.java | 4 +- .../ofbiz/base/conversion/Converters.java | 3 +- .../base/conversion/DateTimeConverters.java | 21 +- .../apache/ofbiz/base/util/ObjectType.java | 242 +----------------- .../apache/ofbiz/entity/GenericEntity.java | 2 +- .../minilang/method/ifops/IfInstanceOf.java | 2 +- 6 files changed, 16 insertions(+), 258 deletions(-) diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/conversion/AbstractConverter.java b/framework/base/src/main/java/org/apache/ofbiz/base/conversion/AbstractConverter.java index 1d76a10ef6..b99aba1179 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/conversion/AbstractConverter.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/conversion/AbstractConverter.java @@ -18,8 +18,6 @@ *******************************************************************************/ package org.apache.ofbiz.base.conversion; -import org.apache.ofbiz.base.util.ObjectType; - /** Abstract Converter class. This class handles converter registration * and it implements the canConvert, getSourceClass, * and getTargetClass methods. @@ -42,7 +40,7 @@ public abstract class AbstractConverter implements Converter, Conver } public boolean canConvert(Class sourceClass, Class targetClass) { - return ObjectType.instanceOf(sourceClass, this.getSourceClass()) && ObjectType.instanceOf(targetClass, this.getTargetClass()); + return getSourceClass().isAssignableFrom(sourceClass) && getTargetClass().isAssignableFrom(targetClass); } public Class getSourceClass() { diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/conversion/Converters.java b/framework/base/src/main/java/org/apache/ofbiz/base/conversion/Converters.java index 0367852afc..926db49b43 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/conversion/Converters.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/conversion/Converters.java @@ -27,7 +27,6 @@ import java.util.concurrent.ConcurrentHashMap; import org.apache.ofbiz.base.lang.SourceMonitored; import org.apache.ofbiz.base.util.Debug; -import org.apache.ofbiz.base.util.ObjectType; import org.apache.ofbiz.base.util.UtilGenerics; /** A Converter factory and repository. */ @@ -203,7 +202,7 @@ OUTER: } public Converter createConverter(Class sourceClass, Class targetClass) { - if (ObjectType.instanceOf(sourceClass, targetClass)) { + if (targetClass.isAssignableFrom(sourceClass)) { return new PassThruConverter<>(sourceClass, targetClass); } return null; diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/conversion/DateTimeConverters.java b/framework/base/src/main/java/org/apache/ofbiz/base/conversion/DateTimeConverters.java index d5f0d5c07c..e170a5d606 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/conversion/DateTimeConverters.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/conversion/DateTimeConverters.java @@ -26,7 +26,6 @@ import java.util.Date; import java.util.Locale; import java.util.TimeZone; -import org.apache.ofbiz.base.util.ObjectType; import org.apache.ofbiz.base.util.TimeDuration; import org.apache.ofbiz.base.util.UtilDateTime; import org.apache.ofbiz.base.util.UtilValidate; @@ -42,7 +41,7 @@ public class DateTimeConverters implements ConverterLoader { @Override public boolean canConvert(Class sourceClass, Class targetClass) { - return ObjectType.instanceOf(sourceClass, this.getSourceClass()) && targetClass == this.getTargetClass(); + return getSourceClass().isAssignableFrom(sourceClass) && targetClass == getTargetClass(); } public Date convert(Calendar obj) throws ConversionException { @@ -80,7 +79,7 @@ public class DateTimeConverters implements ConverterLoader { @Override public boolean canConvert(Class sourceClass, Class targetClass) { - return ObjectType.instanceOf(sourceClass, this.getSourceClass()) && targetClass == this.getTargetClass(); + return getSourceClass().isAssignableFrom(sourceClass) && targetClass == getTargetClass(); } public Timestamp convert(Calendar obj) throws ConversionException { @@ -170,7 +169,7 @@ public class DateTimeConverters implements ConverterLoader { @Override public boolean canConvert(Class sourceClass, Class targetClass) { - return ObjectType.instanceOf(sourceClass, this.getSourceClass()) && targetClass == this.getTargetClass(); + return getSourceClass().isAssignableFrom(sourceClass) && targetClass == getTargetClass(); } public java.sql.Timestamp convert(java.util.Date obj) throws ConversionException { @@ -279,7 +278,7 @@ public class DateTimeConverters implements ConverterLoader { @Override public boolean canConvert(Class sourceClass, Class targetClass) { - return ObjectType.instanceOf(sourceClass, this.getSourceClass()) && targetClass == this.getTargetClass(); + return getSourceClass().isAssignableFrom(sourceClass) && targetClass == getTargetClass(); } public java.util.Date convert(Number obj) throws ConversionException { @@ -304,7 +303,7 @@ public class DateTimeConverters implements ConverterLoader { @Override public boolean canConvert(Class sourceClass, Class targetClass) { - return ObjectType.instanceOf(sourceClass, this.getSourceClass()) && targetClass == this.getTargetClass(); + return getSourceClass().isAssignableFrom(sourceClass) && targetClass == getTargetClass(); } public java.sql.Date convert(Number obj) throws ConversionException { @@ -323,7 +322,7 @@ public class DateTimeConverters implements ConverterLoader { @Override public boolean canConvert(Class sourceClass, Class targetClass) { - return ObjectType.instanceOf(sourceClass, this.getSourceClass()) && targetClass == this.getTargetClass(); + return getSourceClass().isAssignableFrom(sourceClass) && targetClass == getTargetClass(); } public java.sql.Time convert(Number obj) throws ConversionException { @@ -338,7 +337,7 @@ public class DateTimeConverters implements ConverterLoader { @Override public boolean canConvert(Class sourceClass, Class targetClass) { - return ObjectType.instanceOf(sourceClass, this.getSourceClass()) && targetClass == this.getTargetClass(); + return getSourceClass().isAssignableFrom(sourceClass) && targetClass == getTargetClass(); } public java.sql.Timestamp convert(Number obj) throws ConversionException { @@ -560,7 +559,7 @@ public class DateTimeConverters implements ConverterLoader { @Override public boolean canConvert(Class sourceClass, Class targetClass) { - return ObjectType.instanceOf(sourceClass, this.getSourceClass()) && targetClass == this.getTargetClass(); + return getSourceClass().isAssignableFrom(sourceClass) && targetClass == getTargetClass(); } public java.sql.Date convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException { @@ -593,7 +592,7 @@ public class DateTimeConverters implements ConverterLoader { @Override public boolean canConvert(Class sourceClass, Class targetClass) { - return ObjectType.instanceOf(sourceClass, this.getSourceClass()) && targetClass == this.getTargetClass(); + return getSourceClass().isAssignableFrom(sourceClass) && targetClass == getTargetClass(); } public java.sql.Time convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException { @@ -622,7 +621,7 @@ public class DateTimeConverters implements ConverterLoader { @Override public boolean canConvert(Class sourceClass, Class targetClass) { - return ObjectType.instanceOf(sourceClass, this.getSourceClass()) && targetClass == this.getTargetClass(); + return getSourceClass().isAssignableFrom(sourceClass) && targetClass == getTargetClass(); } public java.sql.Timestamp convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException { diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/ObjectType.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/ObjectType.java index a8e3187a85..5a4da71f0e 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/ObjectType.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/ObjectType.java @@ -19,8 +19,6 @@ package org.apache.ofbiz.base.util; import java.io.Serializable; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.util.Collection; import java.util.HashMap; import java.util.List; @@ -168,203 +166,6 @@ public class ObjectType { return o; } - /** - * Tests if a class properly implements the specified interface. - * @param objectClass Class to test - * @param interfaceName Name of the interface to test against - * @return true if interfaceName is an interface of objectClass - * @throws ClassNotFoundException - */ - public static boolean interfaceOf(Class objectClass, String interfaceName) throws ClassNotFoundException { - Class interfaceClass = loadClass(interfaceName); - - return interfaceOf(objectClass, interfaceClass); - } - - /** - * Tests if a class properly implements the specified interface. - * @param objectClass Class to test - * @param interfaceObject to test against - * @return true if interfaceObject is an interface of the objectClass - */ - public static boolean interfaceOf(Class objectClass, Object interfaceObject) { - Class interfaceClass = interfaceObject.getClass(); - - return interfaceOf(objectClass, interfaceClass); - } - - /** - * Returns an instance of the specified class using the constructor matching the specified parameters. - * @param className Name of the class to instantiate - * @param parameters Parameters passed to the constructor - * @return An instance of the className - * @throws ClassNotFoundException - * @throws InstantiationException - * @throws IllegalAccessException - */ - public static Object getInstance(String className, Object[] parameters) throws ClassNotFoundException, - InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { - Class[] sig = new Class[parameters.length]; - for (int i = 0; i < sig.length; i++) { - sig[i] = parameters[i].getClass(); - } - Class c = loadClass(className); - Constructor con = c.getConstructor(sig); - Object o = con.newInstance(parameters); - - if (Debug.verboseOn()) { - Debug.logVerbose("Instantiated object: " + o.toString(), module); - } - return o; - } - - /** - * Tests if an object properly implements the specified interface. - * @param obj Object to test - * @param interfaceName Name of the interface to test against - * @return true if interfaceName is an interface of obj - * @throws ClassNotFoundException - */ - public static boolean interfaceOf(Object obj, String interfaceName) throws ClassNotFoundException { - Class interfaceClass = loadClass(interfaceName); - - return interfaceOf(obj, interfaceClass); - } - - /** - * Tests if an object properly implements the specified interface. - * @param obj Object to test - * @param interfaceObject to test against - * @return true if interfaceObject is an interface of obj - */ - public static boolean interfaceOf(Object obj, Object interfaceObject) { - Class interfaceClass = interfaceObject.getClass(); - - return interfaceOf(obj, interfaceClass); - } - - /** - * Tests if an object properly implements the specified interface. - * @param obj Object to test - * @param interfaceClass Class to test against - * @return true if interfaceClass is an interface of obj - */ - public static boolean interfaceOf(Object obj, Class interfaceClass) { - Class objectClass = obj.getClass(); - - return interfaceOf(objectClass, interfaceClass); - } - - /** - * Tests if a class properly implements the specified interface. - * @param objectClass Class to test - * @param interfaceClass Class to test against - * @return true if interfaceClass is an interface of objectClass - */ - public static boolean interfaceOf(Class objectClass, Class interfaceClass) { - while (objectClass != null) { - Class[] ifaces = objectClass.getInterfaces(); - - for (Class iface: ifaces) { - if (iface == interfaceClass) { - return true; - } - } - objectClass = objectClass.getSuperclass(); - } - return false; - } - - /** - * Tests if a class is a class of or a sub-class of the parent. - * @param objectClass Class to test - * @param parentName Name of the parent class to test against - * @return true if objectClass is a class of or a sub-class of the parent - * @throws ClassNotFoundException - */ - public static boolean isOrSubOf(Class objectClass, String parentName) throws ClassNotFoundException { - Class parentClass = loadClass(parentName); - - return isOrSubOf(objectClass, parentClass); - } - - /** - * Tests if a class is a class of or a sub-class of the parent. - * @param objectClass Class to test - * @param parentObject Object to test against - * @return true if objectClass is a class of or a sub-class of the parent - */ - public static boolean isOrSubOf(Class objectClass, Object parentObject) { - Class parentClass = parentObject.getClass(); - - return isOrSubOf(objectClass, parentClass); - } - - /** - * Tests if an object is an instance of or a sub-class of the parent. - * @param obj Object to test - * @param parentName Name of the parent class to test against - * @return true if obj is an instance of or a sub-class of the parent - * @throws ClassNotFoundException - */ - public static boolean isOrSubOf(Object obj, String parentName) throws ClassNotFoundException { - Class parentClass = loadClass(parentName); - - return isOrSubOf(obj, parentClass); - } - - /** - * Tests if an object is an instance of or a sub-class of the parent. - * @param obj Object to test - * @param parentObject Object to test against - * @return true if obj is an instance of or a sub-class of the parent - */ - public static boolean isOrSubOf(Object obj, Object parentObject) { - Class parentClass = parentObject.getClass(); - - return isOrSubOf(obj, parentClass); - } - - /** - * Tests if an object is an instance of or a sub-class of the parent. - * @param obj Object to test - * @param parentClass Class to test against - * @return true if obj is an instance of or a sub-class of the parent - */ - public static boolean isOrSubOf(Object obj, Class parentClass) { - Class objectClass = obj.getClass(); - - return isOrSubOf(objectClass, parentClass); - } - - /** - * Tests if a class is a class of or a sub-class of the parent. - * @param objectClass Class to test - * @param parentClass Class to test against - * @return true if objectClass is a class of or a sub-class of the parent - */ - public static boolean isOrSubOf(Class objectClass, Class parentClass) { - while (objectClass != null) { - if (objectClass == parentClass) { - return true; - } - objectClass = objectClass.getSuperclass(); - } - return false; - } - - /** - * Tests if a class is a class of a sub-class of or properly implements an interface. - * @param objectClass Class to test - * @param typeObject Object to test against - * @return true if objectClass is a class of a sub-class of, or properly implements an interface - */ - public static boolean instanceOf(Class objectClass, Object typeObject) { - Class typeClass = typeObject.getClass(); - - return instanceOf(objectClass, typeClass); - } - /** * Tests if a class is a class of a sub-class of or properly implements an interface. * @param objectClass Class to test @@ -375,18 +176,6 @@ public class ObjectType { return instanceOf(objectClass, typeName, null); } - /** - * Tests if an object is an instance of a sub-class of or properly implements an interface. - * @param obj Object to test - * @param typeObject Object to test against - * @return true if obj is an instance of a sub-class of, or properly implements an interface - */ - public static boolean instanceOf(Object obj, Object typeObject) { - Class typeClass = typeObject.getClass(); - - return instanceOf(obj, typeClass); - } - /** * Tests if an object is an instance of a sub-class of or properly implements an interface. * @param obj Object to test @@ -411,7 +200,7 @@ public class ObjectType { throw new IllegalArgumentException("Illegal type found in info map (could not load class for specified type)"); } - return instanceOf(objectClass, infoClass); + return infoClass.isAssignableFrom(objectClass); } /** @@ -428,7 +217,7 @@ public class ObjectType { throw new IllegalArgumentException("Illegal type found in info map (could not load class for specified type)"); } - return instanceOf(obj, infoClass); + return obj == null || infoClass.isInstance(obj); } public static Class loadInfoClass(String typeName, ClassLoader loader) { @@ -458,33 +247,6 @@ public class ObjectType { } } - /** - * Tests if an object is an instance of a sub-class of or properly implements an interface. - * @param obj Object to test - * @param typeClass Class to test against - * @return true if obj is an instance of a sub-class of typeClass - */ - public static boolean instanceOf(Object obj, Class typeClass) { - if (obj == null) { - return true; - } - Class objectClass = obj.getClass(); - return instanceOf(objectClass, typeClass); - } - - /** - * Tests if a class is a class of a sub-class of or properly implements an interface. - * @param objectClass Class to test - * @param typeClass Class to test against - * @return true if objectClass is a class or sub-class of, or implements typeClass - */ - public static boolean instanceOf(Class objectClass, Class typeClass) { - if (typeClass.isInterface() && !objectClass.isInterface()) { - return interfaceOf(objectClass, typeClass); - } - return isOrSubOf(objectClass, typeClass); - } - /** See also {@link #simpleTypeOrObjectConvert(Object obj, String type, String format, TimeZone timeZone, Locale locale, boolean noTypeFail)}. */ public static Object simpleTypeOrObjectConvert(Object obj, String type, String format, Locale locale, boolean noTypeFail) throws GeneralException { return simpleTypeOrObjectConvert(obj, type, format, null, locale, noTypeFail); diff --git a/framework/entity/src/main/java/org/apache/ofbiz/entity/GenericEntity.java b/framework/entity/src/main/java/org/apache/ofbiz/entity/GenericEntity.java index f6e1dfa8c2..6c24b205e3 100644 --- a/framework/entity/src/main/java/org/apache/ofbiz/entity/GenericEntity.java +++ b/framework/entity/src/main/java/org/apache/ofbiz/entity/GenericEntity.java @@ -493,7 +493,7 @@ public class GenericEntity implements Map, LocalizedMap, value = ((String) value).getBytes(UtilIO.getUtf8()); } if (!ObjectType.instanceOf(value, type.getJavaType())) { - if (!("java.sql.Blob".equals(type.getJavaType()) && (value instanceof byte[] || ObjectType.instanceOf(value, ByteBuffer.class)))) { + if (!("java.sql.Blob".equals(type.getJavaType()) && (value instanceof byte[] || value == null || ByteBuffer.class.isInstance(value)))) { String errMsg = "In entity field [" + this.getEntityName() + "." + name + "] set the value passed in [" + value.getClass().getName() + "] is not compatible with the Java type of the field [" + type.getJavaType() + "]"; // eventually we should do this, but for now we'll do a "soft" failure: throw new IllegalArgumentException(errMsg); Debug.logWarning(new Exception("Location of database type warning"), "=-=-=-=-=-=-=-=-= Database type warning GenericEntity.set =-=-=-=-=-=-=-=-= " + errMsg, module); diff --git a/framework/minilang/src/main/java/org/apache/ofbiz/minilang/method/ifops/IfInstanceOf.java b/framework/minilang/src/main/java/org/apache/ofbiz/minilang/method/ifops/IfInstanceOf.java index 6cbeca16ab..be0d0278b7 100644 --- a/framework/minilang/src/main/java/org/apache/ofbiz/minilang/method/ifops/IfInstanceOf.java +++ b/framework/minilang/src/main/java/org/apache/ofbiz/minilang/method/ifops/IfInstanceOf.java @@ -82,7 +82,7 @@ public final class IfInstanceOf extends MethodOperation { boolean runSubOps = false; Object fieldVal = fieldFma.get(methodContext.getEnvMap()); if (fieldVal != null) { - runSubOps = ObjectType.instanceOf(fieldVal.getClass(), compareClass); + runSubOps = compareClass.isAssignableFrom(fieldVal.getClass()); } if (runSubOps) { return SimpleMethod.runSubOps(subOps, methodContext);