matthiasblaesing closed pull request #315: Fix ClassCastException:
com.sun.tools.javac.tree.JCTree$JCCase cannot be cast to
com.sun.source.tree.BlockTree
URL: https://github.com/apache/incubator-netbeans/pull/315
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/java.hints/src/org/netbeans/modules/java/hints/introduce/IntroduceFieldFix.java
b/java.hints/src/org/netbeans/modules/java/hints/introduce/IntroduceFieldFix.java
index ef0e2a480..cc36bbc66 100644
---
a/java.hints/src/org/netbeans/modules/java/hints/introduce/IntroduceFieldFix.java
+++
b/java.hints/src/org/netbeans/modules/java/hints/introduce/IntroduceFieldFix.java
@@ -24,7 +24,6 @@
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.ModifiersTree;
-import com.sun.source.tree.NewArrayTree;
import com.sun.source.tree.Scope;
import com.sun.source.tree.StatementTree;
import com.sun.source.tree.Tree;
@@ -45,12 +44,9 @@
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Modifier;
-import javax.lang.model.element.TypeElement;
-import javax.lang.model.type.ArrayType;
import javax.lang.model.type.TypeMirror;
import javax.swing.JButton;
import javax.swing.text.BadLocationException;
-import org.netbeans.api.java.source.ElementHandle;
import org.netbeans.api.java.source.JavaSource;
import org.netbeans.api.java.source.SourceUtils;
import org.netbeans.api.java.source.Task;
@@ -105,6 +101,7 @@ public IntroduceFieldFix(TreePathHandle handle, JavaSource
js, String guessedNam
this.targetHandle = target;
}
+ @Override
public String getText() {
return NbBundle.getMessage(IntroduceHint.class, "FIX_IntroduceField");
}
@@ -135,6 +132,7 @@ protected TreePath findTargetClass(WorkingCopy copy,
TreePath resolved) {
return pathToClass;
}
+ @Override
public ChangeInfo implement() throws IOException, BadLocationException {
JButton btnOk = new JButton(NbBundle.getMessage(IntroduceHint.class,
"LBL_Ok"));
btnOk.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(IntroduceHint.class,
"AD_IntrHint_OK"));
@@ -164,7 +162,7 @@ public ChangeInfo implement() throws IOException,
BadLocationException {
* The actual modification. Some javac related data are recorded in
fields, inner class prevents
* unintentional leak if someone keeps a reference to the Fix
*/
- final class Worker implements Task<WorkingCopy> {
+ private final class Worker implements Task<WorkingCopy> {
final String name;
final boolean replaceAll;
final boolean declareFinal;
@@ -198,13 +196,7 @@ private boolean initializeFromMethod(WorkingCopy
parameter, TreePath resolved,
return false;
}
ExpressionStatementTree assignment =
make.ExpressionStatement(make.Assignment(make.Identifier(name), expression));
- BlockTree statements = (BlockTree)
statementPath.getParentPath().getLeaf();
StatementTree statement = (StatementTree) statementPath.getLeaf();
- int index = statements.getStatements().indexOf(statement);
- if (index == (-1)) {
- //really strange...
- return false;
- }
insertStatement(parameter, statementPath.getParentPath(),
statement, assignment, true);
return true;
}
@@ -215,7 +207,7 @@ private boolean initializeConstructors(WorkingCopy
parameter,
for (TreePath constructor : TreeUtils.findConstructors(parameter,
method)) {
//check for syntetic constructor:
if (parameter.getTreeUtilities().isSynthetic(constructor)) {
- List<StatementTree> nueStatements = new
LinkedList<StatementTree>();
+ List<StatementTree> nueStatements = new LinkedList<>();
ExpressionTree reference = make.Identifier(name);
Element clazz =
parameter.getTrees().getElement(pathToClass);
ModifiersTree constrMods = (clazz == null ||
clazz.getKind() != ElementKind.ENUM) ?
make.Modifiers(EnumSet.of(Modifier.PUBLIC)) :
make.Modifiers(Collections.EMPTY_SET);
@@ -264,6 +256,7 @@ private void insertStatement(WorkingCopy parameter,
}
}
+ @Override
public void run(WorkingCopy parameter) throws Exception {
parameter.toPhase(JavaSource.Phase.RESOLVED);
TreePath resolved = handle.resolve(parameter);
@@ -297,9 +290,9 @@ public void run(WorkingCopy parameter) throws Exception {
mods.addAll(access);
final TreeMaker make = parameter.getTreeMaker();
boolean isAnyOccurenceStatic = false;
- Set<Tree> allNewUses = Collections.newSetFromMap(new
IdentityHashMap<Tree, Boolean>());
+ Set<Tree> allNewUses = Collections.newSetFromMap(new
IdentityHashMap<>());
allNewUses.add(resolved.getLeaf());
- Collection<TreePath> duplicates = new ArrayList<TreePath>();
+ Collection<TreePath> duplicates = new ArrayList<>();
if (replaceAll) {
for (TreePath p : SourceUtils.computeDuplicates(parameter,
resolved, new TreePath(parameter.getCompilationUnit()), new AtomicBoolean())) {
if (variableRewrite) {
diff --git
a/java.hints/test/unit/src/org/netbeans/modules/java/hints/introduce/IntroduceHintTest.java
b/java.hints/test/unit/src/org/netbeans/modules/java/hints/introduce/IntroduceHintTest.java
index 66f4bab26..c7d87bfad 100644
---
a/java.hints/test/unit/src/org/netbeans/modules/java/hints/introduce/IntroduceHintTest.java
+++
b/java.hints/test/unit/src/org/netbeans/modules/java/hints/introduce/IntroduceHintTest.java
@@ -1634,6 +1634,31 @@ public void testLocalVariableToField201759c() throws
Exception {
3, 1);
}
+ public void testLocalVariableToFieldInitMethod270296() throws Exception {
+ performFixTest("package test;\n" +
+ "public class Test {\n" +
+ " public void test(int i) {\n" +
+ " switch (i) {\n" +
+ " case 0:\n" +
+ " |String str = \"test\";|\n" +
+ " }\n" +
+ " }\n" +
+ "}",
+ ("package test;\n" +
+ "public class Test {\n" +
+ " private String str;\n" +
+ " public void test(int i) {\n" +
+ " switch (i) {\n" +
+ " case 0:\n" +
+ " str = \"test\";\n" +
+ " }\n" +
+ " }\n" +
+ "}").replaceAll("[ \t\n]+", " "),
+ new DialogDisplayerImpl2(null,
IntroduceFieldPanel.INIT_METHOD, false,
+ EnumSet.of(Modifier.PRIVATE),
false, true),
+ 3, 1);
+ }
+
public void testLocalVariableToConstant1() throws Exception {
performFixTest("package test;\n" +
"import java.util.ArrayList;\n" +
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists