diff -r 751e1d3a8c65 java.hints/src/org/netbeans/modules/java/hints/errors/AddParameterOrLocalFix.java --- a/java.hints/src/org/netbeans/modules/java/hints/errors/AddParameterOrLocalFix.java Sat Apr 11 01:04:14 2009 +0200 +++ b/java.hints/src/org/netbeans/modules/java/hints/errors/AddParameterOrLocalFix.java Mon Apr 13 14:09: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; @@ -298,7 +299,10 @@ 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 (statement.getKind() == Kind.ENHANCED_FOR_LOOP && + tp.getParentPath().getLeaf().equals(((EnhancedForLoopTree) statement).getVariable())) { + wc.rewrite(tp.getParentPath().getLeaf(), vt); + } else if (statementParent.getKind() == Kind.BLOCK) { BlockTree block = (BlockTree) statementParent; FirstUsage fu = new FirstUsage(); diff -r 751e1d3a8c65 java.hints/src/org/netbeans/modules/java/hints/errors/CreateElementUtilities.java --- a/java.hints/src/org/netbeans/modules/java/hints/errors/CreateElementUtilities.java Sat Apr 11 01:04:14 2009 +0200 +++ b/java.hints/src/org/netbeans/modules/java/hints/errors/CreateElementUtilities.java Mon Apr 13 14:09: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,41 @@ 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 + 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) {