Search in sources :

Example 6 with LspVariableBinding

use of org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspVariableBinding in project eclipse.jdt.ls by eclipse.

the class GenerateConstructorsHandler method generateConstructors.

public static TextEdit generateConstructors(IType type, LspMethodBinding[] constructors, LspVariableBinding[] fields, CodeGenerationSettings settings, Range cursor, IProgressMonitor monitor) {
    if (type == null || type.getCompilationUnit() == null || constructors == null || constructors.length == 0) {
        return null;
    }
    try {
        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) {
            ASTNode declarationNode = null;
            if (cursor != null) {
                ASTNode node = NodeFinder.perform(astRoot, DiagnosticsHelper.getStartOffset(type.getCompilationUnit(), cursor), DiagnosticsHelper.getLength(type.getCompilationUnit(), cursor));
                declarationNode = SourceAssistProcessor.getTypeDeclarationNode(node);
            }
            // If cursor position is not specified, then insert to the last by default.
            IJavaElement insertPosition = (declarationNode != null) ? CodeGenerationUtils.findInsertElementAfterLastField(type) : CodeGenerationUtils.findInsertElement(type, cursor);
            Map<String, IVariableBinding> fieldBindings = new HashMap<>();
            for (IVariableBinding binding : typeBinding.getDeclaredFields()) {
                fieldBindings.put(binding.getKey(), binding);
            }
            IVariableBinding[] selectedFields = Arrays.stream(fields).map(field -> fieldBindings.get(field.bindingKey)).filter(binding -> binding != null).toArray(IVariableBinding[]::new);
            IMethodBinding[] superConstructors = getVisibleConstructors(astRoot, typeBinding);
            TextEdit textEdit = new MultiTextEdit();
            for (LspMethodBinding constructor : constructors) {
                Optional<IMethodBinding> selectedSuperConstructor = Arrays.stream(superConstructors).filter(superConstructor -> compareConstructor(superConstructor, constructor)).findAny();
                if (selectedSuperConstructor.isPresent()) {
                    IMethodBinding superConstructor = selectedSuperConstructor.get();
                    AddCustomConstructorOperation constructorOperation = new AddCustomConstructorOperation(astRoot, typeBinding, selectedFields, superConstructor, insertPosition, settings, false, false);
                    constructorOperation.setOmitSuper(superConstructor.getParameterTypes().length == 0);
                    constructorOperation.setVisibility(typeBinding.isEnum() ? Modifier.PRIVATE : Modifier.PUBLIC);
                    constructorOperation.run(null);
                    textEdit.addChild(constructorOperation.getResultingEdit());
                }
            }
            return textEdit;
        }
    } catch (CoreException e) {
        JavaLanguageServerPlugin.logException("Failed to generate constructors", e);
    }
    return null;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Arrays(java.util.Arrays) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IField(org.eclipse.jdt.core.IField) JavaModelException(org.eclipse.jdt.core.JavaModelException) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) LspMethodBinding(org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspMethodBinding) HashMap(java.util.HashMap) CoreException(org.eclipse.core.runtime.CoreException) Range(org.eclipse.lsp4j.Range) ArrayList(java.util.ArrayList) SourceAssistProcessor(org.eclipse.jdt.ls.core.internal.text.correction.SourceAssistProcessor) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) DiagnosticsHelper(org.eclipse.jdt.ls.core.internal.corrections.DiagnosticsHelper) Map(java.util.Map) Bindings(org.eclipse.jdt.internal.corext.dom.Bindings) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ASTNodes(org.eclipse.jdt.internal.corext.dom.ASTNodes) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Modifier(org.eclipse.jdt.core.dom.Modifier) LspVariableBinding(org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspVariableBinding) JavaLanguageServerPlugin(org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin) TextEdit(org.eclipse.text.edits.TextEdit) NodeFinder(org.eclipse.jdt.core.dom.NodeFinder) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) AddCustomConstructorOperation(org.eclipse.jdt.internal.corext.codemanipulation.AddCustomConstructorOperation) IType(org.eclipse.jdt.core.IType) List(java.util.List) CoreASTProvider(org.eclipse.jdt.core.manipulation.CoreASTProvider) IJavaElement(org.eclipse.jdt.core.IJavaElement) StubUtility2Core(org.eclipse.jdt.internal.corext.codemanipulation.StubUtility2Core) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Preferences(org.eclipse.jdt.ls.core.internal.preferences.Preferences) AST(org.eclipse.jdt.core.dom.AST) Optional(java.util.Optional) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) IJavaElement(org.eclipse.jdt.core.IJavaElement) HashMap(java.util.HashMap) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) AddCustomConstructorOperation(org.eclipse.jdt.internal.corext.codemanipulation.AddCustomConstructorOperation) CoreException(org.eclipse.core.runtime.CoreException) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) LspMethodBinding(org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspMethodBinding) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Aggregations

LspVariableBinding (org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspVariableBinding)6 ArrayList (java.util.ArrayList)5 IJavaElement (org.eclipse.jdt.core.IJavaElement)5 JavaModelException (org.eclipse.jdt.core.JavaModelException)5 ASTNode (org.eclipse.jdt.core.dom.ASTNode)5 Arrays (java.util.Arrays)4 List (java.util.List)4 Optional (java.util.Optional)4 CoreException (org.eclipse.core.runtime.CoreException)4 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)4 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 IType (org.eclipse.jdt.core.IType)4 JavaLanguageServerPlugin (org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin)4 DiagnosticsHelper (org.eclipse.jdt.ls.core.internal.corrections.DiagnosticsHelper)4 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)4 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)4 Gson (com.google.gson.Gson)3 Set (java.util.Set)3 Stream (java.util.stream.Stream)3 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)3