Search in sources :

Example 1 with AddCustomConstructorOperation

use of org.eclipse.jdt.internal.corext.codemanipulation.AddCustomConstructorOperation 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

ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 IField (org.eclipse.jdt.core.IField)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IType (org.eclipse.jdt.core.IType)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 AST (org.eclipse.jdt.core.dom.AST)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)1 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)1 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)1 Modifier (org.eclipse.jdt.core.dom.Modifier)1