Skip to content

Commit

Permalink
Handle potential NumberFormatException in chain completion settings.
Browse files Browse the repository at this point in the history
- More generally, handling of improper values in certain cases where the
  JDT UI preference initialization fails to set preferences

Signed-off-by: Roland Grunberg <rgrunber@redhat.com>
  • Loading branch information
rgrunber authored and jukzi committed Feb 15, 2024
1 parent d843e10 commit fbb9338
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@

import org.eclipse.swt.SWT;

import org.eclipse.core.runtime.preferences.IEclipsePreferences;

import org.eclipse.core.resources.ProjectScope;

import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
Expand All @@ -42,7 +46,9 @@
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.manipulation.JavaManipulation;

import org.eclipse.jdt.ui.PreferenceConstants;
import org.eclipse.jdt.ui.tests.core.rules.ProjectTestSetup;
import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext;

Expand Down Expand Up @@ -87,6 +93,27 @@ public void testNullExpectedType() throws Exception {
assertEquals(0, proposals.size());
}

@Test
public void testInvalidPreferenceHandling() throws Exception {
IEclipsePreferences node= new ProjectScope(fJProject.getProject()).getNode(JavaManipulation.getPreferenceNodeId());
node.put(PreferenceConstants.PREF_MAX_CHAIN_LENGTH, "number_four");

StringBuffer buf= new StringBuffer();
buf.append("package test;\n" +
"public class Foo {\n" +
" public void foo () {\n" +
" String s = \"\";\n" +
" int length = $\n" +
" }\n" +
"}");

int completionIndex= getCompletionIndex(buf);
ICompilationUnit cu= getCompilationUnit(pkg, buf, "Foo.java");

List<ICompletionProposal> proposals= computeCompletionProposals(cu, completionIndex);
assertEquals(0, proposals.size());
}

@Test
public void testBasic() throws Exception {
StringBuffer buf= new StringBuffer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,21 @@ private boolean matchesExpectedPrefix(final IJavaElement element) {
}

private List<ICompletionProposal> executeCallChainSearch() {
final int maxChains= Integer.parseInt(JavaManipulation.getPreference(PreferenceConstants.PREF_MAX_CHAINS, ctx.getProject()));
final int minDepth= Integer.parseInt(JavaManipulation.getPreference(PreferenceConstants.PREF_MIN_CHAIN_LENGTH, ctx.getProject()));
final int maxDepth= Integer.parseInt(JavaManipulation.getPreference(PreferenceConstants.PREF_MAX_CHAIN_LENGTH, ctx.getProject()));
final int maxChains, minDepth, maxDepth;
try {
maxChains= Integer.parseInt(JavaManipulation.getPreference(PreferenceConstants.PREF_MAX_CHAINS, ctx.getProject()));
minDepth= Integer.parseInt(JavaManipulation.getPreference(PreferenceConstants.PREF_MIN_CHAIN_LENGTH, ctx.getProject()));
maxDepth= Integer.parseInt(JavaManipulation.getPreference(PreferenceConstants.PREF_MAX_CHAIN_LENGTH, ctx.getProject()));
} catch (NumberFormatException e) {
return Collections.emptyList();
}

excludedTypes= JavaManipulation.getPreference(PreferenceConstants.PREF_CHAIN_IGNORED_TYPES, ctx.getProject()).split("\\|"); //$NON-NLS-1$
for (int i= 0; i < excludedTypes.length; ++i) {
excludedTypes[i]= "L" + excludedTypes[i].replace('.', '/'); //$NON-NLS-1$
String excludedTypesVal = JavaManipulation.getPreference(PreferenceConstants.PREF_CHAIN_IGNORED_TYPES, ctx.getProject());
if (excludedTypesVal != null) {
excludedTypes= excludedTypesVal.split("\\|"); //$NON-NLS-1$
for (int i= 0; i < excludedTypes.length; ++i) {
excludedTypes[i]= "L" + excludedTypes[i].replace('.', '/'); //$NON-NLS-1$
}
}

final IType invocationType= ctx.getCompilationUnit().findPrimaryType();
Expand All @@ -172,7 +180,13 @@ private List<ICompletionProposal> executeCallChainSearch() {
finder.startChainSearch(entrypoints, maxChains, minDepth, maxDepth);
}
});
long timeout= Long.parseLong(JavaManipulation.getPreference(PreferenceConstants.PREF_CHAIN_TIMEOUT, ctx.getProject()));

long timeout;
try {
timeout= Long.parseLong(JavaManipulation.getPreference(PreferenceConstants.PREF_CHAIN_TIMEOUT, ctx.getProject()));
} catch (NumberFormatException e) {
timeout = 1;
}
future.get(timeout, TimeUnit.SECONDS);
} catch (final Exception e) {
finder.cancel();
Expand Down

0 comments on commit fbb9338

Please sign in to comment.