This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 142381
Collapse All | Expand All

(-)a/java.hints/nbproject/project.properties (+6 lines)
Lines 37-42 Link Here
37
# Version 2 license, then the option applies only if the new code is
37
# Version 2 license, then the option applies only if the new code is
38
# made subject to such option by the copyright holder.
38
# made subject to such option by the copyright holder.
39
39
40
auxiliary.org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs=true
41
auxiliary.org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width=4
42
auxiliary.org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab=4
43
auxiliary.org-netbeans-modules-editor-indent.CodeStyle.project.tab-size=4
44
auxiliary.org-netbeans-modules-editor-indent.CodeStyle.project.text-limit-width=100
45
auxiliary.org-netbeans-modules-editor-indent.CodeStyle.usedProfile=project
40
spec.version.base=1.26.0
46
spec.version.base=1.26.0
41
47
42
javac.source=1.5
48
javac.source=1.5
(-)a/java.hints/src/org/netbeans/modules/java/hints/errors/AddParameterOrLocalFix.java (-2 / +5 lines)
Lines 77-82 Link Here
77
import org.openide.filesystems.FileObject;
77
import org.openide.filesystems.FileObject;
78
import org.netbeans.modules.java.hints.infrastructure.ErrorHintsProvider;
78
import org.netbeans.modules.java.hints.infrastructure.ErrorHintsProvider;
79
import org.openide.util.NbBundle;
79
import org.openide.util.NbBundle;
80
import static org.netbeans.modules.java.hints.errors.Utilities.isEnhancedForLoopIdentifier;
80
81
81
82
82
/**
83
/**
Lines 167-173 Link Here
167
168
168
                    working.rewrite(targetTree, result);
169
                    working.rewrite(targetTree, result);
169
                } else {
170
                } else {
170
                    if (ErrorFixesFakeHint.isCreateLocalVariableInPlace()) {
171
                    if (ErrorFixesFakeHint.isCreateLocalVariableInPlace() || isEnhancedForLoopIdentifier(tp)) {
171
                        resolveLocalVariable(working, tp, make, proposedType);
172
                        resolveLocalVariable(working, tp, make, proposedType);
172
                    } else {
173
                    } else {
173
                        resolveLocalVariable55(working, tp, make, proposedType);
174
                        resolveLocalVariable55(working, tp, make, proposedType);
Lines 298-304 Link Here
298
        Tree statementParent = firstUse.getParentPath().getLeaf();
299
        Tree statementParent = firstUse.getParentPath().getLeaf();
299
        VariableTree vt = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), name, make.Type(proposedType), null);
300
        VariableTree vt = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), name, make.Type(proposedType), null);
300
301
301
        if (statementParent.getKind() == Kind.BLOCK) {
302
        if (isEnhancedForLoopIdentifier(tp)) {
303
            wc.rewrite(tp.getParentPath().getLeaf(), vt);
304
        } else if (statementParent.getKind() == Kind.BLOCK) {
302
            BlockTree block = (BlockTree) statementParent;
305
            BlockTree block = (BlockTree) statementParent;
303
306
304
            FirstUsage fu = new FirstUsage();
307
            FirstUsage fu = new FirstUsage();
(-)a/java.hints/src/org/netbeans/modules/java/hints/errors/CreateElementUtilities.java (-4 / +15 lines)
Lines 93-98 Link Here
93
import org.netbeans.modules.java.editor.semantic.Utilities;
93
import org.netbeans.modules.java.editor.semantic.Utilities;
94
import org.netbeans.modules.java.hints.infrastructure.ErrorHintsProvider;
94
import org.netbeans.modules.java.hints.infrastructure.ErrorHintsProvider;
95
import org.openide.ErrorManager;
95
import org.openide.ErrorManager;
96
import static org.netbeans.modules.java.hints.errors.Utilities.getIterableGenericType;
96
97
97
/**
98
/**
98
 *
99
 *
Lines 457-468 Link Here
457
            return Collections.singletonList(info.getTrees().getTypeMirror(new TreePath(parent, vt.getType())));
458
            return Collections.singletonList(info.getTrees().getTypeMirror(new TreePath(parent, vt.getType())));
458
        }
459
        }
459
        
460
        
460
        if (vt.getType() == error) {
461
        TreePath context = parent.getParentPath();
461
            types.add(ElementKind.CLASS);
462
        if (vt.getType() != error || context == null) {
462
            return Collections.<TypeMirror>emptyList();
463
            return null;
463
        }
464
        }
464
        
465
        
465
        return null;
466
        switch (context.getLeaf().getKind()) {
467
            case ENHANCED_FOR_LOOP:
468
                ExpressionTree iterableTree = ((EnhancedForLoopTree) context.getLeaf()).getExpression();
469
                TreePath iterablePath = new TreePath(context, iterableTree);
470
                TypeMirror type = getIterableGenericType(info, iterablePath);
471
                types.add(ElementKind.LOCAL_VARIABLE);
472
                return Collections.singletonList(type);
473
            default:
474
                types.add(ElementKind.CLASS);
475
                return Collections.<TypeMirror>emptyList();
476
        }
466
    }
477
    }
467
    
478
    
468
    private static List<? extends TypeMirror> computeAssert(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
479
    private static List<? extends TypeMirror> computeAssert(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
(-)a/java.hints/src/org/netbeans/modules/java/hints/errors/Utilities.java (+44 lines)
Lines 50-55 Link Here
50
import com.sun.source.tree.ParameterizedTypeTree;
50
import com.sun.source.tree.ParameterizedTypeTree;
51
import com.sun.source.tree.Scope;
51
import com.sun.source.tree.Scope;
52
import com.sun.source.tree.Tree;
52
import com.sun.source.tree.Tree;
53
import com.sun.source.tree.Tree.Kind;
53
import com.sun.source.util.TreePath;
54
import com.sun.source.util.TreePath;
54
import java.io.IOException;
55
import java.io.IOException;
55
import java.util.EnumSet;
56
import java.util.EnumSet;
Lines 59-67 Link Here
59
import javax.lang.model.SourceVersion;
60
import javax.lang.model.SourceVersion;
60
import javax.lang.model.element.Element;
61
import javax.lang.model.element.Element;
61
import javax.lang.model.element.ElementKind;
62
import javax.lang.model.element.ElementKind;
63
import javax.lang.model.element.ExecutableElement;
62
import javax.lang.model.element.TypeElement;
64
import javax.lang.model.element.TypeElement;
63
import javax.lang.model.type.ArrayType;
65
import javax.lang.model.type.ArrayType;
64
import javax.lang.model.type.DeclaredType;
66
import javax.lang.model.type.DeclaredType;
67
import javax.lang.model.type.ExecutableType;
65
import javax.lang.model.type.TypeKind;
68
import javax.lang.model.type.TypeKind;
66
import javax.lang.model.type.TypeMirror;
69
import javax.lang.model.type.TypeMirror;
67
import javax.lang.model.type.WildcardType;
70
import javax.lang.model.type.WildcardType;
Lines 154-159 Link Here
154
            return sb.toString();
157
            return sb.toString();
155
    }
158
    }
156
    
159
    
160
    // true if tp is an IDENTIFIER in a VARIABLE in an ENHANCED_FOR_LOOP
161
    public static boolean isEnhancedForLoopIdentifier(TreePath tp) {
162
        if (tp == null || tp.getLeaf().getKind() != Kind.IDENTIFIER)
163
            return false;
164
        TreePath parent = tp.getParentPath();
165
        if (parent == null || parent.getLeaf().getKind() != Kind.VARIABLE)
166
            return false;
167
        TreePath context = parent.getParentPath();
168
        if (context == null || context.getLeaf().getKind() != Kind.ENHANCED_FOR_LOOP)
169
            return false;
170
        return true;
171
    }
172
173
    // return the generic type of an Iterable (or ArrayType) at a TreePath
174
    public static TypeMirror getIterableGenericType(CompilationInfo info, TreePath iterable) {
175
        TypeElement iterableElement = info.getElements().getTypeElement("java.lang.Iterable"); //NOI18N
176
        if (iterableElement == null) {
177
            return null;
178
        }
179
        TypeMirror iterableType = info.getTrees().getTypeMirror(iterable);
180
        if (iterableType == null) {
181
            return null;
182
        }
183
        TypeMirror designedType = null;
184
        if (iterableType.getKind() == TypeKind.DECLARED) {
185
            DeclaredType declaredType = (DeclaredType) iterableType;
186
            if (!info.getTypes().isSubtype(info.getTypes().erasure(declaredType), info.getTypes().erasure(iterableElement.asType()))) {
187
                return null;
188
            }
189
            ExecutableElement iteratorMethod = (ExecutableElement) iterableElement.getEnclosedElements().get(0);
190
            ExecutableType iteratorMethodType = (ExecutableType) info.getTypes().asMemberOf(declaredType, iteratorMethod);
191
            designedType = ((DeclaredType) iteratorMethodType.getReturnType()).getTypeArguments().get(0);
192
        } else if (iterableType.getKind() == TypeKind.ARRAY) {
193
            designedType = ((ArrayType) iterableType).getComponentType();
194
        }
195
        if (designedType == null) {
196
            return null;
197
        }
198
        return resolveCapturedType(info, designedType);
199
    }
200
157
    public static String getName(TypeMirror tm) {
201
    public static String getName(TypeMirror tm) {
158
        if (tm.getKind().isPrimitive()) {
202
        if (tm.getKind().isPrimitive()) {
159
            return "" + Character.toLowerCase(tm.getKind().name().charAt(0));
203
            return "" + Character.toLowerCase(tm.getKind().name().charAt(0));
(-)a/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/AddParameterOrLocalFixTest.java (+78 lines)
Lines 137-142 Link Here
137
                       "package test; public class Test {public void test() {String foo; {foo = \"bar\";}foo = \"bar\";}}");
137
                       "package test; public class Test {public void test() {String foo; {foo = \"bar\";}foo = \"bar\";}}");
138
    }
138
    }
139
139
140
    public void testEnhancedForLoopEmptyList() throws Exception {
141
        parameter = false;
142
        performFixTest("test/Test.java",
143
                "package test;\n" +
144
                "public class Test {\n" +
145
                "     public void test() {\n" +
146
                "         for (|ttt : java.util.Collections.emptyList()) {}\n" +
147
                "     }\n" +
148
                "}\n",
149
                "AddParameterOrLocalFix:ttt:java.lang.Object:false",
150
                ("package test;\n" +
151
                "public class Test {\n" +
152
                "     public void test() {\n" +
153
                "         for (Object ttt : java.util.Collections.emptyList()) {}\n" +
154
                "     }\n" +
155
                "}\n").replaceAll("[ \t\n]+", " "));
156
    }
157
158
    public void testEnhancedForLoopExtendedNumber() throws Exception {
159
        parameter = false;
160
        performFixTest("test/Test.java",
161
                "package test;\n" +
162
                "public class Test {\n" +
163
                "     public void test() {\n" +
164
                "         java.util.List<? extends Number> l = null;\n" +
165
                "         for (|ttt : l) {}\n" +
166
                "     }\n" +
167
                "}\n",
168
                "AddParameterOrLocalFix:ttt:java.lang.Number:false",
169
                ("package test;\n" +
170
                "public class Test {\n" +
171
                "     public void test() {\n" +
172
                "         java.util.List<? extends Number> l = null;\n" +
173
                "         for (Number ttt : l) {}\n" +
174
                "     }\n" +
175
                "}\n").replaceAll("[ \t\n]+", " "));
176
    }
177
178
    public void testEnhancedForLoopStringArray() throws Exception {
179
        parameter = false;
180
        performFixTest("test/Test.java",
181
                "package test;\n" +
182
                "public class Test {\n" +
183
                "     public void test() {\n" +
184
                "         String[] a = null;\n" +
185
                "         for (|ttt : a) {}\n" +
186
                "     }\n" +
187
                "}\n",
188
                "AddParameterOrLocalFix:ttt:java.lang.String:false",
189
                ("package test;\n" +
190
                "public class Test {\n" +
191
                "     public void test() {\n" +
192
                "         String[] a = null;\n" +
193
                "         for (String ttt : a) {}\n" +
194
                "     }\n" +
195
                "}\n").replaceAll("[ \t\n]+", " "));
196
    }
197
198
    public void testEnhancedForLoopPrimitiveArray() throws Exception {
199
        parameter = false;
200
        performFixTest("test/Test.java",
201
                "package test;\n" +
202
                "public class Test {\n" +
203
                "     public void test() {\n" +
204
                "         int[] a = null;\n" +
205
                "         for (|ttt : a) {}\n" +
206
                "     }\n" +
207
                "}\n",
208
                "AddParameterOrLocalFix:ttt:int:false",
209
                ("package test;\n" +
210
                "public class Test {\n" +
211
                "     public void test() {\n" +
212
                "         int[] a = null;\n" +
213
                "         for (int ttt : a) {}\n" +
214
                "     }\n" +
215
                "}\n").replaceAll("[ \t\n]+", " "));
216
    }
217
140
    protected List<Fix> computeFixes(CompilationInfo info, int pos, TreePath path) throws IOException {
218
    protected List<Fix> computeFixes(CompilationInfo info, int pos, TreePath path) throws IOException {
141
        List<Fix> fixes = CreateElement.analyze(info, pos);
219
        List<Fix> fixes = CreateElement.analyze(info, pos);
142
        List<Fix> result=  new LinkedList<Fix>();
220
        List<Fix> result=  new LinkedList<Fix>();

Return to bug 142381