com.googlecode.objectify.annotation.Parent Java Examples

The following examples show how to use com.googlecode.objectify.annotation.Parent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GetSchemaTreeCommand.java    From nomulus with Apache License 2.0 6 votes vote down vote up
private Class<?> getParentType(Class<?> clazz) {
  for (; clazz != null; clazz = clazz.getSuperclass()) {
    for (Field field : clazz.getDeclaredFields()) {
      if (field.isAnnotationPresent(Parent.class)) {
        try {
          return getSystemClassLoader().loadClass(
              ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]
                  .toString()
                  .replace("? extends ", "")
                  .replace("class ", ""));
        } catch (ClassNotFoundException e) {
          throw new RuntimeException(e);
        }
      }
    }
  }
  return Object.class;
}
 
Example #2
Source File: ModelUtils.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/** Return a string representing the persisted schema of a type or enum. */
static String getSchema(Class<?> clazz) {
  StringBuilder stringBuilder = new StringBuilder();
  Stream<?> body;
  if (clazz.isEnum()) {
    stringBuilder.append("enum ");
    body = Arrays.stream(clazz.getEnumConstants());
  } else {
    stringBuilder.append("class ");
    body =
        getAllFields(clazz)
            .values()
            .stream()
            .filter(field -> !field.isAnnotationPresent(Ignore.class))
            .map(
                field -> {
                  String annotation =
                      field.isAnnotationPresent(Id.class)
                          ? "@Id "
                          : field.isAnnotationPresent(Parent.class) ? "@Parent " : "";
                  String type =
                      field.getType().isArray()
                          ? field.getType().getComponentType().getName() + "[]"
                          : field.getGenericType().toString().replaceFirst("class ", "");
                  return String.format("%s%s %s", annotation, type, field.getName());
                });
  }
  return stringBuilder
      .append(clazz.getName())
      .append(" {\n  ")
      .append(body.map(Object::toString).sorted().collect(Collectors.joining(";\n  ")))
      .append(";\n}")
      .toString();
}
 
Example #3
Source File: EntityTestCase.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/** List all field paths so we can verify that we checked everything. */
private Set<String> getAllPotentiallyIndexedFieldPaths(Class<?> clazz) {
  Set<String> fields = Sets.newHashSet();
  for (; clazz != Object.class; clazz = clazz.getSuperclass()) {
    for (final Field field : clazz.getDeclaredFields()) {
      // Ignore static, @Serialize, @Parent and @Ignore fields, since these are never indexed.
      if (!Modifier.isStatic(field.getModifiers())
          && !field.isAnnotationPresent(Ignore.class)
          && !field.isAnnotationPresent(Parent.class)
          && !field.isAnnotationPresent(Serialize.class)) {
        Class<?> fieldClass = field.getType();
        // If the field is a collection, just pretend that it was a field of that type,
        // because verifyIndexingHelper knows how to descend into collections.
        if (Collection.class.isAssignableFrom(fieldClass)) {
          Type inner = ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
          fieldClass =
              inner instanceof ParameterizedType
                  ? (Class<?>) ((ParameterizedType) inner).getRawType()
                  : (Class<?>) inner;
        }
        // Descend into persisted ImmutableObject classes, but not anything else.
        if (ImmutableObject.class.isAssignableFrom(fieldClass)
            && !VKey.class.isAssignableFrom(fieldClass)) {
          getAllPotentiallyIndexedFieldPaths(fieldClass).stream()
              .map(subfield -> field.getName() + "." + subfield)
              .distinct()
              .forEachOrdered(fields::add);
        } else {
          fields.add(field.getName());
        }
      }
    }
  }
  return fields;
}