コード例 #1
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);
      }
    }
  }