Skip to content

Commit 7974e08

Browse files
authored
Merge pull request #8713 from lahodaj/fix-create-element
Fixing problems related to create element fixes.
2 parents 25ab177 + 539dc57 commit 7974e08

File tree

9 files changed

+371
-16
lines changed

9 files changed

+371
-16
lines changed

java/java.hints/src/org/netbeans/modules/java/hints/errors/AddParameterOrLocalFix.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.sun.source.tree.BlockTree;
2424
import com.sun.source.tree.ExpressionStatementTree;
2525
import com.sun.source.tree.ExpressionTree;
26+
import com.sun.source.tree.ForLoopTree;
2627
import com.sun.source.tree.IdentifierTree;
2728
import com.sun.source.tree.MethodTree;
2829
import com.sun.source.tree.StatementTree;
@@ -33,6 +34,7 @@
3334
import org.netbeans.api.java.source.support.ErrorAwareTreePathScanner;
3435
import java.util.Arrays;
3536
import java.util.EnumSet;
37+
import java.util.List;
3638
import java.util.Set;
3739
import java.util.logging.Level;
3840
import java.util.logging.Logger;
@@ -93,6 +95,7 @@ public String getText() {
9395
case LOCAL_VARIABLE: return NbBundle.getMessage(AddParameterOrLocalFix.class, "LBL_FIX_Create_Local_Variable", name); // NOI18N
9496
case PARAMETER: return NbBundle.getMessage(AddParameterOrLocalFix.class, "LBL_FIX_Create_Parameter", name); // NOI18N
9597
case RESOURCE_VARIABLE: return NbBundle.getMessage(AddParameterOrLocalFix.class, "LBL_FIX_Create_Resource", name); // NOI18N
98+
case OTHER: return NbBundle.getMessage(AddParameterOrLocalFix.class, "LBL_FIX_For_Init_Variable", name); // NOI18N
9699
default:
97100
throw new IllegalStateException(kind.name());
98101
}
@@ -155,6 +158,9 @@ protected void performRewrite(TransformationContext ctx) throws Exception {
155158
case RESOURCE_VARIABLE:
156159
resolveResourceVariable(working, tp, make, proposedType);
157160
break;
161+
case OTHER:
162+
resolveForInitVariable(working, tp, make, proposedType);
163+
break;
158164
default:
159165
throw new IllegalStateException(kind.name());
160166
}
@@ -374,6 +380,26 @@ private void resolveResourceVariable(final WorkingCopy wc, TreePath tp, TreeMake
374380
wc.rewrite(at, vt);
375381
}
376382

383+
private void resolveForInitVariable(final WorkingCopy wc, TreePath tp, TreeMaker make, TypeMirror proposedType) {
384+
final String name = ((IdentifierTree) tp.getLeaf()).getName().toString();
385+
386+
final Element el = wc.getTrees().getElement(tp);
387+
if (el == null) {
388+
return;
389+
}
390+
391+
if (tp.getParentPath().getLeaf().getKind() != Kind.ASSIGNMENT ||
392+
tp.getParentPath().getParentPath().getLeaf().getKind() != Kind.EXPRESSION_STATEMENT) {
393+
//?
394+
return ;
395+
}
396+
397+
AssignmentTree at = (AssignmentTree) tp.getParentPath().getLeaf();
398+
VariableTree vt = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), name, make.Type(proposedType), at.getExpression());
399+
400+
wc.rewrite(tp.getParentPath().getParentPath().getLeaf(), vt);
401+
}
402+
377403
private TreePath findStatement(TreePath tp) {
378404
TreePath statement = tp;
379405

@@ -492,6 +518,7 @@ private static String getSortText(ElementKind kind, String name) {
492518
case PARAMETER: return "Create 7000 " + name;
493519
case LOCAL_VARIABLE: return "Create 5000 " + name;
494520
case RESOURCE_VARIABLE: return "Create 3000 " + name;
521+
case OTHER: return "Create 3000 " + name;
495522
default:
496523
throw new IllegalStateException();
497524
}

java/java.hints/src/org/netbeans/modules/java/hints/errors/Bundle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ LBL_FIX_Create_Parameter=Create parameter "{0}"
141141
LBL_FIX_Create_Local_Variable=Create local variable "{0}"
142142
#{0}: variable name
143143
LBL_FIX_Create_Resource=Create try-with-resource resource "{0}"
144+
#{0}: variable name
145+
LBL_FIX_For_Init_Variable=Create for init variable "{0}"
144146

145147
#NotInitializedVariable
146148
LBL_NotInitializedVariable=Use of uninitialized variable

java/java.hints/src/org/netbeans/modules/java/hints/errors/CreateElement.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ private static List<Fix> analyzeImpl(CompilationInfo info, String diagnosticKey,
489489
}
490490
}
491491

