public static MarkerAnnotation createValAnnotation(
      AST ast, Annotation original, int start, int end) {
    MarkerAnnotation out = null;
    try {
      out = Reflection.markerAnnotationConstructor.newInstance(ast);
    } catch (InstantiationException e) {
      throw Lombok.sneakyThrow(e);
    } catch (IllegalAccessException e) {
      throw Lombok.sneakyThrow(e);
    } catch (InvocationTargetException e) {
      throw Lombok.sneakyThrow(e);
    }

    if (out != null) {
      SimpleName valName = ast.newSimpleName("val");
      valName.setSourceRange(start, end - start + 1);
      if (original.type instanceof SingleTypeReference) {
        out.setTypeName(valName);
        setIndex(valName, 1);
      } else {
        SimpleName lombokName = ast.newSimpleName("lombok");
        lombokName.setSourceRange(start, end - start + 1);
        setIndex(lombokName, 1);
        setIndex(valName, 2);
        QualifiedName fullName = ast.newQualifiedName(lombokName, valName);
        setIndex(fullName, 1);
        fullName.setSourceRange(start, end - start + 1);
        out.setTypeName(fullName);
      }
      out.setSourceRange(start, end - start + 1);
    }

    return out;
  }
  @Override
  public boolean visit(MarkerAnnotation node) {
    ASTNode parent = node.getParent();
    if (isTestAnnotation(node.getTypeName().toString())) {
      if (parent instanceof MethodDeclaration) {
        addTestMethod((MethodDeclaration) parent, JDK15_ANNOTATION);
      } else if (parent instanceof TypeDeclaration) { // TESTNG-24
        m_typeIsTest = true;
        m_annotationType = JDK15_ANNOTATION;
      }
    } else if (isFactoryAnnotation(node.getTypeName().toString())) {
      if (parent instanceof MethodDeclaration) {
        m_annotationType = JDK15_ANNOTATION;
        addFactoryMethod((MethodDeclaration) parent, JDK15_ANNOTATION);
      }
    }

    return false;
  }
 /*
  * @see ASTVisitor#visit(MarkerAnnotation)
  * @since 3.0
  */
 public boolean visit(MarkerAnnotation node) {
   this.fBuffer.append("@"); // $NON-NLS-1$
   node.getTypeName().accept(this);
   return false;
 }
Exemple #4
0
 public boolean visit(MarkerAnnotation node) {
   if (found(node, node) && this.resolveBinding)
     this.foundBinding = node.resolveAnnotationBinding();
   return true;
 }
 /**
  * Builds a new {@link MarkerAnnotation} instance.
  *
  * @param typeName the annotation type name
  * @return a new marker annotation
  */
 public MarkerAnnotation markerAnnotation(Name typeName) {
   final MarkerAnnotation ma = ast.newMarkerAnnotation();
   ma.setTypeName(typeName);
   return ma;
 }
 /* (non-Javadoc)
  * @see org.eclipse.jdt.internal.corext.dom.GenericVisitor#visit(org.eclipse.jdt.core.dom.MarkerAnnotation)
  */
 @Override
 public boolean visit(MarkerAnnotation node) {
   typeRefFound(node.getTypeName());
   return false;
 }
  public static void addFinalAndValAnnotationToModifierList(
      Object converter, List<IExtendedModifier> modifiers, AST ast, LocalDeclaration in) {
    // First check that 'in' has the final flag on, and a @val / @lombok.val annotation.
    if ((in.modifiers & ClassFileConstants.AccFinal) == 0) return;
    if (in.annotations == null) return;
    boolean found = false;
    Annotation valAnnotation = null;

    for (Annotation ann : in.annotations) {
      if (PatchVal.couldBeVal(ann.type)) {
        found = true;
        valAnnotation = ann;
        break;
      }
    }

    if (!found) return;

    // Now check that 'out' is missing either of these.

    if (modifiers == null)
      return; // This is null only if the project is 1.4 or less. Lombok doesn't work in that.
    boolean finalIsPresent = false;
    boolean valIsPresent = false;

    for (Object present : modifiers) {
      if (present instanceof Modifier) {
        ModifierKeyword keyword = ((Modifier) present).getKeyword();
        if (keyword == null) continue;
        if (keyword.toFlagValue() == Modifier.FINAL) finalIsPresent = true;
      }

      if (present instanceof org.eclipse.jdt.core.dom.Annotation) {
        Name typeName = ((org.eclipse.jdt.core.dom.Annotation) present).getTypeName();
        if (typeName != null) {
          String fullyQualifiedName = typeName.getFullyQualifiedName();
          if ("val".equals(fullyQualifiedName) || "lombok.val".equals(fullyQualifiedName)) {
            valIsPresent = true;
          }
        }
      }
    }

    if (!finalIsPresent) {
      modifiers.add(
          createModifier(
              ast,
              ModifierKeyword.FINAL_KEYWORD,
              valAnnotation.sourceStart,
              valAnnotation.sourceEnd));
    }

    if (!valIsPresent) {
      MarkerAnnotation newAnnotation =
          createValAnnotation(
              ast, valAnnotation, valAnnotation.sourceStart, valAnnotation.sourceEnd);
      try {
        Reflection.astConverterRecordNodes.invoke(converter, newAnnotation, valAnnotation);
        Reflection.astConverterRecordNodes.invoke(
            converter, newAnnotation.getTypeName(), valAnnotation.type);
      } catch (IllegalAccessException e) {
        throw Lombok.sneakyThrow(e);
      } catch (InvocationTargetException e) {
        throw Lombok.sneakyThrow(e.getCause());
      }
      modifiers.add(newAnnotation);
    }
  }