Ejemplo n.º 1
0
  private void fillMemberMirrors(final Element element) {

    final List<? extends Element> fieldsOfClass =
        ElementFilter.fieldsIn(element.getEnclosedElements());
    visitFieldsList(fieldsOfClass);

    if (hasParent(element)) {
      fillMemberMirrors(parentType(element).asElement());
    }
  }
Ejemplo n.º 2
0
 private void filterClass(
     Collection<TypeElement> rootElements, Collection<? extends Element> elements) {
   for (Element element : elements) {
     if (element.getKind().equals(ElementKind.CLASS)
         || element.getKind().equals(ElementKind.INTERFACE)) {
       rootElements.add((TypeElement) element);
       filterClass(rootElements, ElementFilter.typesIn(element.getEnclosedElements()));
     }
   }
 }
Ejemplo n.º 3
0
 private boolean doesClassContainNoArgsConstructor(Element el) {
   for (Element subelement : el.getEnclosedElements()) {
     if (subelement.getKind() == ElementKind.CONSTRUCTOR
         && subelement.getModifiers().contains(Modifier.PUBLIC)) {
       TypeMirror mirror = subelement.asType();
       if (mirror.accept(noArgsVisitor, null)) return true;
     }
   }
   return false;
 }
Ejemplo n.º 4
0
  private List<Element> getFixedFieldAnnotateMethod(Element clazz) {
    List<Element> listGetter = new ArrayList<Element>();

    for (Element element : clazz.getEnclosedElements()) {
      if (element.getKind().equals(ElementKind.METHOD)
          && element.getSimpleName().toString().startsWith("get")) {
        listGetter.add(element);
      }
    }
    return listGetter;
  }
 private void processMethodsOfView(EnclosingView enclosingView, Element view) {
   List<? extends Element> enclosedElements = view.getEnclosedElements();
   for (Element e : enclosedElements) {
     System.out.println("Method process: " + e.toString());
     ViewMethod viewMethod = new ViewMethod();
     viewMethod.setMethodName(elementTools.getFieldName(e));
     viewMethod.setReturnType(((ExecutableElement) e).getReturnType());
     viewMethod.setDecorate(e.getAnnotation(NoDecorate.class) == null);
     List<? extends VariableElement> parameters = ((ExecutableElement) e).getParameters();
     for (VariableElement parameterElement : parameters) {
       viewMethod.getParameters().add(parameterElement.asType());
     }
     enclosingView.getMethods().add(viewMethod);
   }
 }
  private boolean haveADefaultContructor(Element element) {
    int constructorsCount = 0;

    for (Element subelement : element.getEnclosedElements()) {
      if (subelement.getKind() == ElementKind.CONSTRUCTOR) {
        //				System.out.println("Constructor found: " + subelement);
        constructorsCount++;
        TypeMirror mirror = subelement.asType();
        if (mirror.accept(noArgsVisitor, null)) return true;
      }
    }
    //		System.out.println("constructors found for " + element.getSimpleName() + " [" +
    // constructorsCount + "]");
    return constructorsCount == 0;
  }
Ejemplo n.º 7
0
    private boolean hasAccessibleConstructor(Element type) {
      List<ExecutableElement> constructors =
          ElementFilter.constructorsIn(type.getEnclosedElements());

      if (constructors.isEmpty()) {
        // It is unclear (not checked) if we will have syntethic no-arg constructor
        // included, so we will assume no constructor to equate having a single constructors.
        return true;
      }

      for (ExecutableElement c : constructors) {
        if (c.getParameters().isEmpty()) {
          return !Visibility.of(c).isPrivate();
        }
      }

      return false;
    }
 // 循环处理每个需要处理的程序对象
 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;
 }
Ejemplo n.º 9
0
 private void fillConstructors(Element element) {
   final List<ExecutableElement> constructorsOfClass =
       ElementFilter.constructorsIn(element.getEnclosedElements());
   visitConstructorList(constructorsOfClass);
 }