Example #1
0
  public static void copyInitializationOfForEachIterable(Parser parser) {
    ASTNode[] astStack;
    int astPtr;
    try {
      astStack = (ASTNode[]) Reflection.astStackField.get(parser);
      astPtr = (Integer) Reflection.astPtrField.get(parser);
    } catch (Exception e) {
      // Most likely we're in ecj or some other plugin usage of the eclipse compiler. No need for
      // this.
      return;
    }

    ForeachStatement foreachDecl = (ForeachStatement) astStack[astPtr];
    ASTNode init = foreachDecl.collection;
    if (init == null) return;
    if (foreachDecl.elementVariable == null
        || !PatchVal.couldBeVal(foreachDecl.elementVariable.type)) return;

    try {
      if (Reflection.iterableCopyField != null)
        Reflection.iterableCopyField.set(foreachDecl.elementVariable, init);
    } catch (Exception e) {
      // In ecj mode this field isn't there and we don't need the copy anyway, so, we ignore the
      // exception.
    }
  }
Example #2
0
  public static void copyInitializationOfLocalDeclaration(Parser parser) {
    ASTNode[] astStack;
    int astPtr;
    try {
      astStack = (ASTNode[]) Reflection.astStackField.get(parser);
      astPtr = (Integer) Reflection.astPtrField.get(parser);
    } catch (Exception e) {
      // Most likely we're in ecj or some other plugin usage of the eclipse compiler. No need for
      // this.
      return;
    }
    AbstractVariableDeclaration variableDecl = (AbstractVariableDeclaration) astStack[astPtr];
    if (!(variableDecl instanceof LocalDeclaration)) return;
    ASTNode init = variableDecl.initialization;
    if (init == null) return;
    if (!PatchVal.couldBeVal(variableDecl.type)) return;

    try {
      if (Reflection.initCopyField != null) Reflection.initCopyField.set(variableDecl, init);
    } catch (Exception e) {
      // In ecj mode this field isn't there and we don't need the copy anyway, so, we ignore the
      // exception.
    }
  }
Example #3
0
  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);
    }
  }