0001: /*
0002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003: *
0004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
0005: *
0006: * The contents of this file are subject to the terms of either the GNU
0007: * General Public License Version 2 only ("GPL") or the Common
0008: * Development and Distribution License("CDDL") (collectively, the
0009: * "License"). You may not use this file except in compliance with the
0010: * License. You can obtain a copy of the License at
0011: * http://www.netbeans.org/cddl-gplv2.html
0012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
0013: * specific language governing permissions and limitations under the
0014: * License. When distributing the software, include this License Header
0015: * Notice in each file and include the License file at
0016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
0017: * particular file as subject to the "Classpath" exception as provided
0018: * by Sun in the GPL Version 2 section of the License file that
0019: * accompanied this code. If applicable, add the following below the
0020: * License Header, with the fields enclosed by brackets [] replaced by
0021: * your own identifying information:
0022: * "Portions Copyrighted [year] [name of copyright owner]"
0023: *
0024: * Contributor(s):
0025: *
0026: * The Original Software is NetBeans. The Initial Developer of the Original
0027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
0028: * Microsystems, Inc. All Rights Reserved.
0029: *
0030: * If you wish your version of this file to be governed by only the CDDL
0031: * or only the GPL Version 2, indicate your decision by adding
0032: * "[Contributor] elects to include this software in this distribution
0033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
0034: * single choice of license, a recipient has the option to distribute
0035: * your version of this file under either the CDDL, the GPL Version 2 or
0036: * to extend the choice of license to its licensees as provided above.
0037: * However, if you add GPL Version 2 code and therefore, elected the GPL
0038: * Version 2 license, then the option applies only if the new code is
0039: * made subject to such option by the copyright holder.
0040: */
0041:
0042: package org.netbeans.api.java.source.gen;
0043:
0044: import com.sun.source.tree.*;
0045: import com.sun.source.util.TreePath;
0046: import java.io.IOException;
0047: import java.util.ArrayList;
0048: import java.util.Arrays;
0049: import java.util.Collections;
0050: import java.util.EnumSet;
0051: import java.util.List;
0052: import javax.lang.model.element.Element;
0053: import javax.lang.model.element.ExecutableElement;
0054: import javax.lang.model.element.Modifier;
0055: import javax.lang.model.element.PackageElement;
0056: import javax.lang.model.element.TypeElement;
0057: import javax.lang.model.element.VariableElement;
0058: import javax.lang.model.type.TypeMirror;
0059: import javax.lang.model.util.Types;
0060: import org.netbeans.api.java.source.Task;
0061: import org.netbeans.api.java.source.JavaSource;
0062: import org.netbeans.api.java.source.JavaSource.Phase;
0063: import org.netbeans.api.java.source.SourceUtils;
0064: import org.netbeans.api.java.source.TestUtilities;
0065: import org.netbeans.api.java.source.TreeMaker;
0066: import org.netbeans.api.java.source.WorkingCopy;
0067: import org.netbeans.junit.NbTestSuite;
0068: import org.openide.filesystems.FileStateInvalidException;
0069: import org.openide.filesystems.FileUtil;
0070:
0071: /**
0072: * Testing import analysis, QualIdentTree.
0073: *
0074: * @author Dusan Balek
0075: * @author Jan Lahoda
0076: * @author Pavel Flaska
0077: */
0078: public class ImportAnalysisTest extends GeneratorTest {
0079:
0080: /** Creates a new instance of ClashingImportsTest */
0081: public ImportAnalysisTest(String name) {
0082: super (name);
0083: }
0084:
0085: public static NbTestSuite suite() {
0086: NbTestSuite suite = new NbTestSuite();
0087: // suite.addTestSuite(ImportAnalysisTest.class);
0088: suite.addTest(new ImportAnalysisTest("testAddImport1"));
0089: suite.addTest(new ImportAnalysisTest("testAddImport2"));
0090: suite.addTest(new ImportAnalysisTest("testAddImport3"));
0091: suite.addTest(new ImportAnalysisTest("testAddImport4"));
0092: suite.addTest(new ImportAnalysisTest("testAddImport5"));
0093: suite.addTest(new ImportAnalysisTest("testAddImport6"));
0094: suite.addTest(new ImportAnalysisTest("testAddImport7"));
0095: suite.addTest(new ImportAnalysisTest("testAddImport8"));
0096: suite.addTest(new ImportAnalysisTest("testAddImport9"));
0097: suite.addTest(new ImportAnalysisTest("testAddImport10"));
0098: // suite.addTest(new ImportAnalysisTest("testAddImport11"));
0099: suite.addTest(new ImportAnalysisTest("testAddImport12"));
0100: suite.addTest(new ImportAnalysisTest("testAddImport13"));
0101: suite.addTest(new ImportAnalysisTest("testAddImport14"));
0102: suite.addTest(new ImportAnalysisTest("testAddImport15"));
0103: // suite.addTest(new ImportAnalysisTest("testAddImport16"));
0104: suite.addTest(new ImportAnalysisTest("testAddImport17"));
0105: suite.addTest(new ImportAnalysisTest("testAddImport18"));
0106: suite.addTest(new ImportAnalysisTest("testAddImportOrder1"));
0107: suite
0108: .addTest(new ImportAnalysisTest(
0109: "testAddImportSamePackage"));
0110: suite.addTest(new ImportAnalysisTest(
0111: "testAddImportThroughMethod1"));
0112: suite.addTest(new ImportAnalysisTest(
0113: "testAddImportThroughMethod2"));
0114: suite.addTest(new ImportAnalysisTest(
0115: "testAddImportThroughMethod3"));
0116: suite.addTest(new ImportAnalysisTest(
0117: "testAddImportThroughMethod4"));
0118: suite.addTest(new ImportAnalysisTest(
0119: "testAddImportThroughMethod5"));
0120: suite.addTest(new ImportAnalysisTest("testDefaultPackage1"));
0121: suite.addTest(new ImportAnalysisTest(
0122: "testImportAddedAfterThrows"));
0123: suite.addTest(new ImportAnalysisTest("testImportGetterSetter"));
0124: suite.addTest(new ImportAnalysisTest(
0125: "testImportClashWithJavaLang"));
0126: suite.addTest(new ImportAnalysisTest(
0127: "testImportNoClashJavaLang"));
0128: suite.addTest(new ImportAnalysisTest(
0129: "testImportNoClashCurrentPackage127486"));
0130: return suite;
0131: }
0132:
0133: @Override
0134: protected void setUp() throws Exception {
0135: super .setUp();
0136: testFile = getFile(getSourceDir(), getSourcePckg()
0137: + "ClashingImports.java");
0138: }
0139:
0140: public void testAddImport1() throws IOException {
0141: JavaSource src = getJavaSource(testFile);
0142: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0143:
0144: public void run(WorkingCopy workingCopy) throws IOException {
0145: workingCopy.toPhase(Phase.RESOLVED);
0146: CompilationUnitTree cut = workingCopy
0147: .getCompilationUnit();
0148: TreeMaker make = workingCopy.getTreeMaker();
0149: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
0150: MethodTree node = (MethodTree) clazz.getMembers()
0151: .get(0);
0152: BlockTree body = node.getBody();
0153: List<StatementTree> stats = new ArrayList<StatementTree>();
0154: for (StatementTree st : body.getStatements()) {
0155: stats.add(st);
0156: }
0157: TypeElement e = workingCopy.getElements()
0158: .getTypeElement("java.util.List");
0159: ExpressionTree type = make.QualIdent(e);
0160: stats.add(make
0161: .Variable(make.Modifiers(Collections
0162: .<Modifier> emptySet()), "utilList",
0163: type, null));
0164: workingCopy.rewrite(body, make.Block(stats, false));
0165: }
0166: };
0167: src.runModificationTask(task).commit();
0168: assertFiles("testAddImport1.pass");
0169: }
0170:
0171: public void testAddImport2() throws IOException {
0172: JavaSource src = getJavaSource(testFile);
0173: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0174:
0175: public void run(WorkingCopy workingCopy) throws IOException {
0176: workingCopy.toPhase(Phase.RESOLVED);
0177: CompilationUnitTree cut = workingCopy
0178: .getCompilationUnit();
0179: TreeMaker make = workingCopy.getTreeMaker();
0180: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
0181: MethodTree node = (MethodTree) clazz.getMembers()
0182: .get(0);
0183: BlockTree body = node.getBody();
0184: List<StatementTree> stats = new ArrayList<StatementTree>();
0185: for (StatementTree st : body.getStatements()) {
0186: stats.add(st);
0187: }
0188: TypeElement list = workingCopy.getElements()
0189: .getTypeElement("java.util.List");
0190: stats.add(make.Variable(make.Modifiers(Collections
0191: .<Modifier> emptySet()), "utilList", make
0192: .Type(workingCopy.getTypes().erasure(
0193: list.asType())), null));
0194: workingCopy.rewrite(body, make.Block(stats, false));
0195: }
0196: };
0197: src.runModificationTask(task).commit();
0198: assertFiles("testAddImport2.pass");
0199: }
0200:
0201: public void testAddImport3() throws IOException {
0202: JavaSource src = getJavaSource(testFile);
0203: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0204:
0205: public void run(WorkingCopy workingCopy) throws IOException {
0206: workingCopy.toPhase(Phase.RESOLVED);
0207: CompilationUnitTree cut = workingCopy
0208: .getCompilationUnit();
0209: TreeMaker make = workingCopy.getTreeMaker();
0210: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
0211: MethodTree node = (MethodTree) clazz.getMembers()
0212: .get(0);
0213: BlockTree body = node.getBody();
0214: List<StatementTree> stats = new ArrayList<StatementTree>();
0215: for (StatementTree st : body.getStatements()) {
0216: stats.add(st);
0217: }
0218: TypeElement list = workingCopy.getElements()
0219: .getTypeElement("java.util.List");
0220: TypeElement collection = workingCopy.getElements()
0221: .getTypeElement("java.util.Collection");
0222: Types types = workingCopy.getTypes();
0223: TypeMirror tm = types.getDeclaredType(list, types
0224: .erasure(collection.asType()));
0225: stats.add(make.Variable(make.Modifiers(Collections
0226: .<Modifier> emptySet()), "utilList", make
0227: .Type(tm), null));
0228: workingCopy.rewrite(body, make.Block(stats, false));
0229: }
0230: };
0231: src.runModificationTask(task).commit();
0232: assertFiles("testAddImport3.pass");
0233: }
0234:
0235: public void testAddImport4() throws IOException {
0236: JavaSource src = getJavaSource(testFile);
0237: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0238:
0239: public void run(WorkingCopy workingCopy) throws IOException {
0240: workingCopy.toPhase(Phase.RESOLVED);
0241: CompilationUnitTree cut = workingCopy
0242: .getCompilationUnit();
0243: TreeMaker make = workingCopy.getTreeMaker();
0244: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
0245: MethodTree node = (MethodTree) clazz.getMembers()
0246: .get(0);
0247: BlockTree body = node.getBody();
0248: List<StatementTree> stats = new ArrayList<StatementTree>();
0249: for (StatementTree st : body.getStatements()) {
0250: stats.add(st);
0251: }
0252: TypeElement list = workingCopy.getElements()
0253: .getTypeElement("java.util.List");
0254: TypeElement collection = workingCopy.getElements()
0255: .getTypeElement("java.util.Collection");
0256: Types types = workingCopy.getTypes();
0257: TypeMirror tm = types.getDeclaredType(list, types
0258: .getWildcardType(types.erasure(collection
0259: .asType()), null));
0260: stats.add(make.Variable(make.Modifiers(Collections
0261: .<Modifier> emptySet()), "utilList", make
0262: .Type(tm), null));
0263: workingCopy.rewrite(body, make.Block(stats, false));
0264: }
0265: };
0266: src.runModificationTask(task).commit();
0267: assertFiles("testAddImport4.pass");
0268: }
0269:
0270: public void testAddImport5() throws IOException {
0271: JavaSource src = getJavaSource(testFile);
0272: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0273:
0274: public void run(WorkingCopy workingCopy) throws IOException {
0275: workingCopy.toPhase(Phase.RESOLVED);
0276: CompilationUnitTree cut = workingCopy
0277: .getCompilationUnit();
0278: TreeMaker make = workingCopy.getTreeMaker();
0279: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
0280: MethodTree node = (MethodTree) clazz.getMembers()
0281: .get(0);
0282: BlockTree body = node.getBody();
0283: List<StatementTree> stats = new ArrayList<StatementTree>();
0284: for (StatementTree st : body.getStatements()) {
0285: stats.add(st);
0286: }
0287: TypeElement list = workingCopy.getElements()
0288: .getTypeElement("java.util.List");
0289: TypeElement collection = workingCopy.getElements()
0290: .getTypeElement("java.util.Collection");
0291: Types types = workingCopy.getTypes();
0292: TypeMirror tm = types.getDeclaredType(list, types
0293: .getWildcardType(null, types.erasure(collection
0294: .asType())));
0295: stats.add(make.Variable(make.Modifiers(Collections
0296: .<Modifier> emptySet()), "utilList", make
0297: .Type(tm), null));
0298: workingCopy.rewrite(body, make.Block(stats, false));
0299: }
0300: };
0301: src.runModificationTask(task).commit();
0302: assertFiles("testAddImport5.pass");
0303: }
0304:
0305: public void testAddImport6() throws IOException {
0306: JavaSource src = getJavaSource(testFile);
0307: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0308:
0309: public void run(WorkingCopy workingCopy) throws IOException {
0310: workingCopy.toPhase(Phase.RESOLVED);
0311: CompilationUnitTree cut = workingCopy
0312: .getCompilationUnit();
0313: TreeMaker make = workingCopy.getTreeMaker();
0314: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
0315: MethodTree node = (MethodTree) clazz.getMembers()
0316: .get(0);
0317: BlockTree body = node.getBody();
0318: List<StatementTree> stats = new ArrayList<StatementTree>();
0319: for (StatementTree st : body.getStatements()) {
0320: stats.add(st);
0321: }
0322: TypeElement list = workingCopy.getElements()
0323: .getTypeElement("java.util.Map.Entry");
0324: Types types = workingCopy.getTypes();
0325: stats.add(make.Variable(make.Modifiers(Collections
0326: .<Modifier> emptySet()), "entry", make
0327: .Type(types.erasure(list.asType())), null));
0328: workingCopy.rewrite(body, make.Block(stats, false));
0329: }
0330: };
0331: src.runModificationTask(task).commit();
0332: assertFiles("testAddImport6.pass");
0333: }
0334:
0335: public void testAddImport7() throws IOException {
0336: JavaSource src = getJavaSource(testFile);
0337: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0338:
0339: public void run(WorkingCopy workingCopy) throws IOException {
0340: workingCopy.toPhase(Phase.RESOLVED);
0341: CompilationUnitTree cut = workingCopy
0342: .getCompilationUnit();
0343: TreeMaker make = workingCopy.getTreeMaker();
0344: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
0345: MethodTree node = (MethodTree) clazz.getMembers()
0346: .get(0);
0347: BlockTree body = node.getBody();
0348: List<StatementTree> stats = new ArrayList<StatementTree>();
0349: for (StatementTree st : body.getStatements()) {
0350: stats.add(st);
0351: }
0352: TypeElement list = workingCopy.getElements()
0353: .getTypeElement("java.util.Map.Entry");
0354: stats.add(make.Variable(make.Modifiers(Collections
0355: .<Modifier> emptySet()), "entry", make
0356: .QualIdent(list), null));
0357: workingCopy.rewrite(body, make.Block(stats, false));
0358: }
0359: };
0360: src.runModificationTask(task).commit();
0361: assertFiles("testAddImport6.pass"); //the same as testAddImport6, so using only one golden file
0362: }
0363:
0364: public void testAddImport8() throws IOException {
0365: JavaSource src = getJavaSource(testFile);
0366: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0367:
0368: public void run(WorkingCopy workingCopy) throws IOException {
0369: workingCopy.toPhase(Phase.RESOLVED);
0370: CompilationUnitTree cut = workingCopy
0371: .getCompilationUnit();
0372: TreeMaker make = workingCopy.getTreeMaker();
0373: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
0374: MethodTree node = (MethodTree) clazz.getMembers()
0375: .get(0);
0376: BlockTree body = node.getBody();
0377: List<StatementTree> stats = new ArrayList<StatementTree>();
0378: for (StatementTree st : body.getStatements()) {
0379: stats.add(st);
0380: }
0381: TypeElement list = workingCopy.getElements()
0382: .getTypeElement("java.util.Map.Entry");
0383: Types types = workingCopy.getTypes();
0384: TypeMirror tm = types.getArrayType(types.erasure(list
0385: .asType()));
0386: stats.add(make.Variable(make.Modifiers(Collections
0387: .<Modifier> emptySet()), "entry",
0388: make.Type(tm), null));
0389: workingCopy.rewrite(body, make.Block(stats, false));
0390: }
0391: };
0392: src.runModificationTask(task).commit();
0393: assertFiles("testAddImport8.pass");
0394: }
0395:
0396: public void testAddImport9() throws IOException {
0397: JavaSource src = getJavaSource(testFile);
0398: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0399:
0400: public void run(WorkingCopy workingCopy) throws IOException {
0401: workingCopy.toPhase(Phase.RESOLVED);
0402: CompilationUnitTree cut = workingCopy
0403: .getCompilationUnit();
0404: TreeMaker make = workingCopy.getTreeMaker();
0405: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
0406: MethodTree node = (MethodTree) clazz.getMembers()
0407: .get(0);
0408: BlockTree body = node.getBody();
0409: List<StatementTree> stats = new ArrayList<StatementTree>();
0410: for (StatementTree st : body.getStatements()) {
0411: stats.add(st);
0412: }
0413: TypeElement list = workingCopy.getElements()
0414: .getTypeElement("java.util.List");
0415: stats.add(make.Variable(make.Modifiers(Collections
0416: .<Modifier> emptySet()), "list1", make
0417: .QualIdent(list), null));
0418: stats.add(make.Variable(make.Modifiers(Collections
0419: .<Modifier> emptySet()), "list2", make
0420: .QualIdent(list), null));
0421: workingCopy.rewrite(body, make.Block(stats, false));
0422: }
0423: };
0424: src.runModificationTask(task).commit();
0425: assertFiles("testAddImport9.pass");
0426: }
0427:
0428: public void testAddImport10() throws IOException {
0429: JavaSource src = getJavaSource(testFile);
0430: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0431:
0432: public void run(WorkingCopy workingCopy) throws IOException {
0433: workingCopy.toPhase(Phase.RESOLVED);
0434: CompilationUnitTree cut = workingCopy
0435: .getCompilationUnit();
0436: TreeMaker make = workingCopy.getTreeMaker();
0437: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
0438: MethodTree node = (MethodTree) clazz.getMembers()
0439: .get(0);
0440: BlockTree body = node.getBody();
0441: List<StatementTree> stats = new ArrayList<StatementTree>();
0442: for (StatementTree st : body.getStatements()) {
0443: stats.add(st);
0444: }
0445: TypeElement list = workingCopy.getElements()
0446: .getTypeElement("java.util.List");
0447: TypeElement awtList = workingCopy.getElements()
0448: .getTypeElement("java.awt.List");
0449: stats.add(make.Variable(make.Modifiers(Collections
0450: .<Modifier> emptySet()), "list1", make
0451: .QualIdent(list), null));
0452: stats.add(make.Variable(make.Modifiers(Collections
0453: .<Modifier> emptySet()), "list2", make
0454: .QualIdent(awtList), null));
0455: workingCopy.rewrite(body, make.Block(stats, false));
0456: }
0457: };
0458: src.runModificationTask(task).commit();
0459: assertFiles("testAddImport10.pass");
0460: }
0461:
0462: public void testAddImport11() throws IOException {
0463: JavaSource src = getJavaSource(testFile);
0464: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0465:
0466: public void run(WorkingCopy workingCopy) throws IOException {
0467: workingCopy.toPhase(Phase.RESOLVED);
0468: CompilationUnitTree cut = workingCopy
0469: .getCompilationUnit();
0470: TreeMaker make = workingCopy.getTreeMaker();
0471: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
0472: MethodTree node = (MethodTree) clazz.getMembers()
0473: .get(0);
0474: BlockTree body = node.getBody();
0475: List<StatementTree> stats = new ArrayList<StatementTree>();
0476: for (StatementTree st : body.getStatements()) {
0477: stats.add(st);
0478: }
0479: TypeElement list = workingCopy.getElements()
0480: .getTypeElement("java.util.Map.Entry");
0481: Types types = workingCopy.getTypes();
0482: TypeMirror tm = types.getArrayType(types.erasure(list
0483: .asType()));
0484: stats.add(make.Variable(make.Modifiers(Collections
0485: .<Modifier> emptySet()), "entry",
0486: make.Type(tm), null));
0487: workingCopy.rewrite(body, make.Block(stats, false));
0488: }
0489: };
0490: src.runModificationTask(task).commit();
0491: assertFiles("testAddImport11.pass");
0492: }
0493:
0494: public void testAddImport12() throws IOException {
0495: JavaSource src = getJavaSource(testFile);
0496: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0497:
0498: public void run(WorkingCopy workingCopy) throws IOException {
0499: workingCopy.toPhase(Phase.RESOLVED);
0500: CompilationUnitTree cut = workingCopy
0501: .getCompilationUnit();
0502: TreeMaker make = workingCopy.getTreeMaker();
0503: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
0504: TypeElement map = workingCopy.getElements()
0505: .getTypeElement("java.util.Map");
0506: ClassTree nue = make.addClassImplementsClause(clazz,
0507: make.QualIdent(map));
0508: workingCopy.rewrite(clazz, nue);
0509: MethodTree node = (MethodTree) clazz.getMembers()
0510: .get(0);
0511: BlockTree body = node.getBody();
0512: List<StatementTree> stats = new ArrayList<StatementTree>();
0513: for (StatementTree st : body.getStatements()) {
0514: stats.add(st);
0515: }
0516: TypeElement list = workingCopy.getElements()
0517: .getTypeElement("java.util.Map.Entry");
0518: Types types = workingCopy.getTypes();
0519: TypeMirror tm = types.getArrayType(types.erasure(list
0520: .asType()));
0521: stats.add(make.Variable(make.Modifiers(Collections
0522: .<Modifier> emptySet()), "entry",
0523: make.Type(tm), null));
0524: workingCopy.rewrite(body, make.Block(stats, false));
0525: }
0526: };
0527: src.runModificationTask(task).commit();
0528: assertFiles("testAddImport12.pass");
0529: }
0530:
0531: public void testAddImport13() throws IOException {
0532: testFile = getFile(getSourceDir(), getSourcePckg()
0533: + "ImportsTest2.java");
0534: JavaSource src = getJavaSource(testFile);
0535: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0536:
0537: public void run(WorkingCopy workingCopy) throws IOException {
0538: workingCopy.toPhase(Phase.RESOLVED);
0539: CompilationUnitTree cut = workingCopy
0540: .getCompilationUnit();
0541: TreeMaker make = workingCopy.getTreeMaker();
0542: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
0543: MethodTree node = (MethodTree) clazz.getMembers()
0544: .get(0);
0545: BlockTree body = node.getBody();
0546: List<StatementTree> stats = new ArrayList<StatementTree>();
0547: for (StatementTree st : body.getStatements()) {
0548: stats.add(st);
0549: }
0550: TypeElement list = workingCopy.getElements()
0551: .getTypeElement("java.util.Map.Entry");
0552: Types types = workingCopy.getTypes();
0553: TypeMirror tm = types.getArrayType(types.erasure(list
0554: .asType()));
0555: stats.add(make.Variable(make.Modifiers(Collections
0556: .<Modifier> emptySet()), "entry",
0557: make.Type(tm), null));
0558: workingCopy.rewrite(body, make.Block(stats, false));
0559: }
0560: };
0561: src.runModificationTask(task).commit();
0562: assertFiles("testAddImport13.pass");
0563: }
0564:
0565: public void testAddImport14() throws IOException {
0566: testFile = getFile(getSourceDir(), getSourcePckg()
0567: + "ImportsTest3.java");
0568: JavaSource src = getJavaSource(testFile);
0569: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0570:
0571: public void run(WorkingCopy workingCopy) throws IOException {
0572: workingCopy.toPhase(Phase.RESOLVED);
0573: CompilationUnitTree cut = workingCopy
0574: .getCompilationUnit();
0575: TreeMaker make = workingCopy.getTreeMaker();
0576: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
0577: MethodTree node = (MethodTree) clazz.getMembers()
0578: .get(0);
0579: BlockTree body = node.getBody();
0580: List<StatementTree> stats = new ArrayList<StatementTree>();
0581: for (StatementTree st : body.getStatements()) {
0582: stats.add(st);
0583: }
0584: TypeElement list = workingCopy.getElements()
0585: .getTypeElement("java.util.Map.Entry");
0586: Types types = workingCopy.getTypes();
0587: TypeMirror tm = types.getArrayType(types.erasure(list
0588: .asType()));
0589: stats.add(make.Variable(make.Modifiers(Collections
0590: .<Modifier> emptySet()), "entry",
0591: make.Type(tm), null));
0592: workingCopy.rewrite(body, make.Block(stats, false));
0593: }
0594: };
0595: src.runModificationTask(task).commit();
0596: assertFiles("testAddImport14.pass");
0597: }
0598:
0599: public void testAddImport15() throws IOException {
0600: testFile = getFile(getSourceDir(), getSourcePckg()
0601: + "ImportsTest4.java");
0602: JavaSource src = getJavaSource(testFile);
0603: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0604:
0605: public void run(WorkingCopy workingCopy) throws IOException {
0606: workingCopy.toPhase(Phase.RESOLVED);
0607: CompilationUnitTree cut = workingCopy
0608: .getCompilationUnit();
0609: TreeMaker make = workingCopy.getTreeMaker();
0610: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
0611: MethodTree node = (MethodTree) clazz.getMembers()
0612: .get(0);
0613: BlockTree body = node.getBody();
0614: List<StatementTree> stats = new ArrayList<StatementTree>();
0615: for (StatementTree st : body.getStatements()) {
0616: stats.add(st);
0617: }
0618: TypeElement list = workingCopy.getElements()
0619: .getTypeElement("java.lang.Math");
0620: ExecutableElement maxMethod = null;
0621: VariableElement pi = null;
0622: for (Element ee : list.getEnclosedElements()) {
0623: if ("max".equals(ee.getSimpleName().toString())) {
0624: maxMethod = (ExecutableElement) ee;
0625: }
0626: if ("PI".equals(ee.getSimpleName().toString())) {
0627: pi = (VariableElement) ee;
0628: }
0629: }
0630:
0631: assertNotNull(maxMethod);
0632: assertNotNull(pi);
0633:
0634: stats.add(make.ExpressionStatement(make
0635: .MethodInvocation(Collections
0636: .<ExpressionTree> emptyList(), make
0637: .QualIdent(maxMethod), Arrays.asList(
0638: make.QualIdent(pi), make.Literal(2)))));
0639: workingCopy.rewrite(body, make.Block(stats, false));
0640: }
0641: };
0642: src.runModificationTask(task).commit();
0643: assertFiles("testAddImport15.pass");
0644: }
0645:
0646: public void testAddImport16() throws IOException {
0647: JavaSource src = getJavaSource(testFile);
0648: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0649:
0650: public void run(WorkingCopy workingCopy) throws IOException {
0651: workingCopy.toPhase(Phase.RESOLVED);
0652: CompilationUnitTree cut = workingCopy
0653: .getCompilationUnit();
0654: TreeMaker make = workingCopy.getTreeMaker();
0655: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
0656: MethodTree node = (MethodTree) clazz.getMembers()
0657: .get(0);
0658: BlockTree body = node.getBody();
0659: List<StatementTree> stats = new ArrayList<StatementTree>();
0660: for (StatementTree st : body.getStatements()) {
0661: stats.add(st);
0662: }
0663: TypeElement list = workingCopy.getElements()
0664: .getTypeElement("java.lang.Math");
0665: ExecutableElement maxMethod = null;
0666: ExecutableElement minMethod = null;
0667: VariableElement pi = null;
0668: for (Element ee : list.getEnclosedElements()) {
0669: if ("max".equals(ee.getSimpleName().toString())) {
0670: maxMethod = (ExecutableElement) ee;
0671: }
0672: if ("min".equals(ee.getSimpleName().toString())) {
0673: minMethod = (ExecutableElement) ee;
0674: }
0675: if ("PI".equals(ee.getSimpleName().toString())) {
0676: pi = (VariableElement) ee;
0677: }
0678: }
0679:
0680: assertNotNull(maxMethod);
0681: assertNotNull(minMethod);
0682: assertNotNull(pi);
0683:
0684: stats.add(make.ExpressionStatement(make
0685: .MethodInvocation(Collections
0686: .<ExpressionTree> emptyList(), make
0687: .QualIdent(maxMethod), Arrays.asList(
0688: make.QualIdent(pi), make.Literal(2)))));
0689: stats.add(make.ExpressionStatement(make
0690: .MethodInvocation(Collections
0691: .<ExpressionTree> emptyList(), make
0692: .QualIdent(minMethod), Arrays.asList(
0693: make.QualIdent(pi), make.Literal(2)))));
0694:
0695: workingCopy.rewrite(body, make.Block(stats, false));
0696: }
0697: };
0698: src.runModificationTask(task).commit();
0699: assertFiles("testAddImport16.pass");
0700: }
0701:
0702: public void testAddImport17() throws IOException {
0703: testFile = getFile(getSourceDir(), getSourcePckg()
0704: + "ImportsTest6.java");
0705: JavaSource src = getJavaSource(testFile);
0706: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0707:
0708: public void run(WorkingCopy workingCopy) throws IOException {
0709: workingCopy.toPhase(Phase.RESOLVED);
0710: CompilationUnitTree cut = workingCopy
0711: .getCompilationUnit();
0712: TreeMaker make = workingCopy.getTreeMaker();
0713: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
0714: MethodTree node = (MethodTree) clazz.getMembers()
0715: .get(0);
0716: BlockTree body = node.getBody();
0717: List<StatementTree> stats = new ArrayList<StatementTree>();
0718: for (StatementTree st : body.getStatements()) {
0719: stats.add(st);
0720: }
0721: TypeElement list = workingCopy.getElements()
0722: .getTypeElement("java.lang.String");
0723: Types types = workingCopy.getTypes();
0724: TypeMirror tm = types.getArrayType(types.erasure(list
0725: .asType()));
0726: stats.add(make.Variable(make.Modifiers(Collections
0727: .<Modifier> emptySet()), "s", make.Type(tm),
0728: null));
0729: workingCopy.rewrite(body, make.Block(stats, false));
0730: }
0731: };
0732: src.runModificationTask(task).commit();
0733: assertFiles("testAddImport17.pass");
0734: }
0735:
0736: public void testAddImport18() throws IOException {
0737: testFile = getFile(getSourceDir(), getSourcePckg()
0738: + "ImportsTest6.java");
0739: JavaSource src = getJavaSource(testFile);
0740: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0741:
0742: public void run(WorkingCopy workingCopy) throws IOException {
0743: workingCopy.toPhase(Phase.RESOLVED);
0744: CompilationUnitTree node = workingCopy
0745: .getCompilationUnit();
0746: TreeMaker make = workingCopy.getTreeMaker();
0747: ExpressionTree pack = node.getPackageName();
0748: PackageElement pe = workingCopy.getElements()
0749: .getPackageElement("org.netbeans.test");
0750: ExpressionTree nuePack = make.QualIdent(pe);
0751:
0752: workingCopy.rewrite(pack, nuePack);
0753: }
0754: };
0755: src.runModificationTask(task).commit();
0756: assertFiles("testAddImport18.pass");
0757: }
0758:
0759: public void testAddImportOrder1() throws IOException {
0760: testFile = getFile(getSourceDir(), getSourcePckg()
0761: + "ImportsTest7.java");
0762: JavaSource src = getJavaSource(testFile);
0763: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0764:
0765: public void run(WorkingCopy workingCopy) throws IOException {
0766: workingCopy.toPhase(Phase.RESOLVED);
0767: CompilationUnitTree cut = workingCopy
0768: .getCompilationUnit();
0769: TreeMaker make = workingCopy.getTreeMaker();
0770: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
0771: MethodTree node = (MethodTree) clazz.getMembers()
0772: .get(0);
0773: BlockTree body = node.getBody();
0774: List<StatementTree> stats = new ArrayList<StatementTree>();
0775: for (StatementTree st : body.getStatements()) {
0776: stats.add(st);
0777: }
0778: TypeElement list = workingCopy.getElements()
0779: .getTypeElement("java.util.LinkedList");
0780: Types types = workingCopy.getTypes();
0781: TypeMirror tm = types.getArrayType(types.erasure(list
0782: .asType()));
0783: stats.add(make.Variable(make.Modifiers(Collections
0784: .<Modifier> emptySet()), "s", make.Type(tm),
0785: null));
0786: workingCopy.rewrite(body, make.Block(stats, false));
0787: }
0788: };
0789: src.runModificationTask(task).commit();
0790: assertFiles("testAddImportOrder1.pass");
0791: }
0792:
0793: public void testAddImportSamePackage() throws IOException {
0794: testFile = getFile(getSourceDir(), getSourcePckg()
0795: + "ImportsTest7.java");
0796: JavaSource src = getJavaSource(testFile);
0797: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0798:
0799: public void run(WorkingCopy workingCopy) throws IOException {
0800: workingCopy.toPhase(Phase.RESOLVED);
0801: CompilationUnitTree cut = workingCopy
0802: .getCompilationUnit();
0803: TreeMaker make = workingCopy.getTreeMaker();
0804: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
0805: MethodTree node = (MethodTree) clazz.getMembers()
0806: .get(0);
0807: BlockTree body = node.getBody();
0808: List<StatementTree> stats = new ArrayList<StatementTree>();
0809: for (StatementTree st : body.getStatements()) {
0810: stats.add(st);
0811: }
0812: TypeElement list = workingCopy
0813: .getElements()
0814: .getTypeElement(
0815: "org.netbeans.test.codegen.ImportsTest6");
0816: Types types = workingCopy.getTypes();
0817: TypeMirror tm = types.getArrayType(types.erasure(list
0818: .asType()));
0819: stats.add(make.Variable(make.Modifiers(Collections
0820: .<Modifier> emptySet()), "s", make.Type(tm),
0821: null));
0822: workingCopy.rewrite(body, make.Block(stats, false));
0823: }
0824: };
0825: src.runModificationTask(task).commit();
0826: assertFiles("testAddImportSamePackage.pass");
0827: }
0828:
0829: //XXX: more tests default package
0830: public void testDefaultPackage1() throws IOException {
0831: testFile = getFile(getSourceDir(),
0832: "ImportAnalysisDefaultPackage1.java");
0833: JavaSource src = getJavaSource(testFile);
0834: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0835:
0836: public void run(WorkingCopy workingCopy) throws IOException {
0837: workingCopy.toPhase(Phase.RESOLVED);
0838: CompilationUnitTree cut = workingCopy
0839: .getCompilationUnit();
0840: TreeMaker make = workingCopy.getTreeMaker();
0841: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
0842: MethodTree node = (MethodTree) clazz.getMembers()
0843: .get(0);
0844: BlockTree body = node.getBody();
0845: List<StatementTree> stats = new ArrayList<StatementTree>();
0846: for (StatementTree st : body.getStatements()) {
0847: stats.add(st);
0848: }
0849: TypeElement exc = workingCopy
0850: .getElements()
0851: .getTypeElement("ImportAnalysisDefaultPackage2");
0852: stats.add(make.Variable(make.Modifiers(Collections
0853: .<Modifier> emptySet()), "s", make
0854: .QualIdent(exc), null));
0855: workingCopy.rewrite(body, make.Block(stats, false));
0856: }
0857: };
0858: src.runModificationTask(task).commit();
0859: assertFiles("testDefaultPackage1.pass");
0860: }
0861:
0862: public void testImportAddedAfterThrows() throws IOException {
0863: testFile = getFile(getSourceDir(), getSourcePckg()
0864: + "ImportsTest7.java");
0865: JavaSource src = getJavaSource(testFile);
0866: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0867:
0868: public void run(WorkingCopy workingCopy) throws IOException {
0869: workingCopy.toPhase(Phase.RESOLVED);
0870: CompilationUnitTree cut = workingCopy
0871: .getCompilationUnit();
0872: TreeMaker make = workingCopy.getTreeMaker();
0873: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
0874: MethodTree node = (MethodTree) clazz.getMembers()
0875: .get(0);
0876: TypeElement exc = workingCopy
0877: .getElements()
0878: .getTypeElement(
0879: "javax.swing.text.BadLocationException");
0880: MethodTree nueMethod = make.addMethodThrows(node,
0881: (ExpressionTree) make.Type(exc.asType()));
0882: workingCopy.rewrite(node, nueMethod);
0883: }
0884: };
0885: src.runModificationTask(task).commit();
0886: assertFiles("testImportAddedAfterThrows.pass");
0887: }
0888:
0889: public void testAddImportThroughMethod1() throws IOException {
0890: JavaSource testSource = JavaSource.forFileObject(FileUtil
0891: .toFileObject(testFile));
0892: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0893:
0894: public void run(WorkingCopy workingCopy)
0895: throws java.io.IOException {
0896: workingCopy.toPhase(Phase.RESOLVED);
0897: TreeMaker make = workingCopy.getTreeMaker();
0898: ClassTree clazz = (ClassTree) workingCopy
0899: .getCompilationUnit().getTypeDecls().get(0);
0900: MethodTree node = (MethodTree) clazz.getMembers()
0901: .get(0);
0902: int offset = (int) (workingCopy.getTrees()
0903: .getSourcePositions().getStartPosition(
0904: workingCopy.getCompilationUnit(), node) + 1);
0905: TreePath context = workingCopy.getTreeUtilities()
0906: .pathFor(offset);
0907: try {
0908: assertEquals("List", SourceUtils.resolveImport(
0909: workingCopy, context, "java.util.List"));
0910: assertEquals("java.awt.List", SourceUtils
0911: .resolveImport(workingCopy, context,
0912: "java.awt.List"));
0913: } catch (IOException e) {
0914: throw new IllegalStateException(e);
0915: }
0916: }
0917: };
0918: testSource.runModificationTask(task).commit();
0919: String res = TestUtilities.copyFileToString(testFile);
0920: System.err.println(res);
0921: assertFiles("testAddImportThroughMethod1.pass");
0922: }
0923:
0924: public void testAddImportThroughMethod2() throws IOException {
0925: JavaSource testSource = JavaSource.forFileObject(FileUtil
0926: .toFileObject(testFile));
0927: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0928:
0929: public void run(WorkingCopy workingCopy)
0930: throws java.io.IOException {
0931: workingCopy.toPhase(Phase.RESOLVED);
0932: TreeMaker make = workingCopy.getTreeMaker();
0933: ClassTree clazz = (ClassTree) workingCopy
0934: .getCompilationUnit().getTypeDecls().get(0);
0935: MethodTree node = (MethodTree) clazz.getMembers()
0936: .get(0);
0937: int offset = (int) (workingCopy.getTrees()
0938: .getSourcePositions().getStartPosition(
0939: workingCopy.getCompilationUnit(), node) + 1);
0940: TreePath context = workingCopy.getTreeUtilities()
0941: .pathFor(offset);
0942: try {
0943: assertEquals("List", SourceUtils.resolveImport(
0944: workingCopy, context, "java.util.List"));
0945: assertEquals("java.awt.List", SourceUtils
0946: .resolveImport(workingCopy, context,
0947: "java.awt.List"));
0948: } catch (IOException e) {
0949: throw new IllegalStateException(e);
0950: }
0951: }
0952: };
0953: testSource.runModificationTask(task).commit();
0954: String res = TestUtilities.copyFileToString(testFile);
0955: System.err.println(res);
0956: assertFiles("testAddImportThroughMethod2.pass");
0957: }
0958:
0959: public void testAddImportThroughMethod3() throws IOException {
0960: JavaSource testSource = JavaSource.forFileObject(FileUtil
0961: .toFileObject(testFile));
0962: Task<WorkingCopy> task = new Task<WorkingCopy>() {
0963:
0964: public void run(WorkingCopy workingCopy)
0965: throws java.io.IOException {
0966: workingCopy.toPhase(Phase.RESOLVED);
0967: TreeMaker make = workingCopy.getTreeMaker();
0968: ClassTree clazz = (ClassTree) workingCopy
0969: .getCompilationUnit().getTypeDecls().get(0);
0970: MethodTree node = (MethodTree) clazz.getMembers()
0971: .get(0);
0972: int offset = (int) (workingCopy.getTrees()
0973: .getSourcePositions().getStartPosition(
0974: workingCopy.getCompilationUnit(), node) + 1);
0975: TreePath context = workingCopy.getTreeUtilities()
0976: .pathFor(offset);
0977: try {
0978: assertEquals("List", SourceUtils.resolveImport(
0979: workingCopy, context, "java.util.List"));
0980: assertEquals("Map", SourceUtils.resolveImport(
0981: workingCopy, context, "java.util.Map"));
0982: assertEquals("java.awt.List", SourceUtils
0983: .resolveImport(workingCopy, context,
0984: "java.awt.List"));
0985: } catch (IOException e) {
0986: throw new IllegalStateException(e);
0987: }
0988: }
0989: };
0990: testSource.runModificationTask(task).commit();
0991: String res = TestUtilities.copyFileToString(testFile);
0992: System.err.println(res);
0993: assertFiles("testAddImportThroughMethod3.pass");
0994: }
0995:
0996: public void testAddImportThroughMethod4() throws IOException {
0997: JavaSource testSource = JavaSource.forFileObject(FileUtil
0998: .toFileObject(testFile));
0999: Task<WorkingCopy> task = new Task<WorkingCopy>() {
1000:
1001: public void run(WorkingCopy workingCopy)
1002: throws java.io.IOException {
1003: workingCopy.toPhase(Phase.RESOLVED);
1004: TreeMaker make = workingCopy.getTreeMaker();
1005: ClassTree clazz = (ClassTree) workingCopy
1006: .getCompilationUnit().getTypeDecls().get(0);
1007: MethodTree node = (MethodTree) clazz.getMembers()
1008: .get(0);
1009: int offset = (int) (workingCopy.getTrees()
1010: .getSourcePositions().getStartPosition(
1011: workingCopy.getCompilationUnit(), node) + 1);
1012: TreePath context = workingCopy.getTreeUtilities()
1013: .pathFor(offset);
1014: try {
1015: assertEquals(
1016: "SuperClassTest",
1017: SourceUtils
1018: .resolveImport(workingCopy,
1019: context,
1020: "org.netbeans.test.codegen.SuperClassTest"));
1021: } catch (IOException e) {
1022: throw new IllegalStateException(e);
1023: }
1024: }
1025: };
1026: testSource.runModificationTask(task).commit();
1027: String res = TestUtilities.copyFileToString(testFile);
1028: System.err.println(res);
1029: assertFiles("testAddImportThroughMethod4.pass");
1030: }
1031:
1032: public void testAddImportThroughMethod5() throws IOException {
1033: JavaSource testSource = JavaSource.forFileObject(FileUtil
1034: .toFileObject(testFile));
1035: Task<WorkingCopy> task = new Task<WorkingCopy>() {
1036:
1037: public void run(WorkingCopy workingCopy)
1038: throws java.io.IOException {
1039: workingCopy.toPhase(Phase.RESOLVED);
1040: TreeMaker make = workingCopy.getTreeMaker();
1041: ClassTree clazz = (ClassTree) workingCopy
1042: .getCompilationUnit().getTypeDecls().get(0);
1043: MethodTree node = (MethodTree) clazz.getMembers()
1044: .get(0);
1045: int offset = (int) (workingCopy.getTrees()
1046: .getSourcePositions().getStartPosition(
1047: workingCopy.getCompilationUnit(), node) + 1);
1048: TreePath context = workingCopy.getTreeUtilities()
1049: .pathFor(offset);
1050: try {
1051: assertEquals(
1052: "SuperClassTest.FirstInnerClass",
1053: SourceUtils
1054: .resolveImport(workingCopy,
1055: context,
1056: "org.netbeans.test.codegen.SuperClassTest.FirstInnerClass"));
1057: } catch (IOException e) {
1058: throw new IllegalStateException(e);
1059: }
1060: }
1061: };
1062: testSource.runModificationTask(task).commit();
1063: String res = TestUtilities.copyFileToString(testFile);
1064: System.err.println(res);
1065: assertFiles("testAddImportThroughMethod4.pass");
1066: }
1067:
1068: public void testImportGetterSetter() throws IOException {
1069: testFile = getFile(getSourceDir(), getSourcePckg()
1070: + "ImportsTest8.java");
1071: JavaSource src = getJavaSource(testFile);
1072: Task<WorkingCopy> task = new Task<WorkingCopy>() {
1073:
1074: public void run(WorkingCopy workingCopy) throws IOException {
1075: workingCopy.toPhase(Phase.RESOLVED);
1076: CompilationUnitTree cut = workingCopy
1077: .getCompilationUnit();
1078: TreeMaker make = workingCopy.getTreeMaker();
1079: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
1080: ExpressionTree type = make.QualIdent(workingCopy
1081: .getElements().getTypeElement(
1082: "java.awt.geom.Point2D.Double"));
1083: VariableTree vt = make.Variable(make.Modifiers(EnumSet
1084: .noneOf(Modifier.class)), "test", type, null);
1085: workingCopy.rewrite(clazz, make.addClassMember(clazz,
1086: vt));
1087: }
1088: };
1089: src.runModificationTask(task).commit();
1090: assertFiles("testImportGetterSetter.pass");
1091: }
1092:
1093: public void testImportClashWithJavaLang() throws IOException {
1094: testFile = getFile(getSourceDir(), getSourcePckg()
1095: + "ImportsTest9.java");
1096: JavaSource src = getJavaSource(testFile);
1097: Task<WorkingCopy> task = new Task<WorkingCopy>() {
1098:
1099: public void run(WorkingCopy workingCopy) throws IOException {
1100: workingCopy.toPhase(Phase.RESOLVED);
1101: CompilationUnitTree cut = workingCopy
1102: .getCompilationUnit();
1103: TreeMaker make = workingCopy.getTreeMaker();
1104: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
1105: ExpressionTree type = make.QualIdent(workingCopy
1106: .getElements().getTypeElement(
1107: "java.awt.geom.Point2D.Double"));
1108: VariableTree vt = make.Variable(make.Modifiers(EnumSet
1109: .noneOf(Modifier.class)), "test", type, null);
1110: workingCopy.rewrite(clazz, make.addClassMember(clazz,
1111: vt));
1112: }
1113: };
1114: src.runModificationTask(task).commit();
1115: assertFiles("testImportClashWithJavaLang.pass");
1116: }
1117:
1118: public void testImportNoClashJavaLang() throws IOException {
1119: testFile = getFile(getSourceDir(), getSourcePckg()
1120: + "ImportsTesta.java");
1121: JavaSource src = getJavaSource(testFile);
1122: Task<WorkingCopy> task = new Task<WorkingCopy>() {
1123:
1124: public void run(WorkingCopy workingCopy) throws IOException {
1125: workingCopy.toPhase(Phase.RESOLVED);
1126: CompilationUnitTree cut = workingCopy
1127: .getCompilationUnit();
1128: TreeMaker make = workingCopy.getTreeMaker();
1129: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
1130: ExpressionTree type = make.QualIdent(workingCopy
1131: .getElements().getTypeElement(
1132: "java.lang.Double"));
1133: VariableTree vt = make.Variable(make.Modifiers(EnumSet
1134: .noneOf(Modifier.class)), "test", type, null);
1135: workingCopy.rewrite(clazz, make.addClassMember(clazz,
1136: vt));
1137: }
1138: };
1139: src.runModificationTask(task).commit();
1140: assertFiles("testImportNoClashJavaLang.pass");
1141: }
1142:
1143: public void testImportNoClashCurrentPackage127486()
1144: throws IOException {
1145: testFile = getFile(getSourceDir(), getSourcePckg()
1146: + "ImportsTest127486.java");
1147: JavaSource src = getJavaSource(testFile);
1148: Task<WorkingCopy> task = new Task<WorkingCopy>() {
1149:
1150: public void run(WorkingCopy workingCopy) throws IOException {
1151: workingCopy.toPhase(Phase.RESOLVED);
1152: CompilationUnitTree cut = workingCopy
1153: .getCompilationUnit();
1154: TreeMaker make = workingCopy.getTreeMaker();
1155: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
1156: ExpressionTree type = make
1157: .QualIdent(workingCopy
1158: .getElements()
1159: .getTypeElement(
1160: "org.netbeans.test.codegen.InputStream"));
1161: VariableTree vt = make.Variable(make.Modifiers(EnumSet
1162: .noneOf(Modifier.class)), "test", type, null);
1163: workingCopy.rewrite(clazz, make.addClassMember(clazz,
1164: vt));
1165: }
1166: };
1167: src.runModificationTask(task).commit();
1168: assertFiles("testImportNoClashCurrentPackage127486.pass");
1169: }
1170:
1171: String getSourcePckg() {
1172: if ("testDefaultPackage1".equals(getName())) {
1173: return "";
1174: } else {
1175: return "org/netbeans/test/codegen/";
1176: }
1177: }
1178:
1179: String getGoldenPckg() {
1180: return "org/netbeans/jmi/javamodel/codegen/ImportAnalysisTest/";
1181: }
1182:
1183: @Override
1184: void assertFiles(final String aGoldenFile) throws IOException,
1185: FileStateInvalidException {
1186: assertFile("File is not correctly generated.", getTestFile(),
1187: getFile(getGoldenDir(), getGoldenPckg() + aGoldenFile),
1188: getWorkDir(), new WhitespaceIgnoringDiff());
1189: }
1190: }
|