Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle potential NumberFormatException in chain completion settings. #1196

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading