diff -r 5c92d99923a1 java.hints/nbproject/project.properties --- a/java.hints/nbproject/project.properties Mon Apr 13 02:10:26 2009 +0400 +++ b/java.hints/nbproject/project.properties Mon Apr 13 19:04:46 2009 +0100 @@ -37,6 +37,12 @@ # Version 2 license, then the option applies only if the new code is # made subject to such option by the copyright holder. +auxiliary.org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs=true +auxiliary.org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width=4 +auxiliary.org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab=4 +auxiliary.org-netbeans-modules-editor-indent.CodeStyle.project.tab-size=4 +auxiliary.org-netbeans-modules-editor-indent.CodeStyle.project.text-limit-width=100 +auxiliary.org-netbeans-modules-editor-indent.CodeStyle.usedProfile=project spec.version.base=1.26.0 javac.source=1.5 diff -r 5c92d99923a1 java.hints/src/org/netbeans/modules/java/hints/errors/AddParameterOrLocalFix.java --- a/java.hints/src/org/netbeans/modules/java/hints/errors/AddParameterOrLocalFix.java Mon Apr 13 02:10:26 2009 +0400 +++ b/java.hints/src/org/netbeans/modules/java/hints/errors/AddParameterOrLocalFix.java Mon Apr 13 19:04:46 2009 +0100 @@ -43,6 +43,7 @@ import com.sun.source.tree.AssignmentTree; import com.sun.source.tree.BlockTree; +import com.sun.source.tree.EnhancedForLoopTree; import com.sun.source.tree.ExpressionStatementTree; import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.IdentifierTree; @@ -167,7 +168,7 @@ working.rewrite(targetTree, result); } else { - if (ErrorFixesFakeHint.isCreateLocalVariableInPlace()) { + if (ErrorFixesFakeHint.isCreateLocalVariableInPlace() || isEnhancedForLoopIdentifier(tp)) { resolveLocalVariable(working, tp, make, proposedType); } else { resolveLocalVariable55(working, tp, make, proposedType); @@ -191,6 +192,17 @@ return true; } return false; + } + + // true if tp is an IDENTIFIER in a VARIABLE in an ENHANCED_FOR_LOOP + private boolean isEnhancedForLoopIdentifier(TreePath tp) { + TreePath parent = tp.getParentPath(); + if (parent == null || tp.getLeaf().getKind() != Kind.IDENTIFIER) + return false; + TreePath context = parent.getParentPath(); + if (context == null || context.getLeaf().getKind() != Kind.ENHANCED_FOR_LOOP) + return false; + return parent.getLeaf().equals(((EnhancedForLoopTree) context.getLeaf()).getVariable()); } private void resolveLocalVariable55(final WorkingCopy wc, TreePath tp, TreeMaker make, TypeMirror proposedType) { @@ -298,7 +310,9 @@ Tree statementParent = firstUse.getParentPath().getLeaf(); VariableTree vt = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), name, make.Type(proposedType), null); - if (statementParent.getKind() == Kind.BLOCK) { + if (isEnhancedForLoopIdentifier(tp)) { + wc.rewrite(tp.getParentPath().getLeaf(), vt); + } else if (statementParent.getKind() == Kind.BLOCK) { BlockTree block = (BlockTree) statementParent; FirstUsage fu = new FirstUsage(); diff -r 5c92d99923a1 java.hints/src/org/netbeans/modules/java/hints/errors/CreateElementUtilities.java --- a/java.hints/src/org/netbeans/modules/java/hints/errors/CreateElementUtilities.java Mon Apr 13 02:10:26 2009 +0400 +++ b/java.hints/src/org/netbeans/modules/java/hints/errors/CreateElementUtilities.java Mon Apr 13 19:04:46 2009 +0100 @@ -93,6 +93,7 @@ import org.netbeans.modules.java.editor.semantic.Utilities; import org.netbeans.modules.java.hints.infrastructure.ErrorHintsProvider; import org.openide.ErrorManager; +import static org.netbeans.modules.java.hints.errors.Utilities.resolveCapturedType; /** * @@ -457,12 +458,40 @@ return Collections.singletonList(info.getTrees().getTypeMirror(new TreePath(parent, vt.getType()))); } - if (vt.getType() == error) { - types.add(ElementKind.CLASS); - return Collections.emptyList(); + TreePath context = parent.getParentPath(); + if (vt.getType() != error || context == null) { + return null; } - return null; + switch (context.getLeaf().getKind()) { + case ENHANCED_FOR_LOOP: + TypeElement iterableElement = info.getElements().getTypeElement("java.lang.Iterable"); //NOI18N + if (iterableElement == null) + return null; + ExpressionTree iterable = ((EnhancedForLoopTree) context.getLeaf()).getExpression(); + TypeMirror iterableType = info.getTrees().getTypeMirror(new TreePath(context, iterable)); + if (iterableType == null) + return null; + TypeMirror designedType = null; + if (iterableType.getKind() == TypeKind.DECLARED) { + DeclaredType iterableDeclaredType = (DeclaredType) iterableType; + if (!info.getTypes().isSubtype(info.getTypes().erasure(iterableDeclaredType), info.getTypes().erasure(iterableElement.asType()))) + return null; + ExecutableElement iteratorMethod = (ExecutableElement) iterableElement.getEnclosedElements().get(0); //XXX + ExecutableType iteratorMethodType = (ExecutableType) info.getTypes().asMemberOf(iterableDeclaredType, iteratorMethod); + designedType = ((DeclaredType) iteratorMethodType.getReturnType()).getTypeArguments().get(0); //XXX: assuming the method are correct... + } else if (iterableType.getKind() == TypeKind.ARRAY) { + designedType = ((ArrayType) iterableType).getComponentType(); + } + if (designedType == null) + return null; + designedType = resolveCapturedType(info, designedType); + types.add(ElementKind.LOCAL_VARIABLE); + return Collections.singletonList(designedType); + default: + types.add(ElementKind.CLASS); + return Collections.emptyList(); + } } private static List computeAssert(Set types, CompilationInfo info, TreePath parent, Tree error, int offset) {