Exemplo n.º 1
0
 @Test
 public void PathClassOfTPathOfQString() {
   assertEquals(
       "variable.property",
       Expressions.path(String.class, Expressions.path(Object.class, "variable"), "property")
           .toString());
 }
Exemplo n.º 2
0
 @Test
 public void roundtrip() throws Exception {
   Path<?> path = ExpressionUtils.path(Object.class, "entity");
   SimplePath<?> path2 = Expressions.path(Object.class, "entity");
   assertEquals(path, serialize(path));
   assertEquals(path2, serialize(path2));
   assertEquals(path2.isNull(), serialize(path2.isNull()));
   assertEquals(path.hashCode(), serialize(path).hashCode());
   assertEquals(path2.hashCode(), serialize(path2).hashCode());
   assertEquals(path2.isNull().hashCode(), serialize(path2.isNull()).hashCode());
 }
Exemplo n.º 3
0
  @Test
  public void expressions() throws Exception {
    Map<Class<?>, Object> args = Maps.newHashMap();
    args.put(Object.class, "obj");
    args.put(BeanPath.class, new EntityPathBase<Object>(Object.class, "obj"));
    args.put(Class.class, Integer.class);
    args.put(Class[].class, new Class<?>[] {Object.class, Object.class});
    args.put(java.util.Date.class, new java.util.Date(0));
    args.put(java.sql.Date.class, new java.sql.Date(0));
    args.put(java.sql.Time.class, new java.sql.Time(0));
    args.put(java.sql.Timestamp.class, new java.sql.Timestamp(0));
    args.put(Expression.class, Expressions.enumPath(Gender.class, "e"));
    args.put(
        Expression[].class,
        new Expression<?>[] {Expressions.enumPath(Gender.class, "e"), Expressions.stringPath("s")});
    args.put(FactoryExpression.class, Projections.tuple(Expressions.stringPath("str")));
    args.put(GroupExpression.class, GroupBy.avg(Expressions.numberPath(Integer.class, "num")));
    args.put(Number.class, 1);
    args.put(Operator.class, Ops.AND);
    args.put(Path.class, Expressions.stringPath("str"));
    args.put(PathBuilderValidator.class, PathBuilderValidator.DEFAULT);
    args.put(PathMetadata.class, PathMetadataFactory.forVariable("obj"));
    args.put(PathInits.class, PathInits.DEFAULT);
    args.put(Predicate.class, Expressions.path(Object.class, "obj").isNull());
    args.put(QueryMetadata.class, new DefaultQueryMetadata());
    args.put(String.class, "obj");

    Reflections reflections = new Reflections();
    Set<Class<? extends Expression>> types = reflections.getSubTypesOf(Expression.class);
    for (Class<?> type : types) {
      if (!type.isInterface()
          && !type.isMemberClass()
          && !Modifier.isAbstract(type.getModifiers())) {
        for (Constructor<?> c : type.getConstructors()) {
          Object[] parameters = new Object[c.getParameterTypes().length];
          for (int i = 0; i < c.getParameterTypes().length; i++) {
            parameters[i] =
                Objects.requireNonNull(
                    args.get(c.getParameterTypes()[i]), c.getParameterTypes()[i].getName());
          }
          c.setAccessible(true);
          Object o = c.newInstance(parameters);
          assertEquals(o, Serialization.serialize(o));
        }
      }
    }
  }
Exemplo n.º 4
0
 @Test
 public void PathClassOfTString() {
   assertEquals("variable", Expressions.path(String.class, "variable").toString());
 }
Exemplo n.º 5
0
 @Test
 public void BooleanPathPathOfQString() {
   assertEquals(
       "variable.property",
       Expressions.booleanPath(Expressions.path(Object.class, "variable"), "property").toString());
 }
Exemplo n.º 6
0
/**
 * {@code Alias} provides alias factory methods
 *
 * <p>Example:
 *
 * <pre>{@code
 * Employee e = alias(Employee.class, "e");
 * for (String name : query.from($(e),employees)
 *     .where($(e.getDepartment().getId()).eq(1001))
 *     .list($(e.getName()))) {
 *     System.out.println(name);
 * }
 * }</pre>
 *
 * <p>using the following static imports
 *
 * <pre>{@code
 * import static com.mysema.query.alias.Alias.$;
 * import static com.mysema.query.alias.Alias.alias;
 * }</pre>
 *
 * @author tiwe
 */
