/** true iff the initial N args match the corresponding types */ public static boolean typeMatches(Object arg, Class<?> parameterType, Optional<? extends TypeCoercer> coercer) { if (parameterType.isPrimitive() && arg == null) return false; if (arg == null) return true; if (Boxing.boxedType(parameterType).isInstance(arg)) return true; if (coercer.isPresent() && coercer.get().tryCoerce(arg, parameterType).isPresent()) return true; return false; }
public static boolean isPrimitiveOrBoxedObject(Object o) { if (o==null) return false; return isPrimitiveOrBoxedClass(o.getClass()); } public static boolean isPrimitiveOrBoxedClass(Class<?> t) {
@Test public static void testIntPrimitiveAndBoxed() { Assert.assertEquals(Integer.TYPE.getName(), "int"); Class<?> t = Boxing.getPrimitiveType("int").get(); Assert.assertEquals(t, Integer.TYPE); Class<?> bt = Boxing.boxedType(t); Assert.assertEquals(bt, Integer.class); }
boolean varargsMatch = true; for (int i=parameterTypes.length-1; i<argsArray.length; i++) { if (!Boxing.boxedType(varargType).isInstance(argsArray[i]) || (varargType.isPrimitive() && argsArray[i]==null)) { varargsMatch = false; Object varargs = Array.newInstance(varargType, argsArray.length+1 - parameterTypes.length); for (int i=parameterTypes.length-1; i<argsArray.length; i++) { Boxing.setInArray(varargs, i+1-parameterTypes.length, argsArray[i], varargType);
@Override public void onEvent(SensorEvent<Boolean> event) { if (AtomicReferences.setIfDifferent(serviceUp, Boxing.unboxSafely(event.getValue(), false))) { log.debug(""+this+" updated on "+event+", "+"enabled="+computeEnablement()); updateEnablement(); } } });
@SuppressWarnings("unchecked") protected Class<T> getType(Entity entity, String className) { try { // TODO use OSGi loader (low priority however); also ensure that allows primitives Maybe<Class<?>> primitive = Boxing.getPrimitiveType(className); if (primitive.isPresent()) return (Class<T>) primitive.get(); return (Class<T>) new ClassLoaderUtils(this, entity).loadClass(className); } catch (ClassNotFoundException e) { if (!className.contains(".")) { // could be assuming "java.lang" package; try again with that try { return (Class<T>) Class.forName("java.lang."+className); } catch (ClassNotFoundException e2) { throw new IllegalArgumentException("Invalid target type for sensor "+name+": " + className+" (also tried java.lang."+className+")"); } } else { throw new IllegalArgumentException("Invalid target type for sensor "+name+": " + className); } } }
for (int i=parameterTypes.length-1; i<argsArray.length; i++) { Object arg = coercer.isPresent() ? coercer.get().coerce(argsArray[i], varargType) : argsArray[i]; Boxing.setInArray(varargs, i+1-parameterTypes.length, arg, varargType);
@Test public static void getPrimitive() { Assert.assertEquals(Boxing.getPrimitiveName(Integer.class).get(), "int"); }
protected boolean checkMembership(Entity e) { if (!Entities.isManaged(e)) return false; if (!Boxing.unboxSafely(e.getAttribute(Startable.SERVICE_UP), false)) return false; return true; } }
@Override public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { String clazzName = reader.getAttribute("class"); Class<?> clazz; if (clazzName == null) { clazz = defaultImpl; } else if (clazzName.equals("string")) { clazz = String.class; } else if (Boxing.getPrimitiveType(clazzName).isPresent()) { clazz = Boxing.getPrimitiveType(clazzName).get(); } else { try { clazz = loader.getReference().loadClass(clazzName); } catch (ClassNotFoundException e) { throw Exceptions.propagate(e); } } Converter converter = lookup.lookupConverterForType(clazz); return converter.unmarshal(reader, context); } }