@Override
  public void process() {
    EclipseMessager messager = (EclipseMessager) _env.getMessager();

    // obtain the declaration of the annotation we want to process
    AnnotationTypeDeclaration annoDecl =
        (AnnotationTypeDeclaration) _env.getTypeDeclaration(JSFillChildrenMethod.class.getName());

    // get the annotated types
    Collection<Declaration> annotatedTypes = _env.getDeclarationsAnnotatedWith(annoDecl);

    for (Declaration decl : annotatedTypes) {
      Collection<AnnotationMirror> mirrors = decl.getAnnotationMirrors();

      // for each annotation found, get a map of element name/value pairs
      for (AnnotationMirror mirror : mirrors) {
        if (!"JSFillChildrenMethod"
            .equals(mirror.getAnnotationType().getDeclaration().getSimpleName())) {
          continue;
        }
        MethodDeclaration methodDeclaration = (MethodDeclaration) decl;
        JSJPQLMethodProcessor.checkReturnType(mirror, methodDeclaration, messager);
        JSJPQLMethodProcessor.checkTransferInfo(mirror, methodDeclaration, true, messager);
        JSJPQLMethodProcessor.checkIfAnnotationValueAttributeIsEntity(mirror, "parent", messager);
      }
    }
  }
 private Map<String, AnnotationValue> getAnnotationValues(
     Declaration declaration, String annotationName) {
   Collection<AnnotationMirror> annotationMirrors = declaration.getAnnotationMirrors();
   for (AnnotationMirror annotationMirror : annotationMirrors) {
     if (annotationName.equals(
         annotationMirror.getAnnotationType().getDeclaration().getQualifiedName())) {
       return getAnnotationValues(annotationMirror);
     }
   }
   return null;
 }
 private Map<String, AnnotationValue> getAnnotationValues(AnnotationMirror a) {
   Map<String, AnnotationValue> values = new HashMap<String, AnnotationValue>();
   Map<AnnotationTypeElementDeclaration, AnnotationValue> elementValues = a.getElementValues();
   for (Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue> entry :
       elementValues.entrySet()) {
     values.put(entry.getKey().getSimpleName(), entry.getValue());
   }
   return values;
 }
  private static boolean checkTransferInfoType(
      AnnotationValue annotationValue, EclipseMessager messager) {
    AnnotationMirror mirror = (AnnotationMirror) annotationValue.getValue();
    Map<AnnotationTypeElementDeclaration, AnnotationValue> valueMap = mirror.getElementValues();
    Set<Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue>> valueSet =
        valueMap.entrySet();

    for (Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue> annoKeyValue : valueSet) {
      if (annoKeyValue.getKey().getSimpleName().equals("type")) {
        String annoValue = (String) annoKeyValue.getValue().getValue();
        if (annoValue != null && annoValue.trim().length() == 0) {
          SourcePosition pos = annoKeyValue.getValue().getPosition();
          messager.printError(pos, TYPE_ATTRIBUTE_SHOULD_BE_NOT_EMPTY);
        }
        return false;
      }
    }
    return true;
  }
