예제 #1
0
 /**
  * Assert that the given object is lenient equals by ignoring null fields value on other object.
  *
  * @param info contains information about the assertion.
  * @param actual the given object.
  * @param other the object to compare {@code actual} to.
  * @throws NullPointerException if the actual type is {@code null}.
  * @throws NullPointerException if the other type is {@code null}.
  * @throws AssertionError if the actual and the given object are not lenient equals.
  * @throws AssertionError if the other object is not an instance of the actual type.
  */
 public <A> void assertIsLenientEqualsToByIgnoringNullFields(
     AssertionInfo info, A actual, A other) {
   assertIsInstanceOf(info, other, actual.getClass());
   List<String> fieldsNames = new LinkedList<String>();
   List<Object> values = new LinkedList<Object>();
   List<String> nullFields = new LinkedList<String>();
   for (Field field : actual.getClass().getDeclaredFields()) {
     try {
       Object otherFieldValue =
           propertySupport.propertyValue(field.getName(), field.getType(), other);
       if (otherFieldValue != null) {
         Object actualFieldValue =
             propertySupport.propertyValue(field.getName(), field.getType(), actual);
         if (!otherFieldValue.equals(actualFieldValue)) {
           fieldsNames.add(field.getName());
           values.add(otherFieldValue);
         }
       } else {
         nullFields.add(field.getName());
       }
     } catch (IntrospectionError e) {
       // Not readeable field, skip.
     }
   }
   if (fieldsNames.isEmpty()) return;
   throw failures.failure(
       info, shouldBeLenientEqualByIgnoring(actual, fieldsNames, values, nullFields));
 }
예제 #2
0
 /**
  * Assert that the given object is lenient equals by ignoring fields.
  *
  * @param info contains information about the assertion.
  * @param actual the given object.
  * @param other the object to compare {@code actual} to.
  * @param fields the fields to ignore in comparison
  * @throws NullPointerException if the actual type is {@code null}.
  * @throws NullPointerException if the other type is {@code null}.
  * @throws AssertionError if the actual and the given object are not lenient equals.
  * @throws AssertionError if the other object is not an instance of the actual type.
  */
 public <A> void assertIsLenientEqualsToByIgnoringFields(
     AssertionInfo info, A actual, A other, String... fields) {
   assertIsInstanceOf(info, other, actual.getClass());
   List<String> fieldsNames = new LinkedList<String>();
   List<Object> expectedValues = new LinkedList<Object>();
   Set<String> ignoredFields = set(fields);
   for (Field field : actual.getClass().getDeclaredFields()) {
     try {
       if (!ignoredFields.contains(field.getName())) {
         String fieldName = field.getName();
         Object actualFieldValue = propertySupport.propertyValue(fieldName, Object.class, actual);
         Object otherFieldValue = propertySupport.propertyValue(fieldName, Object.class, other);
         if (!org.fest.util.Objects.areEqual(actualFieldValue, otherFieldValue)) {
           fieldsNames.add(fieldName);
           expectedValues.add(otherFieldValue);
         }
       }
     } catch (IntrospectionError e) {
       // Not readeable field, skip.
     }
   }
   if (fieldsNames.isEmpty()) return;
   throw failures.failure(
       info, shouldBeLenientEqualByIgnoring(actual, fieldsNames, expectedValues, list(fields)));
 }
