use of org.eclipse.jdt.core.dom.SimpleName in project eclipse.jdt.ls by eclipse.
the class SelfEncapsulateFieldProposal method getNewName.
private SimpleName getNewName(ASTRewrite rewrite) {
try {
AST ast = rewrite.getAST();
SimpleName newNameNode = ast.newSimpleName(getFunctionName());
return newNameNode;
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Get newname for getter/setter ", e);
}
return null;
}
use of org.eclipse.jdt.core.dom.SimpleName in project eclipse.jdt.ls by eclipse.
the class SelfEncapsulateFieldProposal method getStub.
protected MethodDeclaration getStub(ASTRewrite rewrite, ASTNode targetTypeDecl) throws CoreException {
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(targetTypeDecl, getImportRewrite());
AST ast = targetTypeDecl.getAST();
MethodDeclaration decl = ast.newMethodDeclaration();
SimpleName newNameNode = getNewName(rewrite);
decl.setName(newNameNode);
addNewModifiers(rewrite, targetTypeDecl, decl.modifiers());
// $NON-NLS-1$
String bodyStatement = "";
boolean isAbstractMethod = Modifier.isAbstract(decl.getModifiers()) || (fSenderBinding.isInterface() && !Modifier.isStatic(decl.getModifiers()) && !Modifier.isDefault(decl.getModifiers()));
Type returnType = getNewMethodType(rewrite, context);
decl.setReturnType2(returnType);
addNewParameters(rewrite, decl.parameters(), context);
// Use default line delimiter, as generated stub has to be formatted anyway
String lineDelim = "\n";
String name = getFunctionName();
if (!isAbstractMethod) {
if (isGetter) {
bodyStatement = CodeGeneration.getGetterMethodBodyContent(fField.getCompilationUnit(), fField.getDeclaringType().getTypeQualifiedName('.'), name, fField.getElementName(), lineDelim);
} else {
String fieldName = fField.getElementName();
boolean isStatic = Flags.isStatic(decl.getModifiers());
String argname = getArgumentName();
if (argname.equals(fieldName) || !isStatic) {
if (isStatic) {
fieldName = fField.getDeclaringType().getElementName() + '.' + fieldName;
} else {
// $NON-NLS-1$
fieldName = "this." + fieldName;
}
}
bodyStatement = CodeGeneration.getSetterMethodBodyContent(fField.getCompilationUnit(), fField.getDeclaringType().getTypeQualifiedName('.'), name, fieldName, argname, lineDelim);
}
}
bodyStatement = bodyStatement.substring(0, bodyStatement.lastIndexOf(lineDelim));
Block body = null;
if (!isAbstractMethod && !Flags.isAbstract(decl.getModifiers())) {
body = ast.newBlock();
if (bodyStatement.length() > 0) {
ASTNode bodyNode = rewrite.createStringPlaceholder(bodyStatement, ASTNode.RETURN_STATEMENT);
body.statements().add(bodyNode);
}
}
decl.setBody(body);
addNewJavadoc(rewrite, decl, context);
return decl;
}
use of org.eclipse.jdt.core.dom.SimpleName in project eclipse.jdt.ls by eclipse.
the class TypeMismatchSubProcessor method addTypeMismatchInForEachProposals.
public static void addTypeMismatchInForEachProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) {
CompilationUnit astRoot = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(astRoot);
if (selectedNode == null || selectedNode.getLocationInParent() != EnhancedForStatement.EXPRESSION_PROPERTY) {
return;
}
EnhancedForStatement forStatement = (EnhancedForStatement) selectedNode.getParent();
ITypeBinding expressionBinding = forStatement.getExpression().resolveTypeBinding();
if (expressionBinding == null) {
return;
}
ITypeBinding expectedBinding;
if (expressionBinding.isArray()) {
expectedBinding = expressionBinding.getComponentType();
} else {
// $NON-NLS-1$
IMethodBinding iteratorMethod = Bindings.findMethodInHierarchy(expressionBinding, "iterator", new String[0]);
if (iteratorMethod == null) {
return;
}
ITypeBinding[] typeArguments = iteratorMethod.getReturnType().getTypeArguments();
if (typeArguments.length != 1) {
return;
}
expectedBinding = typeArguments[0];
}
AST ast = astRoot.getAST();
expectedBinding = Bindings.normalizeForDeclarationUse(expectedBinding, ast);
SingleVariableDeclaration parameter = forStatement.getParameter();
ICompilationUnit cu = context.getCompilationUnit();
if (parameter.getName().getLength() == 0) {
SimpleName simpleName = null;
if (parameter.getType() instanceof SimpleType) {
SimpleType type = (SimpleType) parameter.getType();
if (type.getName() instanceof SimpleName) {
simpleName = (SimpleName) type.getName();
}
} else if (parameter.getType() instanceof NameQualifiedType) {
simpleName = ((NameQualifiedType) parameter.getType()).getName();
}
if (simpleName != null) {
String name = simpleName.getIdentifier();
int relevance = StubUtility.hasLocalVariableName(cu.getJavaProject(), name) ? 10 : 7;
String label = Messages.format(CorrectionMessages.TypeMismatchSubProcessor_create_loop_variable_description, BasicElementLabels.getJavaElementName(name));
proposals.add(new NewVariableCorrectionProposal(label, cu, NewVariableCorrectionProposal.LOCAL, simpleName, null, relevance));
return;
}
}
String label = Messages.format(CorrectionMessages.TypeMismatchSubProcessor_incompatible_for_each_type_description, new String[] { BasicElementLabels.getJavaElementName(parameter.getName().getIdentifier()), BindingLabelProvider.getBindingLabel(expectedBinding, BindingLabelProvider.DEFAULT_TEXTFLAGS) });
ASTRewrite rewrite = ASTRewrite.create(ast);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.INCOMPATIBLE_FOREACH_TYPE);
ImportRewrite importRewrite = proposal.createImportRewrite(astRoot);
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(ASTResolving.findParentBodyDeclaration(selectedNode), importRewrite);
Type newType = importRewrite.addImport(expectedBinding, ast, importRewriteContext, TypeLocation.LOCAL_VARIABLE);
rewrite.replace(parameter.getType(), newType, null);
proposals.add(proposal);
}
use of org.eclipse.jdt.core.dom.SimpleName in project eclipse.jdt.ls by eclipse.
the class UnresolvedElementsSubProcessor method getArrayAccessProposals.
public static void getArrayAccessProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) {
CompilationUnit root = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(root);
if (!(selectedNode instanceof MethodInvocation)) {
return;
}
MethodInvocation decl = (MethodInvocation) selectedNode;
SimpleName nameNode = decl.getName();
String methodName = nameNode.getIdentifier();
IBinding[] bindings = (new ScopeAnalyzer(root)).getDeclarationsInScope(nameNode, ScopeAnalyzer.METHODS);
for (int i = 0; i < bindings.length; i++) {
String currName = bindings[i].getName();
if (NameMatcher.isSimilarName(methodName, currName)) {
String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_arraychangetomethod_description, BasicElementLabels.getJavaElementName(currName));
proposals.add(new RenameNodeCorrectionProposal(label, context.getCompilationUnit(), nameNode.getStartPosition(), nameNode.getLength(), currName, IProposalRelevance.ARRAY_CHANGE_TO_METHOD));
}
}
// always suggest 'length'
// $NON-NLS-1$
String lengthId = "length";
String label = CorrectionMessages.UnresolvedElementsSubProcessor_arraychangetolength_description;
int offset = nameNode.getStartPosition();
int length = decl.getStartPosition() + decl.getLength() - offset;
proposals.add(new RenameNodeCorrectionProposal(label, context.getCompilationUnit(), offset, length, lengthId, IProposalRelevance.ARRAY_CHANGE_TO_LENGTH));
}
use of org.eclipse.jdt.core.dom.SimpleName in project eclipse.jdt.ls by eclipse.
the class UnresolvedElementsSubProcessor method addEnhancedForWithoutTypeProposals.
private static void addEnhancedForWithoutTypeProposals(ICompilationUnit cu, ASTNode selectedNode, Collection<CUCorrectionProposal> proposals) {
if (selectedNode instanceof SimpleName && (selectedNode.getLocationInParent() == SimpleType.NAME_PROPERTY || selectedNode.getLocationInParent() == NameQualifiedType.NAME_PROPERTY)) {
ASTNode type = selectedNode.getParent();
if (type.getLocationInParent() == SingleVariableDeclaration.TYPE_PROPERTY) {
SingleVariableDeclaration svd = (SingleVariableDeclaration) type.getParent();
if (svd.getLocationInParent() == EnhancedForStatement.PARAMETER_PROPERTY) {
if (svd.getName().getLength() == 0) {
SimpleName simpleName = (SimpleName) selectedNode;
String name = simpleName.getIdentifier();
int relevance = StubUtility.hasLocalVariableName(cu.getJavaProject(), name) ? 10 : 7;
String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_create_loop_variable_description, BasicElementLabels.getJavaElementName(name));
proposals.add(new NewVariableCorrectionProposal(label, cu, NewVariableCorrectionProposal.LOCAL, simpleName, null, relevance));
}
}
}
}
}
Aggregations