/**
   * Get argument information.
   *
   * @see ParameterImpl
   * @return an array of ParameterImpl, one element per argument in the order the arguments are
   *     present.
   */
  public Parameter[] parameters() {
    // generate the parameters on the fly:  they're not cached
    List<VarSymbol> params = sym.params();
    Parameter result[] = new Parameter[params.length()];

    int i = 0;
    for (VarSymbol param : params) {
      result[i++] = new ParameterImpl(env, param);
    }
    return result;
  }
Esempio n. 2
0
 /**
  * Returns the elements of this annotation type. Returns an empty array if there are none.
  * Elements are always public, so no need to filter them.
  */
 public AnnotationTypeElementDoc[] elements() {
   Name.Table names = tsym.name.table;
   List<AnnotationTypeElementDoc> elements = new List<AnnotationTypeElementDoc>();
   for (Scope.Entry e = tsym.members().elems; e != null; e = e.sibling) {
     if (e.sym != null && e.sym.kind == Kinds.MTH) {
       MethodSymbol s = (MethodSymbol) e.sym;
       elements = elements.prepend(env.getAnnotationTypeElementDoc(s));
     }
   }
   return elements.toArray(new AnnotationTypeElementDoc[elements.length()]);
 }
Esempio n. 3
0
  /**
   * Checks if there is a method with the provided name. In case of multiple methods (overloading),
   * only the first method decides if EXISTS_BY_USER or EXISTS_BY_LOMBOK is returned.
   *
   * @param methodName the method name to check for.
   * @param node Any node that represents the Type (JCClassDecl) to look in, or any child node
   *     thereof.
   * @param caseSensitive If the search should be case sensitive.
   * @param params The number of parameters the method should have; varargs count as 0-*. Set to -1
   *     to find any method with the appropriate name regardless of parameter count.
   */
  public static MemberExistsResult methodExists(
      String methodName, JavacNode node, boolean caseSensitive, int params) {
    node = upToTypeNode(node);

    if (node != null && node.get() instanceof JCClassDecl) {
      for (JCTree def : ((JCClassDecl) node.get()).defs) {
        if (def instanceof JCMethodDecl) {
          JCMethodDecl md = (JCMethodDecl) def;
          String name = md.name.toString();
          boolean matches =
              caseSensitive ? name.equals(methodName) : name.equalsIgnoreCase(methodName);
          if (matches) {
            if (params > -1) {
              List<JCVariableDecl> ps = md.params;
              int minArgs = 0;
              int maxArgs = 0;
              if (ps != null && ps.length() > 0) {
                minArgs = ps.length();
                if ((ps.last().mods.flags & Flags.VARARGS) != 0) {
                  maxArgs = Integer.MAX_VALUE;
                  minArgs--;
                } else {
                  maxArgs = minArgs;
                }
              }

              if (params < minArgs || params > maxArgs) continue;
            }
            return getGeneratedBy(def) == null
                ? MemberExistsResult.EXISTS_BY_USER
                : MemberExistsResult.EXISTS_BY_LOMBOK;
          }
        }
      }
    }

    return MemberExistsResult.NOT_EXISTS;
  }
  public AnnotationDesc[] receiverAnnotations() {
    // TODO: change how receiver annotations are output!
    Type recvtype = sym.type.asMethodType().recvtype;
    if (recvtype == null) {
      return new AnnotationDesc[0];
    }

    List<? extends Compound> typeAnnos = recvtype.typeAnnotations;
    AnnotationDesc result[] = new AnnotationDesc[typeAnnos.length()];
    int i = 0;
    for (Attribute.Compound a : typeAnnos) {
      result[i++] = new AnnotationDescImpl(env, a);
    }
    return result;
  }
Esempio n. 5
0
 @Override
 public void visitTry(JCTry tree) {
   aPrint("try ");
   List<?> resources = readObject(tree, "resources", List.nil());
   int len = resources.length();
   switch (len) {
     case 0:
       break;
     case 1:
       print("(");
       JCVariableDecl decl = (JCVariableDecl) resources.get(0);
       flagMod = -1L & ~FINAL;
       printVarDefInline(decl);
       print(") ");
       break;
     default:
       println("(");
       indent++;
       int c = 0;
       for (Object i : resources) {
         align();
         flagMod = -1L & ~FINAL;
         printVarDefInline((JCVariableDecl) i);
         if (++c == len) {
           print(") ");
         } else {
           println(";", (JCTree) i);
         }
       }
       indent--;
   }
   println("{");
   indent++;
   for (JCStatement stat : tree.body.stats) print(stat);
   indent--;
   aPrint("}");
   for (JCCatch catchBlock : tree.catchers) {
     printCatch(catchBlock);
   }
   if (tree.finalizer != null) {
     println(" finally {");
     indent++;
     for (JCStatement stat : tree.finalizer.stats) print(stat);
     indent--;
     aPrint("}");
   }
   println(tree);
 }
 /**
  * Process a single compound annotation, returning its Attribute. Used from MemberEnter for
  * attaching the attributes to the annotated symbol.
  */
 Attribute.Compound enterAnnotation(JCAnnotation a, Type expected, Env<AttrContext> env) {
   // The annotation might have had its type attributed (but not checked)
   // by attr.attribAnnotationTypes during MemberEnter, in which case we do not
   // need to do it again.
   Type at =
       (a.annotationType.type != null
           ? a.annotationType.type
           : attr.attribType(a.annotationType, env));
   a.type = chk.checkType(a.annotationType.pos(), at, expected);
   if (a.type.isErroneous())
     return new Attribute.Compound(a.type, List.<Pair<MethodSymbol, Attribute>>nil());
   if ((a.type.tsym.flags() & Flags.ANNOTATION) == 0) {
     log.error(a.annotationType.pos(), "not.annotation.type", a.type.toString());
     return new Attribute.Compound(a.type, List.<Pair<MethodSymbol, Attribute>>nil());
   }
   List<JCExpression> args = a.args;
   if (args.length() == 1 && args.head.getTag() != JCTree.ASSIGN) {
     // special case: elided "value=" assumed
     args.head = make.at(args.head.pos).Assign(make.Ident(names.value), args.head);
   }
   ListBuffer<Pair<MethodSymbol, Attribute>> buf = new ListBuffer<Pair<MethodSymbol, Attribute>>();
   for (List<JCExpression> tl = args; tl.nonEmpty(); tl = tl.tail) {
     JCExpression t = tl.head;
     if (t.getTag() != JCTree.ASSIGN) {
       log.error(t.pos(), "annotation.value.must.be.name.value");
       continue;
     }
     JCAssign assign = (JCAssign) t;
     if (assign.lhs.getTag() != JCTree.IDENT) {
       log.error(t.pos(), "annotation.value.must.be.name.value");
       continue;
     }
     JCIdent left = (JCIdent) assign.lhs;
     Symbol method =
         rs.resolveQualifiedMethod(left.pos(), env, a.type, left.name, List.<Type>nil(), null);
     left.sym = method;
     left.type = method.type;
     if (method.owner != a.type.tsym)
       log.error(left.pos(), "no.annotation.member", left.name, a.type);
     Type result = method.type.getReturnType();
     Attribute value = enterAttributeValue(result, assign.rhs, env);
     if (!method.type.isErroneous())
       buf.append(new Pair<MethodSymbol, Attribute>((MethodSymbol) method, value));
     t.type = result;
   }
   return new Attribute.Compound(a.type, buf.toList());
 }