use of org.eclipse.jdt.core.dom.ThisExpression in project eclipse.jdt.ls by eclipse.
the class UnresolvedElementsSubProcessor method addQualifierToOuterProposal.
private static void addQualifierToOuterProposal(IInvocationContext context, MethodInvocation invocationNode, IMethodBinding binding, Collection<CUCorrectionProposal> proposals) {
ITypeBinding declaringType = binding.getDeclaringClass();
ITypeBinding parentType = Bindings.getBindingOfParentType(invocationNode);
ITypeBinding currType = parentType;
boolean isInstanceMethod = !Modifier.isStatic(binding.getModifiers());
while (currType != null && !Bindings.isSuperType(declaringType, currType)) {
if (isInstanceMethod && Modifier.isStatic(currType.getModifiers())) {
return;
}
currType = currType.getDeclaringClass();
}
if (currType == null || currType == parentType) {
return;
}
ASTRewrite rewrite = ASTRewrite.create(invocationNode.getAST());
String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changetoouter_description, org.eclipse.jdt.ls.core.internal.corrections.ASTResolving.getTypeSignature(currType));
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.QUALIFY_WITH_ENCLOSING_TYPE);
ImportRewrite imports = proposal.createImportRewrite(context.getASTRoot());
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(invocationNode, imports);
AST ast = invocationNode.getAST();
String qualifier = imports.addImport(currType, importRewriteContext);
Name name = ASTNodeFactory.newName(ast, qualifier);
Expression newExpression;
if (isInstanceMethod) {
ThisExpression expr = ast.newThisExpression();
expr.setQualifier(name);
newExpression = expr;
} else {
newExpression = name;
}
rewrite.set(invocationNode, MethodInvocation.EXPRESSION_PROPERTY, newExpression, null);
proposals.add(proposal);
}
use of org.eclipse.jdt.core.dom.ThisExpression in project eclipse.jdt.ls by eclipse.
the class UnresolvedElementsSubProcessor method addMissingCastParentsProposal.
private static void addMissingCastParentsProposal(ICompilationUnit cu, MethodInvocation invocationNode, Collection<CUCorrectionProposal> proposals) {
Expression sender = invocationNode.getExpression();
if (sender instanceof ThisExpression) {
return;
}
ITypeBinding senderBinding = sender.resolveTypeBinding();
if (senderBinding == null || Modifier.isFinal(senderBinding.getModifiers())) {
return;
}
if (sender instanceof Name && ((Name) sender).resolveBinding() instanceof ITypeBinding) {
// static access
return;
}
ASTNode parent = invocationNode.getParent();
while (parent instanceof Expression && parent.getNodeType() != ASTNode.CAST_EXPRESSION) {
parent = parent.getParent();
}
boolean hasCastProposal = false;
if (parent instanceof CastExpression) {
// (TestCase) x.getName() -> ((TestCase) x).getName
hasCastProposal = useExistingParentCastProposal(cu, (CastExpression) parent, sender, invocationNode.getName(), getArgumentTypes(invocationNode.arguments()), proposals);
}
if (!hasCastProposal) {
// x.getName() -> ((TestCase) x).getName
Expression target = sender;
while (target instanceof ParenthesizedExpression) {
target = ((ParenthesizedExpression) target).getExpression();
}
String label;
if (target.getNodeType() != ASTNode.CAST_EXPRESSION) {
String targetName = null;
if (target.getLength() <= 18) {
targetName = ASTNodes.asString(target);
}
if (targetName == null) {
label = CorrectionMessages.UnresolvedElementsSubProcessor_methodtargetcast_description;
} else {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_methodtargetcast2_description, BasicElementLabels.getJavaCodeString(targetName));
}
} else {
String targetName = null;
if (target.getLength() <= 18) {
targetName = ASTNodes.asString(((CastExpression) target).getExpression());
}
if (targetName == null) {
label = CorrectionMessages.UnresolvedElementsSubProcessor_changemethodtargetcast_description;
} else {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changemethodtargetcast2_description, BasicElementLabels.getJavaCodeString(targetName));
}
}
proposals.add(new CastCorrectionProposal(label, cu, target, (ITypeBinding) null, IProposalRelevance.CHANGE_CAST));
}
}
use of org.eclipse.jdt.core.dom.ThisExpression in project eclipse.jdt.ls by eclipse.
the class ExtractMethodRefactoring method createCallNodes.
// ---- Code generation -----------------------------------------------------------------------
private ASTNode[] createCallNodes(SnippetFinder.Match duplicate, int modifiers) {
List<ASTNode> result = new ArrayList<>(2);
IVariableBinding[] locals = fAnalyzer.getCallerLocals();
for (int i = 0; i < locals.length; i++) {
result.add(createDeclaration(locals[i], null));
}
MethodInvocation invocation = fAST.newMethodInvocation();
invocation.setName(fAST.newSimpleName(fMethodName));
ASTNode typeNode = ASTResolving.findParentType(fAnalyzer.getEnclosingBodyDeclaration());
RefactoringStatus status = new RefactoringStatus();
while (fDestination != typeNode) {
fAnalyzer.checkInput(status, fMethodName, typeNode);
if (!status.isOK()) {
SimpleName destinationTypeName = fAST.newSimpleName(ASTNodes.getEnclosingType(fDestination).getName());
if ((modifiers & Modifier.STATIC) == 0) {
ThisExpression thisExpression = fAST.newThisExpression();
thisExpression.setQualifier(destinationTypeName);
invocation.setExpression(thisExpression);
} else {
invocation.setExpression(destinationTypeName);
}
break;
}
typeNode = typeNode.getParent();
}
List<Expression> arguments = invocation.arguments();
for (int i = 0; i < fParameterInfos.size(); i++) {
ParameterInfo parameter = fParameterInfos.get(i);
arguments.add(ASTNodeFactory.newName(fAST, getMappedName(duplicate, parameter)));
}
if (fLinkedProposalModel != null) {
LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
nameGroup.addPosition(fRewriter.track(invocation.getName()), false);
}
ASTNode call;
int returnKind = fAnalyzer.getReturnKind();
switch(returnKind) {
case ExtractMethodAnalyzer.ACCESS_TO_LOCAL:
IVariableBinding binding = fAnalyzer.getReturnLocal();
if (binding != null) {
VariableDeclarationStatement decl = createDeclaration(getMappedBinding(duplicate, binding), invocation);
call = decl;
} else {
Assignment assignment = fAST.newAssignment();
assignment.setLeftHandSide(ASTNodeFactory.newName(fAST, getMappedBinding(duplicate, fAnalyzer.getReturnValue()).getName()));
assignment.setRightHandSide(invocation);
call = assignment;
}
break;
case ExtractMethodAnalyzer.RETURN_STATEMENT_VALUE:
ReturnStatement rs = fAST.newReturnStatement();
rs.setExpression(invocation);
call = rs;
break;
default:
call = invocation;
}
if (call instanceof Expression && !fAnalyzer.isExpressionSelected()) {
call = fAST.newExpressionStatement((Expression) call);
}
result.add(call);
// return;
if (returnKind == ExtractMethodAnalyzer.RETURN_STATEMENT_VOID && !fAnalyzer.isLastStatementSelected()) {
result.add(fAST.newReturnStatement());
}
return result.toArray(new ASTNode[result.size()]);
}
use of org.eclipse.jdt.core.dom.ThisExpression in project jbosstools-hibernate by jbosstools.
the class ProcessEntityInfo method visit.
@SuppressWarnings("unchecked")
public boolean visit(TypeDeclaration node) {
ITypeBinding typeBinding = node.resolveBinding();
String nodeName = typeBinding == null ? null : typeBinding.getBinaryName();
if (nodeName == null) {
return false;
}
entityInfo = entityInfos.getEntityInfo(nodeName);
if (entityInfo == null) {
return false;
}
if (entityInfo.isAddMappedSuperclassFlag()) {
MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_MAPPEDSUPERCLASS));
ListRewrite lrw = rewriter.getListRewrite(node, TypeDeclaration.MODIFIERS2_PROPERTY);
lrw.insertFirst(matd, null);
}
if (entityInfo.isAddEntityFlag()) {
MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_ENTITY));
ListRewrite lrw = rewriter.getListRewrite(node, TypeDeclaration.MODIFIERS2_PROPERTY);
lrw.insertFirst(matd, null);
}
/**
* /
* if (!entityInfo.isImplicitConstructorFlag() && !entityInfo.isDefaultConstructorFlag() &&
* entityInfo.isAddSerializableInterfaceFlag()) {
* // add serializable interface
* SimpleName sn = null;
* //if (!entityInfo.isAddSerializableInterfaceImportFlag()) {
* sn = rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_SERIALIZABLE);
* //}
* //else {
* // sn = rewriter.getAST().newSimpleName(JPAConst.IMPORT_SERIALIZABLE);
* //}
* SimpleType st = rewriter.getAST().newSimpleType(sn);
* ListRewrite lrw = rewriter.getListRewrite(node, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
* lrw.insertFirst(st, null);
* // add "private static final long serialVersionUID = 1L;"
* // ...
* }
* /*
*/
if (!entityInfo.isImplicitConstructorFlag() && !entityInfo.isDefaultConstructorFlag() && entityInfo.isAddSerializableInterfaceFlag()) {
MethodDeclaration md = rewriter.getAST().newMethodDeclaration();
md.setConstructor(true);
Modifier modifier = rewriter.getAST().newModifier(Modifier.ModifierKeyword.PROTECTED_KEYWORD);
md.modifiers().add(modifier);
Block body = rewriter.getAST().newBlock();
md.setBody(body);
SimpleName sn = rewriter.getAST().newSimpleName(entityInfo.getName());
md.setName(sn);
ListRewrite lrw = rewriter.getListRewrite(node, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
List<?> list = lrw.getOriginalList();
MethodDeclaration insertBeforeNode = null;
Iterator<?> it = list.iterator();
while (it.hasNext()) {
Object obj = it.next();
if (obj instanceof MethodDeclaration) {
insertBeforeNode = (MethodDeclaration) obj;
break;
}
}
if (insertBeforeNode == null) {
lrw.insertLast(md, null);
} else {
lrw.insertBefore(md, insertBeforeNode, null);
}
}
if (enableOptLock && entityInfo.isAddVersionFlag() && !entityInfo.hasVersionAnnotation()) {
// add property "version", add getter/setter getVersion/setVersion,
// add annotation for the property or for the getter
//
// $NON-NLS-1$
final String version = "version";
// $NON-NLS-1$
final String versionType = "Integer";
//
VariableDeclarationFragment vdFragment = rewriter.getAST().newVariableDeclarationFragment();
SimpleName variableName = rewriter.getAST().newSimpleName(version);
vdFragment.setName(variableName);
FieldDeclaration fieldVersion = rewriter.getAST().newFieldDeclaration(vdFragment);
Modifier modifier = rewriter.getAST().newModifier(Modifier.ModifierKeyword.PROTECTED_KEYWORD);
fieldVersion.modifiers().add(modifier);
Name typeName = rewriter.getAST().newName(versionType);
SimpleType type = rewriter.getAST().newSimpleType(typeName);
fieldVersion.setType(type);
//
MethodDeclaration mdGetter = rewriter.getAST().newMethodDeclaration();
modifier = rewriter.getAST().newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
mdGetter.modifiers().add(modifier);
Block body = rewriter.getAST().newBlock();
ReturnStatement returnVersion = rewriter.getAST().newReturnStatement();
variableName = rewriter.getAST().newSimpleName(version);
returnVersion.setExpression(variableName);
body.statements().add(returnVersion);
mdGetter.setBody(body);
// $NON-NLS-1$
SimpleName sn = rewriter.getAST().newSimpleName("getVersion");
mdGetter.setName(sn);
typeName = rewriter.getAST().newName(versionType);
type = rewriter.getAST().newSimpleType(typeName);
mdGetter.setReturnType2(type);
//
MethodDeclaration mdSetter = rewriter.getAST().newMethodDeclaration();
modifier = rewriter.getAST().newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
mdSetter.modifiers().add(modifier);
body = rewriter.getAST().newBlock();
Assignment assignment = rewriter.getAST().newAssignment();
FieldAccess fieldAccess = rewriter.getAST().newFieldAccess();
ThisExpression thisExpression = rewriter.getAST().newThisExpression();
fieldAccess.setExpression(thisExpression);
variableName = rewriter.getAST().newSimpleName(version);
fieldAccess.setName(variableName);
assignment.setLeftHandSide(fieldAccess);
variableName = rewriter.getAST().newSimpleName(version);
assignment.setRightHandSide(variableName);
ExpressionStatement expressionStatement = rewriter.getAST().newExpressionStatement(assignment);
body.statements().add(expressionStatement);
mdSetter.setBody(body);
// $NON-NLS-1$
sn = rewriter.getAST().newSimpleName("setVersion");
mdSetter.setName(sn);
SingleVariableDeclaration svd = rewriter.getAST().newSingleVariableDeclaration();
variableName = rewriter.getAST().newSimpleName(version);
svd.setName(variableName);
typeName = rewriter.getAST().newName(versionType);
type = rewriter.getAST().newSimpleType(typeName);
svd.setType(type);
mdSetter.parameters().add(svd);
//
ListRewrite lrw = rewriter.getListRewrite(node, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
if (entityInfo.getVersionFieldGetter() != FieldGetterType.FIELD && entityInfo.getVersionFieldGetter() != FieldGetterType.FIELD_GETTER) {
lrw.insertLast(fieldVersion, null);
}
if (entityInfo.getVersionFieldGetter() != FieldGetterType.GETTER && entityInfo.getVersionFieldGetter() != FieldGetterType.FIELD_GETTER) {
lrw.insertLast(mdGetter, null);
lrw.insertLast(mdSetter, null);
}
if (annotationStyle == AnnotStyle.FIELDS) {
MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_VERSION));
lrw = rewriter.getListRewrite(fieldVersion, FieldDeclaration.MODIFIERS2_PROPERTY);
lrw.insertFirst(matd, null);
} else if (annotationStyle == AnnotStyle.GETTERS) {
MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_VERSION));
lrw = rewriter.getListRewrite(mdGetter, MethodDeclaration.MODIFIERS2_PROPERTY);
lrw.insertFirst(matd, null);
}
}
return true;
}
use of org.eclipse.jdt.core.dom.ThisExpression in project eclipse-cs by checkstyle.
the class RequireThisQuickfix method handleGetCorrectingASTVisitor.
/**
* {@inheritDoc}
*/
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
return new ASTVisitor() {
@Override
public boolean visit(final SimpleName node) {
if (containsPosition(node, markerStartOffset)) {
replace(node, findFieldReplacement(node, node, 0));
}
return false;
}
@Override
public boolean visit(final MethodInvocation node) {
if (containsPosition(node, markerStartOffset)) {
replace(node, findMethodReplacement(node.getName(), node, node, 0));
}
return false;
}
private Expression findFieldReplacement(final SimpleName name, final ASTNode node, int typeLevel) {
int level = typeLevel;
final ASTNode parent = node.getParent();
if (parent instanceof TypeDeclaration) {
level++;
final TypeDeclaration type = (TypeDeclaration) parent;
for (final FieldDeclaration fieldDeclaration : type.getFields()) {
@SuppressWarnings("unchecked") final List<VariableDeclarationFragment> fragments = fieldDeclaration.fragments();
for (final VariableDeclarationFragment fragment : fragments) {
if (name.getFullyQualifiedName().equals(fragment.getName().getFullyQualifiedName())) {
return createFieldAccessReplacement(level == 1 ? null : type, name);
}
}
}
}
return findFieldReplacement(name, parent, level);
}
private FieldAccess createFieldAccessReplacement(final TypeDeclaration type, final SimpleName name) {
final AST ast = name.getAST();
final FieldAccess fieldAccess = ast.newFieldAccess();
final ThisExpression thisExpr = ast.newThisExpression();
if (type != null) {
thisExpr.setQualifier(copy(type.getName()));
}
fieldAccess.setExpression(thisExpr);
fieldAccess.setName(copy(name));
return fieldAccess;
}
private Expression findMethodReplacement(final SimpleName name, ASTNode contextNode, final MethodInvocation node, int typeLevel) {
int level = typeLevel;
final ASTNode parent = contextNode.getParent();
if (parent instanceof TypeDeclaration) {
level++;
final TypeDeclaration type = (TypeDeclaration) parent;
for (final MethodDeclaration methodDeclaration : type.getMethods()) {
if (name.getFullyQualifiedName().equals(methodDeclaration.getName().getFullyQualifiedName())) {
return createMethodInvocationReplacement(level == 1 ? null : type, node);
}
}
}
return findMethodReplacement(name, parent, node, level);
}
private Expression createMethodInvocationReplacement(final TypeDeclaration type, MethodInvocation origMethodInvocation) {
final AST ast = origMethodInvocation.getAST();
final MethodInvocation methodInvocation = copy(origMethodInvocation);
final ThisExpression thisExpr = ast.newThisExpression();
if (type != null) {
thisExpr.setQualifier(copy(type.getName()));
}
methodInvocation.setExpression(thisExpr);
return methodInvocation;
}
};
}
Aggregations