예제 #3
0
  public void initialize(A constraint) {
    this.constraint = constraint;
    try {
      Method fpmethod = constraint.getClass().getDeclaredMethod("fieldPath", null);
      fieldPath = (String) fpmethod.invoke(constraint, null);

      Method gmethod = constraint.getClass().getDeclaredMethod("groups", null);
      groups = (Class[]) gmethod.invoke(constraint, null);
    } catch (Exception e) {
      // do nothing
    }
  }
 /**
  * Checks whether a given annotation is a multi value constraint and returns the contained
  * constraints if so.
  *
  * @param annotation the annotation to check.
  * @return A list of constraint annotations or the empty list if <code>annotation</code> is not a
  *     multi constraint annotation.
  */
 public <A extends Annotation> List<Annotation> getMultiValueConstraints(A annotation) {
   List<Annotation> annotationList = new ArrayList<Annotation>();
   try {
     final GetMethod getMethod = GetMethod.action(annotation.getClass(), "value");
     final Method method;
     if (System.getSecurityManager() != null) {
       method = AccessController.doPrivileged(getMethod);
     } else {
       method = getMethod.run();
     }
     if (method != null) {
       Class returnType = method.getReturnType();
       if (returnType.isArray() && returnType.getComponentType().isAnnotation()) {
         Annotation[] annotations = (Annotation[]) method.invoke(annotation);
         for (Annotation a : annotations) {
           if (isConstraintAnnotation(a) || isBuiltinConstraint(a.annotationType())) {
             annotationList.add(a);
           }
         }
       }
     }
   } catch (IllegalAccessException iae) {
     // ignore
   } catch (InvocationTargetException ite) {
     // ignore
   }
   return annotationList;
 }
 protected final <A extends Annotation> void assertAnnotation(
     PropertyInfo<?, ?> property,
     Class<A> annotationClass,
     Map<String, Object> expectedAnnotation) {
   A ann1 = property.getAnnotation(annotationClass);
   assertNotNull(ann1);
   Map<String, Object> values = new HashMap<String, Object>();
   for (Method m : ann1.getClass().getDeclaredMethods()) {
     if (m.getName().equals("equals")
         && m.getParameterTypes().length == 1
         && m.getParameterTypes()[0] == Object.class) {
       continue;
     }
     if (m.getName().equals("hashCode") && m.getParameterTypes().length == 0) {
       continue;
     }
     if (m.getName().equals("toString") && m.getParameterTypes().length == 0) {
       continue;
     }
     if (m.getName().equals("annotationType") && m.getParameterTypes().length == 0) {
       continue;
     }
     try {
       Object value = m.invoke(ann1);
       values.put(m.getName(), value);
     } catch (Exception e) {
       AssertionFailedError afe =
           new AssertionFailedError("Could not invoke annotation value " + m);
       afe.initCause(e);
       throw afe;
     }
   }
   assertEquals(expectedAnnotation, values);
 }
예제 #6
0
 public static void main(String[] args) throws Exception {
   A a = InnerA.makeA();
   a.f();
   System.out.println(a.getClass().getName());
   // Reflection still gets into the private class:
   HiddenImplementation.callHiddenMethod(a, "g");
   HiddenImplementation.callHiddenMethod(a, "u");
   HiddenImplementation.callHiddenMethod(a, "v");
   HiddenImplementation.callHiddenMethod(a, "w");
 }
예제 #7
0
  @Override
  public void write(DataOutput out) throws IOException {
    // Serialize class names of each non-null field
    final ClassIndex index = new ClassIndex();

    if (fst != null) index.addClass(fst.getClass());
    if (snd != null) index.addClass(snd.getClass());
    index.write(out);

    // Indicate which fields are null
    out.writeByte((fst == null ? FST_NULL : NOT_NULL) | (snd == null ? SND_NULL : NOT_NULL));

    // Serialize each non-null field
    if (fst != null) {
      out.writeByte(index.getId(fst.getClass()));
      fst.write(out);
    }
    if (snd != null) {
      out.writeByte(index.getId(snd.getClass()));
      snd.write(out);
    }
  }
  @SuppressWarnings("unchecked")
  @Override
  public <A extends Action<R>, R extends Result>
      ActionHandlerValidatorInstance findActionHandlerValidator(A action) {

    ActionHandlerValidatorInstance actionHandlerValidatorInstance =
        actionHandlerValidatorInstances.get(action.getClass());

    if (actionHandlerValidatorInstance == null) {
      ActionHandlerValidatorClass<? extends Action<?>, ? extends Result>
          actionHandlerValidatorClass = actionHandlerValidatorClasses.get(action.getClass());
      if (actionHandlerValidatorClass != null) {
        actionHandlerValidatorInstance = createInstance(actionHandlerValidatorClass);
        if (actionHandlerValidatorInstance != null) {
          actionHandlerValidatorInstances.put(
              (Class<? extends Action<?>>) action.getClass(), actionHandlerValidatorInstance);
        }
      }
    }

    return actionHandlerValidatorInstance;
  }
 public static void main(String[] args) throws Exception {
   A a = HiddenC.makeA();
   a.f();
   System.out.println(a.getClass().getName());
   // Compile error: cannot find symbol 'C':
   /* if(a instanceof C) {
     C c = (C)a;
     c.g();
   } */
   // Oops! Reflection still allows us to call g():
   callHiddenMethod(a, "g");
   // And even methods that are less accessible!
   callHiddenMethod(a, "u");
   callHiddenMethod(a, "v");
   callHiddenMethod(a, "w");
 }
