use of org.eclipse.jdt.core.dom.BodyDeclaration in project che by eclipse.
the class JavadocTagsSubProcessor method getUnusedAndUndocumentedParameterOrExceptionProposals.
public static void getUnusedAndUndocumentedParameterOrExceptionProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ICompilationUnit cu = context.getCompilationUnit();
IJavaProject project = cu.getJavaProject();
if (!JavaCore.ENABLED.equals(project.getOption(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, true))) {
return;
}
int problemId = problem.getProblemId();
boolean isUnusedTypeParam = problemId == IProblem.UnusedTypeParameter;
boolean isUnusedParam = problemId == IProblem.ArgumentIsNeverUsed || isUnusedTypeParam;
String key = isUnusedParam ? JavaCore.COMPILER_PB_UNUSED_PARAMETER_INCLUDE_DOC_COMMENT_REFERENCE : JavaCore.COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION_INCLUDE_DOC_COMMENT_REFERENCE;
if (!JavaCore.ENABLED.equals(project.getOption(key, true))) {
return;
}
ASTNode node = problem.getCoveringNode(context.getASTRoot());
if (node == null) {
return;
}
BodyDeclaration bodyDecl = ASTResolving.findParentBodyDeclaration(node);
if (bodyDecl == null || ASTResolving.getParentMethodOrTypeBinding(bodyDecl) == null) {
return;
}
String label;
if (isUnusedTypeParam) {
label = CorrectionMessages.JavadocTagsSubProcessor_document_type_parameter_description;
} else if (isUnusedParam) {
label = CorrectionMessages.JavadocTagsSubProcessor_document_parameter_description;
} else {
node = ASTNodes.getNormalizedNode(node);
label = CorrectionMessages.JavadocTagsSubProcessor_document_exception_description;
}
ASTRewriteCorrectionProposal proposal = new AddMissingJavadocTagProposal(label, context.getCompilationUnit(), bodyDecl, node, IProposalRelevance.DOCUMENT_UNUSED_ITEM);
proposals.add(proposal);
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project che by eclipse.
the class JavaTextSelection method resolveInMethodBody.
public boolean resolveInMethodBody() {
if (fInMethodBodyRequested)
return fInMethodBody;
fInMethodBodyRequested = true;
resolveSelectedNodes();
ASTNode node = getStartNode();
if (node == null) {
fInMethodBody = true;
} else {
while (node != null) {
int nodeType = node.getNodeType();
if (nodeType == ASTNode.BLOCK && node.getParent() instanceof BodyDeclaration) {
fInMethodBody = node.getParent().getNodeType() == ASTNode.METHOD_DECLARATION;
break;
} else if (nodeType == ASTNode.ANONYMOUS_CLASS_DECLARATION) {
fInMethodBody = false;
break;
}
node = node.getParent();
}
}
return fInMethodBody;
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project che by eclipse.
the class CodeFormatterUtil method format2.
/**
* Creates edits that describe how to format the given string.
* The given node is used to infer the kind to use to format the string.
* Consider to use {@link #format2(int, String, int, String, java.util.Map)} if the kind is already known.
* Returns <code>null</code> if the code could not be formatted for the given kind.
*
* @param node
* Use to infer the kind of the code snippet to format.
* @param source
* The source to format
* @param indentationLevel
* The initial indentation level, used to shift left/right the entire source fragment.
* An initial indentation level of zero or below has no effect.
* @param lineSeparator
* The line separator to use in formatted source,
* if set to <code>null</code>, then the platform default one will be used.
* @param options
* The options map to use for formatting with the default code formatter.
* Recognized options are documented on {@link org.eclipse.jdt.core.JavaCore#getDefaultOptions()}.
* If set to <code>null</code>, then use the current settings from {@link org.eclipse.jdt.core.JavaCore#getOptions()}.
* @return an TextEdit describing the changes required to format source
* @throws IllegalArgumentException
* If the offset and length are not inside the string, a IllegalArgumentException is thrown.
*/
public static TextEdit format2(ASTNode node, String source, int indentationLevel, String lineSeparator, Map<String, String> options) {
int code;
//$NON-NLS-1$
String prefix = "";
//$NON-NLS-1$
String suffix = "";
if (node instanceof Statement) {
code = CodeFormatter.K_STATEMENTS;
if (node.getNodeType() == ASTNode.SWITCH_CASE) {
//$NON-NLS-1$
prefix = "switch(1) {";
//$NON-NLS-1$
suffix = "}";
code = CodeFormatter.K_STATEMENTS;
}
} else if (node instanceof Expression && node.getNodeType() != ASTNode.VARIABLE_DECLARATION_EXPRESSION) {
code = CodeFormatter.K_EXPRESSION;
} else if (node instanceof BodyDeclaration) {
code = CodeFormatter.K_CLASS_BODY_DECLARATIONS;
} else {
switch(node.getNodeType()) {
case ASTNode.ARRAY_TYPE:
case ASTNode.PARAMETERIZED_TYPE:
case ASTNode.PRIMITIVE_TYPE:
case ASTNode.QUALIFIED_TYPE:
case ASTNode.SIMPLE_TYPE:
//$NON-NLS-1$
suffix = " x;";
code = CodeFormatter.K_CLASS_BODY_DECLARATIONS;
break;
case ASTNode.WILDCARD_TYPE:
//$NON-NLS-1$
prefix = "A<";
//$NON-NLS-1$
suffix = "> x;";
code = CodeFormatter.K_CLASS_BODY_DECLARATIONS;
break;
case ASTNode.COMPILATION_UNIT:
code = CodeFormatter.K_COMPILATION_UNIT;
break;
case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
case ASTNode.SINGLE_VARIABLE_DECLARATION:
//$NON-NLS-1$
suffix = ";";
code = CodeFormatter.K_STATEMENTS;
break;
case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
//$NON-NLS-1$
prefix = "A ";
//$NON-NLS-1$
suffix = ";";
code = CodeFormatter.K_STATEMENTS;
break;
case ASTNode.PACKAGE_DECLARATION:
case ASTNode.IMPORT_DECLARATION:
//$NON-NLS-1$
suffix = "\nclass A {}";
code = CodeFormatter.K_COMPILATION_UNIT;
break;
case ASTNode.JAVADOC:
//$NON-NLS-1$
suffix = "void foo();";
code = CodeFormatter.K_CLASS_BODY_DECLARATIONS;
break;
case ASTNode.CATCH_CLAUSE:
//$NON-NLS-1$
prefix = "try {}";
code = CodeFormatter.K_STATEMENTS;
break;
case ASTNode.ANONYMOUS_CLASS_DECLARATION:
//$NON-NLS-1$
prefix = "new A()";
//$NON-NLS-1$
suffix = ";";
code = CodeFormatter.K_STATEMENTS;
break;
case ASTNode.MEMBER_VALUE_PAIR:
//$NON-NLS-1$
prefix = "@Author(";
//$NON-NLS-1$
suffix = ") class x {}";
code = CodeFormatter.K_COMPILATION_UNIT;
break;
case ASTNode.MODIFIER:
//$NON-NLS-1$
suffix = " class x {}";
code = CodeFormatter.K_COMPILATION_UNIT;
break;
case ASTNode.TYPE_PARAMETER:
//$NON-NLS-1$
prefix = "class X<";
//$NON-NLS-1$
suffix = "> {}";
code = CodeFormatter.K_COMPILATION_UNIT;
break;
case ASTNode.MEMBER_REF:
case ASTNode.METHOD_REF:
case ASTNode.METHOD_REF_PARAMETER:
case ASTNode.TAG_ELEMENT:
case ASTNode.TEXT_ELEMENT:
// Javadoc formatting not yet supported:
return null;
default:
//Assert.isTrue(false, "Node type not covered: " + node.getClass().getName()); //$NON-NLS-1$
return null;
}
}
String concatStr = prefix + source + suffix;
TextEdit edit = format2(code, concatStr, prefix.length(), source.length(), indentationLevel, lineSeparator, options);
if (edit != null && prefix.length() > 0) {
edit.moveTree(-prefix.length());
}
return edit;
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project che by eclipse.
the class TypeMismatchSubProcessor method addTypeMismatchProposals.
public static void addTypeMismatchProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
String[] args = problem.getProblemArguments();
if (args.length != 2) {
return;
}
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
AST ast = astRoot.getAST();
ASTNode selectedNode = problem.getCoveredNode(astRoot);
if (!(selectedNode instanceof Expression)) {
return;
}
Expression nodeToCast = (Expression) selectedNode;
Name receiverNode = null;
ITypeBinding castTypeBinding = null;
int parentNodeType = selectedNode.getParent().getNodeType();
if (parentNodeType == ASTNode.ASSIGNMENT) {
Assignment assign = (Assignment) selectedNode.getParent();
Expression leftHandSide = assign.getLeftHandSide();
if (selectedNode.equals(leftHandSide)) {
nodeToCast = assign.getRightHandSide();
}
castTypeBinding = assign.getLeftHandSide().resolveTypeBinding();
if (leftHandSide instanceof Name) {
receiverNode = (Name) leftHandSide;
} else if (leftHandSide instanceof FieldAccess) {
receiverNode = ((FieldAccess) leftHandSide).getName();
}
} else if (parentNodeType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
VariableDeclarationFragment frag = (VariableDeclarationFragment) selectedNode.getParent();
if (selectedNode.equals(frag.getName()) || selectedNode.equals(frag.getInitializer())) {
nodeToCast = frag.getInitializer();
castTypeBinding = ASTNodes.getType(frag).resolveBinding();
receiverNode = frag.getName();
}
} else if (parentNodeType == ASTNode.MEMBER_VALUE_PAIR) {
receiverNode = ((MemberValuePair) selectedNode.getParent()).getName();
castTypeBinding = ASTResolving.guessBindingForReference(nodeToCast);
} else if (parentNodeType == ASTNode.SINGLE_MEMBER_ANNOTATION) {
// use the type name
receiverNode = ((SingleMemberAnnotation) selectedNode.getParent()).getTypeName();
castTypeBinding = ASTResolving.guessBindingForReference(nodeToCast);
} else {
// try to find the binding corresponding to 'castTypeName'
castTypeBinding = ASTResolving.guessBindingForReference(nodeToCast);
}
if (castTypeBinding == null) {
return;
}
ITypeBinding currBinding = nodeToCast.resolveTypeBinding();
if (!(nodeToCast instanceof ArrayInitializer)) {
ITypeBinding castFixType = null;
if (currBinding == null || castTypeBinding.isCastCompatible(currBinding) || nodeToCast instanceof CastExpression) {
castFixType = castTypeBinding;
} else if (JavaModelUtil.is50OrHigher(cu.getJavaProject())) {
ITypeBinding boxUnboxedTypeBinding = boxUnboxPrimitives(castTypeBinding, currBinding, ast);
if (boxUnboxedTypeBinding != castTypeBinding && boxUnboxedTypeBinding.isCastCompatible(currBinding)) {
castFixType = boxUnboxedTypeBinding;
}
}
if (castFixType != null) {
proposals.add(createCastProposal(context, castFixType, nodeToCast, IProposalRelevance.CREATE_CAST));
}
}
//$NON-NLS-1$
boolean nullOrVoid = currBinding == null || "void".equals(currBinding.getName());
// change method return statement to actual type
if (!nullOrVoid && parentNodeType == ASTNode.RETURN_STATEMENT) {
BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(selectedNode);
if (decl instanceof MethodDeclaration) {
MethodDeclaration methodDeclaration = (MethodDeclaration) decl;
currBinding = Bindings.normalizeTypeBinding(currBinding);
if (currBinding == null) {
//$NON-NLS-1$
currBinding = ast.resolveWellKnownType("java.lang.Object");
}
if (currBinding.isWildcardType()) {
currBinding = ASTResolving.normalizeWildcardType(currBinding, true, ast);
}
ASTRewrite rewrite = ASTRewrite.create(ast);
String label = Messages.format(CorrectionMessages.TypeMismatchSubProcessor_changereturntype_description, BasicElementLabels.getJavaElementName(currBinding.getName()));
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.CHANGE_METHOD_RETURN_TYPE, image);
ImportRewrite imports = proposal.createImportRewrite(astRoot);
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(decl, imports);
Type newReturnType = imports.addImport(currBinding, ast, importRewriteContext);
rewrite.replace(methodDeclaration.getReturnType2(), newReturnType, null);
//$NON-NLS-1$
String returnKey = "return";
proposal.addLinkedPosition(rewrite.track(newReturnType), true, returnKey);
ITypeBinding[] typeSuggestions = ASTResolving.getRelaxingTypes(ast, currBinding);
for (int i = 0; i < typeSuggestions.length; i++) {
proposal.addLinkedPositionProposal(returnKey, typeSuggestions[i]);
}
proposals.add(proposal);
}
}
if (!nullOrVoid && receiverNode != null) {
currBinding = Bindings.normalizeTypeBinding(currBinding);
if (currBinding == null) {
//$NON-NLS-1$
currBinding = ast.resolveWellKnownType("java.lang.Object");
}
if (currBinding.isWildcardType()) {
currBinding = ASTResolving.normalizeWildcardType(currBinding, true, ast);
}
addChangeSenderTypeProposals(context, receiverNode, currBinding, true, IProposalRelevance.CHANGE_TYPE_OF_RECEIVER_NODE, proposals);
}
addChangeSenderTypeProposals(context, nodeToCast, castTypeBinding, false, IProposalRelevance.CHANGE_TYPE_OF_NODE_TO_CAST, proposals);
if (castTypeBinding == ast.resolveWellKnownType("boolean") && currBinding != null && !currBinding.isPrimitive() && !Bindings.isVoidType(currBinding)) {
//$NON-NLS-1$
String label = CorrectionMessages.TypeMismatchSubProcessor_insertnullcheck_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());
InfixExpression expression = ast.newInfixExpression();
expression.setLeftOperand((Expression) rewrite.createMoveTarget(nodeToCast));
expression.setRightOperand(ast.newNullLiteral());
expression.setOperator(InfixExpression.Operator.NOT_EQUALS);
rewrite.replace(nodeToCast, expression, null);
proposals.add(new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_NULL_CHECK, image));
}
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project che by eclipse.
the class UnresolvedElementsSubProcessor method addNewTypeProposals.
public static void addNewTypeProposals(ICompilationUnit cu, Name refNode, int kind, int relevance, Collection<ICommandAccess> proposals) throws CoreException {
Name node = refNode;
do {
String typeName = ASTNodes.getSimpleNameIdentifier(node);
Name qualifier = null;
// only propose to create types for qualifiers when the name starts with upper case
boolean isPossibleName = isLikelyTypeName(typeName) || node == refNode;
if (isPossibleName) {
IPackageFragment enclosingPackage = null;
IType enclosingType = null;
if (node.isSimpleName()) {
enclosingPackage = (IPackageFragment) cu.getParent();
// don't suggest member type, user can select it in wizard
} else {
Name qualifierName = ((QualifiedName) node).getQualifier();
IBinding binding = qualifierName.resolveBinding();
if (binding != null && binding.isRecovered()) {
binding = null;
}
if (binding instanceof ITypeBinding) {
enclosingType = (IType) binding.getJavaElement();
} else if (binding instanceof IPackageBinding) {
qualifier = qualifierName;
enclosingPackage = (IPackageFragment) binding.getJavaElement();
} else {
IJavaElement[] res = cu.codeSelect(qualifierName.getStartPosition(), qualifierName.getLength());
if (res != null && res.length > 0 && res[0] instanceof IType) {
enclosingType = (IType) res[0];
} else {
qualifier = qualifierName;
enclosingPackage = JavaModelUtil.getPackageFragmentRoot(cu).getPackageFragment(ASTResolving.getFullName(qualifierName));
}
}
}
int rel = relevance;
if (enclosingPackage != null && isLikelyPackageName(enclosingPackage.getElementName())) {
rel += 3;
}
if (enclosingPackage != null && !enclosingPackage.getCompilationUnit(typeName + JavaModelUtil.DEFAULT_CU_SUFFIX).exists() || enclosingType != null && !enclosingType.isReadOnly() && !enclosingType.getType(typeName).exists()) {
// new member type
IJavaElement enclosing = enclosingPackage != null ? (IJavaElement) enclosingPackage : enclosingType;
//TODO NewCUUsingWizardProposal
if ((kind & SimilarElementsRequestor.CLASSES) != 0) {
// proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_CLASS, enclosing, rel+3));
}
if ((kind & SimilarElementsRequestor.INTERFACES) != 0) {
// proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_INTERFACE, enclosing, rel+2));
}
if ((kind & SimilarElementsRequestor.ENUMS) != 0) {
// proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_ENUM, enclosing, rel));
}
if ((kind & SimilarElementsRequestor.ANNOTATIONS) != 0) {
// proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_ANNOTATION, enclosing, rel + 1));
addNullityAnnotationTypesProposals(cu, node, proposals);
}
}
}
node = qualifier;
} while (node != null);
// type parameter proposals
if (refNode.isSimpleName() && (kind & SimilarElementsRequestor.VARIABLES) != 0) {
CompilationUnit root = (CompilationUnit) refNode.getRoot();
String name = ((SimpleName) refNode).getIdentifier();
BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(refNode);
int baseRel = relevance;
if (isLikelyTypeParameterName(name)) {
baseRel += 8;
}
while (declaration != null) {
IBinding binding = null;
int rel = baseRel;
if (declaration instanceof MethodDeclaration) {
binding = ((MethodDeclaration) declaration).resolveBinding();
if (isLikelyMethodTypeParameterName(name))
rel += 2;
} else if (declaration instanceof TypeDeclaration) {
binding = ((TypeDeclaration) declaration).resolveBinding();
rel++;
}
if (binding != null) {
AddTypeParameterProposal proposal = new AddTypeParameterProposal(cu, binding, root, name, null, rel);
proposals.add(proposal);
}
if (!Modifier.isStatic(declaration.getModifiers())) {
declaration = ASTResolving.findParentBodyDeclaration(declaration.getParent());
} else {
declaration = null;
}
}
}
}
Aggregations