private void handleEmbeddedType(Element element, Set<TypeElement> elements) {
    TypeMirror type = element.asType();
    if (element.getKind() == ElementKind.METHOD) {
      type = ((ExecutableElement) element).getReturnType();
    }
    String typeName = type.toString();

    if (typeName.startsWith(Collection.class.getName())
        || typeName.startsWith(List.class.getName())
        || typeName.startsWith(Set.class.getName())) {
      type = ((DeclaredType) type).getTypeArguments().get(0);

    } else if (typeName.startsWith(Map.class.getName())) {
      type = ((DeclaredType) type).getTypeArguments().get(1);
    }

    TypeElement typeElement = typeExtractor.visit(type);

    if (typeElement != null
        && !TypeUtils.hasAnnotationOfType(typeElement, conf.getEntityAnnotations())) {
      if (!typeElement.getQualifiedName().toString().startsWith("java.")) {
        elements.add(typeElement);
      }
    }
  }
 // Assumes targetKinds and E are sensible.
 private static <E extends Element> List<E> listFilter(
     Iterable<? extends Element> elements, Set<ElementKind> targetKinds, Class<E> clazz) {
   List<E> list = new ArrayList<E>();
   for (Element e : elements) {
     if (targetKinds.contains(e.getKind())) list.add(clazz.cast(e));
   }
   return list;
 }
 // Assumes targetKinds and E are sensible.
 private static <E extends Element> Set<E> setFilter(
     Set<? extends Element> elements, Set<ElementKind> targetKinds, Class<E> clazz) {
   // Return set preserving iteration order of input set.
   Set<E> set = new LinkedHashSet<E>();
   for (Element e : elements) {
     if (targetKinds.contains(e.getKind())) set.add(clazz.cast(e));
   }
   return set;
 }
Example #4
0
 public void printElement(TypeElement typeElement) {
   List<? extends AnnotationMirror> annotationMirrors = typeElement.getAnnotationMirrors();
   generator.log("class: " + typeElement + ", annotations: " + annotationMirrors);
   for (Element ele : typeElement.getEnclosedElements()) {
     if (ele.getKind().equals(ElementKind.METHOD)) {
       ExecutableElement method = (ExecutableElement) ele;
       printMethod(method);
     }
   }
 }
 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;
 }
Example #6
0
  public void parse(TypeElement layoutElement) {
    if (!layoutElement.getInterfaces().isEmpty()) {
      parseSuperLayout(
          (TypeElement) ((DeclaredType) layoutElement.getInterfaces().get(0)).asElement());
    }

    parseName(layoutElement);

    objectTypeSuperclass = layoutElement.getAnnotation(Layout.class).objectTypeSuperclass();

    for (Element element : layoutElement.getEnclosedElements()) {
      if (element.getKind() == ElementKind.FIELD) {
        final String simpleName = element.getSimpleName().toString();

        if (simpleName.endsWith("_IDENTIFIER")) {
          parseIdentifier((VariableElement) element);
        } else {
          throw new AssertionError("Unknown field in layout interface");
        }
      } else if (element.getKind() == ElementKind.METHOD) {
        final String simpleName = element.getSimpleName().toString();

        if (simpleName.equals("create" + name + "Shape")) {
          parseShapeConstructor((ExecutableElement) element);
        } else if (simpleName.equals("create" + name)) {
          parseConstructor((ExecutableElement) element);
        } else if (simpleName.equals("is" + name)) {
          parseGuard((ExecutableElement) element);
        } else if (simpleName.startsWith("get")) {
          parseGetter((ExecutableElement) element);
        } else if (simpleName.startsWith("set")) {
          parseSetter((ExecutableElement) element);
        } else {
          throw new AssertionError("Unknown method in layout interface " + interfaceFullName);
        }
      }
    }

    assert constructorProperties.size() == properties.size();
    assert constructorProperties.containsAll(properties.keySet());
  }
