Skip to content

Commit

Permalink
Merge branch 'master' into explicitencoding
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur authored Nov 9, 2024
2 parents 368db46 + 0871ea2 commit 3190ade
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Pattern;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;

import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
import org.eclipse.jdt.internal.corext.fix.RenameUnusedVariableFixCore;
import org.eclipse.jdt.internal.corext.fix.UnusedCodeFixCore;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;

import org.eclipse.jdt.ui.cleanup.CleanUpRequirements;
import org.eclipse.jdt.ui.cleanup.ICleanUpFix;
Expand Down Expand Up @@ -150,9 +147,7 @@ public int computeNumberOfFixes(CompilationUnit compilationUnit) {
if (id == IProblem.LocalVariableIsNeverUsed) {
ProblemLocation location= new ProblemLocation(problem);
SimpleName name= UnusedCodeFixCore.getUnusedName(compilationUnit, location);
if (JavaModelUtil.is22OrHigher(compilationUnit.getJavaElement().getJavaProject()) &&
name.getParent() instanceof SingleVariableDeclaration nameParent &&
nameParent.getParent() instanceof Pattern) {
if (RenameUnusedVariableFixCore.canRenameToUnnamedVariable(compilationUnit, name)) {
result++;
}
} else if (id == IProblem.LambdaParameterIsNeverUsed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Pattern;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;

import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
import org.eclipse.jdt.internal.corext.fix.RenameUnusedVariableFixCore;
import org.eclipse.jdt.internal.corext.fix.UnusedCodeFixCore;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;

import org.eclipse.jdt.ui.cleanup.CleanUpRequirements;
import org.eclipse.jdt.ui.cleanup.ICleanUpFix;
Expand Down Expand Up @@ -277,9 +275,7 @@ public int computeNumberOfFixes(CompilationUnit compilationUnit) {
if (id == IProblem.LocalVariableIsNeverUsed) {
ProblemLocation location= new ProblemLocation(problem);
SimpleName name= UnusedCodeFixCore.getUnusedName(compilationUnit, location);
if (!JavaModelUtil.is22OrHigher(compilationUnit.getJavaElement().getJavaProject()) ||
!(name.getParent() instanceof SingleVariableDeclaration nameParent) ||
!(nameParent.getParent() instanceof Pattern)) {
if (!RenameUnusedVariableFixCore.canRenameToUnnamedVariable(compilationUnit, name)) {
result++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.EnhancedForStatement;
import org.eclipse.jdt.core.dom.ForStatement;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.LambdaExpression;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Pattern;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.TryStatement;
import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;

import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;

Expand Down Expand Up @@ -78,15 +80,10 @@ public static RenameUnusedVariableFixCore createRenameToUnnamedFix(CompilationUn
if (name != null) {
IBinding binding= name.resolveBinding();
if (binding != null) {
if (JavaModelUtil.is22OrHigher(compilationUnit.getJavaElement().getJavaProject())) {
if (name.getParent() instanceof SingleVariableDeclaration parent &&
parent.getParent() instanceof Pattern ||
name.getParent() instanceof VariableDeclarationFragment parent2 &&
parent2.getParent() instanceof LambdaExpression) {
String label= FixMessages.UnusedCodeFix_RenameToUnnamedVariable_description;
RenameToUnnamedVariableOperation operation= new RenameToUnnamedVariableOperation(name);
return new RenameUnusedVariableFixCore(label, compilationUnit, new CompilationUnitRewriteOperation[] { operation }, getCleanUpOptions());
}
if (canRenameToUnnamedVariable(compilationUnit, name)) {
String label= FixMessages.UnusedCodeFix_RenameToUnnamedVariable_description;
RenameToUnnamedVariableOperation operation= new RenameToUnnamedVariableOperation(name);
return new RenameUnusedVariableFixCore(label, compilationUnit, new CompilationUnitRewriteOperation[] { operation }, getCleanUpOptions());
}
}
}
Expand Down Expand Up @@ -131,16 +128,8 @@ public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, IProble
if (name != null) {
IBinding binding= name.resolveBinding();
if (binding instanceof IVariableBinding) {
VariableDeclarationFragment parent= ASTNodes.getParent(name, VariableDeclarationFragment.class);
if (parent == null || id == IProblem.LambdaParameterIsNeverUsed) {
if (JavaModelUtil.is22OrHigher(compilationUnit.getJavaElement().getJavaProject())) {
if (name.getParent() instanceof SingleVariableDeclaration nameParent &&
nameParent.getParent() instanceof Pattern ||
name.getParent() instanceof VariableDeclarationFragment varFragment &&
varFragment.getParent() instanceof LambdaExpression) {
result.add(new RenameToUnnamedVariableOperation(name));
}
}
if (canRenameToUnnamedVariable(compilationUnit, name)) {
result.add(new RenameToUnnamedVariableOperation(name));
}
}
}
Expand All @@ -153,8 +142,23 @@ public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, IProble
return new RenameUnusedVariableFixCore(FixMessages.UnusedCodeFix_change_name, compilationUnit, result.toArray(new CompilationUnitRewriteOperation[result.size()]));
}

public static boolean isFormalParameterInEnhancedForStatement(SimpleName name) {
return name.getParent() instanceof SingleVariableDeclaration && name.getParent().getLocationInParent() == EnhancedForStatement.PARAMETER_PROPERTY;
public static boolean canRenameToUnnamedVariable(CompilationUnit compilationUnit, SimpleName name) {
if (JavaModelUtil.is22OrHigher(compilationUnit.getJavaElement().getJavaProject())) {
if (name.getParent() instanceof SingleVariableDeclaration nameParent) {
if (nameParent.getParent() instanceof Pattern || nameParent.getParent() instanceof EnhancedForStatement) {
return true;
}
} else if (name.getParent() instanceof VariableDeclarationFragment varFragment) {
if (varFragment.getParent() instanceof LambdaExpression) {
return true;
} else if (varFragment.getParent() instanceof VariableDeclarationExpression varFragmentParent) {
if (varFragmentParent.getParent() instanceof TryStatement || varFragmentParent.getParent() instanceof ForStatement) {
return true;
}
}
}
}
return false;
}

public static SimpleName getUnusedName(CompilationUnit compilationUnit, IProblemLocation problem) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.NodeFinder;
import org.eclipse.jdt.core.dom.ParenthesizedExpression;
import org.eclipse.jdt.core.dom.Pattern;
import org.eclipse.jdt.core.dom.PostfixExpression;
import org.eclipse.jdt.core.dom.PrefixExpression;
import org.eclipse.jdt.core.dom.QualifiedName;
Expand All @@ -90,7 +89,6 @@
import org.eclipse.jdt.internal.corext.dom.ReplaceRewrite;
import org.eclipse.jdt.internal.corext.dom.StatementRewrite;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.corext.util.Messages;

import org.eclipse.jdt.ui.cleanup.CleanUpOptions;
Expand Down Expand Up @@ -930,7 +928,7 @@ public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, IProble

if ((removeUnusedLocalVariables && id == IProblem.LocalVariableIsNeverUsed) || (removeUnusedPrivateFields && id == IProblem.UnusedPrivateField)) {
SimpleName name= getUnusedName(compilationUnit, problem);
if (name != null) {
if (name != null && !RenameUnusedVariableFixCore.canRenameToUnnamedVariable(compilationUnit, name)) {
IBinding binding= name.resolveBinding();
if (binding instanceof IVariableBinding && !isFormalParameterInEnhancedForStatement(name) && (!((IVariableBinding) binding).isField() || isSideEffectFree(name, compilationUnit))) {
VariableDeclarationFragment parent= ASTNodes.getParent(name, VariableDeclarationFragment.class);
Expand All @@ -941,16 +939,7 @@ public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, IProble
}
variableDeclarations.get(varDecl).add(name);
} else {
if (id == IProblem.LocalVariableIsNeverUsed) {
SimpleName nameNode= UnusedCodeFixCore.getUnusedName(compilationUnit, problem);
if (!JavaModelUtil.is22OrHigher(compilationUnit.getJavaElement().getJavaProject()) ||
!(nameNode.getParent() instanceof SingleVariableDeclaration nameParent) ||
!(nameParent.getParent() instanceof Pattern)) {
result.add(new RemoveUnusedMemberOperation(new SimpleName[] { name }, false));
}
} else {
result.add(new RemoveUnusedMemberOperation(new SimpleName[] { name }, false));
}
result.add(new RemoveUnusedMemberOperation(new SimpleName[] { name }, false));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,161 @@ case R(_, long _) -> {}
assertEqualString(preview1, expected1);
}

@Test
public void testRenameToUnnamedProposal3() throws Exception {
fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin");
fJProject1.setRawClasspath(projectsetup.getDefaultClasspath(), null);
JavaProjectHelper.set22CompilerOptions(fJProject1);

fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src");
IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);

String test= """
package test;
public class Unused {
public static void main(String[] args) {
for (String arg : args) {
System.out.println("abc");
}
}
}
""";

ICompilationUnit cu= pack1.createCompilationUnit("Unused.java", test, false, null);

CompilationUnit astRoot= getASTRoot(cu);
ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot, 1);
assertCorrectLabels(proposals);

CUCorrectionProposal proposal1= (CUCorrectionProposal) proposals.get(0);
String preview1= getPreviewContent(proposal1);

String expected1= """
package test;
public class Unused {
public static void main(String[] args) {
for (String _ : args) {
System.out.println("abc");
}
}
}
""";

assertEqualString(preview1, expected1);
}

@Test
public void testRenameToUnnamedProposal4() throws Exception {
fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin");
fJProject1.setRawClasspath(projectsetup.getDefaultClasspath(), null);
JavaProjectHelper.set22CompilerOptions(fJProject1);

fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src");
IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);

String test= """
package test;
import java.io.FileInputStream;
public class Unused {
public static void main(String[] args) {
try (FileInputStream x = new FileInputStream("a.b")) {
System.out.println("abc");
} catch (Exception e) {
}
}
}
""";

ICompilationUnit cu= pack1.createCompilationUnit("Unused.java", test, false, null);

CompilationUnit astRoot= getASTRoot(cu);
ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot, 1);
assertCorrectLabels(proposals);

CUCorrectionProposal proposal1= (CUCorrectionProposal) proposals.get(0);
String preview1= getPreviewContent(proposal1);

String expected1= """
package test;
import java.io.FileInputStream;
public class Unused {
public static void main(String[] args) {
try (FileInputStream _ = new FileInputStream("a.b")) {
System.out.println("abc");
} catch (Exception e) {
}
}
}
""";

assertEqualString(preview1, expected1);
}

@Test
public void testRenameToUnnamedProposal5() throws Exception {
fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin");
fJProject1.setRawClasspath(projectsetup.getDefaultClasspath(), null);
JavaProjectHelper.set22CompilerOptions(fJProject1);

fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src");
IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);

String test= """
package test;
public class Unused {
private static int x = 1;
private static int sideEffect() {
return ++x;
}
public static void main(String[] args) {
for (int i = 0, se = sideEffect(); i < 9; ++i) {
System.out.println("abc");
}
}
}
""";

ICompilationUnit cu= pack1.createCompilationUnit("Unused.java", test, false, null);

CompilationUnit astRoot= getASTRoot(cu);
ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot, 1);
assertCorrectLabels(proposals);

CUCorrectionProposal proposal1= (CUCorrectionProposal) proposals.get(0);
String preview1= getPreviewContent(proposal1);

String expected1= """
package test;
public class Unused {
private static int x = 1;
private static int sideEffect() {
return ++x;
}
public static void main(String[] args) {
for (int i = 0, _ = sideEffect(); i < 9; ++i) {
System.out.println("abc");
}
}
}
""";

assertEqualString(preview1, expected1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@

import org.eclipse.jdt.internal.ui.search.SearchMessages;
import org.eclipse.jdt.internal.ui.text.JavaWordFinder;
import org.eclipse.jdt.internal.ui.util.SelectionUtil;


/**
Expand All @@ -75,7 +76,6 @@ public class JavaElementHyperlinkDetector extends AbstractHyperlinkDetector {
private static IRegion fLastWordRegion;

private static IJavaElement[] fLastElements;

/*
* @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion, boolean)
*/
Expand All @@ -99,14 +99,13 @@ private IHyperlink[] detectHyperlinksCached(IRegion region) {
if (input == null)
return null;

IDocumentProvider documentProvider= textEditor.getDocumentProvider();
IEditorInput editorInput= textEditor.getEditorInput();
IDocument document= documentProvider.getDocument(editorInput);
IRegion wordRegion= JavaWordFinder.findWord(document, offset);
if (wordRegion == null || wordRegion.getLength() == 0)
return null;
try {
IDocumentProvider documentProvider= textEditor.getDocumentProvider();
IEditorInput editorInput= textEditor.getEditorInput();
IDocument document= documentProvider.getDocument(editorInput);
IRegion wordRegion= JavaWordFinder.findWord(document, offset);
if (wordRegion == null || wordRegion.getLength() == 0)
return null;

if (isInheritDoc(document, wordRegion) && getClass() != JavaElementHyperlinkDetector.class)
return null;

Expand Down Expand Up @@ -143,7 +142,9 @@ private IHyperlink[] detectHyperlinksCached(IRegion region) {
return null;

return CollectionsUtil.toArray(links, IHyperlink.class);

} catch (RuntimeException e) {
SelectionUtil.logException("computing hyperlink", e, textEditor.getTitle(), document, offset); //$NON-NLS-1$
return null;
} catch (JavaModelException e) {
return null;
}
Expand Down
Loading

0 comments on commit 3190ade

Please sign in to comment.