private void serialize(Serializer serializer, Collection<EntityType> models) {
    for (EntityType model : models) {
      try {
        Type type = conf.getTypeMappings().getPathType(model, model, true);
        String packageName = type.getPackageName();
        String className =
            !packageName.isEmpty()
                ? (packageName + "." + type.getSimpleName())
                : type.getSimpleName();

        // skip if type is excluded class or in excluded package
        if (conf.isExcludedPackage(model.getPackageName())
            || conf.isExcludedClass(model.getFullName())) {
          continue;
        }

        Set<TypeElement> elements = context.typeElements.get(model.getFullName());

        if (elements == null) {
          elements = new HashSet<TypeElement>();
        }
        for (Property property : model.getProperties()) {
          if (property.getType().getCategory() == TypeCategory.CUSTOM) {
            Set<TypeElement> customElements =
                context.typeElements.get(property.getType().getFullName());
            if (customElements != null) {
              elements.addAll(customElements);
            }
          }
        }

        processingEnv
            .getMessager()
            .printMessage(Kind.NOTE, "Generating " + className + " for " + elements);
        JavaFileObject fileObject =
            processingEnv
                .getFiler()
                .createSourceFile(className, elements.toArray(new Element[elements.size()]));
        Writer writer = fileObject.openWriter();
        try {
          SerializerConfig serializerConfig = conf.getSerializerConfig(model);
          serializer.serialize(model, serializerConfig, new JavaWriter(writer));
        } finally {
          if (writer != null) {
            writer.close();
          }
        }

      } catch (IOException e) {
        System.err.println(e.getMessage());
        processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage());
      }
    }
  }
Пример #2
0
  @Override
  public void serialize(
      final EntityType model, SerializerConfig serializerConfig, CodeWriter writer)
      throws IOException {
    // intro
    intro(model, writer);

    String localName = writer.getRawName(model);

    for (Constructor c : model.getConstructors()) {
      // begin
      writer.beginConstructor(
          c.getParameters(),
          new Function<Parameter, Parameter>() {
            @Override
            public Parameter apply(Parameter p) {
              return new Parameter(
                  p.getName(), typeMappings.getExprType(p.getType(), model, false, false, true));
            }
          });

      // body
      writer.beginLine("super(" + localName + ".class");
      writer.append(", new Class[]{");
      boolean first = true;

      for (Parameter p : c.getParameters()) {
        if (!first) {
          writer.append(", ");
        }
        if (Types.PRIMITIVES.containsKey(p.getType())) {
          Type primitive = Types.PRIMITIVES.get(p.getType());
          writer.append(primitive.getFullName() + ".class");
        } else {
          writer.append(writer.getRawName(p.getType()));
          writer.append(".class");
        }
        first = false;
      }
      writer.append("}");

      for (Parameter p : c.getParameters()) {
        writer.append(", " + p.getName());
      }

      // end
      writer.append(");\n");
      writer.end();
    }

    // outro
    outro(model, writer);
  }
 private void serialize(Map<String, EntityType> types, Serializer serializer) throws IOException {
   for (EntityType entityType : types.values()) {
     if (serialized.add(entityType)) {
       Type type = typeMappings.getPathType(entityType, entityType, true);
       String packageName = type.getPackageName();
       String className =
           packageName.length() > 0
               ? (packageName + "." + type.getSimpleName())
               : type.getSimpleName();
       write(serializer, className.replace('.', '/') + ".java", entityType);
     }
   }
 }
 private void handleProperty(EntityType entityType, Class<?> cl, org.hibernate.mapping.Property p)
     throws NoSuchMethodException, ClassNotFoundException {
   Type propertyType = getType(cl, p.getName());
   if (p.isComposite()) {
     Class<?> embeddedClass = Class.forName(propertyType.getFullName());
     EntityType embeddedType = createEmbeddableType(embeddedClass);
     Iterator<?> properties = ((Component) p.getValue()).getPropertyIterator();
     while (properties.hasNext()) {
       handleProperty(
           embeddedType, embeddedClass, (org.hibernate.mapping.Property) properties.next());
     }
     propertyType = embeddedType;
   } else if (propertyType.getCategory() == TypeCategory.ENTITY) {
     propertyType = createEntityType(Class.forName(propertyType.getFullName()));
   }
   AnnotatedElement annotated = getAnnotatedElement(cl, p.getName());
   Property property = createProperty(entityType, p.getName(), propertyType, annotated);
   entityType.addProperty(property);
 }
 private void addExternalParents(EntityType entityType) {
   Deque<Type> superTypes = new ArrayDeque<Type>();
   if (entityType.getSuperType() != null) {
     superTypes.push(entityType.getSuperType().getType());
   }
   while (!superTypes.isEmpty()) {
     Type superType = superTypes.pop();
     if (!context.allTypes.containsKey(superType.getFullName())) {
       TypeElement typeElement =
           processingEnv.getElementUtils().getTypeElement(superType.getFullName());
       if (typeElement == null) {
         throw new IllegalStateException("Found no type for " + superType.getFullName());
       }
       EntityType superEntityType = elementHandler.handleEntityType(typeElement);
       if (entityType.getSuperType() != null) {
         superTypes.push(entityType.getSuperType().getType());
       }
       context.allTypes.put(superType.getFullName(), superEntityType);
     }
   }
 }
Пример #6
0
  protected void intro(EntityType model, CodeWriter writer) throws IOException {
    String simpleName = model.getSimpleName();
    Type queryType = typeMappings.getPathType(model, model, false);

    // package
    if (!queryType.getPackageName().isEmpty()) {
      writer.packageDecl(queryType.getPackageName());
    }

    // imports
    writer.imports(NumberExpression.class.getPackage());
    writer.imports(ConstructorExpression.class, Generated.class);

    // javadoc
    writer.javadoc(queryType + " is a Querydsl Projection type for " + simpleName);

    writer.line("@Generated(\"", getClass().getName(), "\")");

    // class header
    //        writer.suppressWarnings("serial");
    Type superType = new ClassType(TypeCategory.SIMPLE, ConstructorExpression.class, model);
    writer.beginClass(queryType, superType);
    writer.privateStaticFinal(Types.LONG_P, "serialVersionUID", String.valueOf(model.hashCode()));
  }
 @Nullable
 private Property createProperty(
     EntityType entityType, String propertyName, Type propertyType, AnnotatedElement annotated) {
   String[] inits = new String[0];
   if (annotated.isAnnotationPresent(QueryInit.class)) {
     inits = annotated.getAnnotation(QueryInit.class).value();
   }
   if (annotated.isAnnotationPresent(QueryType.class)) {
     QueryType queryType = annotated.getAnnotation(QueryType.class);
     if (queryType.value().equals(PropertyType.NONE)) {
       return null;
     }
     propertyType = propertyType.as(queryType.value().getCategory());
   }
   return new Property(entityType, propertyName, propertyType, inits);
 }
Пример #8
0
 @Test
 public void HidaBezGruppe() {
   TypeFactory typeFactory = new TypeFactory();
   Type type = typeFactory.createEntityType(HidaBezGruppe.class);
   System.out.println(type.getGenericName(true));
 }