예제 #1
0
  @Override
  public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Processing @Implemented...");
    for (TypeElement annotation : annotations) {
      for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
        TypeElement serviceElement = (TypeElement) element;
        PackageElement packageElement = getPackage(serviceElement);
        final String packageName =
            packageElement == null ? "" : packageElement.getQualifiedName().toString();
        Implemented implemented = serviceElement.getAnnotation(Implemented.class);
        final String implName =
            "".equals(implemented.value())
                ? serviceElement.getSimpleName().toString() + "Impl"
                : implemented.value();
        final List<FieldDescriptor> fields = createFieldList(serviceElement);
        final String localName = determineLocalName(serviceElement);
        final boolean isPublic = serviceElement.getModifiers().contains(Modifier.PUBLIC);
        ImplementedDescriptor annotationDescriptor =
            new ImplementedDescriptor(isPublic, packageName, implName, localName, fields);
        createImplSourceFile(annotationDescriptor, serviceElement, packageName + "." + implName);
      }
    }

    return true;
  }
  private void createBeanInfo(TypeElement classElement) throws Exception {
    if (verbose) {
      processingEnv
          .getMessager()
          .printMessage(
              Diagnostic.Kind.NOTE, "Generating BeanInfo for " + classElement.getSimpleName());
    }

    String className = classElement.getSimpleName().toString();
    String qualifiedName = classElement.getQualifiedName().toString();
    PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
    String packageName = packageElement.getQualifiedName().toString();

    Map<String, VariableElement> fields = new HashMap<String, VariableElement>();

    Writer writer = null;

    try {
      // Build Bean Info
      TypeElement curClassElement = classElement;
      do {

        for (Element e : curClassElement.getEnclosedElements()) {
          if (e.getKind().equals(ElementKind.FIELD)
              && !e.getModifiers().contains(Modifier.TRANSIENT)
              && !e.getModifiers().contains(Modifier.STATIC)) {

            fields.put(e.getSimpleName().toString(), (VariableElement) e);
          }
        }

        TypeMirror t = curClassElement.getSuperclass();
        if (t instanceof DeclaredType) {
          curClassElement = (TypeElement) ((DeclaredType) t).asElement();
        } else {
          curClassElement = null;
        }

      } while (curClassElement != null);
      VelocityContext vc = new VelocityContext();
      vc.put("className", className);
      vc.put("packageName", packageName);
      vc.put("fields", fields);
      // Create source file
      JavaFileObject jfo = processingEnv.getFiler().createSourceFile(qualifiedName + "BeanInfo");
      if (verbose) {
        processingEnv
            .getMessager()
            .printMessage(Diagnostic.Kind.NOTE, "creating source file: " + jfo.toUri());
      }
      writer = jfo.openWriter();
      template.merge(vc, writer);
    } finally {
      closeQuietly(writer);
    }
  }
예제 #3
0
 public static String getPackageName(Elements elementUtils, List<NavigableAnnotatedClass> type)
     throws NoPackageNameException {
   List<String> packageNames = new ArrayList<>();
   PackageElement packageElement;
   for (NavigableAnnotatedClass navigableAnnotatedClass : type) {
     packageElement = elementUtils.getPackageOf(navigableAnnotatedClass.getTypeElement());
     if (!packageElement.isUnnamed()) {
       packageNames.add(packageElement.getQualifiedName().toString());
     }
   }
   return StringUtils.longestCommonSubstring(packageNames);
 }
  public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
      throw new TemplateModelException(
          "The nameForEnumConstant method must have an enum type definition and an enum constant declaration as parameters.");
    }

    BeansWrapper wrapper =
        new BeansWrapperBuilder(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS).build();
    Object unwrapped = wrapper.unwrap((TemplateModel) list.get(0));
    if (!(unwrapped instanceof EnumValue)) {
      throw new TemplateModelException(
          "The nameForEnumConstant method must have an enum value as a parameter.");
    }
    EnumValue enumValue = (EnumValue) unwrapped;
    EnumTypeDefinition typeDefinition = enumValue.getTypeDefinition();

    String name = CXMLClientModule.scrubIdentifier(typeDefinition.getName());
    String simpleName = CXMLClientModule.scrubIdentifier(typeDefinition.getSimpleName().toString());
    String clientName = CXMLClientModule.scrubIdentifier(typeDefinition.getClientSimpleName());
    String simpleNameDecap =
        CXMLClientModule.scrubIdentifier(Introspector.decapitalize(simpleName));
    String clientNameDecap =
        CXMLClientModule.scrubIdentifier(Introspector.decapitalize(clientName));
    if (name == null) {
      name = "anonymous_" + clientNameDecap;
    }
    PackageElement pckg = typeDefinition.getPackage().getDelegate();
    String packageUnderscored =
        CXMLClientModule.scrubIdentifier(
            pckg != null ? pckg.getQualifiedName().toString().replace('.', '_') : "");
    String nsid =
        CXMLClientModule.scrubIdentifier(namespaces2ids.get(typeDefinition.getNamespace()));

    String constantName = CXMLClientModule.scrubIdentifier(enumValue.getSimpleName().toString());
    String constantClientName =
        CXMLClientModule.scrubIdentifier(
            enumValue.getAnnotation(ClientName.class) != null
                ? enumValue.getAnnotation(ClientName.class).value()
                : constantName);
    return String.format(
        this.pattern,
        this.projectLabel,
        nsid,
        name,
        clientName,
        clientNameDecap,
        simpleName,
        simpleNameDecap,
        packageUnderscored,
        constantClientName,
        constantName);
  }
