diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/codemanipulation/OverrideMethodsOperation.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/codemanipulation/OverrideMethodsOperation.java index aa01576b55..51c1c059fd 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/codemanipulation/OverrideMethodsOperation.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/codemanipulation/OverrideMethodsOperation.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019 Microsoft Corporation and others. + * Copyright (c) 2019-2020 Microsoft Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 * which accompanies this distribution, and is available at @@ -22,6 +22,8 @@ import java.util.stream.Stream; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaModelException; @@ -38,6 +40,7 @@ import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext; +import org.eclipse.jdt.core.manipulation.CoreASTProvider; import org.eclipse.jdt.core.dom.rewrite.ListRewrite; import org.eclipse.jdt.internal.core.manipulation.StubUtility; import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings; @@ -45,23 +48,28 @@ import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility2Core; import org.eclipse.jdt.internal.corext.dom.ASTNodes; import org.eclipse.jdt.internal.corext.dom.Bindings; -import org.eclipse.jdt.internal.corext.dom.IASTSharedValues; -import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser; import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager; import org.eclipse.text.edits.MultiTextEdit; import org.eclipse.text.edits.TextEdit; public class OverrideMethodsOperation { - + // For test purpose public static List listOverridableMethods(IType type) { + return listOverridableMethods(type, new NullProgressMonitor()); + } + + public static List listOverridableMethods(IType type, IProgressMonitor monitor) { if (type == null || type.getCompilationUnit() == null) { return Collections.emptyList(); } List overridables = new ArrayList<>(); - RefactoringASTParser astParser = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL); - CompilationUnit astRoot = astParser.parse(type.getCompilationUnit(), true); + CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(type.getCompilationUnit(), CoreASTProvider.WAIT_YES, monitor); + if (astRoot == null) { + return Collections.emptyList(); + } + try { ITypeBinding typeBinding = ASTNodes.getTypeBinding(astRoot, type); if (typeBinding == null) { @@ -89,13 +97,21 @@ public static List listOverridableMethods(IType type) { return overridables; } + // For test purpose public static TextEdit addOverridableMethods(IType type, OverridableMethod[] overridableMethods) { + return addOverridableMethods(type, overridableMethods, new NullProgressMonitor()); + } + + public static TextEdit addOverridableMethods(IType type, OverridableMethod[] overridableMethods, IProgressMonitor monitor) { if (type == null || type.getCompilationUnit() == null || overridableMethods == null || overridableMethods.length == 0) { return null; } - RefactoringASTParser astParser = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL); - CompilationUnit astRoot = astParser.parse(type.getCompilationUnit(), true); + CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(type.getCompilationUnit(), CoreASTProvider.WAIT_YES, monitor); + if (astRoot == null) { + return null; + } + try { ITypeBinding typeBinding = ASTNodes.getTypeBinding(astRoot, type); if (typeBinding == null) { diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/CodeActionHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/CodeActionHandler.java index a80e43ad31..1854fc2130 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/CodeActionHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/CodeActionHandler.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2017-2019 Red Hat Inc. and others. + * Copyright (c) 2017-2020 Red Hat Inc. and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 * which accompanies this distribution, and is available at @@ -84,14 +84,20 @@ public List> getCodeActionCommands(CodeActionParams if (unit == null || monitor.isCanceled()) { return Collections.emptyList(); } + + CompilationUnit astRoot = getASTRoot(unit, monitor); + if (astRoot == null || monitor.isCanceled()) { + return Collections.emptyList(); + } + int start = DiagnosticsHelper.getStartOffset(unit, params.getRange()); int end = DiagnosticsHelper.getEndOffset(unit, params.getRange()); InnovationContext context = new InnovationContext(unit, start, end - start); - context.setASTRoot(getASTRoot(unit)); + context.setASTRoot(astRoot); + IProblemLocationCore[] locations = this.getProblemLocationCores(unit, params.getContext().getDiagnostics()); if (monitor.isCanceled()) { return Collections.emptyList(); } - IProblemLocationCore[] locations = this.getProblemLocationCores(unit, params.getContext().getDiagnostics()); List codeActionKinds = new ArrayList<>(); if (params.getContext().getOnly() != null && !params.getContext().getOnly().isEmpty()) { @@ -161,7 +167,7 @@ public List> getCodeActionCommands(CodeActionParams return Collections.emptyList(); } if (containsKind(codeActionKinds, CodeActionKind.Source)) { - codeActions.addAll(sourceAssistProcessor.getSourceActionCommands(params, context, locations)); + codeActions.addAll(sourceAssistProcessor.getSourceActionCommands(params, context, locations, monitor)); } if (monitor.isCanceled()) { return Collections.emptyList(); @@ -233,7 +239,11 @@ private static int getProblemId(Diagnostic diagnostic) { } public static CompilationUnit getASTRoot(ICompilationUnit unit) { - return CoreASTProvider.getInstance().getAST(unit, CoreASTProvider.WAIT_YES, new NullProgressMonitor()); + return getASTRoot(unit, new NullProgressMonitor()); + } + + public static CompilationUnit getASTRoot(ICompilationUnit unit, IProgressMonitor monitor) { + return CoreASTProvider.getInstance().getAST(unit, CoreASTProvider.WAIT_YES, monitor); } private static class ChangeCorrectionProposalComparator implements Comparator { diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateConstructorsHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateConstructorsHandler.java index 8fa0b2a4ad..b3b76e13b4 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateConstructorsHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateConstructorsHandler.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019 Microsoft Corporation and others. + * Copyright (c) 2019-2020 Microsoft Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 * which accompanies this distribution, and is available at @@ -21,6 +21,8 @@ import java.util.Optional; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.jdt.core.IField; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IType; @@ -33,13 +35,12 @@ import org.eclipse.jdt.core.dom.IVariableBinding; import org.eclipse.jdt.core.dom.Modifier; import org.eclipse.jdt.core.dom.VariableDeclarationFragment; +import org.eclipse.jdt.core.manipulation.CoreASTProvider; import org.eclipse.jdt.internal.corext.codemanipulation.AddCustomConstructorOperation; import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings; import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility2Core; import org.eclipse.jdt.internal.corext.dom.ASTNodes; import org.eclipse.jdt.internal.corext.dom.Bindings; -import org.eclipse.jdt.internal.corext.dom.IASTSharedValues; -import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser; import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; import org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspMethodBinding; import org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspVariableBinding; @@ -51,20 +52,27 @@ import org.eclipse.text.edits.TextEdit; public class GenerateConstructorsHandler { - + // For test purpose public static CheckConstructorsResponse checkConstructorsStatus(CodeActionParams params) { - IType type = SourceAssistProcessor.getSelectionType(params); - return checkConstructorStatus(type); + return checkConstructorsStatus(params, new NullProgressMonitor()); + } + + public static CheckConstructorsResponse checkConstructorsStatus(CodeActionParams params, IProgressMonitor monitor) { + IType type = SourceAssistProcessor.getSelectionType(params, monitor); + return checkConstructorStatus(type, monitor); } - public static CheckConstructorsResponse checkConstructorStatus(IType type) { + public static CheckConstructorsResponse checkConstructorStatus(IType type, IProgressMonitor monitor) { if (type == null || type.getCompilationUnit() == null) { return new CheckConstructorsResponse(); } try { - RefactoringASTParser astParser = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL); - CompilationUnit astRoot = astParser.parse(type.getCompilationUnit(), true); + CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(type.getCompilationUnit(), CoreASTProvider.WAIT_YES, monitor); + if (astRoot == null) { + return new CheckConstructorsResponse(); + } + ITypeBinding typeBinding = ASTNodes.getTypeBinding(astRoot, type); if (typeBinding == null) { return new CheckConstructorsResponse(); @@ -123,27 +131,35 @@ private static IMethodBinding[] getVisibleConstructors(CompilationUnit astRoot, } } - public static WorkspaceEdit generateConstructors(GenerateConstructorsParams params) { - IType type = SourceAssistProcessor.getSelectionType(params.context); - TextEdit edit = generateConstructors(type, params.constructors, params.fields); + public static WorkspaceEdit generateConstructors(GenerateConstructorsParams params, IProgressMonitor monitor) { + IType type = SourceAssistProcessor.getSelectionType(params.context, monitor); + TextEdit edit = generateConstructors(type, params.constructors, params.fields, monitor); return (edit == null) ? null : SourceAssistProcessor.convertToWorkspaceEdit(type.getCompilationUnit(), edit); } - public static TextEdit generateConstructors(IType type, LspMethodBinding[] constructors, LspVariableBinding[] fields) { + public static TextEdit generateConstructors(IType type, LspMethodBinding[] constructors, LspVariableBinding[] fields, IProgressMonitor monitor) { Preferences preferences = JavaLanguageServerPlugin.getPreferencesManager().getPreferences(); CodeGenerationSettings settings = new CodeGenerationSettings(); settings.createComments = preferences.isCodeGenerationTemplateGenerateComments(); - return generateConstructors(type, constructors, fields, settings); + return generateConstructors(type, constructors, fields, settings, monitor); } + // For test purpose public static TextEdit generateConstructors(IType type, LspMethodBinding[] constructors, LspVariableBinding[] fields, CodeGenerationSettings settings) { + return generateConstructors(type, constructors, fields, settings, new NullProgressMonitor()); + } + + public static TextEdit generateConstructors(IType type, LspMethodBinding[] constructors, LspVariableBinding[] fields, CodeGenerationSettings settings, IProgressMonitor monitor) { if (type == null || type.getCompilationUnit() == null || constructors == null || constructors.length == 0) { return null; } try { - RefactoringASTParser astParser = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL); - CompilationUnit astRoot = astParser.parse(type.getCompilationUnit(), true); + CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(type.getCompilationUnit(), CoreASTProvider.WAIT_YES, monitor); + if (astRoot == null) { + return null; + } + ITypeBinding typeBinding = ASTNodes.getTypeBinding(astRoot, type); if (typeBinding != null) { Map fieldBindings = new HashMap<>(); diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateDelegateMethodsHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateDelegateMethodsHandler.java index aa790e8fb6..58c4ea5d9a 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateDelegateMethodsHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateDelegateMethodsHandler.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019 Microsoft Corporation and others. + * Copyright (c) 2019-2020 Microsoft Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 * which accompanies this distribution, and is available at @@ -21,6 +21,8 @@ import java.util.Map; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.jdt.core.IField; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaModelException; @@ -29,13 +31,12 @@ import org.eclipse.jdt.core.dom.IMethodBinding; import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.core.dom.IVariableBinding; +import org.eclipse.jdt.core.manipulation.CoreASTProvider; import org.eclipse.jdt.internal.corext.codemanipulation.AddDelegateMethodsOperation; import org.eclipse.jdt.internal.corext.codemanipulation.AddDelegateMethodsOperation.DelegateEntry; import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings; import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility2Core; import org.eclipse.jdt.internal.corext.dom.ASTNodes; -import org.eclipse.jdt.internal.corext.dom.IASTSharedValues; -import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser; import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; import org.eclipse.jdt.ls.core.internal.corext.util.JdtFlags; import org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspMethodBinding; @@ -73,15 +74,23 @@ private static boolean isArray(IField field) throws JavaModelException { return Signature.getArrayCount(field.getTypeSignature()) > 0; } + // For test purpose. public static CheckDelegateMethodsResponse checkDelegateMethodsStatus(CodeActionParams params) { - IType type = SourceAssistProcessor.getSelectionType(params); + return checkDelegateMethodsStatus(params, new NullProgressMonitor()); + } + + public static CheckDelegateMethodsResponse checkDelegateMethodsStatus(CodeActionParams params, IProgressMonitor monitor) { + IType type = SourceAssistProcessor.getSelectionType(params, monitor); if (type == null || type.getCompilationUnit() == null) { return new CheckDelegateMethodsResponse(); } try { - RefactoringASTParser astParser = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL); - CompilationUnit astRoot = astParser.parse(type.getCompilationUnit(), true); + CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(type.getCompilationUnit(), CoreASTProvider.WAIT_YES, monitor); + if (astRoot == null) { + return new CheckDelegateMethodsResponse(); + } + ITypeBinding typeBinding = ASTNodes.getTypeBinding(astRoot, type); if (typeBinding == null) { return new CheckDelegateMethodsResponse(); @@ -107,23 +116,31 @@ public static CheckDelegateMethodsResponse checkDelegateMethodsStatus(CodeAction return new CheckDelegateMethodsResponse(); } - public static WorkspaceEdit generateDelegateMethods(GenerateDelegateMethodsParams params) { - IType type = SourceAssistProcessor.getSelectionType(params.context); + public static WorkspaceEdit generateDelegateMethods(GenerateDelegateMethodsParams params, IProgressMonitor monitor) { + IType type = SourceAssistProcessor.getSelectionType(params.context, monitor); Preferences preferences = JavaLanguageServerPlugin.getPreferencesManager().getPreferences(); CodeGenerationSettings settings = new CodeGenerationSettings(); settings.createComments = preferences.isCodeGenerationTemplateGenerateComments(); - TextEdit edit = generateDelegateMethods(type, params.delegateEntries, settings); + TextEdit edit = generateDelegateMethods(type, params.delegateEntries, settings, monitor); return (edit == null) ? null : SourceAssistProcessor.convertToWorkspaceEdit(type.getCompilationUnit(), edit); } + // For test purpose. public static TextEdit generateDelegateMethods(IType type, LspDelegateEntry[] delegateEntries, CodeGenerationSettings settings) { + return generateDelegateMethods(type, delegateEntries, settings, new NullProgressMonitor()); + } + + public static TextEdit generateDelegateMethods(IType type, LspDelegateEntry[] delegateEntries, CodeGenerationSettings settings, IProgressMonitor monitor) { if (type == null || type.getCompilationUnit() == null || delegateEntries == null || delegateEntries.length == 0) { return null; } try { - RefactoringASTParser astParser = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL); - CompilationUnit astRoot = astParser.parse(type.getCompilationUnit(), true); + CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(type.getCompilationUnit(), CoreASTProvider.WAIT_YES, monitor); + if (astRoot == null) { + return null; + } + ITypeBinding typeBinding = ASTNodes.getTypeBinding(astRoot, type); if (typeBinding == null) { return null; diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateToStringHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateToStringHandler.java index 8155c15baf..ee6aa83a43 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateToStringHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateToStringHandler.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019 Microsoft Corporation and others. + * Copyright (c) 2019-2020 Microsoft Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 * which accompanies this distribution, and is available at @@ -17,18 +17,19 @@ import org.apache.commons.lang3.StringUtils; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.core.dom.IVariableBinding; +import org.eclipse.jdt.core.manipulation.CoreASTProvider; import org.eclipse.jdt.internal.corext.codemanipulation.tostringgeneration.GenerateToStringOperation; import org.eclipse.jdt.internal.corext.codemanipulation.tostringgeneration.ToStringGenerationSettingsCore; import org.eclipse.jdt.internal.corext.codemanipulation.tostringgeneration.ToStringGenerationSettingsCore.CustomBuilderSettings; import org.eclipse.jdt.internal.corext.dom.ASTNodes; -import org.eclipse.jdt.internal.corext.dom.IASTSharedValues; -import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; import org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspVariableBinding; @@ -42,20 +43,33 @@ public class GenerateToStringHandler { private static final String METHODNAME_TOSTRING = "toString"; public static final String DEFAULT_TEMPLATE = "${object.className} [${member.name()}=${member.value}, ${otherMembers}]"; + // For test purpose public static CheckToStringResponse checkToStringStatus(CodeActionParams params) { - IType type = SourceAssistProcessor.getSelectionType(params); - return checkToStringStatus(type); + return checkToStringStatus(params, new NullProgressMonitor()); } + public static CheckToStringResponse checkToStringStatus(CodeActionParams params, IProgressMonitor monitor) { + IType type = SourceAssistProcessor.getSelectionType(params, monitor); + return checkToStringStatus(type, monitor); + } + + // For test purpose public static CheckToStringResponse checkToStringStatus(IType type) { + return checkToStringStatus(type, new NullProgressMonitor()); + } + + public static CheckToStringResponse checkToStringStatus(IType type, IProgressMonitor monitor) { CheckToStringResponse response = new CheckToStringResponse(); if (type == null) { return response; } try { - RefactoringASTParser astParser = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL); - CompilationUnit astRoot = astParser.parse(type.getCompilationUnit(), true); + CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(type.getCompilationUnit(), CoreASTProvider.WAIT_YES, monitor); + if (astRoot == null) { + return response; + } + ITypeBinding typeBinding = ASTNodes.getTypeBinding(astRoot, type); if (typeBinding != null) { response.type = type.getTypeQualifiedName(); @@ -69,17 +83,17 @@ public static CheckToStringResponse checkToStringStatus(IType type) { return response; } - public static WorkspaceEdit generateToString(GenerateToStringParams params) { - IType type = SourceAssistProcessor.getSelectionType(params.context); + public static WorkspaceEdit generateToString(GenerateToStringParams params, IProgressMonitor monitor) { + IType type = SourceAssistProcessor.getSelectionType(params.context, monitor); if (type == null || type.getCompilationUnit() == null) { return null; } - TextEdit edit = generateToString(type, params.fields); + TextEdit edit = generateToString(type, params.fields, monitor); return (edit == null) ? null : SourceAssistProcessor.convertToWorkspaceEdit(type.getCompilationUnit(), edit); } - public static TextEdit generateToString(IType type, LspVariableBinding[] fields) { + public static TextEdit generateToString(IType type, LspVariableBinding[] fields, IProgressMonitor monitor) { if (type == null || type.getCompilationUnit() == null) { return null; } @@ -102,17 +116,25 @@ public static TextEdit generateToString(IType type, LspVariableBinding[] fields) settings.is60orHigher = !JavaModelUtil.isVersionLessThan(version, JavaCore.VERSION_1_6); } - return generateToString(type, fields, settings); + return generateToString(type, fields, settings, monitor); } + // For test purpose public static TextEdit generateToString(IType type, LspVariableBinding[] fields, ToStringGenerationSettingsCore settings) { + return generateToString(type, fields, settings, new NullProgressMonitor()); + } + + public static TextEdit generateToString(IType type, LspVariableBinding[] fields, ToStringGenerationSettingsCore settings, IProgressMonitor monitor) { if (type == null) { return null; } try { - RefactoringASTParser astParser = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL); - CompilationUnit astRoot = astParser.parse(type.getCompilationUnit(), true); + CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(type.getCompilationUnit(), CoreASTProvider.WAIT_YES, monitor); + if (astRoot == null) { + return null; + } + ITypeBinding typeBinding = ASTNodes.getTypeBinding(astRoot, type); if (typeBinding != null) { IVariableBinding[] selectedFields = JdtDomModels.convertToVariableBindings(typeBinding, fields); diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/HashCodeEqualsHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/HashCodeEqualsHandler.java index bb8ba4de23..82eeb48682 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/HashCodeEqualsHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/HashCodeEqualsHandler.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019 Microsoft Corporation and others. + * Copyright (c) 2019-2020 Microsoft Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 * which accompanies this distribution, and is available at @@ -17,17 +17,18 @@ import java.util.List; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.IMethodBinding; import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.core.dom.IVariableBinding; +import org.eclipse.jdt.core.manipulation.CoreASTProvider; import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings; import org.eclipse.jdt.internal.corext.codemanipulation.GenerateHashCodeEqualsOperation; import org.eclipse.jdt.internal.corext.dom.ASTNodes; -import org.eclipse.jdt.internal.corext.dom.IASTSharedValues; -import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser; import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; import org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspVariableBinding; import org.eclipse.jdt.ls.core.internal.preferences.Preferences; @@ -40,19 +41,27 @@ public class HashCodeEqualsHandler { private static final String METHODNAME_HASH_CODE = "hashCode"; private static final String METHODNAME_EQUALS = "equals"; + // For test purpose public static CheckHashCodeEqualsResponse checkHashCodeEqualsStatus(CodeActionParams params) { - IType type = SourceAssistProcessor.getSelectionType(params); - return checkHashCodeEqualsStatus(type); + return checkHashCodeEqualsStatus(params, new NullProgressMonitor()); } - public static CheckHashCodeEqualsResponse checkHashCodeEqualsStatus(IType type) { + public static CheckHashCodeEqualsResponse checkHashCodeEqualsStatus(CodeActionParams params, IProgressMonitor monitor) { + IType type = SourceAssistProcessor.getSelectionType(params, monitor); + return checkHashCodeEqualsStatus(type, monitor); + } + + public static CheckHashCodeEqualsResponse checkHashCodeEqualsStatus(IType type, IProgressMonitor monitor) { CheckHashCodeEqualsResponse response = new CheckHashCodeEqualsResponse(); if (type == null) { return response; } try { - RefactoringASTParser astParser = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL); - CompilationUnit astRoot = astParser.parse(type.getCompilationUnit(), true); + CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(type.getCompilationUnit(), CoreASTProvider.WAIT_YES, monitor); + if (astRoot == null) { + return response; + } + ITypeBinding typeBinding = ASTNodes.getTypeBinding(astRoot, type); if (typeBinding != null) { response.type = type.getTypeQualifiedName(); @@ -65,29 +74,37 @@ public static CheckHashCodeEqualsResponse checkHashCodeEqualsStatus(IType type) return response; } - public static WorkspaceEdit generateHashCodeEquals(GenerateHashCodeEqualsParams params) { - IType type = SourceAssistProcessor.getSelectionType(params.context); + public static WorkspaceEdit generateHashCodeEquals(GenerateHashCodeEqualsParams params, IProgressMonitor monitor) { + IType type = SourceAssistProcessor.getSelectionType(params.context, monitor); Preferences preferences = JavaLanguageServerPlugin.getPreferencesManager().getPreferences(); boolean useJava7Objects = preferences.isHashCodeEqualsTemplateUseJava7Objects(); boolean useInstanceof = preferences.isHashCodeEqualsTemplateUseInstanceof(); boolean useBlocks = preferences.isCodeGenerationTemplateUseBlocks(); boolean generateComments = preferences.isCodeGenerationTemplateGenerateComments(); - TextEdit edit = generateHashCodeEqualsTextEdit(type, params.fields, params.regenerate, useJava7Objects, useInstanceof, useBlocks, generateComments); + TextEdit edit = generateHashCodeEqualsTextEdit(type, params.fields, params.regenerate, useJava7Objects, useInstanceof, useBlocks, generateComments, monitor); return (edit == null) ? null : SourceAssistProcessor.convertToWorkspaceEdit(type.getCompilationUnit(), edit); } + // For test purpose public static TextEdit generateHashCodeEqualsTextEdit(GenerateHashCodeEqualsParams params, boolean useJava7Objects, boolean useInstanceof, boolean useBlocks, boolean generateComments) { - IType type = SourceAssistProcessor.getSelectionType(params.context); - return generateHashCodeEqualsTextEdit(type, params.fields, params.regenerate, useJava7Objects, useInstanceof, useBlocks, generateComments); + return generateHashCodeEqualsTextEdit(params, useJava7Objects, useInstanceof, useBlocks, generateComments, new NullProgressMonitor()); } - public static TextEdit generateHashCodeEqualsTextEdit(IType type, LspVariableBinding[] fields, boolean regenerate, boolean useJava7Objects, boolean useInstanceof, boolean useBlocks, boolean generateComments) { + public static TextEdit generateHashCodeEqualsTextEdit(GenerateHashCodeEqualsParams params, boolean useJava7Objects, boolean useInstanceof, boolean useBlocks, boolean generateComments, IProgressMonitor monitor) { + IType type = SourceAssistProcessor.getSelectionType(params.context, monitor); + return generateHashCodeEqualsTextEdit(type, params.fields, params.regenerate, useJava7Objects, useInstanceof, useBlocks, generateComments, monitor); + } + + public static TextEdit generateHashCodeEqualsTextEdit(IType type, LspVariableBinding[] fields, boolean regenerate, boolean useJava7Objects, boolean useInstanceof, boolean useBlocks, boolean generateComments, IProgressMonitor monitor) { if (type == null) { return null; } try { - RefactoringASTParser astParser = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL); - CompilationUnit astRoot = astParser.parse(type.getCompilationUnit(), true); + CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(type.getCompilationUnit(), CoreASTProvider.WAIT_YES, monitor); + if (astRoot == null) { + return null; + } + ITypeBinding typeBinding = ASTNodes.getTypeBinding(astRoot, type); if (typeBinding != null) { IVariableBinding[] variableBindings = JdtDomModels.convertToVariableBindings(typeBinding, fields); diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/JDTLanguageServer.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/JDTLanguageServer.java index fadd7f2c24..1c15020a03 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/JDTLanguageServer.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/JDTLanguageServer.java @@ -858,43 +858,43 @@ public CompletableFuture> selectionRange(SelectionRangePara @Override public CompletableFuture listOverridableMethods(CodeActionParams params) { logInfo(">> java/listOverridableMethods"); - return computeAsync((monitor) -> OverrideMethodsHandler.listOverridableMethods(params)); + return computeAsync((monitor) -> OverrideMethodsHandler.listOverridableMethods(params, monitor)); } @Override public CompletableFuture addOverridableMethods(AddOverridableMethodParams params) { logInfo(">> java/addOverridableMethods"); - return computeAsync((monitor) -> OverrideMethodsHandler.addOverridableMethods(params)); + return computeAsync((monitor) -> OverrideMethodsHandler.addOverridableMethods(params, monitor)); } @Override public CompletableFuture checkHashCodeEqualsStatus(CodeActionParams params) { logInfo(">> java/checkHashCodeEqualsStatus"); - return computeAsync((monitor) -> HashCodeEqualsHandler.checkHashCodeEqualsStatus(params)); + return computeAsync((monitor) -> HashCodeEqualsHandler.checkHashCodeEqualsStatus(params, monitor)); } @Override public CompletableFuture generateHashCodeEquals(GenerateHashCodeEqualsParams params) { logInfo(">> java/generateHashCodeEquals"); - return computeAsync((monitor) -> HashCodeEqualsHandler.generateHashCodeEquals(params)); + return computeAsync((monitor) -> HashCodeEqualsHandler.generateHashCodeEquals(params, monitor)); } @Override public CompletableFuture checkToStringStatus(CodeActionParams params) { logInfo(">> java/checkToStringStatus"); - return computeAsync((monitor) -> GenerateToStringHandler.checkToStringStatus(params)); + return computeAsync((monitor) -> GenerateToStringHandler.checkToStringStatus(params, monitor)); } @Override public CompletableFuture generateToString(GenerateToStringParams params) { logInfo(">> java/generateToString"); - return computeAsync((monitor) -> GenerateToStringHandler.generateToString(params)); + return computeAsync((monitor) -> GenerateToStringHandler.generateToString(params, monitor)); } @Override public CompletableFuture organizeImports(CodeActionParams params) { logInfo(">> java/organizeImports"); - return computeAsync((monitor) -> OrganizeImportsHandler.organizeImports(client, params)); + return computeAsync((monitor) -> OrganizeImportsHandler.organizeImports(client, params, monitor)); } @Override @@ -912,25 +912,25 @@ public CompletableFuture generateAccessors(GenerateAccessorsParam @Override public CompletableFuture checkConstructorsStatus(CodeActionParams params) { logInfo(">> java/checkConstructorsStatus"); - return computeAsync((monitor) -> GenerateConstructorsHandler.checkConstructorsStatus(params)); + return computeAsync((monitor) -> GenerateConstructorsHandler.checkConstructorsStatus(params, monitor)); } @Override public CompletableFuture generateConstructors(GenerateConstructorsParams params) { logInfo(">> java/generateConstructors"); - return computeAsync((monitor) -> GenerateConstructorsHandler.generateConstructors(params)); + return computeAsync((monitor) -> GenerateConstructorsHandler.generateConstructors(params, monitor)); } @Override public CompletableFuture checkDelegateMethodsStatus(CodeActionParams params) { logInfo(">> java/checkDelegateMethodsStatus"); - return computeAsync((monitor) -> GenerateDelegateMethodsHandler.checkDelegateMethodsStatus(params)); + return computeAsync((monitor) -> GenerateDelegateMethodsHandler.checkDelegateMethodsStatus(params, monitor)); } @Override public CompletableFuture generateDelegateMethods(GenerateDelegateMethodsParams params) { logInfo(">> java/generateDelegateMethods"); - return computeAsync((monitor) -> GenerateDelegateMethodsHandler.generateDelegateMethods(params)); + return computeAsync((monitor) -> GenerateDelegateMethodsHandler.generateDelegateMethods(params, monitor)); } @Override diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/OrganizeImportsHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/OrganizeImportsHandler.java index 95ed9ee02c..9231645efa 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/OrganizeImportsHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/OrganizeImportsHandler.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019 Microsoft Corporation and others. + * Copyright (c) 2019-2020 Microsoft Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 * which accompanies this distribution, and is available at @@ -22,6 +22,7 @@ import java.util.stream.Stream; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.jdt.core.ICompilationUnit; @@ -36,12 +37,11 @@ import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; import org.eclipse.jdt.core.manipulation.CodeStyleConfiguration; +import org.eclipse.jdt.core.manipulation.CoreASTProvider; import org.eclipse.jdt.core.manipulation.ImportReferencesCollector; import org.eclipse.jdt.core.manipulation.OrganizeImportsOperation; import org.eclipse.jdt.core.search.TypeNameMatch; import org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext; -import org.eclipse.jdt.internal.corext.dom.IASTSharedValues; -import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.jdt.ls.core.internal.JDTUtils; import org.eclipse.jdt.ls.core.internal.JSONUtility; @@ -64,12 +64,21 @@ public final class OrganizeImportsHandler { public static final String CLIENT_COMMAND_ID_CHOOSEIMPORTS = "java.action.organizeImports.chooseImports"; + // For test purpose public static TextEdit organizeImports(ICompilationUnit unit, Function chooseImports) { + return organizeImports(unit, chooseImports, new NullProgressMonitor()); + } + + public static TextEdit organizeImports(ICompilationUnit unit, Function chooseImports, IProgressMonitor monitor) { if (unit == null) { return null; } - RefactoringASTParser astParser = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL); - CompilationUnit astRoot = astParser.parse(unit, true); + + CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(unit, CoreASTProvider.WAIT_YES, monitor); + if (astRoot == null) { + return null; + } + OrganizeImportsOperation op = new OrganizeImportsOperation(unit, astRoot, true, false, true, (TypeNameMatch[][] openChoices, ISourceRange[] ranges) -> { List selections = new ArrayList<>(); for (int i = 0; i < openChoices.length; i++) { @@ -96,7 +105,7 @@ public static TextEdit organizeImports(ICompilationUnit unit, Function chosen != null && typeMaps.containsKey(chosen.id)).map(chosen -> typeMaps.get(chosen.id)).toArray(TypeNameMatch[]::new); }); try { - JobHelpers.waitForJobs(DocumentLifeCycleHandler.DOCUMENT_LIFE_CYCLE_JOBS, new NullProgressMonitor()); + JobHelpers.waitForJobs(DocumentLifeCycleHandler.DOCUMENT_LIFE_CYCLE_JOBS, monitor); TextEdit edit = op.createTextEdit(null); // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=283287 // and https://github.com/redhat-developer/vscode-java/issues/1472 @@ -191,7 +200,7 @@ private static void addImports(CompilationUnit root, ICompilationUnit unit, Stri } } - public static WorkspaceEdit organizeImports(JavaClientConnection connection, CodeActionParams params) { + public static WorkspaceEdit organizeImports(JavaClientConnection connection, CodeActionParams params, IProgressMonitor monitor) { String uri = params.getTextDocument().getUri(); final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri()); if (unit == null) { @@ -202,7 +211,7 @@ public static WorkspaceEdit organizeImports(JavaClientConnection connection, Cod Object commandResult = connection.executeClientCommand(CLIENT_COMMAND_ID_CHOOSEIMPORTS, uri, selections); String json = commandResult == null ? null : new Gson().toJson(commandResult); return JSONUtility.toModel(json, ImportCandidate[].class); - }); + }, monitor); return SourceAssistProcessor.convertToWorkspaceEdit(unit, edit); } diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/OverrideMethodsHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/OverrideMethodsHandler.java index 2deaba1158..e78592d049 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/OverrideMethodsHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/OverrideMethodsHandler.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019 Microsoft Corporation and others. + * Copyright (c) 2019-2020 Microsoft Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 * which accompanies this distribution, and is available at @@ -15,6 +15,7 @@ import java.util.List; +import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.ls.core.internal.codemanipulation.OverrideMethodsOperation; import org.eclipse.jdt.ls.core.internal.codemanipulation.OverrideMethodsOperation.OverridableMethod; @@ -25,16 +26,16 @@ public class OverrideMethodsHandler { - public static OverridableMethodsResponse listOverridableMethods(CodeActionParams params) { - IType type = SourceAssistProcessor.getSelectionType(params); + public static OverridableMethodsResponse listOverridableMethods(CodeActionParams params, IProgressMonitor monitor) { + IType type = SourceAssistProcessor.getSelectionType(params, monitor); String typeName = type == null ? "" : type.getTypeQualifiedName(); - List methods = OverrideMethodsOperation.listOverridableMethods(type); + List methods = OverrideMethodsOperation.listOverridableMethods(type, monitor); return new OverridableMethodsResponse(typeName, methods); } - public static WorkspaceEdit addOverridableMethods(AddOverridableMethodParams params) { - IType type = SourceAssistProcessor.getSelectionType(params.context); - TextEdit edit = OverrideMethodsOperation.addOverridableMethods(type, params.overridableMethods); + public static WorkspaceEdit addOverridableMethods(AddOverridableMethodParams params, IProgressMonitor monitor) { + IType type = SourceAssistProcessor.getSelectionType(params.context, monitor); + TextEdit edit = OverrideMethodsOperation.addOverridableMethods(type, params.overridableMethods, monitor); if (edit == null) { return null; } diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/text/correction/SourceAssistProcessor.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/text/correction/SourceAssistProcessor.java index 4340aed755..1e77d8b0e2 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/text/correction/SourceAssistProcessor.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/text/correction/SourceAssistProcessor.java @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (c) 2017-2019 Microsoft Corporation and others. +* Copyright (c) 2017-2020 Microsoft Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 * which accompanies this distribution, and is available at @@ -23,6 +23,7 @@ import java.util.stream.Stream; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.jdt.core.ICompilationUnit; @@ -38,8 +39,6 @@ import org.eclipse.jdt.core.manipulation.CoreASTProvider; import org.eclipse.jdt.core.manipulation.OrganizeImportsOperation; import org.eclipse.jdt.internal.corext.dom.ASTNodes; -import org.eclipse.jdt.internal.corext.dom.IASTSharedValues; -import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser; import org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore; import org.eclipse.jdt.ls.core.internal.ChangeUtil; import org.eclipse.jdt.ls.core.internal.JDTUtils; @@ -90,7 +89,7 @@ public SourceAssistProcessor(PreferenceManager preferenceManager) { this.preferenceManager = preferenceManager; } - public List> getSourceActionCommands(CodeActionParams params, IInvocationContext context, IProblemLocationCore[] locations) { + public List> getSourceActionCommands(CodeActionParams params, IInvocationContext context, IProblemLocationCore[] locations, IProgressMonitor monitor) { List> $ = new ArrayList<>(); ICompilationUnit cu = context.getCompilationUnit(); IType type = getSelectionType(context); @@ -100,9 +99,8 @@ public List> getSourceActionCommands(CodeActionParam try { IJavaElement element = JDTUtils.findElementAtSelection(cu, params.getRange().getEnd().getLine(), params.getRange().getEnd().getCharacter(), this.preferenceManager, new NullProgressMonitor()); if (element instanceof IField) { - generateConstructors = getGenerateConstructorsAction(params, context, type, JavaCodeActionKind.QUICK_ASSIST); + generateConstructors = getGenerateConstructorsAction(params, context, type, JavaCodeActionKind.QUICK_ASSIST, monitor); addSourceActionCommand($, params.getContext(), generateConstructors); - } } catch (JavaModelException e) { JavaLanguageServerPlugin.logException(e); @@ -130,7 +128,7 @@ public List> getSourceActionCommands(CodeActionParam addSourceActionCommand($, params.getContext(), getterSetter); // Generate hashCode() and equals() - if (supportsHashCodeEquals(context, type)) { + if (supportsHashCodeEquals(context, type, monitor)) { Optional> hashCodeEquals = getHashCodeEqualsAction(params); addSourceActionCommand($, params.getContext(), hashCodeEquals); } @@ -147,7 +145,7 @@ public List> getSourceActionCommands(CodeActionParam Optional> generateToStringCommand = getGenerateToStringAction(params); addSourceActionCommand($, params.getContext(), generateToStringCommand); } else { - TextEdit toStringEdit = GenerateToStringHandler.generateToString(type, new LspVariableBinding[0]); + TextEdit toStringEdit = GenerateToStringHandler.generateToString(type, new LspVariableBinding[0], monitor); Optional> generateToStringCommand = convertToWorkspaceEditAction(params.getContext(), context.getCompilationUnit(), ActionMessages.GenerateToStringAction_label, JavaCodeActionKind.SOURCE_GENERATE_TO_STRING, toStringEdit); addSourceActionCommand($, params.getContext(), generateToStringCommand); @@ -156,7 +154,7 @@ public List> getSourceActionCommands(CodeActionParam // Generate Constructors if (generateConstructors == null) { - generateConstructors = getGenerateConstructorsAction(params, context, type, JavaCodeActionKind.SOURCE_GENERATE_CONSTRUCTORS); + generateConstructors = getGenerateConstructorsAction(params, context, type, JavaCodeActionKind.SOURCE_GENERATE_CONSTRUCTORS, monitor); } else if (generateConstructors.isPresent()) { Command command = new Command(ActionMessages.GenerateConstructorsAction_ellipsisLabel, COMMAND_ID_ACTION_GENERATECONSTRUCTORSPROMPT, Collections.singletonList(params)); if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(JavaCodeActionKind.SOURCE_GENERATE_CONSTRUCTORS)) { @@ -267,13 +265,17 @@ private Optional> getGetterSetterAction(CodeActionPa } } - private boolean supportsHashCodeEquals(IInvocationContext context, IType type) { + private boolean supportsHashCodeEquals(IInvocationContext context, IType type, IProgressMonitor monitor) { try { if (type == null || type.isAnnotation() || type.isInterface() || type.isEnum() || type.getCompilationUnit() == null) { return false; } - RefactoringASTParser astParser = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL); - CompilationUnit astRoot = astParser.parse(type.getCompilationUnit(), true); + + CompilationUnit astRoot = context.getASTRoot(); + if (astRoot == null) { + return false; + } + ITypeBinding typeBinding = ASTNodes.getTypeBinding(astRoot, type); return (typeBinding == null) ? false : Arrays.stream(typeBinding.getDeclaredFields()).filter(f -> !Modifier.isStatic(f.getModifiers())).findAny().isPresent(); } catch (JavaModelException e) { @@ -325,7 +327,7 @@ private Optional> getGenerateToStringAction(CodeActi } } - private Optional> getGenerateConstructorsAction(CodeActionParams params, IInvocationContext context, IType type, String kind) { + private Optional> getGenerateConstructorsAction(CodeActionParams params, IInvocationContext context, IType type, String kind, IProgressMonitor monitor) { try { if (type == null || type.isAnnotation() || type.isInterface() || type.isAnonymous() || type.getCompilationUnit() == null) { return Optional.empty(); @@ -335,12 +337,12 @@ private Optional> getGenerateConstructorsAction(Code } if (preferenceManager.getClientPreferences().isGenerateConstructorsPromptSupported()) { - CheckConstructorsResponse status = GenerateConstructorsHandler.checkConstructorStatus(type); + CheckConstructorsResponse status = GenerateConstructorsHandler.checkConstructorStatus(type, monitor); if (status.constructors.length == 0) { return Optional.empty(); } if (status.constructors.length == 1 && status.fields.length == 0) { - TextEdit edit = GenerateConstructorsHandler.generateConstructors(type, status.constructors, status.fields); + TextEdit edit = GenerateConstructorsHandler.generateConstructors(type, status.constructors, status.fields, monitor); return convertToWorkspaceEditAction(params.getContext(), type.getCompilationUnit(), ActionMessages.GenerateConstructorsAction_label, kind, edit); } @@ -448,11 +450,15 @@ public static IType getSelectionType(IInvocationContext context) { } public static IType getSelectionType(CodeActionParams params) { - InnovationContext context = getInnovationContext(params); + return getSelectionType(params, new NullProgressMonitor()); + } + + public static IType getSelectionType(CodeActionParams params, IProgressMonitor monitor) { + InnovationContext context = getInnovationContext(params, monitor); return (context == null) ? null : getSelectionType(context); } - public static InnovationContext getInnovationContext(CodeActionParams params) { + public static InnovationContext getInnovationContext(CodeActionParams params, IProgressMonitor monitor) { final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri()); if (unit == null) { return null; @@ -460,7 +466,11 @@ public static InnovationContext getInnovationContext(CodeActionParams params) { int start = DiagnosticsHelper.getStartOffset(unit, params.getRange()); int end = DiagnosticsHelper.getEndOffset(unit, params.getRange()); InnovationContext context = new InnovationContext(unit, start, end - start); - CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(unit, CoreASTProvider.WAIT_YES, new NullProgressMonitor()); + CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(unit, CoreASTProvider.WAIT_YES, monitor); + if (astRoot == null) { + return null; + } + context.setASTRoot(astRoot); return context; }