public DataConverter(Class<T> clazz)
      throws TooManyPrimaryKeys, NotAnnotatedAsTableClassException, PrimaryKeyIsNotColumnException,
          NoPrimaryKeyException {
    if (clazz.getAnnotation(Table.class) == null) {
      throw new NotAnnotatedAsTableClassException();
    } else {
      if (!clazz.getDeclaredAnnotation(Table.class).name().equals("")) {
        tableName = clazz.getDeclaredAnnotation(Table.class).name();
      } else {
        tableName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, clazz.getSimpleName());
      }
    }
    fields = new ArrayList<FieldConverter>();
    for (Field field : clazz.getDeclaredFields()) {
      Class<?> type = field.getType();
      Annotation primaryKeyAnnotation = field.getDeclaredAnnotation(PrimaryKey.class),
          columnAnnotation = field.getDeclaredAnnotation(Column.class);

      if (primaryKeyAnnotation != null) {
        if (columnAnnotation == null) {
          throw new PrimaryKeyIsNotColumnException();
        }
        if (primaryKeyField != null) {
          throw new TooManyPrimaryKeys();
        }
        primaryKeyField = field;
      }
      if (columnAnnotation != null) {
        String name = null;
        if (!field.getDeclaredAnnotation(Column.class).name().equals("")) {
          name = field.getDeclaredAnnotation(Column.class).name();
        } else {
          name = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, field.getName());
        }
        FieldConverter currentFieldConverter = new FieldConverter(name, field);
        fields.add(currentFieldConverter);
        if (field.equals(primaryKeyField)) {
          primaryKeyFieldConverter = currentFieldConverter;
        }
      }
      Annotation[] annotations = field.getDeclaredAnnotations();
      for (Annotation annotation : annotations) {
        annotation.getClass();
      }
    }
    if (primaryKeyField == null) {
      throw new NoPrimaryKeyException();
    }
  }
Beispiel #2
0
 public void load(@Nonnull Object object) {
   Preconditions.checkNotNull(object, "object cannot be null.");
   for (Field field : load(object.getClass())) {
     TickField annotation = field.getDeclaredAnnotation(TickField.class);
     try {
       TickFieldGenerated generated =
           new TickFieldGenerated((BAutoInt) field.get(object), annotation);
       field.set(object, generated);
       list.add(generated);
     } catch (IllegalAccessException e) {
       throw new RuntimeException(e);
     }
   }
 }
  @Override
  public void visitField(Field field, String parentPath) {
    if (field.isAnnotationPresent(Id.class)) {
      validateId(field, parentPath == null);
    } else {
      core.framework.api.mongo.Field mongoField =
          field.getDeclaredAnnotation(core.framework.api.mongo.Field.class);
      if (mongoField == null) throw Exceptions.error("field must have @Field, field={}", field);
      String mongoFieldName = mongoField.name();

      Set<String> fields = this.fields.computeIfAbsent(parentPath, key -> Sets.newHashSet());
      if (fields.contains(mongoFieldName)) {
        throw Exceptions.error(
            "field is duplicated, field={}, mongoField={}", field, mongoFieldName);
      }
      fields.add(mongoFieldName);
    }
  }
Beispiel #4
0
  @Override
  public FieldVisitor visitField(
      int access, String name, String desc, String signature, Object value) {
    try {
      Field field = context.getTestClass().getDeclaredField(name);
      Cut cut = field.getDeclaredAnnotation(Cut.class);

      if (cut != null) {
        context.setCutField(new CutDescriptor(field));
      } else {
        DescriptorKey key = new DescriptorKey(field.getGenericType(), field.getName());
        FieldDescriptor descriptor = new FieldDescriptor(field, order++);
        context.putFieldDescriptor(key, descriptor);
      }
      return new TestFieldAnalyzer(context, field);
    } catch (NoSuchFieldException e) {
      throw new RuntimeException(e);
    }
  }
Beispiel #5
0
  private static Collection<Field> load(Class<?> clazz) {
    if (LOADED_CLASSES.contains(clazz)) {
      return Collections.unmodifiableCollection(FIELDS.get(clazz));
    }

    for (Field field : clazz.getDeclaredFields()) {
      TickField annotation = field.getDeclaredAnnotation(TickField.class);
      if (annotation != null) {
        Preconditions.checkArgument(
            field.getType().isAssignableFrom(BAutoInt.class),
            "%s must by of type %s",
            field,
            BAutoInt.class.getName());

        field.setAccessible(true);
        FIELDS.put(clazz, field);
      }
    }
    LOADED_CLASSES.add(clazz);
    return Collections.unmodifiableCollection(FIELDS.get(clazz));
  }