Java Code Examples for org.eclipse.jdt.core.dom.AST#newSuperConstructorInvocation()
The following examples show how to use
org.eclipse.jdt.core.dom.AST#newSuperConstructorInvocation() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConstructorFromSuperclassProposal.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
private SuperConstructorInvocation addEnclosingInstanceAccess(ASTRewrite rewrite, ImportRewriteContext importRewriteContext, List<SingleVariableDeclaration> parameters, String[] paramNames, ITypeBinding enclosingInstance) { AST ast= rewrite.getAST(); SuperConstructorInvocation invocation= ast.newSuperConstructorInvocation(); SingleVariableDeclaration var= ast.newSingleVariableDeclaration(); var.setType(getImportRewrite().addImport(enclosingInstance, ast, importRewriteContext, TypeLocation.PARAMETER)); String[] enclosingArgNames= StubUtility.getArgumentNameSuggestions(getCompilationUnit().getJavaProject(), enclosingInstance.getTypeDeclaration().getName(), 0, paramNames); String firstName= enclosingArgNames[0]; var.setName(ast.newSimpleName(firstName)); parameters.add(var); Name enclosing= ast.newSimpleName(firstName); invocation.setExpression(enclosing); String key= "arg_name_" + firstName; //$NON-NLS-1$ addLinkedPosition(rewrite.track(enclosing), false, key); for (int i= 0; i < enclosingArgNames.length; i++) { addLinkedPositionProposal(key, enclosingArgNames[i]); // alternative names } return invocation; }
Example 2
Source File: ChangeSignatureProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private void addNewConstructorToSubclass(AbstractTypeDeclaration subclass, CompilationUnitRewrite cuRewrite) { AST ast= subclass.getAST(); MethodDeclaration newConstructor= ast.newMethodDeclaration(); newConstructor.setName(ast.newSimpleName(subclass.getName().getIdentifier())); newConstructor.setConstructor(true); newConstructor.setJavadoc(null); newConstructor.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getAccessModifier(subclass))); newConstructor.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID)); Block body= ast.newBlock(); newConstructor.setBody(body); SuperConstructorInvocation superCall= ast.newSuperConstructorInvocation(); addArgumentsToNewSuperConstructorCall(superCall, cuRewrite); body.statements().add(superCall); String msg= RefactoringCoreMessages.ChangeSignatureRefactoring_add_constructor; TextEditGroup description= cuRewrite.createGroupDescription(msg); cuRewrite.getASTRewrite().getListRewrite(subclass, subclass.getBodyDeclarationsProperty()).insertFirst(newConstructor, description); // TODO use AbstractTypeDeclaration }
Example 3
Source File: ConstructorFromSuperclassProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private SuperConstructorInvocation addEnclosingInstanceAccess(ASTRewrite rewrite, ImportRewriteContext importRewriteContext, List<SingleVariableDeclaration> parameters, String[] paramNames, ITypeBinding enclosingInstance) { AST ast= rewrite.getAST(); SuperConstructorInvocation invocation= ast.newSuperConstructorInvocation(); SingleVariableDeclaration var= ast.newSingleVariableDeclaration(); var.setType(getImportRewrite().addImport(enclosingInstance, ast, importRewriteContext)); String[] enclosingArgNames= StubUtility.getArgumentNameSuggestions(getCompilationUnit().getJavaProject(), enclosingInstance.getTypeDeclaration().getName(), 0, paramNames); String firstName= enclosingArgNames[0]; var.setName(ast.newSimpleName(firstName)); parameters.add(var); Name enclosing= ast.newSimpleName(firstName); invocation.setExpression(enclosing); String key= "arg_name_" + firstName; //$NON-NLS-1$ addLinkedPosition(rewrite.track(enclosing), false, key); for (int i= 0; i < enclosingArgNames.length; i++) { addLinkedPositionProposal(key, enclosingArgNames[i], null); // alternative names } return invocation; }
Example 4
Source File: PrivateConstructorBodyCreationFragment.java From SparkBuilderGenerator with MIT License | 5 votes |
private void populateBodyWithSuperConstructorCall(AST ast, TypeDeclaration builderType, Block body, List<ConstructorParameterSetterBuilderField> builderFields) { SuperConstructorInvocation superInvocation = ast.newSuperConstructorInvocation(); builderFields.stream() .sorted((first, second) -> first.getIndex().compareTo(second.getIndex())) .forEach(constructorParameter -> addConstructorParameter(ast, builderType, superInvocation, constructorParameter)); if (!builderFields.isEmpty()) { body.statements().add(superInvocation); } }
Example 5
Source File: ConstructorFromSuperclassProposal.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private MethodDeclaration createNewMethodDeclaration(AST ast, IMethodBinding binding, ASTRewrite rewrite, ImportRewriteContext importRewriteContext, CodeGenerationSettings commentSettings) throws CoreException { String name= fTypeNode.getName().getIdentifier(); MethodDeclaration decl= ast.newMethodDeclaration(); decl.setConstructor(true); decl.setName(ast.newSimpleName(name)); Block body= ast.newBlock(); decl.setBody(body); SuperConstructorInvocation invocation= null; List<SingleVariableDeclaration> parameters= decl.parameters(); String[] paramNames= getArgumentNames(binding); ITypeBinding enclosingInstance= getEnclosingInstance(); if (enclosingInstance != null) { invocation= addEnclosingInstanceAccess(rewrite, importRewriteContext, parameters, paramNames, enclosingInstance); } if (binding == null) { decl.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); } else { decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, binding.getModifiers())); ITypeBinding[] params= binding.getParameterTypes(); for (int i= 0; i < params.length; i++) { SingleVariableDeclaration var= ast.newSingleVariableDeclaration(); var.setType(getImportRewrite().addImport(params[i], ast, importRewriteContext, TypeLocation.LOCAL_VARIABLE)); var.setName(ast.newSimpleName(paramNames[i])); parameters.add(var); } List<Type> thrownExceptions= decl.thrownExceptionTypes(); ITypeBinding[] excTypes= binding.getExceptionTypes(); for (int i= 0; i < excTypes.length; i++) { Type excType= getImportRewrite().addImport(excTypes[i], ast, importRewriteContext, TypeLocation.EXCEPTION); thrownExceptions.add(excType); } if (invocation == null) { invocation= ast.newSuperConstructorInvocation(); } List<Expression> arguments= invocation.arguments(); for (int i= 0; i < paramNames.length; i++) { Name argument= ast.newSimpleName(paramNames[i]); arguments.add(argument); addLinkedPosition(rewrite.track(argument), false, "arg_name_" + paramNames[i]); //$NON-NLS-1$ } } String bodyStatement = (invocation == null) ? "" : ASTNodes.asFormattedString(invocation, 0, String.valueOf('\n'), getCompilationUnit().getJavaProject().getOptions(true)); //$NON-NLS-1$ String placeHolder= CodeGeneration.getMethodBodyContent(getCompilationUnit(), name, name, true, bodyStatement, String.valueOf('\n')); if (placeHolder != null) { ASTNode todoNode= rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT); body.statements().add(todoNode); } if (commentSettings != null) { String string= CodeGeneration.getMethodComment(getCompilationUnit(), name, decl, null, String.valueOf('\n')); if (string != null) { Javadoc javadoc= (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC); decl.setJavadoc(javadoc); } } return decl; }
Example 6
Source File: StubUtility2.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
public static MethodDeclaration createConstructorStub(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, ImportRewriteContext context, IMethodBinding binding, String type, int modifiers, boolean omitSuperForDefConst, boolean todo, CodeGenerationSettings settings) throws CoreException { AST ast= rewrite.getAST(); MethodDeclaration decl= ast.newMethodDeclaration(); decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, modifiers & ~Modifier.ABSTRACT & ~Modifier.NATIVE)); decl.setName(ast.newSimpleName(type)); decl.setConstructor(true); createTypeParameters(imports, context, ast, binding, decl); List<SingleVariableDeclaration> parameters= createParameters(unit.getJavaProject(), imports, context, ast, binding, null, decl); createThrownExceptions(decl, binding, imports, context, ast); Block body= ast.newBlock(); decl.setBody(body); String delimiter= StubUtility.getLineDelimiterUsed(unit); String bodyStatement= ""; //$NON-NLS-1$ if (!omitSuperForDefConst || !parameters.isEmpty()) { SuperConstructorInvocation invocation= ast.newSuperConstructorInvocation(); SingleVariableDeclaration varDecl= null; for (Iterator<SingleVariableDeclaration> iterator= parameters.iterator(); iterator.hasNext();) { varDecl= iterator.next(); invocation.arguments().add(ast.newSimpleName(varDecl.getName().getIdentifier())); } bodyStatement= ASTNodes.asFormattedString(invocation, 0, delimiter, unit.getJavaProject().getOptions(true)); } if (todo) { String placeHolder= CodeGeneration.getMethodBodyContent(unit, type, binding.getName(), true, bodyStatement, delimiter); if (placeHolder != null) { ReturnStatement todoNode= (ReturnStatement) rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT); body.statements().add(todoNode); } } else { ReturnStatement statementNode= (ReturnStatement) rewrite.createStringPlaceholder(bodyStatement, ASTNode.RETURN_STATEMENT); body.statements().add(statementNode); } if (settings != null && settings.createComments) { String string= CodeGeneration.getMethodComment(unit, type, decl, binding, delimiter); if (string != null) { Javadoc javadoc= (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC); decl.setJavadoc(javadoc); } } return decl; }
Example 7
Source File: StubUtility2.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
public static MethodDeclaration createConstructorStub(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, ImportRewriteContext context, ITypeBinding typeBinding, IMethodBinding superConstructor, IVariableBinding[] variableBindings, int modifiers, CodeGenerationSettings settings) throws CoreException { AST ast= rewrite.getAST(); MethodDeclaration decl= ast.newMethodDeclaration(); decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, modifiers & ~Modifier.ABSTRACT & ~Modifier.NATIVE)); decl.setName(ast.newSimpleName(typeBinding.getName())); decl.setConstructor(true); List<SingleVariableDeclaration> parameters= decl.parameters(); if (superConstructor != null) { createTypeParameters(imports, context, ast, superConstructor, decl); createParameters(unit.getJavaProject(), imports, context, ast, superConstructor, null, decl); createThrownExceptions(decl, superConstructor, imports, context, ast); } Block body= ast.newBlock(); decl.setBody(body); String delimiter= StubUtility.getLineDelimiterUsed(unit); if (superConstructor != null) { SuperConstructorInvocation invocation= ast.newSuperConstructorInvocation(); SingleVariableDeclaration varDecl= null; for (Iterator<SingleVariableDeclaration> iterator= parameters.iterator(); iterator.hasNext();) { varDecl= iterator.next(); invocation.arguments().add(ast.newSimpleName(varDecl.getName().getIdentifier())); } body.statements().add(invocation); } List<String> prohibited= new ArrayList<String>(); for (final Iterator<SingleVariableDeclaration> iterator= parameters.iterator(); iterator.hasNext();) prohibited.add(iterator.next().getName().getIdentifier()); String param= null; List<String> list= new ArrayList<String>(prohibited); String[] excluded= null; for (int i= 0; i < variableBindings.length; i++) { SingleVariableDeclaration var= ast.newSingleVariableDeclaration(); var.setType(imports.addImport(variableBindings[i].getType(), ast, context)); excluded= new String[list.size()]; list.toArray(excluded); param= suggestParameterName(unit, variableBindings[i], excluded); list.add(param); var.setName(ast.newSimpleName(param)); parameters.add(var); } list= new ArrayList<String>(prohibited); for (int i= 0; i < variableBindings.length; i++) { excluded= new String[list.size()]; list.toArray(excluded); final String paramName= suggestParameterName(unit, variableBindings[i], excluded); list.add(paramName); final String fieldName= variableBindings[i].getName(); Expression expression= null; if (paramName.equals(fieldName) || settings.useKeywordThis) { FieldAccess access= ast.newFieldAccess(); access.setExpression(ast.newThisExpression()); access.setName(ast.newSimpleName(fieldName)); expression= access; } else expression= ast.newSimpleName(fieldName); Assignment assignment= ast.newAssignment(); assignment.setLeftHandSide(expression); assignment.setRightHandSide(ast.newSimpleName(paramName)); assignment.setOperator(Assignment.Operator.ASSIGN); body.statements().add(ast.newExpressionStatement(assignment)); } if (settings != null && settings.createComments) { String string= CodeGeneration.getMethodComment(unit, typeBinding.getName(), decl, superConstructor, delimiter); if (string != null) { Javadoc javadoc= (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC); decl.setJavadoc(javadoc); } } return decl; }
Example 8
Source File: ConstructorFromSuperclassProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private MethodDeclaration createNewMethodDeclaration(AST ast, IMethodBinding binding, ASTRewrite rewrite, ImportRewriteContext importRewriteContext, CodeGenerationSettings commentSettings) throws CoreException { String name= fTypeNode.getName().getIdentifier(); MethodDeclaration decl= ast.newMethodDeclaration(); decl.setConstructor(true); decl.setName(ast.newSimpleName(name)); Block body= ast.newBlock(); decl.setBody(body); SuperConstructorInvocation invocation= null; List<SingleVariableDeclaration> parameters= decl.parameters(); String[] paramNames= getArgumentNames(binding); ITypeBinding enclosingInstance= getEnclosingInstance(); if (enclosingInstance != null) { invocation= addEnclosingInstanceAccess(rewrite, importRewriteContext, parameters, paramNames, enclosingInstance); } if (binding == null) { decl.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); } else { decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, binding.getModifiers())); ITypeBinding[] params= binding.getParameterTypes(); for (int i= 0; i < params.length; i++) { SingleVariableDeclaration var= ast.newSingleVariableDeclaration(); var.setType(getImportRewrite().addImport(params[i], ast, importRewriteContext)); var.setName(ast.newSimpleName(paramNames[i])); parameters.add(var); } List<Type> thrownExceptions= decl.thrownExceptionTypes(); ITypeBinding[] excTypes= binding.getExceptionTypes(); for (int i= 0; i < excTypes.length; i++) { Type excType= getImportRewrite().addImport(excTypes[i], ast, importRewriteContext); thrownExceptions.add(excType); } if (invocation == null) { invocation= ast.newSuperConstructorInvocation(); } List<Expression> arguments= invocation.arguments(); for (int i= 0; i < paramNames.length; i++) { Name argument= ast.newSimpleName(paramNames[i]); arguments.add(argument); addLinkedPosition(rewrite.track(argument), false, "arg_name_" + paramNames[i]); //$NON-NLS-1$ } } String bodyStatement= (invocation == null) ? "" : ASTNodes.asFormattedString(invocation, 0, String.valueOf('\n'), getCompilationUnit().getJavaProject().getOptions(true)); //$NON-NLS-1$ String placeHolder= CodeGeneration.getMethodBodyContent(getCompilationUnit(), name, name, true, bodyStatement, String.valueOf('\n')); if (placeHolder != null) { ASTNode todoNode= rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT); body.statements().add(todoNode); } if (commentSettings != null) { String string= CodeGeneration.getMethodComment(getCompilationUnit(), name, decl, null, String.valueOf('\n')); if (string != null) { Javadoc javadoc= (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC); decl.setJavadoc(javadoc); } } return decl; }