@SuppressWarnings("PMD")
public final class Alias {

  private static final AliasFactory aliasFactory =
      new AliasFactory(new DefaultPathFactory(), new DefaultTypeSystem());

  private static final SimplePath<Object> it = Expressions.path(Object.class, "it");

  // exclude $-methods from Checkstyle checks
  // CHECKSTYLE:OFF
  /**
   * Convert the given alias to an expression
   *
   * @param <D>
   * @return expression
   */
  public static <D extends Expression<?>> D $() {
    return aliasFactory.getCurrentAndReset();
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @param <D>
   * @return expression
   */
  public static <D> ArrayPath<D[], D> $(D[] arg) {
    return aliasFactory.getCurrentAndReset();
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @return expression
   */
  public static NumberPath<BigDecimal> $(BigDecimal arg) {
    return aliasFactory.getCurrentAndReset();
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @return expression
   */
  public static NumberPath<BigInteger> $(BigInteger arg) {
    return aliasFactory.getCurrentAndReset();
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @return expression
   */
  public static BooleanPath $(Boolean arg) {
    return aliasFactory.getCurrentAndReset();
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @return expression
   */
  public static NumberPath<Byte> $(Byte arg) {
    return aliasFactory.getCurrentAndReset();
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @param <T>
   * @return expression
   */
  public static <T extends Enum<T>> EnumPath<T> $(T arg) {
    return aliasFactory.getCurrentAndReset();
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @param <D>
   * @return expression
   */
  public static <D> CollectionPath<D, SimpleExpression<D>> $(Collection<D> arg) {
    return aliasFactory.getCurrentAndReset();
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @param <D>
   * @return expression
   */
  public static <D extends Comparable<?>> ComparablePath<D> $(D arg) {
    return Alias.getPath(arg);
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @return expression
   */
  public static NumberPath<Double> $(Double arg) {
    return aliasFactory.getCurrentAndReset();
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @return expression
   */
  public static NumberPath<Float> $(Float arg) {
    return aliasFactory.getCurrentAndReset();
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @return expression
   */
  public static NumberPath<Integer> $(Integer arg) {
    return aliasFactory.getCurrentAndReset();
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @return expression
   */
  public static DatePath<java.sql.Date> $(java.sql.Date arg) {
    return aliasFactory.getCurrentAndReset();
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @return expression
   */
  public static DateTimePath<java.util.Date> $(java.util.Date arg) {
    return aliasFactory.getCurrentAndReset();
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @param <D>
   * @return expression
   */
  public static <D> ListPath<D, SimpleExpression<D>> $(List<D> arg) {
    return aliasFactory.getCurrentAndReset();
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @return expression
   */
  public static NumberPath<Long> $(Long arg) {
    return aliasFactory.getCurrentAndReset();
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @param <K>
   * @param <V>
   * @return expression
   */
  public static <K, V> MapPath<K, V, SimpleExpression<V>> $(Map<K, V> arg) {
    return aliasFactory.getCurrentAndReset();
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @param <D>
   * @return expression
   */
  public static <D> SetPath<D, SimpleExpression<D>> $(Set<D> arg) {
    return aliasFactory.getCurrentAndReset();
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @return expression
   */
  public static NumberPath<Short> $(Short arg) {
    return aliasFactory.getCurrentAndReset();
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @return expression
   */
  public static StringPath $(String arg) {
    return aliasFactory.getCurrentAndReset();
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @return expression
   */
  public static TimePath<Time> $(Time arg) {
    return aliasFactory.getCurrentAndReset();
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @return expression
   */
  public static DateTimePath<Timestamp> $(Timestamp arg) {
    return aliasFactory.getCurrentAndReset();
  }

  /**
   * Convert the given alias to an expression
   *
   * @param arg alias
   * @param <D>
   * @return expression
   */
  @SuppressWarnings("unchecked")
  @Nullable
  public static <D> EntityPathBase<D> $(D arg) {
    EntityPathBase<D> rv = aliasFactory.getCurrentAndReset();
    if (rv != null) {
      return rv;
    } else if (arg instanceof EntityPath<?>) {
      return (EntityPathBase<D>) arg; // NOSONAR
    } else if (arg instanceof ManagedObject) {
      return (EntityPathBase<D>) ((ManagedObject) arg).__mappedPath();
    } else {
      return null;
    }
  }

  @SuppressWarnings("unchecked")
  @Nullable
  private static <D, P extends Path<D>> P getPath(D arg) {
    P rv = aliasFactory.getCurrentAndReset();
    if (rv != null) {
      return rv;
    } else if (arg instanceof Path<?>) {
      return (P) arg;
    } else if (arg instanceof ManagedObject) {
      return (P) ((ManagedObject) arg).__mappedPath();
    } else {
      return null;
    }
  }

  // CHECKSTYLE:ON

  /**
   * Create a new alias proxy of the given type
   *
   * @param cl type of the alias
   * @return alias instance
   */
  public static <A> A alias(Class<A> cl) {
    return alias(cl, CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, cl.getSimpleName()));
  }

  /**
   * Create a new alias proxy of the given type for the given expression
   *
   * @param cl type of the alias
   * @param expr underlying expression
   * @return alias instance
   */
  public static <A> A alias(Class<A> cl, Expression<? extends A> expr) {
    return aliasFactory.createAliasForExpr(cl, expr);
  }

  /**
   * Create a new alias proxy of the given type for the given variable
   *
   * @param cl type of the alias
   * @param var variable name for the underlying expression
   * @return alias instance
   */
  public static <A> A alias(Class<A> cl, String var) {
    return aliasFactory.createAliasForVariable(cl, var);
  }

  /**
   * Convert the given alias to an expression
   *
   * @param <D>
   * @param arg alias instance
   * @return underlying expression
   */
  @SuppressWarnings("unchecked")
  public static <D> Expression<D> getAny(D arg) {
    Expression<D> current = aliasFactory.getCurrentAndReset();
    if (current != null) {
      return current;
    } else if (arg instanceof ManagedObject) {
      return (Expression<D>) ((ManagedObject) arg).__mappedPath();
    } else {
      throw new IllegalArgumentException("No path mapped to " + arg);
    }
  }

  /** Reset the alias */
  public static void resetAlias() {
    aliasFactory.reset();
  }

  /**
   * Return the default variable
   *
   * @param <D>
   * @return expression
   */
  @SuppressWarnings("unchecked")
  public static <D> SimplePath<D> var() {
    return (SimplePath<D>) it;
  }

  /**
   * Create a new variable path
   *
   * @param arg alias
   * @param <D>
   * @return expression
   */
  @SuppressWarnings("unchecked")
  public static <D extends Comparable<?>> ComparablePath<D> var(D arg) {
    return Expressions.comparablePath((Class<D>) arg.getClass(), "var" + arg);
  }

  /**
   * Create a new variable path
   *
   * @param arg alias
   * @param <D>
   * @return expression
   */
  @SuppressWarnings("unchecked")
  public static <D extends Number & Comparable<D>> NumberPath<D> var(D arg) {
    return Expressions.numberPath(
        (Class<D>) arg.getClass(), "var" + arg.getClass().getSimpleName() + arg);
  }

  /**
   * Create a new variable path
   *
   * @param arg alias
   * @param <D>
   * @return expression
   */
  @SuppressWarnings("unchecked")
  public static <D> EntityPathBase<D> var(D arg) {
    String var = "var" + arg.getClass().getSimpleName() + "_" + arg.toString().replace(' ', '_');
    return new PathBuilder<D>((Class) arg.getClass(), var);
  }

  /**
   * Create a new variable path
   *
   * @param arg alias
   * @return expression
   */
  public static StringPath var(String arg) {
    return Expressions.stringPath(arg.replace(' ', '_'));
  }

  private Alias() {}
}