Search in sources :

Example 1 with LspMethodBinding

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

the class GenerateConstructorsHandler method checkConstructorStatus.

public static CheckConstructorsResponse checkConstructorStatus(IType type, IProgressMonitor monitor) {
    if (type == null || type.getCompilationUnit() == null) {
        return new CheckConstructorsResponse();
    }
    try {
        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();
        }
        IMethodBinding[] superConstructors = getVisibleConstructors(astRoot, typeBinding);
        Map<IJavaElement, IVariableBinding> fieldsToBindings = new HashMap<>();
        for (IVariableBinding field : typeBinding.getDeclaredFields()) {
            if (field.isSynthetic() || Modifier.isStatic(field.getModifiers())) {
                continue;
            }
            if (Modifier.isFinal(field.getModifiers())) {
                ASTNode declaringNode = astRoot.findDeclaringNode(field);
                // Do not add final fields which have been set in the <clinit>
                if (declaringNode instanceof VariableDeclarationFragment && ((VariableDeclarationFragment) declaringNode).getInitializer() != null) {
                    continue;
                }
            }
            fieldsToBindings.put(field.getJavaElement(), field);
        }
        List<IVariableBinding> fields = new ArrayList<>();
        // Sort the fields by the order in which they appear in the source or class file.
        for (IField field : type.getFields()) {
            IVariableBinding fieldBinding = fieldsToBindings.remove(field);
            if (fieldBinding != null) {
                fields.add(fieldBinding);
            }
        }
        // @formatter:off
        return new CheckConstructorsResponse(Arrays.stream(superConstructors).map(binding -> new LspMethodBinding(binding)).toArray(LspMethodBinding[]::new), fields.stream().map(binding -> new LspVariableBinding(binding)).toArray(LspVariableBinding[]::new));
    // @formatter:on
    } catch (JavaModelException e) {
        JavaLanguageServerPlugin.logException("Failed to check constructor status", e);
    }
    return new CheckConstructorsResponse();
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) LspVariableBinding(org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspVariableBinding) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) IField(org.eclipse.jdt.core.IField) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) LspMethodBinding(org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspMethodBinding)

Example 2 with LspMethodBinding

use of org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspMethodBinding 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)2 HashMap (java.util.HashMap)2 IField (org.eclipse.jdt.core.IField)2 IJavaElement (org.eclipse.jdt.core.IJavaElement)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)2 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)2 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)2 LspMethodBinding (org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspMethodBinding)2 LspVariableBinding (org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspVariableBinding)2 Arrays (java.util.Arrays)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