/** * Invoke a method on the given object with the given arguments. * * @param object The object the method should be invoked on. * @param methodName The name of the method to invoke. * @param arguments The arguments to the invoked method as null, a Tuple, an array or a single argument of any type. * * @return The result of the method invocation. */ public Object invokeMethod(Object object, String methodName, Object arguments) { if (arguments == null) { return invokeMethod(object, methodName, MetaClassHelper.EMPTY_ARRAY); } if (arguments instanceof Tuple) { Tuple tuple = (Tuple) arguments; return invokeMethod(object, methodName, tuple.toArray()); } if (arguments instanceof Object[]) { return invokeMethod(object, methodName, (Object[]) arguments); } else { return invokeMethod(object, methodName, new Object[]{arguments}); } }
public static Tuple createTuple(Object[] array) { return new Tuple(array); }
public void testHashCodeAndEquals() { Tuple a = new Tuple(new Object[]{"a", "b", "c"}); Tuple b = new Tuple(new Object[]{"a", "b", "c"}); Tuple c = new Tuple(new Object[]{"d", "b", "c"}); Tuple d = new Tuple(new Object[]{"a", "b"}); Tuple2<String, String> e = new Tuple2<String, String>("a", "b"); Tuple2<String, String> f = new Tuple2<String, String>("a", "c"); assertEquals("hashcode", a.hashCode(), b.hashCode()); assertTrue("hashcode", a.hashCode() != c.hashCode()); assertEquals("a and b", a, b); assertFalse("a != c", a.equals(c)); assertFalse("!a.equals(null)", a.equals(null)); assertTrue("d.equals(e)", d.equals(e)); assertTrue("e.equals(d)", e.equals(d)); assertFalse("!e.equals(f)", e.equals(f)); assertFalse("!f.equals(e)", f.equals(e)); }
@Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Tuple)) return false; Tuple that = (Tuple) o; int size = size(); if (size != that.size()) return false; for (int i = 0; i < size; i++) { if (!DefaultTypeTransformation.compareEqual(get(i), that.get(i))) { return false; } } return true; }
public boolean equals(Object that) { if (that instanceof Tuple) { return equals((Tuple) that); } return false; }
public boolean equals(Object that) { if (that instanceof Tuple) { return equals((Tuple) that); } return false; }
public List<Object> getUpdatedParams(List<Object> params, List<Tuple> indexPropList) { List<Object> updatedParams = new ArrayList<Object>(); for (Tuple tuple : indexPropList) { int index = (Integer) tuple.get(0); String prop = (String) tuple.get(1); if (index < 0 || index >= params.size()) throw new IllegalArgumentException("Invalid index " + index + " should be in range 1.." + params.size()); updatedParams.add(prop.equals("<this>") ? params.get(index) : InvokerHelper.getProperty(params.get(index), prop)); } return updatedParams; }
private static Object[] unwrapArgs(Object arguments) { if (arguments == null) { return MetaClassHelper.EMPTY_ARRAY; } else if (arguments instanceof Tuple) { Tuple tuple = (Tuple) arguments; return tuple.toArray(); } else if (arguments instanceof Object[]) { return (Object[])arguments; } else { return new Object[]{arguments}; } }
public static Tuple createTuple(Object[] array) { return new Tuple(array); }
public boolean equals(Object that) { if (that instanceof Tuple) { return equals((Tuple) that); } return false; }
public List<Object> getUpdatedParams(List<Object> params, List<Tuple> indexPropList) { List<Object> updatedParams = new ArrayList<Object>(); for (Tuple tuple : indexPropList) { int index = (Integer) tuple.get(0); String prop = (String) tuple.get(1); if (index < 0 || index >= params.size()) throw new IllegalArgumentException("Invalid index " + index + " should be in range 1.." + params.size()); try { updatedParams.add(prop.equals("<this>") ? params.get(index) : InvokerHelper.getProperty(params.get(index), prop)); } catch(MissingPropertyException mpe) { throw new IllegalArgumentException("Property '" + prop + "' not found for parameter " + index); } } return updatedParams; }
@Override public Object invokeMethod(final Object object, final String name, final Object args) { if (args == null) { return invokeMethod(object, name, MetaClassHelper.EMPTY_ARRAY); } else if (args instanceof Tuple) { return invokeMethod(object, name, ((Tuple) args).toArray()); } else if (args instanceof Object[]) { return invokeMethod(object, name, (Object[]) args); } else { return invokeMethod(object, name, new Object[]{args}); } }
@SuppressWarnings("unchecked") @Override public List<E> subList(int fromIndex, int toIndex) { int size = toIndex - fromIndex; E[] newContent = (E[]) new Object[size]; System.arraycopy(contents, fromIndex, newContent, 0, size); return new Tuple<>(newContent); }
public boolean equals(Object that) { if (that instanceof Tuple) { return equals((Tuple) that); } return false; }
@Override public Object invokeMethod(Object object, String name, Object args) { if (args == null) { return invokeMethod(object, name, MetaClassHelper.EMPTY_ARRAY); } if (args instanceof Tuple) { return invokeMethod(object, name, ((Tuple) args).toArray()); } if (args instanceof Object[]) { return invokeMethod(object, name, (Object[]) args); } else { return invokeMethod(object, name, new Object[]{args}); } }
private static String adaptForNamedParams(String sql, List<Tuple> indexPropList) { StringBuilder newSql = new StringBuilder(); int txtIndex = 0; Matcher matcher = NAMED_QUERY_PATTERN.matcher(sql); while (matcher.find()) { newSql.append(sql, txtIndex, matcher.start()).append('?'); String indexStr = matcher.group(1); if (indexStr == null) indexStr = matcher.group(3); int index = (indexStr == null || indexStr.length() == 0 || ":".equals(indexStr)) ? 0 : Integer.parseInt(indexStr) - 1; String prop = matcher.group(2); if (prop == null) prop = matcher.group(4); indexPropList.add(new Tuple(index, prop == null || prop.length() == 0 ? "<this>" : prop)); txtIndex = matcher.end(); } newSql.append(sql.substring(txtIndex)); // append ending SQL after last param. return newSql.toString(); }
static Object[] unwrapArgs(Object arguments) { if (arguments == null) { return MetaClassHelper.EMPTY_ARRAY; } else if (arguments instanceof Tuple) { Tuple tuple = (Tuple) arguments; return tuple.toArray(); } else if (arguments instanceof Object[]) { return (Object[])arguments; } else { return new Object[]{arguments}; } }
public static Tuple createTuple(Object[] array) { return new Tuple(array); }