Example #7
0
  protected boolean isExplicitTypeDefinition(Element declaration) {
    if (declaration.getKind() != ElementKind.CLASS) {
      debug("%s isn't a potential JAXB type because it's not a class.", declaration);
      return false;
    }

    PackageElement pckg =
        this.context.getProcessingEnvironment().getElementUtils().getPackageOf(declaration);
    if ((pckg != null) && (pckg.getAnnotation(Ignore.class) != null)) {
      debug(
          "%s isn't a potential JAXB type because its package is annotated as to be ignored.",
          declaration);
      return false;
    }

    if (isThrowable(declaration)) {
      debug(
          "%s isn't a potential JAXB type because it's an instance of java.lang.Throwable.",
          declaration);
      return false;
    }

    List<? extends AnnotationMirror> annotationMirrors = declaration.getAnnotationMirrors();
    boolean explicitXMLTypeOrElement = false;
    for (AnnotationMirror mirror : annotationMirrors) {
      Element annotationDeclaration = mirror.getAnnotationType().asElement();
      if (annotationDeclaration != null) {
        String fqn =
            annotationDeclaration instanceof TypeElement
                ? ((TypeElement) annotationDeclaration).getQualifiedName().toString()
                : "";
        // exclude all XmlTransient types and all jaxws types.
        if (XmlTransient.class.getName().equals(fqn)
            || fqn.startsWith("javax.xml.ws")
            || fqn.startsWith("javax.ws.rs")
            || fqn.startsWith("javax.jws")) {
          debug("%s isn't a potential JAXB type because of annotation %s.", declaration, fqn);
          return false;
        } else {
          explicitXMLTypeOrElement =
              (XmlType.class.getName().equals(fqn)) || (XmlRootElement.class.getName().equals(fqn));
        }
      }

      if (explicitXMLTypeOrElement) {
        break;
      }
    }

    return explicitXMLTypeOrElement;
  }
Example #8
0
  @Override
  public Image getColumnImage(Object element, int columnIndex) {
    ProcessStep processStep = (ProcessStep) element;
    Element el = null;

    if (columnIndex > processStep.size()) return null;

    if (columnIndex == 0) el = processStep.get(0);

    if (columnIndex == 2) el = processStep.get(1);

    if (columnIndex == 4) el = processStep.get(2);

    if (el != null) {
      if (el.getKind() == Element.Kind.Event) return EVENT;

      if (el.getKind() == Element.Kind.Function) return FUNCTION;

      if (el.getKind() == Element.Kind.Null) return NONE;
    }

    return null;
  }
    @Override
    public void finished(TaskEvent e) {
      if (e.getKind() != TaskEvent.Kind.ANALYZE) return;

      if (!elements.remove(e.getTypeElement().getQualifiedName())) return;

      if (e.getTypeElement().getSimpleName().contentEquals("MyClass")) {
        Element owner = e.getTypeElement().getEnclosingElement();
        if (owner.getKind() != ElementKind.PACKAGE)
          throw new RuntimeException("class owner should be a package: " + owner);
        if (owner.getAnnotationMirrors().size() != 1)
          throw new RuntimeException("the owner package should have one annotation: " + owner);
      }
    }
Example #10
0
    @Override
    public JsonType visitDeclared(DeclaredType declaredType, Void o) {
      if (isJsonPrimitive(declaredType)) {
        // 'primitive'-ish things
        return new JsonPrimitive(declaredType.toString());

      } else if (isInstanceOf(declaredType, Collection.class)) {

        if (declaredType.getTypeArguments().size() == 0) {
          return new JsonArray(new JsonPrimitive(Object.class.getName()));
        } else {
          TypeMirror elem = declaredType.getTypeArguments().get(0);
          return new JsonArray(elem.accept(this, o));
        }

      } else if (isInstanceOf(declaredType, Map.class)) {

        if (declaredType.getTypeArguments().size() == 0) {
          return new JsonDict(
              new JsonPrimitive(Object.class.getName()), new JsonPrimitive(Object.class.getName()));
        } else {
          TypeMirror key = declaredType.getTypeArguments().get(0);
          TypeMirror val = declaredType.getTypeArguments().get(1);
          return new JsonDict(key.accept(this, o), val.accept(this, o));
        }

      } else {
        TypeElement element = (TypeElement) declaredType.asElement();
        if (element.getKind() == ElementKind.ENUM) {
          List<String> enumConstants = new ArrayList();
          for (Element e : element.getEnclosedElements()) {
            if (e.getKind() == ElementKind.ENUM_CONSTANT) {
              enumConstants.add(e.toString());
            }
          }
          JsonPrimitive primitive =
              new JsonPrimitive(String.class.getName()); // TODO is this always a string?
          primitive.setRestrictions(enumConstants);
          return primitive;
        } else {
          return buildType(declaredType, element);
        }
      }
    }