예제 #5
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;
  }
예제 #6
0
 PackageInfo getPackage(PackageElement pkg) {
   PackageInfo pkgInfo = packages.get(pkg.getQualifiedName().toString());
   if (pkgInfo == null) {
     return addPackage(pkg);
   } else {
     return pkgInfo;
   }
 }
예제 #7
0
 @Override
 public Stream<TypeInfo> declaredTypeInfoStream() {
   return element
       .getEnclosedElements()
       .stream()
       .filter(el -> Util.isTypeInfoKind(el))
       .map(TypeElement.class::cast)
       .map(el -> TypeInfoTypeElement.wrap(processingEnv, el));
 }
예제 #8
0
 public CodeTypeElement(
     Set<Modifier> modifiers, ElementKind kind, PackageElement packageElement, String simpleName) {
   super(modifiers);
   this.kind = kind;
   this.packageElement = packageElement;
   this.simpleName = CodeNames.of(simpleName);
   if (this.packageElement != null) {
     this.packageName = packageElement.getQualifiedName();
   } else {
     this.packageName = CodeNames.of("default");
   }
   this.qualifiedName = createQualifiedName();
 }
예제 #9
0
 @Override
 public String visitDeclared(DeclaredType mirror, Void p) {
   Name name = mirror.asElement().getSimpleName();
   final String prefix;
   Element enclosingElement = mirror.asElement().getEnclosingElement();
   if (mirror.getEnclosingType().getKind() != TypeKind.NONE) {
     prefix = visit(mirror.getEnclosingType()) + ".";
   } else if (enclosingElement.getKind() == ElementKind.PACKAGE) {
     PackageElement pkg = (PackageElement) enclosingElement;
     prefix = getPrefixForTopLevelClass(pkg.getQualifiedName().toString(), name);
   } else if (enclosingElement.getKind().isClass() || enclosingElement.getKind().isInterface()) {
     prefix = shorten((TypeElement) enclosingElement) + ".";
   } else {
     prefix = enclosingElement.toString() + ".";
   }
   final String suffix;
   if (!mirror.getTypeArguments().isEmpty()) {
     List<String> shortTypeArguments = Lists.transform(mirror.getTypeArguments(), this);
     suffix = "<" + Joiner.on(", ").join(shortTypeArguments) + ">";
   } else {
     suffix = "";
   }
   return prefix + name + suffix;
 }
예제 #10
0
 @Override
 String name() {
   return element.getQualifiedName().toString();
 }
예제 #11
0
 PackageInfo addPackage(PackageElement pkg) {
   PackageInfo pkgInfo = new PackageInfo(this, pkg);
   packages.put(pkg.getQualifiedName().toString(), pkgInfo);
   return pkgInfo;
 }
예제 #12
0
 private static String getPackageName(TypeElement element) {
   PackageElement packageElement = (PackageElement) element.getEnclosingElement();
   String packageName = packageElement.getQualifiedName().toString();
   return packageName.replaceAll("template.", "");
 }
예제 #13
0
 /**
  * {@inheritDoc} This implementation scans the enclosed elements.
  *
  * @param e {@inheritDoc}
  * @param p {@inheritDoc}
  * @return the result of scanning
  */
 public R visitPackage(PackageElement e, P p) {
   return scan(e.getEnclosedElements(), p);
 }
 public final String getPackageName() {
   PackageElement packageOf = context.getElementUtils().getPackageOf(element);
   return context.getElementUtils().getName(packageOf.getQualifiedName()).toString();
 }