use of org.eclipse.jdt.core.dom.AST in project che by eclipse.
the class IntroduceFactoryRefactoring method createFactoryChange.
/**
* Perform the AST rewriting necessary on the given <code>CompilationUnit</code> to create the
* factory method. The method will reside on the type identified by
* <code>fFactoryOwningClass</code>.
*
* @param unitRewriter the ASTRewrite to be used
* @param unit the <code>CompilationUnit</code> where factory method will be created
* @param gd the <code>GroupDescription</code> to associate with the changes made
* @throws CoreException if an exception occurs while accessing its corresponding resource
*/
private void createFactoryChange(ASTRewrite unitRewriter, CompilationUnit unit, TextEditGroup gd) throws CoreException {
// ================================================================================
// First add the factory itself (method, class, and interface as needed/directed by user)
AST ast = unit.getAST();
fFactoryMethod = createFactoryMethod(ast, fCtorBinding, unitRewriter);
AbstractTypeDeclaration factoryOwner = (AbstractTypeDeclaration) unit.findDeclaringNode(fFactoryOwningClass.resolveBinding().getKey());
fImportRewriter.addImport(fCtorOwningClass.resolveBinding());
int idx = ASTNodes.getInsertionIndex(fFactoryMethod, factoryOwner.bodyDeclarations());
// Guard against bug in getInsertionIndex()
if (idx < 0)
idx = 0;
unitRewriter.getListRewrite(factoryOwner, factoryOwner.getBodyDeclarationsProperty()).insertAt(fFactoryMethod, idx, gd);
}
use of org.eclipse.jdt.core.dom.AST in project che by eclipse.
the class ExtractTempRefactoring method createTempType.
private Type createTempType() throws CoreException {
Expression expression = getSelectedExpression().getAssociatedExpression();
Type resultingType = null;
ITypeBinding typeBinding = expression.resolveTypeBinding();
ASTRewrite rewrite = fCURewrite.getASTRewrite();
AST ast = rewrite.getAST();
if (expression instanceof ClassInstanceCreation && (typeBinding == null || typeBinding.getTypeArguments().length == 0)) {
resultingType = (Type) rewrite.createCopyTarget(((ClassInstanceCreation) expression).getType());
} else if (expression instanceof CastExpression) {
resultingType = (Type) rewrite.createCopyTarget(((CastExpression) expression).getType());
} else {
if (typeBinding == null) {
typeBinding = ASTResolving.guessBindingForReference(expression);
}
if (typeBinding != null) {
typeBinding = Bindings.normalizeForDeclarationUse(typeBinding, ast);
ImportRewrite importRewrite = fCURewrite.getImportRewrite();
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(expression, importRewrite);
resultingType = importRewrite.addImport(typeBinding, ast, context);
} else {
//$NON-NLS-1$
resultingType = ast.newSimpleType(ast.newSimpleName("Object"));
}
}
if (fLinkedProposalModel != null) {
LinkedProposalPositionGroup typeGroup = fLinkedProposalModel.getPositionGroup(KEY_TYPE, true);
typeGroup.addPosition(rewrite.track(resultingType), false);
if (typeBinding != null) {
ITypeBinding[] relaxingTypes = ASTResolving.getNarrowingTypes(ast, typeBinding);
for (int i = 0; i < relaxingTypes.length; i++) {
typeGroup.addProposal(relaxingTypes[i], fCURewrite.getCu(), relaxingTypes.length - i);
}
}
}
return resultingType;
}
use of org.eclipse.jdt.core.dom.AST in project che by eclipse.
the class ExtractMethodAnalyzer method initReturnType.
private void initReturnType(ImportRewrite rewriter) {
AST ast = fEnclosingBodyDeclaration.getAST();
fReturnType = null;
fReturnTypeBinding = null;
switch(fReturnKind) {
case ACCESS_TO_LOCAL:
VariableDeclaration declaration = ASTNodes.findVariableDeclaration(fReturnValue, fEnclosingBodyDeclaration);
fReturnType = ASTNodeFactory.newType(ast, declaration, rewriter, new ContextSensitiveImportRewriteContext(declaration, rewriter));
if (declaration.resolveBinding() != null) {
fReturnTypeBinding = declaration.resolveBinding().getType();
}
break;
case EXPRESSION:
Expression expression = (Expression) getFirstSelectedNode();
if (expression.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
fExpressionBinding = ((ClassInstanceCreation) expression).getType().resolveBinding();
} else {
fExpressionBinding = expression.resolveTypeBinding();
}
if (fExpressionBinding != null) {
if (fExpressionBinding.isNullType()) {
getStatus().addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_null_type, JavaStatusContext.create(fCUnit, expression));
} else {
ITypeBinding normalizedBinding = Bindings.normalizeForDeclarationUse(fExpressionBinding, ast);
if (normalizedBinding != null) {
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fEnclosingBodyDeclaration, rewriter);
fReturnType = rewriter.addImport(normalizedBinding, ast, context);
fReturnTypeBinding = normalizedBinding;
}
}
} else {
fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
//$NON-NLS-1$
fReturnTypeBinding = ast.resolveWellKnownType("void");
getStatus().addError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_determine_return_type, JavaStatusContext.create(fCUnit, expression));
}
break;
case RETURN_STATEMENT_VALUE:
LambdaExpression enclosingLambdaExpr = ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode());
if (enclosingLambdaExpr != null) {
fReturnType = ASTNodeFactory.newReturnType(enclosingLambdaExpr, ast, rewriter, null);
IMethodBinding methodBinding = enclosingLambdaExpr.resolveMethodBinding();
fReturnTypeBinding = methodBinding != null ? methodBinding.getReturnType() : null;
} else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
fReturnType = ((MethodDeclaration) fEnclosingBodyDeclaration).getReturnType2();
fReturnTypeBinding = fReturnType != null ? fReturnType.resolveBinding() : null;
}
break;
default:
fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
//$NON-NLS-1$
fReturnTypeBinding = ast.resolveWellKnownType("void");
}
if (fReturnType == null) {
fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
//$NON-NLS-1$
fReturnTypeBinding = ast.resolveWellKnownType("void");
}
}
use of org.eclipse.jdt.core.dom.AST in project xtext-xtend by eclipse.
the class JavaASTFlattener method visit.
@Override
public boolean visit(final PrefixExpression node) {
final Expression operand = node.getOperand();
PrefixExpression.Operator _operator = node.getOperator();
boolean _matched = false;
if (Objects.equal(_operator, PrefixExpression.Operator.DECREMENT)) {
_matched = true;
}
if (!_matched) {
if (Objects.equal(_operator, PrefixExpression.Operator.INCREMENT)) {
_matched = true;
}
}
if (_matched) {
if ((operand instanceof ArrayAccess)) {
final String arrayName = this.computeArrayName(((ArrayAccess) operand));
StringConcatenation _builder = new StringConcatenation();
_builder.append("_tPreInx_");
_builder.append(arrayName);
final String idxName = _builder.toString();
String op = "-";
PrefixExpression.Operator _operator_1 = node.getOperator();
boolean _equals = Objects.equal(_operator_1, PrefixExpression.Operator.INCREMENT);
if (_equals) {
op = "+";
}
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("{val ");
_builder_1.append(idxName);
_builder_1.append("=");
this.appendToBuffer(_builder_1.toString());
((ArrayAccess) operand).getIndex().accept(this);
StringConcatenation _builder_2 = new StringConcatenation();
_builder_2.append(" ");
_builder_2.append("val ");
_builder_2.append(idxName, " ");
_builder_2.append("_res=");
_builder_2.append(arrayName, " ");
_builder_2.append(".get(");
_builder_2.append(idxName, " ");
_builder_2.append(")");
_builder_2.append(op, " ");
_builder_2.append("1");
this.appendToBuffer(_builder_2.toString());
StringConcatenation _builder_3 = new StringConcatenation();
_builder_3.append(" ");
_builder_3.append(arrayName, " ");
_builder_3.append(".set(");
_builder_3.append(idxName, " ");
_builder_3.append(", ");
_builder_3.append(idxName, " ");
_builder_3.append("_res) ");
_builder_3.append(idxName, " ");
_builder_3.append("_res}");
this.appendToBuffer(_builder_3.toString());
return false;
} else {
final AST dummyAST = AST.newAST(node.getAST().apiLevel());
final Assignment assigment = dummyAST.newAssignment();
final InfixExpression infixOp = dummyAST.newInfixExpression();
ASTNode _copySubtree = ASTNode.copySubtree(dummyAST, operand);
infixOp.setLeftOperand(((Expression) _copySubtree));
PrefixExpression.Operator _operator_2 = node.getOperator();
boolean _equals_1 = Objects.equal(_operator_2, PrefixExpression.Operator.DECREMENT);
if (_equals_1) {
infixOp.setOperator(InfixExpression.Operator.MINUS);
} else {
infixOp.setOperator(InfixExpression.Operator.PLUS);
}
infixOp.setRightOperand(dummyAST.newNumberLiteral("1"));
ASTNode _copySubtree_1 = ASTNode.copySubtree(dummyAST, operand);
final Expression leftSide = ((Expression) _copySubtree_1);
assigment.setLeftHandSide(leftSide);
assigment.setRightHandSide(infixOp);
this.appendToBuffer("{");
Type type = null;
if ((operand instanceof SimpleName)) {
type = this._aSTFlattenerUtils.findDeclaredType(((SimpleName) operand));
}
this.handleAssignment(assigment, leftSide, type);
this.appendToBuffer("}");
return false;
}
}
if (!_matched) {
if (Objects.equal(_operator, PrefixExpression.Operator.COMPLEMENT)) {
_matched = true;
node.getOperand().accept(this);
this.appendToBuffer(".bitwiseNot");
}
}
if (!_matched) {
{
this.appendToBuffer(node.getOperator().toString());
node.getOperand().accept(this);
}
}
return false;
}
use of org.eclipse.jdt.core.dom.AST 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;
}
Aggregations