Example #11
0
  /** @throws IOException */
  public List<Api> parse(TypeElement typeElement) throws IOException {

    RequestMapping requestMapping = typeElement.getAnnotation(RequestMapping.class);
    if (requestMapping == null) {
      generator.log("Controller missing @RequestMapping, " + typeElement);
      return null;
    }

    ApiDoc apiDoc = typeElement.getAnnotation(ApiDoc.class);
    if (apiDoc == null) {
      generator.log("Controller missing @ApiDoc, " + typeElement);
      return null;
    }

    // printElement(typeElement);

    List<Api> apiList = Lists.newArrayList();

    String controllerName = typeElement.getQualifiedName().toString();

    for (Element ele : typeElement.getEnclosedElements()) {
      if (ele.getKind().equals(ElementKind.METHOD)
          && ele.getModifiers().contains(Modifier.PUBLIC)) {

        Api api = new Api();
        api.apiDoc = apiDoc;
        api.packageMapping = requestMapping;
        api.controllerName = controllerName;

        ExecutableElement method = (ExecutableElement) ele;
        this.parseMethod(api, method);
        if (api.apiMethodDoc != null) {
          apiList.add(api);
        }
      }
    }

    return apiList;
  }
Example #12
0
 boolean isUnnamedPackage(Element e) {
   return (e != null && e.getKind() == ElementKind.PACKAGE && ((PackageElement) e).isUnnamed());
 }
 // 循环处理每个需要处理的程序对象
 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;
 }
Example #14
0
  @Override
  public boolean process(Set<? extends TypeElement> elements, RoundEnvironment env) {
    System.out.println("processing: " + env.toString());
    List<MethodToLog> methodToLogs = new ArrayList<>();
    List<String> classNames = new ArrayList<>();

    for (Element element : env.getElementsAnnotatedWith(XLog.class)) {
      if (element.getKind() != ElementKind.METHOD
          && element.getKind() != ElementKind.CONSTRUCTOR
          && element.getKind() != ElementKind.CLASS) {
        throw new IllegalStateException(
            String.format(
                "@%s annotation must be on as method, constructor or class.",
                element.getSimpleName()));
      }
      if (element instanceof TypeElement) {
        // class
        String pkgName = ((TypeElement) element).getQualifiedName().toString();
        if (!classNames.contains(pkgName)) {
          classNames.add(pkgName);
        }
      } else if (element instanceof ExecutableElement) {
        // method or constructor
        ExecutableElement e = (ExecutableElement) element;

        int type = XLogUtils.TYPE_METHOD;
        if (e.getKind() == ElementKind.METHOD) {
          type = XLogUtils.TYPE_METHOD;
        } else if (e.getKind() == ElementKind.CONSTRUCTOR) {
          type = XLogUtils.TYPE_CONSTRUCTOR;
        }

        TypeElement te = findEnclosingTypeElement(e);
        System.out.println(te.getQualifiedName().toString() + "." + e.getSimpleName());
        System.out.println(e.getReturnType());
        List<String> parameters = new ArrayList<>();
        List<String> parameterNames = new ArrayList<>();
        for (VariableElement ve : e.getParameters()) {
          System.out.println(ve.asType());
          System.out.println(ve.getSimpleName());
          parameters.add(ve.asType().toString());
          parameterNames.add(ve.getSimpleName().toString());
        }
        MethodToLog methodToLog =
            new MethodToLog(
                type,
                te.getQualifiedName().toString(),
                e.getSimpleName().toString(),
                parameters,
                parameterNames);
        methodToLogs.add(methodToLog);

        if (!classNames.contains(methodToLog.getClassName())) {
          classNames.add(methodToLog.getClassName());
        }
      }
    }

    if (methodToLogs.size() > 0) {
      generateXLogProcessor("_" + MD5(env.toString()), methodToLogs, classNames);
    }

    return true;
  }
Example #15
0
 /**
  * Whether the specified declaration is throwable.
  *
  * @param declaration The declaration to determine whether it is throwable.
  * @return Whether the specified declaration is throwable.
  */
 protected boolean isThrowable(Element declaration) {
   return declaration.getKind() == ElementKind.CLASS
       && ((DecoratedTypeMirror) declaration.asType()).isInstanceOf(Throwable.class);
 }