KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > gen > AnnotationOnLocVarTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.api.java.source.gen;
20
21 import com.sun.source.tree.AnnotationTree;
22 import com.sun.source.tree.AssignmentTree;
23 import com.sun.source.tree.BlockTree;
24 import com.sun.source.tree.ExpressionTree;
25 import com.sun.source.tree.MethodTree;
26 import com.sun.source.tree.ModifiersTree;
27 import com.sun.source.tree.StatementTree;
28 import com.sun.source.tree.VariableTree;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.FileNotFoundException JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.List JavaDoc;
35 import javax.lang.model.element.Modifier;
36 import org.netbeans.api.java.source.transform.Transformer;
37 import org.netbeans.junit.NbTestSuite;
38 import org.openide.filesystems.FileStateInvalidException;
39
40 /**
41  *
42  * @author Pavel Flaska
43  */

44 public class AnnotationOnLocVarTest extends GeneratorTest {
45     
46     /** Creates a new instance of AnnotationAttributeValueTest */
47     public AnnotationOnLocVarTest(String JavaDoc name) {
48         super(name);
49     }
50     
51     public static NbTestSuite suite() {
52         NbTestSuite suite = new NbTestSuite(AnnotationOnLocVarTest.class);
53         return suite;
54     }
55     
56     protected void setUp() throws Exception JavaDoc {
57         super.setUp();
58         testFile = getFile(getSourceDir(), getSourcePckg() + "AnnOnLocalVar.java");
59     }
60
61     public void testAddAnnToLocVar() throws IOException JavaDoc {
62         System.err.println("testAddAnnToLocVar");
63         process(
64             new Transformer<Void JavaDoc, Object JavaDoc>() {
65                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
66                     super.visitMethod(node, p);
67                     if ("<init>".contentEquals(node.getName())) {
68                         BlockTree body = node.getBody();
69                         List JavaDoc<? extends StatementTree> statements = body.getStatements();
70                         VariableTree statement = (VariableTree) statements.get(1);
71                         // mods will be replaced by a new one
72
ModifiersTree mods = statement.getModifiers();
73                         List JavaDoc<AnnotationTree> anns = new ArrayList JavaDoc<AnnotationTree>(1);
74                         List JavaDoc<AssignmentTree> attribs = new ArrayList JavaDoc<AssignmentTree>(4);
75                         attribs.add(make.Assignment(make.Identifier("id"), make.Literal(Integer.valueOf(666))));
76                         attribs.add(make.Assignment(make.Identifier("synopsis"), make.Literal("fat")));
77                         attribs.add(make.Assignment(make.Identifier("engineer"), make.Literal("PaF")));
78                         attribs.add(make.Assignment(make.Identifier("date"), make.Literal("2005")));
79                         anns.add(make.Annotation(make.Identifier("AnnotationType"), attribs));
80                         model.setElement(statement, model.getElement(statement));
81                         model.setType(statement, model.getType(statement));
82                         changes.rewrite(mods, make.Modifiers(mods.getFlags(), anns));
83                     }
84                     return null;
85                 }
86             }
87         );
88         assertFiles("testAddAnnToLocVar_AnnotationOnLocVarTest.pass");
89     }
90
91     public void testAddLocVarWithAnn() throws java.io.IOException JavaDoc, FileStateInvalidException {
92         System.err.println("testAddLocVarWithAnn");
93         process(
94             new Transformer<Void JavaDoc, Object JavaDoc>() {
95                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
96                     super.visitMethod(node, p);
97                     if ("<init>".contentEquals(node.getName())) {
98                         BlockTree bt = node.getBody();
99                         List JavaDoc<StatementTree> statements = new ArrayList JavaDoc<StatementTree>(bt.getStatements());
100                         statements.remove(0); // remove super(), which is in class file, but not present in source
101
List JavaDoc<AnnotationTree> anns = new ArrayList JavaDoc<AnnotationTree>(1);
102                         List JavaDoc<AssignmentTree> attribs = new ArrayList JavaDoc<AssignmentTree>(4);
103                         attribs.add(make.Assignment(make.Identifier("id"), make.Literal(Integer.valueOf(777))));
104                         attribs.add(make.Assignment(make.Identifier("synopsis"), make.Literal("thin")));
105                         attribs.add(make.Assignment(make.Identifier("engineer"), make.Literal("Snoopy")));
106                         attribs.add(make.Assignment(make.Identifier("date"), make.Literal("2001")));
107                         anns.add(make.Annotation(make.Identifier("AnnotationType"), attribs));
108                         statements.add(0, make.Variable(
109                             make.Modifiers(Collections.singleton(Modifier.FINAL), anns),
110                             "testVar",
111                             make.Identifier("java.util.List"),
112                             make.NewClass(
113                                 null,
114                                 Collections.EMPTY_LIST,
115                                 make.Identifier("java.util.ArrayList"),
116                                 Collections.singletonList(make.Literal(Integer.valueOf(3))),
117                                 null
118                             )
119                         ));
120                         BlockTree njuBlock = make.Block(statements, false);
121                         model.setPos(njuBlock, model.getPos(bt));
122                         changes.rewrite(bt, njuBlock);
123                     }
124                     return null;
125                 }
126             }
127         );
128         assertFiles("testAddLocVarWithAnn_AnnotationOnLocVarTest.pass");
129     }
130     
131     String JavaDoc getSourcePckg() {
132         return "org/netbeans/test/codegen/";
133     }
134
135     String JavaDoc getGoldenPckg() {
136         return "org/netbeans/jmi/javamodel/codegen/AnnotationOnLocVarTest/";
137     }
138 }
139
Popular Tags