/** * 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; }
public static Connection getConnection(Connection c) throws Exception { Connection conn = c; if (c != null && !c.isClosed()) { return c; } else { Class clazz = Class.forName("org.luciddb.jdbc.LucidDbLocalDriver"); LucidDbLocalDriver driver = (LucidDbLocalDriver) clazz.newInstance(); String urlPrefix = driver.getUrlPrefix(); Properties props = new Properties(); props.setProperty("user", "sa"); props.setProperty("password", ""); props.setProperty("requireExistingEngine", "true"); c = DriverManager.getConnection(urlPrefix, props); } return c; }
/** * 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; }
private Object unmarshall(Object value) { if (value instanceof ObjectName) { ObjectName name = (ObjectName) value; return new MBean(_server, name); } else if (value instanceof ObjectName[]) { ObjectName[] names = (ObjectName[]) value; MBean[] mbeans = new MBean[names.length]; for (int i = 0; i < names.length; i++) mbeans[i] = new MBean(_server, names[i]); return mbeans; } else if (value instanceof CompositeData) { CompositeData compositeValue = (CompositeData) value; CompositeType type = compositeValue.getCompositeType(); if (type != null) { String typeName = type.getTypeName(); try { ClassLoader loader = Thread.currentThread().getContextClassLoader(); Class typeClass = Class.forName(typeName, false, loader); Method from = typeClass.getMethod("from", new Class[] {CompositeData.class}); if (from != null) return from.invoke(null, compositeValue); } catch (Exception e) { log.log(Level.FINER, e.toString(), e); } } return new CompositeDataBean(compositeValue); } else return value; }
/** 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; }
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); }