예제 #10
0
 /**
  * Assert that the given object is lenient equals to other object by comparing given fields value
  * only.
  *
  * @param info contains information about the assertion.
  * @param actual the given object.
  * @param other the object to compare {@code actual} to.
  * @param fields accepted fields
  * @throws NullPointerException if the actual type is {@code null}.
  * @throws NullPointerException if the other type is {@code null}.
  * @throws AssertionError if the actual and the given object are not lenient equals.
  * @throws AssertionError if the other object is not an instance of the actual type.
  * @throws IntrospectionError if a field does not exist in actual.
  */
 public <A> void assertIsLenientEqualsToByAcceptingFields(
     AssertionInfo info, A actual, A other, String... fields) {
   assertIsInstanceOf(info, other, actual.getClass());
   List<String> rejectedFieldsNames = new LinkedList<String>();
   List<Object> expectedValues = new LinkedList<Object>();
   for (String fieldName : fields) {
     Object actualFieldValue = propertySupport.propertyValue(fieldName, Object.class, actual);
     Object otherFieldValue = propertySupport.propertyValue(fieldName, Object.class, other);
     if (!(actualFieldValue == otherFieldValue
         || (actualFieldValue != null && actualFieldValue.equals(otherFieldValue)))) {
       rejectedFieldsNames.add(fieldName);
       expectedValues.add(otherFieldValue);
     }
   }
   if (rejectedFieldsNames.isEmpty()) return;
   throw failures.failure(
       info,
       shouldBeLenientEqualByAccepting(actual, rejectedFieldsNames, expectedValues, list(fields)));
 }
예제 #11
0
  public <A extends Action<R>, R extends Result> void execute(
      final A action, final AsyncCallback<R> callback) {
    String className = action.getClass().getName();
    int namePos = className.lastIndexOf('.') + 1;
    className = className.substring(namePos);
    ((ServiceDefTarget) realService).setServiceEntryPoint(baseUrl + className);

    realService.execute(
        action,
        new AsyncCallback<Result>() {
          public void onFailure(Throwable caught) {
            callback.onFailure(caught);
          }

          @SuppressWarnings("unchecked")
          public void onSuccess(Result result) {
            callback.onSuccess((R) result);
          }
        });
  }
예제 #12
0
  @SuppressWarnings("unchecked")
  public final <A> A getFirst(String key, A defaultValue) {
    String value = getFirst(key);
    if (value == null) {
      return defaultValue;
    }

    Class<A> type = (Class<A>) defaultValue.getClass();

    Constructor<A> c = null;
    try {
      c = type.getConstructor(String.class);
    } catch (Exception ex) {
      throw new IllegalArgumentException(type.getName() + " has no String constructor", ex);
    }
    A retVal = defaultValue;
    try {
      retVal = c.newInstance(value);
    } catch (Exception ex) {
    }
    return retVal;
  }
예제 #13
0
 @SuppressWarnings("unchecked")
 private <A extends Accessible> A findAccessible(String acc, A newObject) {
   return (A) accessibleDao.find(acc, newObject.getClass());
 }
예제 #14
0
 @Override
 public boolean isDefinedAt(A a) {
   return (clazz.equals(a.getClass())) && guard.test(a);
 }