Example #5
0
 public String getAutoTypeFromAnnotation(AnnotationMirror annotation) {
   Class annotation_class = NativeTypeTranslator.getClassFromType(annotation.getAnnotationType());
   if (annotation_class.equals(GLint.class)) return "GL11.GL_INT";
   else if (annotation_class.equals(GLbyte.class)) return "GL11.GL_BYTE";
   else if (annotation_class.equals(GLshort.class)) return "GL11.GL_SHORT";
   if (annotation_class.equals(GLuint.class)) return "GL11.GL_UNSIGNED_INT";
   else if (annotation_class.equals(GLubyte.class)) return "GL11.GL_UNSIGNED_BYTE";
   else if (annotation_class.equals(GLushort.class)) return "GL11.GL_UNSIGNED_SHORT";
   else if (annotation_class.equals(GLfloat.class)) return "GL11.GL_FLOAT";
   else if (annotation_class.equals(GLdouble.class)) return "GL11.GL_DOUBLE";
   else return null;
 }
  public static boolean checkTransferInfo(
      AnnotationMirror mirror,
      MethodDeclaration methodDeclaration,
      boolean checkReturnType,
      EclipseMessager messager) {
    Map<AnnotationTypeElementDeclaration, AnnotationValue> valueMap = mirror.getElementValues();
    Set<Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue>> valueSet =
        valueMap.entrySet();

    boolean found = false;
    for (Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue> annoKeyValue : valueSet) {
      if (annoKeyValue.getKey().getSimpleName().equals("transferInfo")) {
        found = true;
        checkTransferInfoType(annoKeyValue.getValue(), messager);
        checkIfAnnotationValueAttributeIsEntity(
            (AnnotationMirror) annoKeyValue.getValue().getValue(), "mappedBy", messager);
        break;
      }
    }
    if (!found && checkReturnType) {
      TypeMirror returnType = methodDeclaration.getReturnType();
      String returnTypeName = returnType.toString();
      if (returnTypeName.equals("java.util.List<?>")
          || returnTypeName.equals("java.util.List")
          || returnTypeName.equals("java.util.Collection<?>")
          || returnTypeName.equals("java.util.Collection")) {
        // System.out.println(returnType);
        SourcePosition pos = mirror.getPosition();
        messager.printFixableError(
            pos,
            CLASS_OF_THE_COLLECTION_ELEMENTS_IS_NOT_SPECIFIED_AND_TRANSFER_INFO_PARAMETER_IS_MISSING,
            PLUGIN_ID,
            ERROR_transferInfo_is_missing);
        return false;
      }
    }
    return true;
  }
  private static boolean checkUpdateInfo(
      AnnotationMirror mirror, MethodDeclaration methodDeclaration, EclipseMessager messager) {
    Map<AnnotationTypeElementDeclaration, AnnotationValue> valueMap = mirror.getElementValues();
    Set<Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue>> valueSet =
        valueMap.entrySet();

    for (Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue> annoKeyValue : valueSet) {
      if (annoKeyValue.getKey().getSimpleName().equals("updateInfo")) {
        checkIfAnnotationValueAttributeIsEntity(
            (AnnotationMirror) annoKeyValue.getValue().getValue(), "updateEntity", messager);
      }
    }
    return true;
  }
  public static boolean checkIfAnnotationValueAttributeIsEntity(
      AnnotationMirror mirror, String attributeName, EclipseMessager messager) {
    Map<AnnotationTypeElementDeclaration, AnnotationValue> valueMap = mirror.getElementValues();
    Set<Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue>> valueSet =
        valueMap.entrySet();

    for (Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue> annoKeyValue : valueSet) {
      if (annoKeyValue.getKey().getSimpleName().equals(attributeName)) {
        ClassDeclaration annoValue = (ClassDeclaration) annoKeyValue.getValue().getValue();
        Entity entityAnnotation = annoValue.getAnnotation(Entity.class);
        if (entityAnnotation == null) {
          SourcePosition pos = annoKeyValue.getValue().getPosition();
          messager.printError(
              pos,
              "'"
                  + attributeName
                  + "' attribute value class should be annotated with 'javax.persistence.Entity'");
          return false;
        }
      }
    }
    return true;
  }
 public void error(AnnotationMirror annotationMirror, String message) {
   env_.getMessager().printError(annotationMirror.getPosition(), message);
 }
