コード例 #1
0
 private void addProperty(ClassNode cNode, PropertyNode pNode) {
   final FieldNode fn = pNode.getField();
   cNode.getFields().remove(fn);
   cNode.addProperty(
       pNode.getName(),
       pNode.getModifiers() | ACC_FINAL,
       pNode.getType(),
       pNode.getInitialExpression(),
       pNode.getGetterBlock(),
       pNode.getSetterBlock());
   final FieldNode newfn = cNode.getField(fn.getName());
   cNode.getFields().remove(newfn);
   cNode.addField(fn);
 }
コード例 #2
0
 private void createConstructorMapCommon(ClassNode cNode, BlockStatement body) {
   final List<FieldNode> fList = cNode.getFields();
   for (FieldNode fNode : fList) {
     if (fNode.isPublic()) continue; // public fields will be rejected elsewhere
     if (cNode.getProperty(fNode.getName()) != null) continue; // a property
     if (fNode.isFinal() && fNode.isStatic()) continue;
     if (fNode.getName().contains("$")) continue; // internal field
     if (fNode.isFinal() && fNode.getInitialExpression() != null)
       body.addStatement(checkFinalArgNotOverridden(cNode, fNode));
     body.addStatement(createConstructorStatementDefault(fNode));
   }
   final Parameter[] params = new Parameter[] {new Parameter(HASHMAP_TYPE, "args")};
   doAddConstructor(
       cNode,
       new ConstructorNode(
           ACC_PUBLIC,
           params,
           ClassNode.EMPTY_ARRAY,
           new IfStatement(
               equalsNullExpr(new VariableExpression("args")), new EmptyStatement(), body)));
 }
コード例 #3
0
  public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    // temporarily have weaker check which allows for old Deprecated Annotation
    //        if (!MY_TYPE.equals(node.getClassNode())) return;
    if (!node.getClassNode().getName().endsWith(".Immutable")) return;
    List<PropertyNode> newProperties = new ArrayList<PropertyNode>();

    if (parent instanceof ClassNode) {
      final List<String> knownImmutableClasses = getKnownImmutableClasses(node);
      final List<String> knownImmutables = getKnownImmutables(node);

      ClassNode cNode = (ClassNode) parent;
      String cName = cNode.getName();
      checkNotInterface(cNode, MY_TYPE_NAME);
      makeClassFinal(cNode);

      final List<PropertyNode> pList = getInstanceProperties(cNode);
      for (PropertyNode pNode : pList) {
        adjustPropertyForImmutability(pNode, newProperties);
      }
      for (PropertyNode pNode : newProperties) {
        cNode.getProperties().remove(pNode);
        addProperty(cNode, pNode);
      }
      final List<FieldNode> fList = cNode.getFields();
      for (FieldNode fNode : fList) {
        ensureNotPublic(cName, fNode);
      }
      createConstructors(cNode, knownImmutableClasses, knownImmutables);
      if (!hasAnnotation(cNode, EqualsAndHashCodeASTTransformation.MY_TYPE)) {
        createHashCode(cNode, true, false, false, null, null);
        createEquals(cNode, false, false, false, null, null);
      }
      if (!hasAnnotation(cNode, ToStringASTTransformation.MY_TYPE)) {
        createToString(cNode, false, false, null, null, false, true);
      }
    }
  }