Prevent generating QualIdents inside import clauses
authorJan Lahoda <jlahoda@netbeans.org>
Thu, 20 Jan 2011 23:30:03 +0100
changeset 5239263091c58de
parent 521 28cbfe7484a0
child 524 3b13a4ec61b0
Prevent generating QualIdents inside import clauses
api/src/org/netbeans/modules/jackpot30/spi/JavaFix.java
api/test/unit/src/org/netbeans/modules/jackpot30/spi/JavaFixTest.java
file/test/unit/src/org/netbeans/modules/jackpot30/file/fqn-rewrite-test.hint
file/test/unit/src/org/netbeans/modules/jackpot30/file/fqn-rewrite-test.test
kit/nbproject/genfiles.properties
     1.1 --- a/api/src/org/netbeans/modules/jackpot30/spi/JavaFix.java	Sun Jan 16 19:22:56 2011 +0100
     1.2 +++ b/api/src/org/netbeans/modules/jackpot30/spi/JavaFix.java	Thu Jan 20 23:30:03 2011 +0100
     1.3 @@ -525,7 +525,17 @@
     1.4                  wc.rewrite(tp.getLeaf(), parsed);
     1.5              }
     1.6  
     1.7 -            new ReplaceParameters(wc, canShowUI, parameters, parametersMulti, parameterNames).scan(new TreePath(tp.getParentPath(), parsed), null);
     1.8 +            //prevent generating QualIdents inside import clauses - might be better to solve that inside ImportAnalysis2,
     1.9 +            //but that seems not to be straightforward:
    1.10 +            boolean inImport = parsed.getKind() == Kind.IMPORT;
    1.11 +            TreePath w = tp.getParentPath();
    1.12 +
    1.13 +            while (!inImport && w != null) {
    1.14 +                inImport |= w.getLeaf().getKind() == Kind.IMPORT;
    1.15 +                w = w.getParentPath();
    1.16 +            }
    1.17 +
    1.18 +            new ReplaceParameters(wc, canShowUI, inImport, parameters, parametersMulti, parameterNames).scan(new TreePath(tp.getParentPath(), parsed), null);
    1.19          }
    1.20      }
    1.21  
    1.22 @@ -535,14 +545,16 @@
    1.23  
    1.24          private final WorkingCopy wc;
    1.25          private final boolean canShowUI;
    1.26 +        private final boolean inImport;
    1.27          private final Map<String, TreePath> parameters;
    1.28          private final Map<String, Collection<TreePath>> parametersMulti;
    1.29          private final Map<String, String> parameterNames;
    1.30  
    1.31 -        public ReplaceParameters(WorkingCopy wc, boolean canShowUI, Map<String, TreePath> parameters, Map<String, Collection<TreePath>> parametersMulti, Map<String, String> parameterNames) {
    1.32 +        public ReplaceParameters(WorkingCopy wc, boolean canShowUI, boolean inImport, Map<String, TreePath> parameters, Map<String, Collection<TreePath>> parametersMulti, Map<String, String> parameterNames) {
    1.33              this.parameters = parameters;
    1.34              this.wc = wc;
    1.35              this.canShowUI = canShowUI;
    1.36 +            this.inImport = inImport;
    1.37              this.parametersMulti = parametersMulti;
    1.38              this.parameterNames = parameterNames;
    1.39          }
    1.40 @@ -589,7 +601,7 @@
    1.41  
    1.42              Element e = wc.getTrees().getElement(getCurrentPath());
    1.43  
    1.44 -            if (e != null && isStaticElement(e)) {
    1.45 +            if (e != null && isStaticElement(e) && !inImport) {
    1.46                  wc.rewrite(node, wc.getTreeMaker().QualIdent(e));
    1.47              }
    1.48  
    1.49 @@ -626,7 +638,7 @@
    1.50              //check correct dependency:
    1.51              checkDependency(wc, e, canShowUI);
    1.52  
    1.53 -            if (isStaticElement(e)) {
    1.54 +            if (isStaticElement(e) && !inImport) {
    1.55                  wc.rewrite(node, wc.getTreeMaker().QualIdent(e));
    1.56  
    1.57                  return null;
     2.1 --- a/api/test/unit/src/org/netbeans/modules/jackpot30/spi/JavaFixTest.java	Sun Jan 16 19:22:56 2011 +0100
     2.2 +++ b/api/test/unit/src/org/netbeans/modules/jackpot30/spi/JavaFixTest.java	Thu Jan 20 23:30:03 2011 +0100
     2.3 @@ -423,6 +423,18 @@
     2.4  		           "}\n");
     2.5      }
     2.6  
     2.7 +    public void testCarefulRewriteInImports() throws Exception {
     2.8 +        performRewriteTest("package test;\n" +
     2.9 +                           "import javax.swing.text.AbstractDocument;\n" +
    2.10 +                           "public class Test {\n" +
    2.11 +                           "}\n",
    2.12 +                           "javax.swing.text.AbstractDocument => javax.swing.text.Document",
    2.13 +                           "package test;\n" +
    2.14 +                           "import javax.swing.text.Document;\n" +
    2.15 +                           "public class Test {\n" +
    2.16 +		           "}\n");
    2.17 +    }
    2.18 +
    2.19      public void performRewriteTest(String code, String rule, String golden) throws Exception {
    2.20  	prepareTest("test/Test.java", code);
    2.21  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/file/test/unit/src/org/netbeans/modules/jackpot30/file/fqn-rewrite-test.hint	Thu Jan 20 23:30:03 2011 +0100
     3.3 @@ -0,0 +1,3 @@
     3.4 +   javax.swing.text.AbstractDocument
     3.5 +=> javax.swing.text.Document
     3.6 +;;
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/file/test/unit/src/org/netbeans/modules/jackpot30/file/fqn-rewrite-test.test	Thu Jan 20 23:30:03 2011 +0100
     4.3 @@ -0,0 +1,19 @@
     4.4 +%%TestCase pos-1
     4.5 +package test;
     4.6 +import javax.swing.text.AbstractDocument;
     4.7 +public class Test {
     4.8 +    private AbstractDocument doc;
     4.9 +}
    4.10 +%%=>
    4.11 +package test;
    4.12 +import javax.swing.text.Document;
    4.13 +public class Test {
    4.14 +    private AbstractDocument doc;
    4.15 +}
    4.16 +%%=>
    4.17 +package test;
    4.18 +import javax.swing.text.AbstractDocument;
    4.19 +import javax.swing.text.Document;
    4.20 +public class Test {
    4.21 +    private Document doc;
    4.22 +}
     5.1 --- a/kit/nbproject/genfiles.properties	Sun Jan 16 19:22:56 2011 +0100
     5.2 +++ b/kit/nbproject/genfiles.properties	Thu Jan 20 23:30:03 2011 +0100
     5.3 @@ -3,6 +3,6 @@
     5.4  build.xml.stylesheet.CRC32=79c3b980@1.28.0.7
     5.5  # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
     5.6  # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
     5.7 -nbproject/build-impl.xml.data.CRC32=704f6194
     5.8 +nbproject/build-impl.xml.data.CRC32=0e536aba
     5.9  nbproject/build-impl.xml.script.CRC32=c129207c
    5.10  nbproject/build-impl.xml.stylesheet.CRC32=238281d1@1.44