Example #10
0
  public ResourceMethod(MethodDeclaration delegate, Resource parent) {
    super(delegate);

    Set<String> httpMethods = new TreeSet<String>();
    Collection<AnnotationMirror> mirrors = delegate.getAnnotationMirrors();
    for (AnnotationMirror mirror : mirrors) {
      AnnotationTypeDeclaration annotationDeclaration = mirror.getAnnotationType().getDeclaration();
      HttpMethod httpMethodInfo = annotationDeclaration.getAnnotation(HttpMethod.class);
      if (httpMethodInfo != null) {
        // request method designator found.
        httpMethods.add(httpMethodInfo.value());
      }
    }

    if (httpMethods.isEmpty()) {
      throw new IllegalStateException(
          "A resource method must specify an HTTP method by using a request method designator annotation.");
    }

    this.httpMethods = httpMethods;

    Set<String> consumes;
    Consumes consumesInfo = delegate.getAnnotation(Consumes.class);
    if (consumesInfo != null) {
      consumes = new TreeSet<String>(Arrays.asList(JAXRSUtils.value(consumesInfo)));
    } else {
      consumes = new TreeSet<String>(parent.getConsumesMime());
    }
    this.consumesMime = consumes;

    Set<String> produces;
    Produces producesInfo = delegate.getAnnotation(Produces.class);
    if (producesInfo != null) {
      produces = new TreeSet<String>(Arrays.asList(JAXRSUtils.value(producesInfo)));
    } else {
      produces = new TreeSet<String>(parent.getProducesMime());
    }
    this.producesMime = produces;

    String subpath = null;
    Path pathInfo = delegate.getAnnotation(Path.class);
    if (pathInfo != null) {
      subpath = pathInfo.value();
    }

    ResourceEntityParameter entityParameter;
    List<ResourceEntityParameter> declaredEntityParameters =
        new ArrayList<ResourceEntityParameter>();
    List<ResourceParameter> resourceParameters;
    ResourceRepresentationMetadata outputPayload;
    ResourceMethodSignature signatureOverride =
        delegate.getAnnotation(ResourceMethodSignature.class);
    if (signatureOverride == null) {
      entityParameter = null;
      resourceParameters = new ArrayList<ResourceParameter>();
      // if we're not overriding the signature, assume we use the real method signature.
      for (ParameterDeclaration parameterDeclaration : getParameters()) {
        if (ResourceParameter.isResourceParameter(parameterDeclaration)) {
          resourceParameters.add(new ResourceParameter(parameterDeclaration));
        } else if (ResourceParameter.isFormBeanParameter(parameterDeclaration)) {
          resourceParameters.addAll(ResourceParameter.getFormBeanParameters(parameterDeclaration));
        } else if (parameterDeclaration.getAnnotation(Context.class) == null) {
          entityParameter = new ResourceEntityParameter(this, parameterDeclaration);
          declaredEntityParameters.add(entityParameter);
        }
      }

      DecoratedTypeMirror returnTypeMirror;
      TypeHint hintInfo = getAnnotation(TypeHint.class);
      if (hintInfo != null) {
        try {
          Class hint = hintInfo.value();
          AnnotationProcessorEnvironment env = net.sf.jelly.apt.Context.getCurrentEnvironment();
          if (TypeHint.NO_CONTENT.class.equals(hint)) {
            returnTypeMirror =
                (DecoratedTypeMirror)
                    TypeMirrorDecorator.decorate(env.getTypeUtils().getVoidType());
          } else {
            String hintName = hint.getName();

            if (TypeHint.NONE.class.equals(hint)) {
              hintName = hintInfo.qualifiedName();
            }

            if (!"##NONE".equals(hintName)) {
              TypeDeclaration type = env.getTypeDeclaration(hintName);
              returnTypeMirror =
                  (DecoratedTypeMirror)
                      TypeMirrorDecorator.decorate(env.getTypeUtils().getDeclaredType(type));
            } else {
              returnTypeMirror = (DecoratedTypeMirror) getReturnType();
            }
          }
        } catch (MirroredTypeException e) {
          returnTypeMirror = (DecoratedTypeMirror) TypeMirrorDecorator.decorate(e.getTypeMirror());
        }
        returnTypeMirror.setDocComment(((DecoratedTypeMirror) getReturnType()).getDocComment());
      } else {
        returnTypeMirror = (DecoratedTypeMirror) getReturnType();

        if (getJavaDoc().get("returnWrapped")
            != null) { // support jax-doclets. see http://jira.codehaus.org/browse/ENUNCIATE-690
          String fqn = getJavaDoc().get("returnWrapped").get(0);
          AnnotationProcessorEnvironment env = net.sf.jelly.apt.Context.getCurrentEnvironment();
          TypeDeclaration type = env.getTypeDeclaration(fqn);
          if (type != null) {
            returnTypeMirror =
                (DecoratedTypeMirror)
                    TypeMirrorDecorator.decorate(env.getTypeUtils().getDeclaredType(type));
          }
        }

        // in the case where the return type is com.sun.jersey.api.JResponse,
        // we can use the type argument to get the entity type
        if (returnTypeMirror.isClass()
            && returnTypeMirror.isInstanceOf("com.sun.jersey.api.JResponse")) {
          DecoratedClassType jresponse = (DecoratedClassType) returnTypeMirror;
          if (!jresponse.getActualTypeArguments().isEmpty()) {
            DecoratedTypeMirror responseType =
                (DecoratedTypeMirror)
                    TypeMirrorDecorator.decorate(
                        jresponse.getActualTypeArguments().iterator().next());
            if (responseType.isDeclared()) {
              responseType.setDocComment(returnTypeMirror.getDocComment());
              returnTypeMirror = responseType;
            }
          }
        }
      }

      outputPayload =
          returnTypeMirror.isVoid() ? null : new ResourceRepresentationMetadata(returnTypeMirror);
    } else {
      entityParameter = loadEntityParameter(signatureOverride);
      declaredEntityParameters.add(entityParameter);
      resourceParameters = loadResourceParameters(signatureOverride);
      outputPayload = loadOutputPayload(signatureOverride);
    }

    JavaDoc.JavaDocTagList doclets =
        getJavaDoc()
            .get(
                "RequestHeader"); // support jax-doclets. see
                                  // http://jira.codehaus.org/browse/ENUNCIATE-690
    if (doclets != null) {
      for (String doclet : doclets) {
        int firstspace = doclet.indexOf(' ');
        String header = firstspace > 0 ? doclet.substring(0, firstspace) : doclet;
        String doc =
            ((firstspace > 0) && (firstspace + 1 < doclet.length()))
                ? doclet.substring(firstspace + 1)
                : "";
        resourceParameters.add(
            new ExplicitResourceParameter(this, doc, header, ResourceParameterType.HEADER));
      }
    }

    ArrayList<ResponseCode> statusCodes = new ArrayList<ResponseCode>();
    ArrayList<ResponseCode> warnings = new ArrayList<ResponseCode>();
    StatusCodes codes = getAnnotation(StatusCodes.class);
    if (codes != null) {
      for (org.codehaus.enunciate.jaxrs.ResponseCode code : codes.value()) {
        ResponseCode rc = new ResponseCode();
        rc.setCode(code.code());
        rc.setCondition(code.condition());
        statusCodes.add(rc);
      }
    }

    doclets =
        getJavaDoc()
            .get("HTTP"); // support jax-doclets. see http://jira.codehaus.org/browse/ENUNCIATE-690
    if (doclets != null) {
      for (String doclet : doclets) {
        int firstspace = doclet.indexOf(' ');
        String code = firstspace > 0 ? doclet.substring(0, firstspace) : doclet;
        String doc =
            ((firstspace > 0) && (firstspace + 1 < doclet.length()))
                ? doclet.substring(firstspace + 1)
                : "";
        try {
          ResponseCode rc = new ResponseCode();
          rc.setCode(Integer.parseInt(code));
          rc.setCondition(doc);
          statusCodes.add(rc);
        } catch (NumberFormatException e) {
          // fall through...
        }
      }
    }

    Warnings warningInfo = getAnnotation(Warnings.class);
    if (warningInfo != null) {
      for (org.codehaus.enunciate.jaxrs.ResponseCode code : warningInfo.value()) {
        ResponseCode rc = new ResponseCode();
        rc.setCode(code.code());
        rc.setCondition(code.condition());
        warnings.add(rc);
      }
    }

    codes = parent.getAnnotation(StatusCodes.class);
    if (codes != null) {
      for (org.codehaus.enunciate.jaxrs.ResponseCode code : codes.value()) {
        ResponseCode rc = new ResponseCode();
        rc.setCode(code.code());
        rc.setCondition(code.condition());
        statusCodes.add(rc);
      }
    }

    warningInfo = parent.getAnnotation(Warnings.class);
    if (warningInfo != null) {
      for (org.codehaus.enunciate.jaxrs.ResponseCode code : warningInfo.value()) {
        ResponseCode rc = new ResponseCode();
        rc.setCode(code.code());
        rc.setCondition(code.condition());
        warnings.add(rc);
      }
    }

    ResponseHeaders responseHeaders = parent.getAnnotation(ResponseHeaders.class);
    if (responseHeaders != null) {
      for (ResponseHeader header : responseHeaders.value()) {
        this.responseHeaders.put(header.name(), header.description());
      }
    }

    responseHeaders = getAnnotation(ResponseHeaders.class);
    if (responseHeaders != null) {
      for (ResponseHeader header : responseHeaders.value()) {
        this.responseHeaders.put(header.name(), header.description());
      }
    }

    doclets =
        getJavaDoc()
            .get(
                "ResponseHeader"); // support jax-doclets. see
                                   // http://jira.codehaus.org/browse/ENUNCIATE-690
    if (doclets != null) {
      for (String doclet : doclets) {
        int firstspace = doclet.indexOf(' ');
        String header = firstspace > 0 ? doclet.substring(0, firstspace) : doclet;
        String doc =
            ((firstspace > 0) && (firstspace + 1 < doclet.length()))
                ? doclet.substring(firstspace + 1)
                : "";
        this.responseHeaders.put(header, doc);
      }
    }

    this.entityParameter = entityParameter;
    this.resourceParameters = resourceParameters;
    this.subpath = subpath;
    this.parent = parent;
    this.statusCodes = statusCodes;
    this.warnings = warnings;
    this.representationMetadata = outputPayload;
    this.declaredEntityParameters = declaredEntityParameters;
  }