use of org.eclipse.jdt.core.dom.ReturnStatement in project che by eclipse.
the class DelegateMethodCreator method createReturnStatement.
/**
* Creates a new return statement for the method invocation.
*
* @param invocation the method invocation to create a return statement for
* @return the corresponding statement
*/
private ReturnStatement createReturnStatement(final MethodInvocation invocation) {
Assert.isNotNull(invocation);
final ReturnStatement statement = invocation.getAST().newReturnStatement();
statement.setExpression(invocation);
return statement;
}
use of org.eclipse.jdt.core.dom.ReturnStatement in project che by eclipse.
the class StubUtility2 method createDelegationStub.
public static MethodDeclaration createDelegationStub(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, ImportRewriteContext context, IMethodBinding delegate, IVariableBinding delegatingField, CodeGenerationSettings settings) throws CoreException {
Assert.isNotNull(delegate);
Assert.isNotNull(delegatingField);
Assert.isNotNull(settings);
AST ast = rewrite.getAST();
MethodDeclaration decl = ast.newMethodDeclaration();
decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, delegate.getModifiers() & ~Modifier.SYNCHRONIZED & ~Modifier.ABSTRACT & ~Modifier.NATIVE));
decl.setName(ast.newSimpleName(delegate.getName()));
decl.setConstructor(false);
createTypeParameters(imports, context, ast, delegate, decl);
decl.setReturnType2(imports.addImport(delegate.getReturnType(), ast, context));
List<SingleVariableDeclaration> params = createParameters(unit.getJavaProject(), imports, context, ast, delegate, null, decl);
createThrownExceptions(decl, delegate, imports, context, ast);
Block body = ast.newBlock();
decl.setBody(body);
String delimiter = StubUtility.getLineDelimiterUsed(unit);
Statement statement = null;
MethodInvocation invocation = ast.newMethodInvocation();
invocation.setName(ast.newSimpleName(delegate.getName()));
List<Expression> arguments = invocation.arguments();
for (int i = 0; i < params.size(); i++) arguments.add(ast.newSimpleName(params.get(i).getName().getIdentifier()));
if (settings.useKeywordThis) {
FieldAccess access = ast.newFieldAccess();
access.setExpression(ast.newThisExpression());
access.setName(ast.newSimpleName(delegatingField.getName()));
invocation.setExpression(access);
} else
invocation.setExpression(ast.newSimpleName(delegatingField.getName()));
if (delegate.getReturnType().isPrimitive() && delegate.getReturnType().getName().equals("void")) {
//$NON-NLS-1$
statement = ast.newExpressionStatement(invocation);
} else {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(invocation);
statement = returnStatement;
}
body.statements().add(statement);
ITypeBinding declaringType = delegatingField.getDeclaringClass();
if (declaringType == null) {
// can be null for
return decl;
}
String qualifiedName = declaringType.getQualifiedName();
IPackageBinding packageBinding = declaringType.getPackage();
if (packageBinding != null) {
if (packageBinding.getName().length() > 0 && qualifiedName.startsWith(packageBinding.getName()))
qualifiedName = qualifiedName.substring(packageBinding.getName().length());
}
if (settings.createComments) {
/*
* TODO: have API for delegate method comments This is an inlined
* version of
* {@link CodeGeneration#getMethodComment(ICompilationUnit, String, MethodDeclaration, IMethodBinding, String)}
*/
delegate = delegate.getMethodDeclaration();
String declaringClassQualifiedName = delegate.getDeclaringClass().getQualifiedName();
String linkToMethodName = delegate.getName();
String[] parameterTypesQualifiedNames = StubUtility.getParameterTypeNamesForSeeTag(delegate);
String string = StubUtility.getMethodComment(unit, qualifiedName, decl, delegate.isDeprecated(), linkToMethodName, declaringClassQualifiedName, parameterTypesQualifiedNames, true, delimiter);
if (string != null) {
Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
decl.setJavadoc(javadoc);
}
}
return decl;
}
use of org.eclipse.jdt.core.dom.ReturnStatement in project che by eclipse.
the class StubUtility2 method createConstructorStub.
/* This method should work with all AST levels. */
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);
//$NON-NLS-1$
String bodyStatement = "";
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;
}
use of org.eclipse.jdt.core.dom.ReturnStatement in project che by eclipse.
the class StubUtility2 method createImplementationStub.
public static MethodDeclaration createImplementationStub(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, ImportRewriteContext context, IMethodBinding binding, String[] parameterNames, String type, CodeGenerationSettings settings, boolean inInterface) throws CoreException {
Assert.isNotNull(imports);
Assert.isNotNull(rewrite);
AST ast = rewrite.getAST();
MethodDeclaration decl = ast.newMethodDeclaration();
decl.modifiers().addAll(getImplementationModifiers(ast, binding, inInterface, imports, context));
decl.setName(ast.newSimpleName(binding.getName()));
decl.setConstructor(false);
ITypeBinding bindingReturnType = binding.getReturnType();
if (bindingReturnType.isWildcardType()) {
ITypeBinding bound = bindingReturnType.getBound();
bindingReturnType = (bound != null) ? bound : bindingReturnType.getErasure();
}
IJavaProject javaProject = unit.getJavaProject();
if (JavaModelUtil.is50OrHigher(javaProject)) {
createTypeParameters(imports, context, ast, binding, decl);
} else {
bindingReturnType = bindingReturnType.getErasure();
}
decl.setReturnType2(imports.addImport(bindingReturnType, ast, context));
List<SingleVariableDeclaration> parameters = createParameters(javaProject, imports, context, ast, binding, parameterNames, decl);
createThrownExceptions(decl, binding, imports, context, ast);
String delimiter = unit.findRecommendedLineSeparator();
int modifiers = binding.getModifiers();
if (!(inInterface && Modifier.isAbstract(modifiers))) {
// generate a method body
Map<String, String> options = javaProject.getOptions(true);
Block body = ast.newBlock();
decl.setBody(body);
//$NON-NLS-1$
String bodyStatement = "";
if (Modifier.isAbstract(modifiers)) {
Expression expression = ASTNodeFactory.newDefaultExpression(ast, decl.getReturnType2(), decl.getExtraDimensions());
if (expression != null) {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(expression);
bodyStatement = ASTNodes.asFormattedString(returnStatement, 0, delimiter, options);
}
} else {
SuperMethodInvocation invocation = ast.newSuperMethodInvocation();
ITypeBinding declaringType = binding.getDeclaringClass();
if (declaringType.isInterface()) {
String qualifier = imports.addImport(declaringType.getErasure(), context);
Name name = ASTNodeFactory.newName(ast, qualifier);
invocation.setQualifier(name);
}
invocation.setName(ast.newSimpleName(binding.getName()));
SingleVariableDeclaration varDecl = null;
for (Iterator<SingleVariableDeclaration> iterator = parameters.iterator(); iterator.hasNext(); ) {
varDecl = iterator.next();
invocation.arguments().add(ast.newSimpleName(varDecl.getName().getIdentifier()));
}
Expression expression = invocation;
Type returnType = decl.getReturnType2();
if (returnType instanceof PrimitiveType && ((PrimitiveType) returnType).getPrimitiveTypeCode().equals(PrimitiveType.VOID)) {
bodyStatement = ASTNodes.asFormattedString(ast.newExpressionStatement(expression), 0, delimiter, options);
} else {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(expression);
bodyStatement = ASTNodes.asFormattedString(returnStatement, 0, delimiter, options);
}
}
String placeHolder = CodeGeneration.getMethodBodyContent(unit, type, binding.getName(), false, bodyStatement, delimiter);
if (placeHolder != null) {
ReturnStatement todoNode = (ReturnStatement) rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
body.statements().add(todoNode);
}
}
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);
}
}
if (settings != null && settings.overrideAnnotation && JavaModelUtil.is50OrHigher(javaProject)) {
addOverrideAnnotation(javaProject, rewrite, decl, binding);
}
return decl;
}
use of org.eclipse.jdt.core.dom.ReturnStatement in project che by eclipse.
the class AbstractMethodCorrectionProposal method getStub.
private MethodDeclaration getStub(ASTRewrite rewrite, ASTNode targetTypeDecl) throws CoreException {
AST ast = targetTypeDecl.getAST();
MethodDeclaration decl = ast.newMethodDeclaration();
SimpleName newNameNode = getNewName(rewrite);
decl.setConstructor(isConstructor());
addNewModifiers(rewrite, targetTypeDecl, decl.modifiers());
ArrayList<String> takenNames = new ArrayList<String>();
addNewTypeParameters(rewrite, takenNames, decl.typeParameters());
decl.setName(newNameNode);
IVariableBinding[] declaredFields = fSenderBinding.getDeclaredFields();
for (int i = 0; i < declaredFields.length; i++) {
// avoid to take parameter names that are equal to field names
takenNames.add(declaredFields[i].getName());
}
//$NON-NLS-1$
String bodyStatement = "";
if (!isConstructor()) {
Type returnType = getNewMethodType(rewrite);
decl.setReturnType2(returnType);
boolean isVoid = returnType instanceof PrimitiveType && PrimitiveType.VOID.equals(((PrimitiveType) returnType).getPrimitiveTypeCode());
if (!fSenderBinding.isInterface() && !isVoid) {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(ASTNodeFactory.newDefaultExpression(ast, returnType, 0));
bodyStatement = ASTNodes.asFormattedString(returnStatement, 0, String.valueOf('\n'), getCompilationUnit().getJavaProject().getOptions(true));
}
}
addNewParameters(rewrite, takenNames, decl.parameters());
addNewExceptions(rewrite, decl.thrownExceptionTypes());
Block body = null;
if (!fSenderBinding.isInterface()) {
body = ast.newBlock();
String placeHolder = CodeGeneration.getMethodBodyContent(getCompilationUnit(), fSenderBinding.getName(), newNameNode.getIdentifier(), isConstructor(), bodyStatement, String.valueOf('\n'));
if (placeHolder != null) {
ReturnStatement todoNode = (ReturnStatement) rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
body.statements().add(todoNode);
}
}
decl.setBody(body);
CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(getCompilationUnit().getJavaProject());
if (settings.createComments && !fSenderBinding.isAnonymous()) {
String string = CodeGeneration.getMethodComment(getCompilationUnit(), fSenderBinding.getName(), decl, null, String.valueOf('\n'));
if (string != null) {
Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
decl.setJavadoc(javadoc);
}
}
return decl;
}
Aggregations