private Object rowCountResult(int rowCount) { final Object result; if (method.returnsVoid()) { result = null; } else if (Integer.class.equals(method.getReturnType()) || Integer.TYPE.equals(method.getReturnType())) { result = Integer.valueOf(rowCount); } else if (Long.class.equals(method.getReturnType()) || Long.TYPE.equals(method.getReturnType())) { result = Long.valueOf(rowCount); } else if (Boolean.class.equals(method.getReturnType()) || Boolean.TYPE.equals(method.getReturnType())) { result = Boolean.valueOf(rowCount > 0); } else { throw new BindingException( "Mapper method '" + command.getName() + "' has an unsupported return type: " + method.getReturnType()); } return result; }
static { // compute primitives/primitiveMap/primitiveToWrapper for (Class<?> c : primitiveWrappers) { try { Field f = c.getField("TYPE"); Class<?> p = (Class<?>) f.get(null); primitives.add(p); primitiveMap.put(p.getName(), p); primitiveToWrapper.put(p.getName(), c); } catch (Exception e) { throw new AssertionError(e); } } // compute editableTypes for (Class<?> c : primitives) { editableTypes.add(c.getName()); } for (Class<?> c : primitiveWrappers) { editableTypes.add(c.getName()); } for (Class<?> c : extraEditableClasses) { editableTypes.add(c.getName()); } // compute numericalTypes for (Class<?> c : primitives) { String name = c.getName(); if (!name.equals(Boolean.TYPE.getName())) { numericalTypes.add(name); } } for (Class<?> c : primitiveWrappers) { String name = c.getName(); if (!name.equals(Boolean.class.getName())) { numericalTypes.add(name); } } }
public class Utils { private Utils() {} private static Set<Integer> tableNavigationKeys = new HashSet<Integer>( Arrays.asList( new Integer[] { KeyEvent.VK_TAB, KeyEvent.VK_ENTER, KeyEvent.VK_HOME, KeyEvent.VK_END, KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT, KeyEvent.VK_UP, KeyEvent.VK_DOWN, KeyEvent.VK_PAGE_UP, KeyEvent.VK_PAGE_DOWN })); private static final Set<Class<?>> primitiveWrappers = new HashSet<Class<?>>( Arrays.asList( new Class<?>[] { Byte.class, Short.class, Integer.class, Long.class, Float.class, Double.class, Character.class, Boolean.class })); private static final Set<Class<?>> primitives = new HashSet<Class<?>>(); private static final Map<String, Class<?>> primitiveMap = new HashMap<String, Class<?>>(); private static final Map<String, Class<?>> primitiveToWrapper = new HashMap<String, Class<?>>(); private static final Set<String> editableTypes = new HashSet<String>(); private static final Set<Class<?>> extraEditableClasses = new HashSet<Class<?>>( Arrays.asList( new Class<?>[] { BigDecimal.class, BigInteger.class, Number.class, String.class, ObjectName.class })); private static final Set<String> numericalTypes = new HashSet<String>(); private static final Set<String> extraNumericalTypes = new HashSet<String>( Arrays.asList( new String[] { BigDecimal.class.getName(), BigInteger.class.getName(), Number.class.getName() })); private static final Set<String> booleanTypes = new HashSet<String>( Arrays.asList(new String[] {Boolean.TYPE.getName(), Boolean.class.getName()})); static { // compute primitives/primitiveMap/primitiveToWrapper for (Class<?> c : primitiveWrappers) { try { Field f = c.getField("TYPE"); Class<?> p = (Class<?>) f.get(null); primitives.add(p); primitiveMap.put(p.getName(), p); primitiveToWrapper.put(p.getName(), c); } catch (Exception e) { throw new AssertionError(e); } } // compute editableTypes for (Class<?> c : primitives) { editableTypes.add(c.getName()); } for (Class<?> c : primitiveWrappers) { editableTypes.add(c.getName()); } for (Class<?> c : extraEditableClasses) { editableTypes.add(c.getName()); } // compute numericalTypes for (Class<?> c : primitives) { String name = c.getName(); if (!name.equals(Boolean.TYPE.getName())) { numericalTypes.add(name); } } for (Class<?> c : primitiveWrappers) { String name = c.getName(); if (!name.equals(Boolean.class.getName())) { numericalTypes.add(name); } } } /** * This method returns the class matching the name className. It's used to cater for the primitive * types. */ public static Class<?> getClass(String className) throws ClassNotFoundException { Class<?> c; if ((c = primitiveMap.get(className)) != null) return c; return Class.forName(className); } /** Check if the given collection is a uniform collection of the given type. */ public static boolean isUniformCollection(Collection<?> c, Class<?> e) { if (e == null) { throw new IllegalArgumentException("Null reference type"); } if (c == null) { throw new IllegalArgumentException("Null collection"); } if (c.isEmpty()) { return false; } for (Object o : c) { if (o == null || !e.isAssignableFrom(o.getClass())) { return false; } } return true; } /** * Check if the given element denotes a supported array-friendly data structure, i.e. a data * structure jconsole can render as an array. */ public static boolean canBeRenderedAsArray(Object elem) { if (isSupportedArray(elem)) return true; if (elem instanceof Collection) { Collection<?> c = (Collection<?>) elem; if (c.isEmpty()) { // Empty collections of any Java type are not handled as arrays // return false; } else { // - Collections of CompositeData/TabularData are not handled // as arrays // - Collections of other Java types are handled as arrays // return !isUniformCollection(c, CompositeData.class) && !isUniformCollection(c, TabularData.class); } } if (elem instanceof Map) { return !(elem instanceof TabularData); } return false; } /** * Check if the given element is an array. * * <p>Multidimensional arrays are not supported. * * <p>Non-empty 1-dimensional arrays of CompositeData and TabularData are not handled as arrays * but as tabular data. */ public static boolean isSupportedArray(Object elem) { if (elem == null || !elem.getClass().isArray()) { return false; } Class<?> ct = elem.getClass().getComponentType(); if (ct.isArray()) { return false; } if (Array.getLength(elem) > 0 && (CompositeData.class.isAssignableFrom(ct) || TabularData.class.isAssignableFrom(ct))) { return false; } return true; } /** * This method provides a readable classname if it's an array, i.e. either the classname of the * component type for arrays of java reference types or the name of the primitive type for arrays * of java primitive types. Otherwise, it returns null. */ public static String getArrayClassName(String name) { String className = null; if (name.startsWith("[")) { int index = name.lastIndexOf("["); className = name.substring(index, name.length()); if (className.startsWith("[L")) { className = className.substring(2, className.length() - 1); } else { try { Class<?> c = Class.forName(className); className = c.getComponentType().getName(); } catch (ClassNotFoundException e) { // Should not happen throw new IllegalArgumentException("Bad class name " + name, e); } } } return className; } /** * This methods provides a readable classname. If the supplied name parameter denotes an array * this method returns either the classname of the component type for arrays of java reference * types or the name of the primitive type for arrays of java primitive types followed by n-times * "[]" where 'n' denotes the arity of the array. Otherwise, if the supplied name doesn't denote * an array it returns the same classname. */ public static String getReadableClassName(String name) { String className = getArrayClassName(name); if (className == null) return name; int index = name.lastIndexOf("["); StringBuilder brackets = new StringBuilder(className); for (int i = 0; i <= index; i++) { brackets.append("[]"); } return brackets.toString(); } /** This method tells whether the type is editable (means can be created with a String or not) */ public static boolean isEditableType(String type) { return editableTypes.contains(type); } /** * This method inserts a default value for the standard java types, else it inserts the text name * of the expected class type. It acts to give a clue as to the input type. */ public static String getDefaultValue(String type) { if (numericalTypes.contains(type) || extraNumericalTypes.contains(type)) { return "0"; } if (booleanTypes.contains(type)) { return "true"; } type = getReadableClassName(type); int i = type.lastIndexOf('.'); if (i > 0) { return type.substring(i + 1, type.length()); } else { return type; } } /** Try to create a Java object using a one-string-param constructor. */ public static Object newStringConstructor(String type, String param) throws Exception { Constructor c = Utils.getClass(type).getConstructor(String.class); try { return c.newInstance(param); } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); if (t instanceof Exception) { throw (Exception) t; } else { throw e; } } } /** Try to convert a string value into a numerical value. */ private static Number createNumberFromStringValue(String value) throws NumberFormatException { final String suffix = value.substring(value.length() - 1); if ("L".equalsIgnoreCase(suffix)) { return Long.valueOf(value.substring(0, value.length() - 1)); } if ("F".equalsIgnoreCase(suffix)) { return Float.valueOf(value.substring(0, value.length() - 1)); } if ("D".equalsIgnoreCase(suffix)) { return Double.valueOf(value.substring(0, value.length() - 1)); } try { return Integer.valueOf(value); } catch (NumberFormatException e) { // OK: Ignore exception... } try { return Long.valueOf(value); } catch (NumberFormatException e1) { // OK: Ignore exception... } try { return Double.valueOf(value); } catch (NumberFormatException e2) { // OK: Ignore exception... } throw new NumberFormatException( "Cannot convert string value '" + value + "' into a numerical value"); } /** * This method attempts to create an object of the given "type" using the "value" parameter. e.g. * calling createObjectFromString("java.lang.Integer", "10") will return an Integer object * initialized to 10. */ public static Object createObjectFromString(String type, String value) throws Exception { Object result; if (primitiveToWrapper.containsKey(type)) { if (type.equals(Character.TYPE.getName())) { result = new Character(value.charAt(0)); } else { result = newStringConstructor(((Class<?>) primitiveToWrapper.get(type)).getName(), value); } } else if (type.equals(Character.class.getName())) { result = new Character(value.charAt(0)); } else if (Number.class.isAssignableFrom(Utils.getClass(type))) { result = createNumberFromStringValue(value); } else if (value == null || value.toString().equals("null")) { // hack for null value result = null; } else { // try to create a Java object using // the one-string-param constructor result = newStringConstructor(type, value); } return result; } /** * This method is responsible for converting the inputs given by the user into a useful object * array for passing into a parameter array. */ public static Object[] getParameters(XTextField[] inputs, String[] params) throws Exception { Object result[] = new Object[inputs.length]; Object userInput; for (int i = 0; i < inputs.length; i++) { userInput = inputs[i].getValue(); // if it's already a complex object, use the value // else try to instantiate with string constructor if (userInput instanceof XObject) { result[i] = ((XObject) userInput).getObject(); } else { result[i] = createObjectFromString(params[i].toString(), (String) userInput); } } return result; } /** If the exception is wrapped, unwrap it. */ public static Throwable getActualException(Throwable e) { if (e instanceof ExecutionException) e = e.getCause(); if (e instanceof MBeanException || e instanceof RuntimeMBeanException || e instanceof RuntimeOperationsException || e instanceof ReflectionException) { Throwable t = e.getCause(); if (t != null) return t; } return e; } @SuppressWarnings("serial") public static class ReadOnlyTableCellEditor extends DefaultCellEditor { public ReadOnlyTableCellEditor(JTextField tf) { super(tf); tf.addFocusListener(new EditFocusAdapter(this)); tf.addKeyListener(new CopyKeyAdapter()); } } public static class EditFocusAdapter extends FocusAdapter { private CellEditor editor; public EditFocusAdapter(CellEditor editor) { this.editor = editor; } @Override public void focusLost(FocusEvent e) { editor.stopCellEditing(); } }; public static class CopyKeyAdapter extends KeyAdapter { private static final String defaultEditorKitCopyActionName = DefaultEditorKit.copyAction; private static final String transferHandlerCopyActionName = (String) TransferHandler.getCopyAction().getValue(Action.NAME); @Override public void keyPressed(KeyEvent e) { // Accept "copy" key strokes KeyStroke ks = KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers()); JComponent comp = (JComponent) e.getSource(); for (int i = 0; i < 3; i++) { InputMap im = comp.getInputMap(i); Object key = im.get(ks); if (defaultEditorKitCopyActionName.equals(key) || transferHandlerCopyActionName.equals(key)) { return; } } // Accept JTable navigation key strokes if (!tableNavigationKeys.contains(e.getKeyCode())) { e.consume(); } } @Override public void keyTyped(KeyEvent e) { e.consume(); } } }
private boolean compare_PrimitiveArray( int operation, Class type, Object primarray, Object value2) { if (Integer.TYPE.isAssignableFrom(type)) { int[] array = (int[]) primarray; for (int i = 0, size = array.length; i < size; i++) { if (compare_Integer(operation, array[i], value2)) { return true; } } return false; } if (Long.TYPE.isAssignableFrom(type)) { long[] array = (long[]) primarray; for (int i = 0, size = array.length; i < size; i++) { if (compare_Long(operation, array[i], value2)) { return true; } } return false; } if (Byte.TYPE.isAssignableFrom(type)) { byte[] array = (byte[]) primarray; for (int i = 0, size = array.length; i < size; i++) { if (compare_Byte(operation, array[i], value2)) { return true; } } return false; } if (Short.TYPE.isAssignableFrom(type)) { short[] array = (short[]) primarray; for (int i = 0, size = array.length; i < size; i++) { if (compare_Short(operation, array[i], value2)) { return true; } } return false; } if (Character.TYPE.isAssignableFrom(type)) { char[] array = (char[]) primarray; for (int i = 0, size = array.length; i < size; i++) { if (compare_Character(operation, array[i], value2)) { return true; } } return false; } if (Float.TYPE.isAssignableFrom(type)) { float[] array = (float[]) primarray; for (int i = 0, size = array.length; i < size; i++) { if (compare_Float(operation, array[i], value2)) { return true; } } return false; } if (Double.TYPE.isAssignableFrom(type)) { double[] array = (double[]) primarray; for (int i = 0, size = array.length; i < size; i++) { if (compare_Double(operation, array[i], value2)) { return true; } } return false; } if (Boolean.TYPE.isAssignableFrom(type)) { boolean[] array = (boolean[]) primarray; for (int i = 0, size = array.length; i < size; i++) { if (compare_Boolean(operation, array[i], value2)) { return true; } } return false; } return false; }
private static void linkGlobalProperty(Class clz, Map<String, String> propertiesMap) { for (Field f : clz.getDeclaredFields()) { GlobalProperty at = f.getAnnotation(GlobalProperty.class); if (at == null) { continue; } if (!Modifier.isStatic(f.getModifiers())) { throw new CloudRuntimeException(String.format("%s.%s is annotated by @GlobalProperty but it's not defined with static modifier", clz.getName(), f.getName())); } Object valueToSet = null; String name = at.name(); if (Map.class.isAssignableFrom(f.getType())) { Map ret = linkGlobalPropertyMap(name); if (ret.isEmpty() && at.required()) { throw new IllegalArgumentException(String.format("A required global property[%s] missing in zstack.properties", name)); } valueToSet = ret; } else if (List.class.isAssignableFrom(f.getType())) { List ret = linkGlobalPropertyList(name); if (ret.isEmpty() && at.required()) { throw new IllegalArgumentException(String.format("A required global property[%s] missing in zstack.properties", name)); } valueToSet = ret; } else { String value = getGlobalProperty(name); if (value == null && at.defaultValue().equals(GlobalProperty.DEFAULT_NULL_STRING) && at.required()) { throw new IllegalArgumentException(String.format("A required global property[%s] missing in zstack.properties", name)); } if (value == null) { value = at.defaultValue(); } if (GlobalProperty.DEFAULT_NULL_STRING.equals(value)) { value = null; } if (value != null) { value = StringTemplate.subsititute(value, propertiesMap); } if (Integer.class.isAssignableFrom(f.getType()) || Integer.TYPE.isAssignableFrom(f.getType())) { valueToSet = TypeUtils.stringToValue(value, Integer.class, 0); } else if (Long.class.isAssignableFrom(f.getType()) || Long.TYPE.isAssignableFrom(f.getType())) { valueToSet = TypeUtils.stringToValue(value, Long.class, 0L); } else if (Float.class.isAssignableFrom(f.getType()) || Float.TYPE.isAssignableFrom(f.getType())) { valueToSet = TypeUtils.stringToValue(value, Float.class, 0F); } else if (Double.class.isAssignableFrom(f.getType()) || Double.TYPE.isAssignableFrom(f.getType())) { valueToSet = TypeUtils.stringToValue(value, Double.class, 0D); } else if (String.class.isAssignableFrom(f.getType())) { valueToSet = value; } else if (Boolean.class.isAssignableFrom(f.getType()) || Boolean.TYPE.isAssignableFrom(f.getType())) { valueToSet = TypeUtils.stringToValue(value, Boolean.class); } else { throw new CloudRuntimeException(String.format("%s.%s of type[%s] is unsupported by global property. try use Platform.getGlobalProperty() and parse by yourself", clz.getName(), f.getName(), f.getType().getName())); } } f.setAccessible(true); try { f.set(null, valueToSet); globalProperties.put(name, valueToSet == null ? "null" : valueToSet.toString()); logger.debug(String.format("linked global property[%s.%s], value: %s", clz.getName(), f.getName(), valueToSet)); } catch (IllegalAccessException e) { throw new CloudRuntimeException(String.format("unable to link global property[%s.%s]", clz.getName(), f.getName()), e); } } }
private static Map<String, Class<?>> createCommonJavaClassesMap() { Map<String, Class<?>> m = new HashMap<>(); m.put("java.lang.String", java.lang.String.class); m.put("String", java.lang.String.class); m.put("java.lang.CharSequence", java.lang.CharSequence.class); m.put("CharSequence", java.lang.CharSequence.class); m.put("java.sql.Timestamp", java.sql.Timestamp.class); m.put("Timestamp", java.sql.Timestamp.class); m.put("java.sql.Time", java.sql.Time.class); m.put("Time", java.sql.Time.class); m.put("java.sql.Date", java.sql.Date.class); m.put("Date", java.sql.Date.class); m.put("java.util.Locale", java.util.Locale.class); m.put("java.util.TimeZone", java.util.TimeZone.class); m.put("java.lang.Byte", java.lang.Byte.class); m.put("java.lang.Character", java.lang.Character.class); m.put("java.lang.Integer", java.lang.Integer.class); m.put("Integer", java.lang.Integer.class); m.put("java.lang.Long", java.lang.Long.class); m.put("Long", java.lang.Long.class); m.put("java.lang.Short", java.lang.Short.class); m.put("java.lang.Float", java.lang.Float.class); m.put("Float", java.lang.Float.class); m.put("java.lang.Double", java.lang.Double.class); m.put("Double", java.lang.Double.class); m.put("java.math.BigDecimal", java.math.BigDecimal.class); m.put("BigDecimal", java.math.BigDecimal.class); m.put("java.math.BigInteger", java.math.BigInteger.class); m.put("BigInteger", java.math.BigInteger.class); m.put("java.lang.Boolean", java.lang.Boolean.class); m.put("Boolean", java.lang.Boolean.class); m.put("java.lang.Object", java.lang.Object.class); m.put("Object", java.lang.Object.class); m.put("java.sql.Blob", java.sql.Blob.class); m.put("Blob", java.sql.Blob.class); m.put("java.nio.ByteBuffer", java.nio.ByteBuffer.class); m.put("java.sql.Clob", java.sql.Clob.class); m.put("Clob", java.sql.Clob.class); m.put("java.util.Date", java.util.Date.class); m.put("java.util.Collection", java.util.Collection.class); m.put("Collection", java.util.Collection.class); m.put("java.util.List", java.util.List.class); m.put("List", java.util.List.class); m.put("java.util.ArrayList", java.util.ArrayList.class); m.put("ArrayList", java.util.ArrayList.class); m.put("java.util.Map", java.util.Map.class); m.put("Map", java.util.Map.class); m.put("java.util.HashMap", java.util.HashMap.class); m.put("java.util.Set", java.util.Set.class); m.put("Set", java.util.Set.class); m.put("java.util.HashSet", java.util.HashSet.class); m.put("groovy.util.Node", groovy.util.Node.class); m.put("Node", groovy.util.Node.class); m.put("org.moqui.util.MNode", org.moqui.util.MNode.class); m.put("MNode", org.moqui.util.MNode.class); m.put(Boolean.TYPE.getName(), Boolean.TYPE); m.put(Short.TYPE.getName(), Short.TYPE); m.put(Integer.TYPE.getName(), Integer.TYPE); m.put(Long.TYPE.getName(), Long.TYPE); m.put(Float.TYPE.getName(), Float.TYPE); m.put(Double.TYPE.getName(), Double.TYPE); m.put(Byte.TYPE.getName(), Byte.TYPE); m.put(Character.TYPE.getName(), Character.TYPE); m.put("long[]", long[].class); m.put("char[]", char[].class); return m; }