1 19 20 25 26 package org.netbeans.test.java.generating.InnerClasses; 27 28 import com.sun.source.tree.ClassTree; 29 import com.sun.source.tree.CompilationUnitTree; 30 import com.sun.source.tree.ExpressionTree; 31 import com.sun.source.tree.MethodTree; 32 import com.sun.source.tree.ModifiersTree; 33 import com.sun.source.tree.Tree; 34 import com.sun.source.tree.VariableTree; 35 import java.io.IOException ; 36 import java.util.ArrayList ; 37 import java.util.Collections ; 38 import java.util.EnumSet ; 39 import java.util.EnumSet ; 40 import java.util.HashMap ; 41 import java.util.List ; 42 import java.util.Map ; 43 import javax.lang.model.element.Element; 44 import javax.lang.model.element.Modifier; 45 import org.netbeans.api.java.source.CancellableTask; 46 import org.netbeans.api.java.source.JavaSource; 47 import org.netbeans.api.java.source.JavaSource.Phase; 48 import org.netbeans.api.java.source.TreeMaker; 49 import org.netbeans.api.java.source.WorkingCopy; 50 import org.openide.loaders.*; 51 import org.netbeans.junit.*; 52 import org.netbeans.test.java.Common; 53 import org.openide.filesystems.FileObject; 54 55 79 80 81 public class InnerClasses extends org.netbeans.test.java.XRunner { 82 83 public static void main(java.lang.String [] args) { 84 junit.textui.TestRunner.run(suite()); 85 } 86 87 public InnerClasses() { 88 super(""); 89 } 90 91 public InnerClasses(java.lang.String testName) { 92 super(testName); 93 } 94 95 public static NbTest suite() { 96 return new NbTestSuite(InnerClasses.class); 97 } 98 99 106 public boolean go(Object o, java.io.PrintWriter log) throws Exception { 107 108 boolean passed = true; 109 110 FileObject fo = (FileObject) o; 111 JavaSource js = JavaSource.forFileObject(fo); 112 createInnerClass(js); 113 114 152 return passed; 153 } 154 155 157 protected void setUp() { 158 super.setUp(); 159 name = "JavaTestSourceInnerClasses"; 160 packageName = "org.netbeans.test.java.testsources"; 161 } 162 163 164 public void createInnerClass(JavaSource js) throws IOException { 165 CancellableTask task = new CancellableTask<WorkingCopy>() { 166 public void cancel() { 167 throw new UnsupportedOperationException ("Not supported yet."); 168 } 169 170 public void run(WorkingCopy workingCopy) throws Exception { 171 workingCopy.toPhase(Phase.RESOLVED); 172 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 173 TreeMaker make = workingCopy.getTreeMaker(); 174 ClassTree clazz = null; 175 for (Tree typeDecl : cut.getTypeDecls()) { 176 if (Tree.Kind.CLASS == typeDecl.getKind()) { 177 clazz = (ClassTree) typeDecl; 178 } 179 } EnumSet <Modifier> modifiers = EnumSet.of(Modifier.STATIC,Modifier.PUBLIC); 181 Tree extendsTree = make.QualIdent(workingCopy.getElements().getTypeElement("java.util.List")); 182 List <ExpressionTree> implementsList = Collections.<ExpressionTree> singletonList(make.Identifier("Serializable")); 183 Map <String ,String > params = new HashMap <String , String >(); 184 params.put("param1", "String"); 185 MethodTree mt = Common.createMethod(make, "method", params); 186 VariableTree vt = Common.createField(make, "variable", EnumSet.of(Modifier.PROTECTED), "double"); 187 List <? extends Tree> members = new ArrayList <Tree>(); 188 ClassTree innerClass = make.Class( 189 make.Modifiers(modifiers), 190 "MyInner", 191 Collections.EMPTY_LIST, 192 extendsTree, 193 implementsList, 194 Collections.EMPTY_LIST); 195 innerClass = make.addClassMember(innerClass, mt); 196 innerClass = make.addClassMember(innerClass, vt); 197 innerClass = make.addClassMember(innerClass, make.Block(Collections.EMPTY_LIST, false)); 198 ClassTree modifiedClazz = make.addClassMember(clazz, innerClass); 199 workingCopy.rewrite(clazz, modifiedClazz); 200 } 201 }; 202 js.runModificationTask(task).commit(); 203 } 204 205 209 231 } 232 | Popular Tags |