Remove more unused stuff, add proper javadoc
This commit is contained in:
parent
894b523a9b
commit
846a066071
@ -22,81 +22,6 @@ import java.util.logging.Logger;
|
|||||||
public class BeanInspector {
|
public class BeanInspector {
|
||||||
private static final Logger LOGGER = Logger.getLogger(BeanInspector.class.getName());
|
private static final Logger LOGGER = Logger.getLogger(BeanInspector.class.getName());
|
||||||
|
|
||||||
/**
|
|
||||||
* Check for equality based on bean properties of two target objects.
|
|
||||||
*/
|
|
||||||
public static boolean equalProperties(Object obj1, Object obj2) {
|
|
||||||
if (obj1 == null || obj2 == null) {
|
|
||||||
System.out.println("");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
System.out.println("equalProperties: " + obj1.getClass().getName() + " " + obj2.getClass().getName());
|
|
||||||
if (!obj1.getClass().getName().equals(obj2.getClass().getName())) {
|
|
||||||
System.out.println("");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// compare each of the properties !!
|
|
||||||
BeanInfo beanInfo1;
|
|
||||||
BeanInfo beanInfo2;
|
|
||||||
PropertyDescriptor[] Properties_1 = null;
|
|
||||||
PropertyDescriptor[] Properties_2 = null;
|
|
||||||
try {
|
|
||||||
|
|
||||||
beanInfo1 = Introspector.getBeanInfo(obj1.getClass());
|
|
||||||
beanInfo2 = Introspector.getBeanInfo(obj2.getClass());
|
|
||||||
Properties_1 = beanInfo1.getPropertyDescriptors();
|
|
||||||
Properties_2 = beanInfo2.getPropertyDescriptors();
|
|
||||||
beanInfo1.getMethodDescriptors();
|
|
||||||
} catch (IntrospectionException ex) {
|
|
||||||
LOGGER.log(Level.FINEST, "Could not introspect object.", ex);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
boolean BeansInside = false;
|
|
||||||
boolean BeansEqual = true;
|
|
||||||
for (int i = 0; i < Properties_1.length; i++) {
|
|
||||||
if (Properties_1[i].isHidden() || Properties_1[i].isExpert()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
//String name = Properties_1[i].getDisplayName(); //System.out.println("name = "+name );
|
|
||||||
//Class type = Properties_1[i].getPropertyType(); //System.out.println("type = "+type.getName() );
|
|
||||||
Method getter_1 = Properties_1[i].getReadMethod();
|
|
||||||
Method getter_2 = Properties_2[i].getReadMethod();
|
|
||||||
Method setter_1 = Properties_1[i].getWriteMethod();
|
|
||||||
// Only display read/write properties.
|
|
||||||
if (getter_1 == null || setter_1 == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
System.out.println("getter_1 = " + getter_1.getName() + " getter_2 = " + getter_2.getName());
|
|
||||||
Object args_1[] = {};
|
|
||||||
Object args_2[] = {};
|
|
||||||
try {
|
|
||||||
Object value_1 = getter_1.invoke(obj1, args_1);
|
|
||||||
Object value_2 = getter_2.invoke(obj2, args_2);
|
|
||||||
BeansInside = true;
|
|
||||||
if (!BeanInspector.equalProperties(value_1, value_2)) {
|
|
||||||
BeansEqual = false;
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println(" BeanTest.equalProperties " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (BeansInside) {
|
|
||||||
return BeansEqual;
|
|
||||||
}
|
|
||||||
// here we have Integer or Double ...
|
|
||||||
if (obj1 instanceof Integer
|
|
||||||
|| obj1 instanceof Boolean
|
|
||||||
|| obj1 instanceof Float
|
|
||||||
|| obj1 instanceof Double
|
|
||||||
|| obj1 instanceof Long
|
|
||||||
|| obj1 instanceof String) {
|
|
||||||
return obj1.equals(obj2);
|
|
||||||
}
|
|
||||||
|
|
||||||
System.err.println(" Attention no match !!!");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Produce a String representation of an arbitrary object.
|
* Produce a String representation of an arbitrary object.
|
||||||
*
|
*
|
||||||
@ -285,27 +210,27 @@ public class BeanInspector {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static Pair<String[], Object[]> getPublicPropertiesOf(Object target, boolean requireSetter, boolean showHidden) {
|
public static Pair<String[], Object[]> getPublicPropertiesOf(Object target, boolean requireSetter, boolean showHidden) {
|
||||||
BeanInfo Info = null;
|
BeanInfo info;
|
||||||
PropertyDescriptor[] Properties = null;
|
PropertyDescriptor[] pDescriptors;
|
||||||
try {
|
try {
|
||||||
Info = Introspector.getBeanInfo(target.getClass());
|
info = Introspector.getBeanInfo(target.getClass());
|
||||||
Properties = Info.getPropertyDescriptors();
|
pDescriptors = info.getPropertyDescriptors();
|
||||||
Info.getMethodDescriptors();
|
info.getMethodDescriptors();
|
||||||
} catch (IntrospectionException ex) {
|
} catch (IntrospectionException ex) {
|
||||||
System.err.println("BeanTest: Couldn't introspect");
|
System.err.println("BeanTest: Couldn't introspect");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
String[] nameArray = new String[Properties.length];
|
String[] nameArray = new String[pDescriptors.length];
|
||||||
Object[] valArray = new Object[Properties.length];
|
Object[] valArray = new Object[pDescriptors.length];
|
||||||
for (int i = 0; i < Properties.length; i++) {
|
for (int i = 0; i < pDescriptors.length; i++) {
|
||||||
if ((Properties[i].isHidden() && !showHidden) || Properties[i].isExpert()) {
|
if ((pDescriptors[i].isHidden() && !showHidden) || pDescriptors[i].isExpert()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
String name = Properties[i].getDisplayName();
|
String name = pDescriptors[i].getDisplayName();
|
||||||
Method getter = Properties[i].getReadMethod();
|
Method getter = pDescriptors[i].getReadMethod();
|
||||||
Method setter = Properties[i].getWriteMethod();
|
Method setter = pDescriptors[i].getWriteMethod();
|
||||||
// Only display read/write properties.
|
// Only display read/write pDescriptors.
|
||||||
if (getter == null || (setter == null && requireSetter)) {
|
if (getter == null || (setter == null && requireSetter)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -322,69 +247,9 @@ public class BeanInspector {
|
|||||||
return nameVals;
|
return nameVals;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param obj Description of the Parameter
|
|
||||||
*/
|
|
||||||
public static void showInfo(Object obj) {
|
|
||||||
System.out.println("Inspecting " + obj.getClass().getName());
|
|
||||||
// object itself
|
|
||||||
try {
|
|
||||||
if (obj instanceof java.lang.Integer) {
|
|
||||||
System.out.println(" Prop = Integer" + obj.toString());
|
|
||||||
}
|
|
||||||
if (obj instanceof java.lang.Boolean) {
|
|
||||||
System.out.println(" Prop = Boolean" + obj.toString());
|
|
||||||
}
|
|
||||||
if (obj instanceof java.lang.Long) {
|
|
||||||
System.out.println(" Prop = Long" + obj.toString());
|
|
||||||
}
|
|
||||||
if (obj instanceof java.lang.Double) {
|
|
||||||
System.out.println(" Prop = Long" + obj.toString());
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
//System.out.println(" ERROR +"+ e.getMessage());
|
|
||||||
}
|
|
||||||
// then the properties
|
|
||||||
BeanInfo Info = null;
|
|
||||||
PropertyDescriptor[] Properties = null;
|
|
||||||
try {
|
|
||||||
Info = Introspector.getBeanInfo(obj.getClass());
|
|
||||||
Properties = Info.getPropertyDescriptors();
|
|
||||||
Info.getMethodDescriptors();
|
|
||||||
} catch (IntrospectionException ex) {
|
|
||||||
System.err.println("BeanTest: Couldn't introspect");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < Properties.length; i++) {
|
|
||||||
if (Properties[i].isHidden() || Properties[i].isExpert()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
String name = Properties[i].getDisplayName();
|
|
||||||
Method getter = Properties[i].getReadMethod();
|
|
||||||
Method setter = Properties[i].getWriteMethod();
|
|
||||||
// Only display read/write properties.
|
|
||||||
if (getter == null || setter == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Object args[] = {};
|
|
||||||
try {
|
|
||||||
Object value = getter.invoke(obj, args);
|
|
||||||
System.out.println("Inspecting name = " + name);
|
|
||||||
if (value instanceof Integer) {
|
|
||||||
Object args2[] = {999};
|
|
||||||
setter.invoke(obj, args2);
|
|
||||||
}
|
|
||||||
showInfo(value);
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println("BeanTest ERROR +" + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Call a method by a given name with given arguments, if the method is
|
* Call a method by a given name with given arguments, if the method is
|
||||||
* available. Returns the return values of the call or null if it isnt
|
* available. Returns the return values of the call or null if it isn't
|
||||||
* found. This of course means that the caller is unable to distinguish
|
* found. This of course means that the caller is unable to distinguish
|
||||||
* between "method not found" and "method found and it returned null".
|
* between "method not found" and "method found and it returned null".
|
||||||
*
|
*
|
||||||
@ -455,11 +320,11 @@ public class BeanInspector {
|
|||||||
Class<?> cls = obj.getClass();
|
Class<?> cls = obj.getClass();
|
||||||
Method[] meths = cls.getMethods();
|
Method[] meths = cls.getMethods();
|
||||||
for (Method method : meths) {
|
for (Method method : meths) {
|
||||||
if (method.getName().equals(mName)) { // name match
|
if (method.getName().equals(mName)) {
|
||||||
Class[] methParamTypes = method.getParameterTypes();
|
Class[] methParamTypes = method.getParameterTypes();
|
||||||
if (paramTypes == null && methParamTypes.length == 0) {
|
if (paramTypes == null && methParamTypes.length == 0) {
|
||||||
return method;
|
return method;
|
||||||
} // full match
|
}
|
||||||
else {
|
else {
|
||||||
if (paramTypes != null && (methParamTypes.length == paramTypes.length)) {
|
if (paramTypes != null && (methParamTypes.length == paramTypes.length)) {
|
||||||
boolean mismatch = false;
|
boolean mismatch = false;
|
||||||
@ -472,8 +337,8 @@ public class BeanInspector {
|
|||||||
}
|
}
|
||||||
if (!mismatch) {
|
if (!mismatch) {
|
||||||
return method;
|
return method;
|
||||||
} // parameter match, otherwise search on
|
}
|
||||||
} // parameter mismatch, search on
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -624,15 +489,15 @@ public class BeanInspector {
|
|||||||
PropertyDescriptor[] propertyDescriptors = bi.getPropertyDescriptors();
|
PropertyDescriptor[] propertyDescriptors = bi.getPropertyDescriptors();
|
||||||
ArrayList<String> memberInfoList = new ArrayList<>();
|
ArrayList<String> memberInfoList = new ArrayList<>();
|
||||||
|
|
||||||
for (int i = 0; i < propertyDescriptors.length; i++) {
|
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
|
||||||
if (propertyDescriptors[i].isExpert()) {
|
if (propertyDescriptor.isExpert()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
String name = propertyDescriptors[i].getDisplayName();
|
String name = propertyDescriptor.getDisplayName();
|
||||||
|
|
||||||
Method getter = propertyDescriptors[i].getReadMethod();
|
Method getter = propertyDescriptor.getReadMethod();
|
||||||
Method setter = propertyDescriptors[i].getWriteMethod();
|
Method setter = propertyDescriptor.getWriteMethod();
|
||||||
// Only display read/write properties.
|
// Only display read/write properties.
|
||||||
if (getter == null || setter == null) {
|
if (getter == null || setter == null) {
|
||||||
continue;
|
continue;
|
||||||
@ -645,7 +510,7 @@ public class BeanInspector {
|
|||||||
// Don't try to set null values:
|
// Don't try to set null values:
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
// If it's a user-defined property we give a warning.
|
// If it's a user-defined property we give a warning.
|
||||||
String getterClass = propertyDescriptors[i].getReadMethod().getDeclaringClass().getName();
|
String getterClass = propertyDescriptor.getReadMethod().getDeclaringClass().getName();
|
||||||
if (getterClass.indexOf("java.") != 0) {
|
if (getterClass.indexOf("java.") != 0) {
|
||||||
System.err.println("Warning: Property \"" + name + "\" has null initial value. Skipping.");
|
System.err.println("Warning: Property \"" + name + "\" has null initial value. Skipping.");
|
||||||
}
|
}
|
||||||
@ -658,7 +523,7 @@ public class BeanInspector {
|
|||||||
|
|
||||||
memberInfoBf.append("\tType: ");
|
memberInfoBf.append("\tType: ");
|
||||||
|
|
||||||
if (propertyDescriptors[i].isHidden()) {
|
if (propertyDescriptor.isHidden()) {
|
||||||
memberInfoBf.append("restricted, ");
|
memberInfoBf.append("restricted, ");
|
||||||
} else {
|
} else {
|
||||||
memberInfoBf.append("common, ");
|
memberInfoBf.append("common, ");
|
||||||
@ -708,8 +573,8 @@ public class BeanInspector {
|
|||||||
* Take an object of primitive type (like int, Integer etc) and convert it
|
* Take an object of primitive type (like int, Integer etc) and convert it
|
||||||
* to double.
|
* to double.
|
||||||
*
|
*
|
||||||
* @param val
|
* @param val Value to convert
|
||||||
* @return
|
* @return Value converted to a double
|
||||||
* @throws IllegalArgumentException
|
* @throws IllegalArgumentException
|
||||||
*/
|
*/
|
||||||
public static double toDouble(Object val) throws IllegalArgumentException {
|
public static double toDouble(Object val) throws IllegalArgumentException {
|
||||||
@ -739,9 +604,9 @@ public class BeanInspector {
|
|||||||
* Take a String and convert it to a destined data type using the
|
* Take a String and convert it to a destined data type using the
|
||||||
* appropriate function.
|
* appropriate function.
|
||||||
*
|
*
|
||||||
* @param str
|
* @param str String to convert
|
||||||
* @param destType
|
* @param destType The type to cast to
|
||||||
* @return
|
* @return An object of the type {@code destType} with the value of {@code str} converted
|
||||||
*/
|
*/
|
||||||
public static Object stringToPrimitive(String str, Class<?> destType) throws NumberFormatException {
|
public static Object stringToPrimitive(String str, Class<?> destType) throws NumberFormatException {
|
||||||
if ((destType == Integer.class) || (destType == int.class)) {
|
if ((destType == Integer.class) || (destType == int.class)) {
|
||||||
@ -770,9 +635,9 @@ public class BeanInspector {
|
|||||||
/**
|
/**
|
||||||
* Take a double value and convert it to a primitive object.
|
* Take a double value and convert it to a primitive object.
|
||||||
*
|
*
|
||||||
* @param d
|
* @param d Double to convert
|
||||||
* @param destType
|
* @param destType Destination type
|
||||||
* @return
|
* @return The converted double
|
||||||
*/
|
*/
|
||||||
public static Object doubleToPrimitive(Double d, Class<?> destType) {
|
public static Object doubleToPrimitive(Double d, Class<?> destType) {
|
||||||
if ((destType == Double.class) || (destType == double.class)) {
|
if ((destType == Double.class) || (destType == double.class)) {
|
||||||
|
@ -450,7 +450,6 @@ public final class PropertySheetPanel extends JPanel implements PropertyChangeLi
|
|||||||
try {
|
try {
|
||||||
PropertyDescriptor[] oldProps = propertyDescriptors.clone();
|
PropertyDescriptor[] oldProps = propertyDescriptors.clone();
|
||||||
newProps = new PropertyDescriptor[oldProps.length];
|
newProps = new PropertyDescriptor[oldProps.length];
|
||||||
//int findFirst=findFirstProp(props[0], oldProps);
|
|
||||||
int firstNonNull = 0;
|
int firstNonNull = 0;
|
||||||
for (int i = 0; i < oldProps.length; i++) {
|
for (int i = 0; i < oldProps.length; i++) {
|
||||||
if (i < swProps.length) {
|
if (i < swProps.length) {
|
||||||
@ -480,8 +479,7 @@ public final class PropertySheetPanel extends JPanel implements PropertyChangeLi
|
|||||||
* @param firstLook
|
* @param firstLook
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private int findFirstNonNullAfter(PropertyDescriptor[] arr,
|
private int findFirstNonNullAfter(PropertyDescriptor[] arr, int firstLook) {
|
||||||
int firstLook) {
|
|
||||||
for (int i = firstLook; i < arr.length; i++) {
|
for (int i = firstLook; i < arr.length; i++) {
|
||||||
if (arr[i] != null) {
|
if (arr[i] != null) {
|
||||||
return i;
|
return i;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user