コード例 #1
0
  @Override
  public boolean handle(
      AnnotationValues<Synchronized> annotation, JCAnnotation ast, Node annotationNode) {
    Node methodNode = annotationNode.up();

    if (methodNode == null
        || methodNode.getKind() != Kind.METHOD
        || !(methodNode.get() instanceof JCMethodDecl)) {
      annotationNode.addError("@Synchronized is legal only on methods.");
      return true;
    }

    JCMethodDecl method = (JCMethodDecl) methodNode.get();

    if ((method.mods.flags & Flags.ABSTRACT) != 0) {
      annotationNode.addError("@Synchronized is legal only on concrete methods.");
      return true;
    }
    boolean isStatic = (method.mods.flags & Flags.STATIC) != 0;
    String lockName = annotation.getInstance().value();
    boolean autoMake = false;
    if (lockName.length() == 0) {
      autoMake = true;
      lockName = isStatic ? STATIC_LOCK_NAME : INSTANCE_LOCK_NAME;
    }

    TreeMaker maker = methodNode.getTreeMaker();

    if (fieldExists(lockName, methodNode) == MemberExistsResult.NOT_EXISTS) {
      if (!autoMake) {
        annotationNode.addError("The field " + new String(lockName) + " does not exist.");
        return true;
      }
      JCExpression objectType = chainDots(maker, methodNode, "java", "lang", "Object");
      // We use 'new Object[0];' because quite unlike 'new Object();', empty arrays *ARE*
      // serializable!
      JCNewArray newObjectArray =
          maker.NewArray(
              chainDots(maker, methodNode, "java", "lang", "Object"),
              List.<JCExpression>of(maker.Literal(TypeTags.INT, 0)),
              null);
      JCVariableDecl fieldDecl =
          maker.VarDef(
              maker.Modifiers(Flags.FINAL | (isStatic ? Flags.STATIC : 0)),
              methodNode.toName(lockName),
              objectType,
              newObjectArray);
      injectField(methodNode.up(), fieldDecl);
    }

    if (method.body == null) return false;

    JCExpression lockNode = maker.Ident(methodNode.toName(lockName));

    method.body = maker.Block(0, List.<JCStatement>of(maker.Synchronized(lockNode, method.body)));

    methodNode.rebuild();

    return true;
  }
コード例 #2
0
 /**
  * Generates a new statement that checks if the given variable is null, and if so, throws a {@code
  * NullPointerException} with the variable name as message.
  */
 public static JCStatement generateNullCheck(TreeMaker treeMaker, JavacNode variable) {
   JCVariableDecl varDecl = (JCVariableDecl) variable.get();
   if (isPrimitive(varDecl.vartype)) return null;
   Name fieldName = varDecl.name;
   JCExpression npe = chainDots(variable, "java", "lang", "NullPointerException");
   JCTree exception =
       treeMaker.NewClass(
           null,
           List.<JCExpression>nil(),
           npe,
           List.<JCExpression>of(treeMaker.Literal(fieldName.toString())),
           null);
   JCStatement throwStatement = treeMaker.Throw(exception);
   return treeMaker.If(
       treeMaker.Binary(CTC_EQUAL, treeMaker.Ident(fieldName), treeMaker.Literal(CTC_BOT, null)),
       throwStatement,
       null);
 }
コード例 #3
0
 private static void addSuppressWarningsAll(
     JCModifiers mods, JavacNode node, int pos, JCTree source) {
   TreeMaker maker = node.getTreeMaker();
   JCExpression suppressWarningsType = chainDots(node, "java", "lang", "SuppressWarnings");
   JCLiteral allLiteral = maker.Literal("all");
   suppressWarningsType.pos = pos;
   allLiteral.pos = pos;
   JCAnnotation annotation =
       recursiveSetGeneratedBy(
           maker.Annotation(suppressWarningsType, List.<JCExpression>of(allLiteral)), source);
   annotation.pos = pos;
   mods.annotations = mods.annotations.append(annotation);
 }