492-
if (!wasMemberSelect && (fixTypes.contains(ElementKind.LOCAL_VARIABLE) || fixTypes.contains(ElementKind.PARAMETER) || fixTypes.contains(ElementKind.RESOURCE_VARIABLE))) {
492+
if (!wasMemberSelect && (fixTypes.contains(ElementKind.LOCAL_VARIABLE) || fixTypes.contains(ElementKind.PARAMETER) || fixTypes.contains(ElementKind.RESOURCE_VARIABLE) || fixTypes.contains(ElementKind.OTHER))) {
493493
ExecutableElement ee = null;
494494

495495
if (firstMethod != null) {
@@ -503,6 +503,8 @@ private static List<Fix> analyzeImpl(CompilationInfo info, String diagnosticKey,
503503
result.add(new AddParameterOrLocalFix(info, type, simpleName, ElementKind.LOCAL_VARIABLE, identifierPos).toEditorFix());
504504
if (fixTypes.contains(ElementKind.RESOURCE_VARIABLE))
505505
result.add(new AddParameterOrLocalFix(info, type, simpleName, ElementKind.RESOURCE_VARIABLE, identifierPos).toEditorFix());
506+
if (fixTypes.contains(ElementKind.OTHER))
507+
result.add(new AddParameterOrLocalFix(info, type, simpleName, ElementKind.OTHER, identifierPos).toEditorFix());
506508
}
507509

508510
return result;

java/java.hints/src/org/netbeans/modules/java/hints/errors/CreateElementUtilities.java

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import com.sun.source.tree.UnaryTree;
5454
import com.sun.source.tree.VariableTree;
5555
import com.sun.source.tree.WhileLoopTree;
56+
import com.sun.source.tree.YieldTree;
5657
import com.sun.source.util.TreePath;
5758
import java.io.IOException;
5859
import java.util.ArrayList;
@@ -133,7 +134,11 @@ public static List<? extends TypeMirror> resolveType(Set<ElementKind> types, Com
133134
return Collections.singletonList(info.getTypes().getNoType(TypeKind.VOID));
134135

135136
case RETURN:
136-
return computeReturn(types, info, currentPath, unresolved, offset);
137+
return computeReturn(types, info, currentPath, unresolved);
138+
case YIELD:
139+
return computeYield(types, info, currentPath, unresolved, offset);
140+
case CASE:
141+
return computeCase(types, info, currentPath, unresolved, offset);
137142
case TYPE_PARAMETER:
138143
return computeTypeParameter(types, info, currentPath, unresolved, offset);
139144
case PARAMETERIZED_TYPE:
@@ -208,7 +213,7 @@ public static List<? extends TypeMirror> resolveType(Set<ElementKind> types, Com
208213
return computeImport(types, info, currentPath, unresolved, offset);
209214

210215
case LAMBDA_EXPRESSION:
211-
return computeLambdaRetrun(types, info, currentPath, unresolved, offset);
216+
return computeLambdaReturn(types, info, currentPath, unresolved, offset);
212217

213218
case BLOCK:
214219
case BREAK:
@@ -234,8 +239,7 @@ public static List<? extends TypeMirror> resolveType(Set<ElementKind> types, Com
234239
case NULL_LITERAL:
235240
//ignored:
236241
return null;
237-
238-
case CASE:
242+
239243
case ANNOTATION:
240244
case UNBOUNDED_WILDCARD:
241245
case EXTENDS_WILDCARD:
@@ -401,6 +405,13 @@ private static List<? extends TypeMirror> computeAssignment(Set<ElementKind> typ
401405
types.clear();
402406
types.add(ElementKind.RESOURCE_VARIABLE);
403407
}
408+
if (parent.getParentPath() != null &&
409+
parent.getParentPath().getLeaf().getKind() == Kind.EXPRESSION_STATEMENT &&
410+
parent.getParentPath().getParentPath() != null &&
411+
parent.getParentPath().getParentPath().getLeaf().getKind() == Kind.FOR_LOOP &&
412+
((ForLoopTree) parent.getParentPath().getParentPath().getLeaf()).getInitializer().contains(parent.getParentPath().getLeaf())) {
413+
types.add(ElementKind.OTHER);
414+
}
404415
}
405416

406417
if (at.getExpression() == error) {
@@ -545,17 +556,32 @@ private static List<? extends TypeMirror> computeAssert(Set<ElementKind> types,
545556
return null;
546557
}
547558

548-
private static List<? extends TypeMirror> computeLambdaRetrun(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
559+
private static List<? extends TypeMirror> computeLambdaReturn(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
549560
LambdaExpressionTree let = (LambdaExpressionTree)parent.getLeaf();
550561
if (let.getBody() != error) {
551562
return null;
552563
}
553-
TreePath parentParent = parent.getParentPath();
554-
TypeMirror m = info.getTrees().getTypeMirror(parentParent);
555-
if (org.netbeans.modules.java.hints.errors.Utilities.isValidType(m)) {
556-
return Collections.singletonList(m);
564+
565+
List<TypeMirror> result = new ArrayList<>();
566+
for (TypeMirror target : resolveType(types, info, parent.getParentPath(), let, offset, null, null)) {
567+
if (!org.netbeans.modules.java.hints.errors.Utilities.isValidType(target) ||
568+
target.getKind() != TypeKind.DECLARED) {
569+
continue;
570+
}
571+
572+
DeclaredType declaredTarget = (DeclaredType) target;
573+
ExecutableElement functionalMethod =
574+
info.getElementUtilities()
575+
.getDescriptorElement((TypeElement) (declaredTarget).asElement());
576+
577+
if (functionalMethod == null) {
578+
continue;
579+
}
580+
581+
result.add(((ExecutableType) info.getTypes().asMemberOf(declaredTarget, functionalMethod)).getReturnType());
557582
}
558-
return resolveType(types, info, parentParent, let, offset, null, null);
583+
584+
return result;
559585
}
560586

561587
private static List<? extends TypeMirror> computeParenthesis(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
@@ -672,7 +698,7 @@ private static List<? extends TypeMirror> computeUnary(Set<ElementKind> types, C
672698
return null;
673699
}
674700

675-
private static List<? extends TypeMirror> computeReturn(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
701+
private static List<? extends TypeMirror> computeReturn(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error) {
676702
ReturnTree rt = (ReturnTree) parent.getLeaf();
677703

678704
if (rt.getExpression() == error) {
@@ -698,6 +724,45 @@ private static List<? extends TypeMirror> computeReturn(Set<ElementKind> types,
698724
return null;
699725
}
700726

727+
private static List<? extends TypeMirror> computeYield(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
728+
YieldTree yieldStatement = (YieldTree) parent.getLeaf();
729+
730+
if (yieldStatement.getValue() == error) {
731+
Tree yieldTargetTree = info.getTreeUtilities().getBreakContinueTargetTree(parent);
732+
TreePath switchExpression = parent;
733+
734+
while (switchExpression != null && switchExpression.getLeaf() != yieldTargetTree) {
735+
switchExpression = switchExpression.getParentPath();
736+
}
737+
738+
if (switchExpression == null) {
739+
return null;
740+
}
741+
742+
types.add(ElementKind.PARAMETER);
743+
types.add(ElementKind.LOCAL_VARIABLE);
744+
types.add(ElementKind.FIELD);
745+
746+
return resolveType(types, info, switchExpression.getParentPath(), switchExpression.getLeaf(), offset, null, null);
747+
}
748+
749+
return null;
750+
}
751+
752+
private static List<? extends TypeMirror> computeCase(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
753+
TreePath switchCandidate = parent.getParentPath();
754+
755+
if (switchCandidate.getLeaf().getKind() == Kind.SWITCH_EXPRESSION) {
756+
types.add(ElementKind.PARAMETER);
757+
types.add(ElementKind.LOCAL_VARIABLE);
758+
types.add(ElementKind.FIELD);
759+
760+
return resolveType(types, info, switchCandidate.getParentPath(), switchCandidate.getLeaf(), offset, null, null);
761+
}
762+
763+
return null;
764+
}
765+
701766
private static List<? extends TypeMirror> computeTypeParameter(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
702767
TypeParameterTree tpt = (TypeParameterTree) parent.getLeaf();
703768

0 commit comments

Comments
 (0)