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());
      }
    }
  }
 private void validateMetaTypes() {
   for (Collection<EntityType> entityTypes :
       Arrays.asList(
           context.supertypes.values(),
           context.entityTypes.values(),
           context.extensionTypes.values(),
           context.embeddableTypes.values(),
           context.projectionTypes.values())) {
     for (EntityType entityType : entityTypes) {
       for (Property property : entityType.getProperties()) {
         if (property.getInits() != null && property.getInits().size() > 0) {
           validateInits(entityType, property);
         }
       }
     }
   }
 }
 protected void validateInits(EntityType entityType, Property property) {
   for (String init : property.getInits()) {
     if (!init.startsWith("*") && property.getType() instanceof EntityType) {
       String initProperty = init.contains(".") ? init.substring(0, init.indexOf('.')) : init;
       if (!((EntityType) property.getType()).getPropertyNames().contains(initProperty)) {
         processingEnv
             .getMessager()
             .printMessage(
                 Kind.ERROR,
                 "Illegal inits of "
                     + entityType.getFullName()
                     + "."
                     + property.getName()
                     + ": "
                     + initProperty
                     + " not found");
       }
     }
   }
 }
 // 循环处理每个需要处理的程序对象
 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
   // 定义一个文件输出流,用于生成额外的文件
   PrintStream ps = null;
   try {
     // 遍历每个被@Persistent修饰的class文件
     for (Element t : roundEnv.getElementsAnnotatedWith(Persistent.class)) {
       // 获取正在处理的类名
       Name clazzName = t.getSimpleName();
       // 获取类定义前的@Persistent Annotation
       Persistent per = t.getAnnotation(Persistent.class);
       // 创建文件输出流
       ps = new PrintStream(new FileOutputStream(clazzName + ".hbm.xml"));
       // 执行输出
       ps.println("<?xml version=\"1.0\"?>");
       ps.println("<!DOCTYPE hibernate-mapping PUBLIC");
       ps.println("	\"-//Hibernate/Hibernate " + "Mapping DTD 3.0//EN\"");
       ps.println("	\"http://www.hibernate.org/dtd/" + "hibernate-mapping-3.0.dtd\">");
       ps.println("<hibernate-mapping>");
       ps.print("	<class name=\"" + t);
       // 输出per的table()的值
       ps.println("\" table=\"" + per.table() + "\">");
       for (Element f : t.getEnclosedElements()) {
         // 只处理成员变量上的Annotation
         if (f.getKind() == ElementKind.FIELD) // ①
         {
           // 获取成员变量定义前的@Id Annotation
           Id id = f.getAnnotation(Id.class); // ②
           // 当@Id Annotation存在时输出<id.../>元素
           if (id != null) {
             ps.println(
                 "		<id name=\""
                     + f.getSimpleName()
                     + "\" column=\""
                     + id.column()
                     + "\" type=\""
                     + id.type()
                     + "\">");
             ps.println("		<generator class=\"" + id.generator() + "\"/>");
             ps.println("		</id>");
           }
           // 获取成员变量定义前的@Property Annotation
           Property p = f.getAnnotation(Property.class); // ③
           // 当@Property Annotation存在时输出<property.../>元素
           if (p != null) {
             ps.println(
                 "		<property name=\""
                     + f.getSimpleName()
                     + "\" column=\""
                     + p.column()
                     + "\" type=\""
                     + p.type()
                     + "\"/>");
           }
         }
       }
       ps.println("	</class>");
       ps.println("</hibernate-mapping>");
     }
   } catch (Exception ex) {
     ex.printStackTrace();
   } finally {
     if (ps != null) {
       try {
         ps.close();
       } catch (Exception ex) {
         ex.printStackTrace();
       }
     }
   }
   return true;
 }