use of org.eclipse.jdt.core.dom.MethodDeclaration in project xtext-xtend by eclipse.
the class JavaASTFlattener method visit.
@Override
public boolean visit(final MethodDeclaration it) {
Javadoc _javadoc = it.getJavadoc();
boolean _tripleNotEquals = (_javadoc != null);
if (_tripleNotEquals) {
it.getJavadoc().accept(this);
}
final Function1<ASTNode, StringBuffer> _function = (ASTNode node) -> {
StringBuffer _xifexpression = null;
if ((node instanceof MethodDeclaration)) {
StringBuffer _xifexpression_1 = null;
boolean _isConstructor = ((MethodDeclaration) node).isConstructor();
boolean _not = (!_isConstructor);
if (_not) {
StringBuffer _xifexpression_2 = null;
boolean _isOverrideMethod = this._aSTFlattenerUtils.isOverrideMethod(((MethodDeclaration) node));
if (_isOverrideMethod) {
_xifexpression_2 = this.appendToBuffer("override ");
} else {
_xifexpression_2 = this.appendToBuffer("def ");
}
_xifexpression_1 = _xifexpression_2;
}
_xifexpression = _xifexpression_1;
}
return _xifexpression;
};
final Function1<ASTNode, StringBuffer> afterAnnotationProcessingCallback = _function;
this.appendModifiers(it, it.modifiers(), afterAnnotationProcessingCallback);
boolean _isPackageVisibility = this._aSTFlattenerUtils.isPackageVisibility(Iterables.<Modifier>filter(it.modifiers(), Modifier.class));
if (_isPackageVisibility) {
ASTNode _parent = it.getParent();
if ((_parent instanceof TypeDeclaration)) {
ASTNode _parent_1 = it.getParent();
boolean _isInterface = ((TypeDeclaration) _parent_1).isInterface();
boolean _not = (!_isInterface);
if (_not) {
this.appendToBuffer("package ");
}
}
}
boolean _isConstructor = it.isConstructor();
if (_isConstructor) {
this.appendToBuffer(" new");
}
boolean _isEmpty = it.typeParameters().isEmpty();
boolean _not_1 = (!_isEmpty);
if (_not_1) {
boolean _isConstructor_1 = it.isConstructor();
if (_isConstructor_1) {
this.addProblem(it, "Type parameters for constructors are not supported");
}
this.appendTypeParameters(it.typeParameters());
}
boolean _isConstructor_2 = it.isConstructor();
boolean _not_2 = (!_isConstructor_2);
if (_not_2) {
Type _returnType2 = it.getReturnType2();
boolean _tripleNotEquals_1 = (_returnType2 != null);
if (_tripleNotEquals_1) {
it.getReturnType2().accept(this);
} else {
this.appendToBuffer("void");
}
this.appendSpaceToBuffer();
it.getName().accept(this);
}
this.appendToBuffer("(");
final Consumer<SingleVariableDeclaration> _function_1 = (SingleVariableDeclaration p) -> {
Boolean _isAssignedInBody = this._aSTFlattenerUtils.isAssignedInBody(it.getBody(), p.getName());
if ((_isAssignedInBody).booleanValue()) {
if ((it.isConstructor() && (!it.getBody().statements().isEmpty()))) {
final Expression firstInBody = IterableExtensions.<Expression>head(this._aSTFlattenerUtils.findAssignmentsInBlock(it.getBody(), p));
if ((firstInBody != null)) {
ConstructorInvocation _findParentOfType = this._aSTFlattenerUtils.<ConstructorInvocation>findParentOfType(firstInBody, ConstructorInvocation.class);
boolean _tripleNotEquals_2 = (_findParentOfType != null);
if (_tripleNotEquals_2) {
this.addProblem(p, "Final parameter modified in constructor call");
} else {
SuperConstructorInvocation _findParentOfType_1 = this._aSTFlattenerUtils.<SuperConstructorInvocation>findParentOfType(firstInBody, SuperConstructorInvocation.class);
boolean _tripleNotEquals_3 = (_findParentOfType_1 != null);
if (_tripleNotEquals_3) {
this.addProblem(p, "Final parameter modified in super constructor call");
}
}
}
}
final VariableDeclarationFragment varFrag = p.getAST().newVariableDeclarationFragment();
varFrag.setName(p.getAST().newSimpleName(p.getName().toString()));
AST _aST = p.getAST();
SimpleName _name = p.getName();
String _plus = (_name + "_finalParam_");
p.setName(_aST.newSimpleName(_plus));
varFrag.setInitializer(p.getAST().newSimpleName(p.getName().toString()));
final VariableDeclarationStatement varDecl = p.getAST().newVariableDeclarationStatement(varFrag);
ASTNode _createInstance = p.getAST().createInstance(SimpleType.class);
final Type typeCopy = ((Type) _createInstance);
varDecl.setType(typeCopy);
it.getBody().statements().add(0, varDecl);
}
};
ListExtensions.<SingleVariableDeclaration>reverseView(it.parameters()).forEach(_function_1);
this.visitAllSeparatedByComma(it.parameters());
this.appendToBuffer(")");
this.appendExtraDimensions(it.getExtraDimensions());
List<? extends ASTNode> throwsTypes = CollectionLiterals.<ASTNode>newArrayList();
boolean _java8orHigher = this.java8orHigher();
boolean _not_3 = (!_java8orHigher);
if (_not_3) {
throwsTypes = it.thrownExceptions();
} else {
throwsTypes = this._aSTFlattenerUtils.genericChildListProperty(it, "thrownExceptionTypes");
}
boolean _isEmpty_1 = throwsTypes.isEmpty();
boolean _not_4 = (!_isEmpty_1);
if (_not_4) {
this.appendToBuffer(" throws ");
this.visitAllSeparatedByComma(throwsTypes);
}
this.appendSpaceToBuffer();
Block _body = it.getBody();
boolean _tripleNotEquals_2 = (_body != null);
if (_tripleNotEquals_2) {
it.getBody().accept(this);
} else {
this.appendLineWrapToBuffer();
}
return false;
}
use of org.eclipse.jdt.core.dom.MethodDeclaration in project AutoRefactor by JnRouvignac.
the class BooleanRefactoring method withThenReturnStmt.
private boolean withThenReturnStmt(IfStatement node, ReturnStatement thenRs, ReturnStatement elseRs) {
ReturnStatement newRs = getReturnStatement(node, thenRs.getExpression(), elseRs.getExpression());
if (newRs != null) {
ctx.getRefactorings().replace(node, newRs);
ctx.getRefactorings().remove(elseRs);
return DO_NOT_VISIT_SUBTREE;
}
final MethodDeclaration md = getAncestor(node, MethodDeclaration.class);
final Type returnType = md.getReturnType2();
if (returnType != null && returnType.isPrimitiveType()) {
final PrimitiveType pt = (PrimitiveType) returnType;
if (PrimitiveType.BOOLEAN.equals(pt.getPrimitiveTypeCode())) {
final Boolean thenBool = getBooleanLiteral(thenRs.getExpression());
final Boolean elseBool = getBooleanLiteral(elseRs.getExpression());
newRs = getReturnStatement(node, thenBool, elseBool, thenRs.getExpression(), elseRs.getExpression());
if (newRs != null) {
ctx.getRefactorings().replace(node, newRs);
ctx.getRefactorings().remove(elseRs);
return DO_NOT_VISIT_SUBTREE;
}
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.MethodDeclaration in project AutoRefactor by JnRouvignac.
the class ASTBuilder method method.
/**
* Builds a new {@link MethodDeclaration} node.
*
* @param modifiers list of modifiers of the method
* @param methodName the method name
* @param parameters list of parameters
* @param block the block of the method
* @return a new method declaration
*/
public MethodDeclaration method(List<IExtendedModifier> modifiers, String methodName, List<SingleVariableDeclaration> parameters, Block block) {
final MethodDeclaration md = ast.newMethodDeclaration();
modifiers(md).addAll(modifiers);
md.setName(simpleName(methodName));
md.parameters().addAll(parameters);
md.setBody(block);
return md;
}
use of org.eclipse.jdt.core.dom.MethodDeclaration in project eclipse-pmd by acanda.
the class UseUtilityClassQuickFix method addPrivateConstructor.
@SuppressWarnings("unchecked")
private void addPrivateConstructor(final TypeDeclaration typeDeclaration, final ASTRewrite rewrite) {
final AST ast = typeDeclaration.getAST();
final MethodDeclaration constructor = (MethodDeclaration) ast.createInstance(MethodDeclaration.class);
constructor.setConstructor(true);
final Modifier modifier = (Modifier) ast.createInstance(Modifier.class);
modifier.setKeyword(ModifierKeyword.PRIVATE_KEYWORD);
constructor.modifiers().add(modifier);
constructor.setName(ASTUtil.copy(typeDeclaration.getName()));
final Block body = (Block) ast.createInstance(Block.class);
constructor.setBody(body);
final ListRewrite statementRewrite = rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY);
final ASTNode comment = rewrite.createStringPlaceholder("// hide constructor of utility class", ASTNode.EMPTY_STATEMENT);
statementRewrite.insertFirst(comment, null);
final int position = findConstructorPosition(typeDeclaration);
final ListRewrite bodyDeclarationRewrite = rewrite.getListRewrite(typeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
bodyDeclarationRewrite.insertAt(constructor, position, null);
}
use of org.eclipse.jdt.core.dom.MethodDeclaration in project flux by eclipse.
the class ASTResolving method getPossibleReferenceBinding.
private static ITypeBinding getPossibleReferenceBinding(ASTNode node) {
ASTNode parent = node.getParent();
switch(parent.getNodeType()) {
case ASTNode.ASSIGNMENT:
Assignment assignment = (Assignment) parent;
if (node.equals(assignment.getLeftHandSide())) {
// field write access: xx= expression
return assignment.getRightHandSide().resolveTypeBinding();
}
// read access
return assignment.getLeftHandSide().resolveTypeBinding();
case ASTNode.INFIX_EXPRESSION:
InfixExpression infix = (InfixExpression) parent;
InfixExpression.Operator op = infix.getOperator();
if (op == InfixExpression.Operator.CONDITIONAL_AND || op == InfixExpression.Operator.CONDITIONAL_OR) {
//$NON-NLS-1$
return infix.getAST().resolveWellKnownType("boolean");
} else if (op == InfixExpression.Operator.LEFT_SHIFT || op == InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED || op == InfixExpression.Operator.RIGHT_SHIFT_SIGNED) {
//$NON-NLS-1$
return infix.getAST().resolveWellKnownType("int");
}
if (node.equals(infix.getLeftOperand())) {
// xx operation expression
ITypeBinding rigthHandBinding = infix.getRightOperand().resolveTypeBinding();
if (rigthHandBinding != null) {
return rigthHandBinding;
}
} else {
// expression operation xx
ITypeBinding leftHandBinding = infix.getLeftOperand().resolveTypeBinding();
if (leftHandBinding != null) {
return leftHandBinding;
}
}
if (op != InfixExpression.Operator.EQUALS && op != InfixExpression.Operator.NOT_EQUALS) {
//$NON-NLS-1$
return infix.getAST().resolveWellKnownType("int");
}
break;
case ASTNode.INSTANCEOF_EXPRESSION:
InstanceofExpression instanceofExpression = (InstanceofExpression) parent;
return instanceofExpression.getRightOperand().resolveBinding();
case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
VariableDeclarationFragment frag = (VariableDeclarationFragment) parent;
if (frag.getInitializer().equals(node)) {
return frag.getName().resolveTypeBinding();
}
break;
case ASTNode.SUPER_METHOD_INVOCATION:
SuperMethodInvocation superMethodInvocation = (SuperMethodInvocation) parent;
IMethodBinding superMethodBinding = ASTNodes.getMethodBinding(superMethodInvocation.getName());
if (superMethodBinding != null) {
return getParameterTypeBinding(node, superMethodInvocation.arguments(), superMethodBinding);
}
break;
case ASTNode.METHOD_INVOCATION:
MethodInvocation methodInvocation = (MethodInvocation) parent;
IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
if (methodBinding != null) {
return getParameterTypeBinding(node, methodInvocation.arguments(), methodBinding);
}
break;
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
{
SuperConstructorInvocation superInvocation = (SuperConstructorInvocation) parent;
IMethodBinding superBinding = superInvocation.resolveConstructorBinding();
if (superBinding != null) {
return getParameterTypeBinding(node, superInvocation.arguments(), superBinding);
}
break;
}
case ASTNode.CONSTRUCTOR_INVOCATION:
{
ConstructorInvocation constrInvocation = (ConstructorInvocation) parent;
IMethodBinding constrBinding = constrInvocation.resolveConstructorBinding();
if (constrBinding != null) {
return getParameterTypeBinding(node, constrInvocation.arguments(), constrBinding);
}
break;
}
case ASTNode.CLASS_INSTANCE_CREATION:
{
ClassInstanceCreation creation = (ClassInstanceCreation) parent;
IMethodBinding creationBinding = creation.resolveConstructorBinding();
if (creationBinding != null) {
return getParameterTypeBinding(node, creation.arguments(), creationBinding);
}
break;
}
case ASTNode.PARENTHESIZED_EXPRESSION:
return guessBindingForReference(parent);
case ASTNode.ARRAY_ACCESS:
if (((ArrayAccess) parent).getIndex().equals(node)) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
} else {
ITypeBinding parentBinding = getPossibleReferenceBinding(parent);
if (parentBinding == null) {
//$NON-NLS-1$
parentBinding = parent.getAST().resolveWellKnownType("java.lang.Object");
}
return parentBinding.createArrayType(1);
}
case ASTNode.ARRAY_CREATION:
if (((ArrayCreation) parent).dimensions().contains(node)) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
}
break;
case ASTNode.ARRAY_INITIALIZER:
ASTNode initializerParent = parent.getParent();
int dim = 1;
while (initializerParent instanceof ArrayInitializer) {
initializerParent = initializerParent.getParent();
dim++;
}
Type creationType = null;
if (initializerParent instanceof ArrayCreation) {
creationType = ((ArrayCreation) initializerParent).getType();
} else if (initializerParent instanceof VariableDeclaration) {
VariableDeclaration varDecl = (VariableDeclaration) initializerParent;
creationType = ASTNodes.getType(varDecl);
dim -= varDecl.getExtraDimensions();
} else if (initializerParent instanceof MemberValuePair) {
String name = ((MemberValuePair) initializerParent).getName().getIdentifier();
IMethodBinding annotMember = findAnnotationMember((Annotation) initializerParent.getParent(), name);
if (annotMember != null) {
return getReducedDimensionBinding(annotMember.getReturnType(), dim);
}
}
if (creationType instanceof ArrayType) {
ITypeBinding creationTypeBinding = ((ArrayType) creationType).resolveBinding();
if (creationTypeBinding != null) {
return Bindings.getComponentType(creationTypeBinding, dim);
}
}
break;
case ASTNode.CONDITIONAL_EXPRESSION:
ConditionalExpression expression = (ConditionalExpression) parent;
if (node.equals(expression.getExpression())) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("boolean");
}
if (node.equals(expression.getElseExpression())) {
return expression.getThenExpression().resolveTypeBinding();
}
return expression.getElseExpression().resolveTypeBinding();
case ASTNode.POSTFIX_EXPRESSION:
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
case ASTNode.PREFIX_EXPRESSION:
if (((PrefixExpression) parent).getOperator() == PrefixExpression.Operator.NOT) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("boolean");
}
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
case ASTNode.IF_STATEMENT:
case ASTNode.WHILE_STATEMENT:
case ASTNode.DO_STATEMENT:
if (node instanceof Expression) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("boolean");
}
break;
case ASTNode.SWITCH_STATEMENT:
if (((SwitchStatement) parent).getExpression().equals(node)) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
}
break;
case ASTNode.RETURN_STATEMENT:
MethodDeclaration decl = ASTResolving.findParentMethodDeclaration(parent);
if (decl != null && !decl.isConstructor()) {
return decl.getReturnType2().resolveBinding();
}
LambdaExpression lambdaExpr = ASTResolving.findEnclosingLambdaExpression(parent);
if (lambdaExpr != null) {
IMethodBinding lambdaMethodBinding = lambdaExpr.resolveMethodBinding();
if (lambdaMethodBinding != null && lambdaMethodBinding.getReturnType() != null) {
return lambdaMethodBinding.getReturnType();
}
}
break;
case ASTNode.CAST_EXPRESSION:
return ((CastExpression) parent).getType().resolveBinding();
case ASTNode.THROW_STATEMENT:
case ASTNode.CATCH_CLAUSE:
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("java.lang.Exception");
case ASTNode.FIELD_ACCESS:
if (node.equals(((FieldAccess) parent).getName())) {
return getPossibleReferenceBinding(parent);
}
break;
case ASTNode.SUPER_FIELD_ACCESS:
return getPossibleReferenceBinding(parent);
case ASTNode.QUALIFIED_NAME:
if (node.equals(((QualifiedName) parent).getName())) {
return getPossibleReferenceBinding(parent);
}
break;
case ASTNode.SWITCH_CASE:
if (node.equals(((SwitchCase) parent).getExpression()) && parent.getParent() instanceof SwitchStatement) {
return ((SwitchStatement) parent.getParent()).getExpression().resolveTypeBinding();
}
break;
case ASTNode.ASSERT_STATEMENT:
if (node.getLocationInParent() == AssertStatement.EXPRESSION_PROPERTY) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("boolean");
}
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("java.lang.String");
case ASTNode.SINGLE_MEMBER_ANNOTATION:
{
//$NON-NLS-1$
IMethodBinding annotMember = findAnnotationMember((Annotation) parent, "value");
if (annotMember != null) {
return annotMember.getReturnType();
}
break;
}
case ASTNode.MEMBER_VALUE_PAIR:
{
String name = ((MemberValuePair) parent).getName().getIdentifier();
IMethodBinding annotMember = findAnnotationMember((Annotation) parent.getParent(), name);
if (annotMember != null) {
return annotMember.getReturnType();
}
break;
}
default:
}
return null;
}
Aggregations