use of org.eclipse.jdt.core.dom.Modifier in project eclipse-pmd by acanda.
the class SingularFieldQuickFix method replaceAssignment.
/**
* Replaces the assignment with a variable declaration. If the assignment is the only one in the block for this
* variable, the final modifier is added to the declaration.
*/
@SuppressWarnings("unchecked")
private void replaceAssignment(final VariableDeclarationFragment node, final Assignment assignment, final boolean finalDeclaration) {
final FieldDeclaration fieldDeclaration = (FieldDeclaration) node.getParent();
final VariableDeclarationStatement declaration = (VariableDeclarationStatement) node.getAST().createInstance(VariableDeclarationStatement.class);
declaration.setType(ASTUtil.copy(fieldDeclaration.getType()));
final VariableDeclarationFragment fragment = (VariableDeclarationFragment) node.getAST().createInstance(VariableDeclarationFragment.class);
fragment.setName(ASTUtil.copy(node.getName()));
fragment.setInitializer(ASTUtil.copy(assignment.getRightHandSide()));
declaration.fragments().add(fragment);
if (finalDeclaration) {
final Modifier modifier = (Modifier) node.getAST().createInstance(Modifier.class);
modifier.setKeyword(ModifierKeyword.FINAL_KEYWORD);
declaration.modifiers().add(modifier);
}
ASTUtil.replace(assignment.getParent(), declaration);
}
use of org.eclipse.jdt.core.dom.Modifier in project che by eclipse.
the class ModifierRewrite method internalSetModifiers.
/**
* Sets the given modifiers and removes all other modifiers that match the consideredFlags mask.
* Does not touch other flags and leaves annotations in place.
*
* @param modifiers the modifiers to set
* @param consideredFlags mask of modifiers to consider
* @param editGroup the edit group in which to collect the corresponding text edits, or
* <code>null</code> if ungrouped
* @return a tracked position that contains the changed modifiers
*/
private PositionInformation internalSetModifiers(int modifiers, int consideredFlags, TextEditGroup editGroup) {
int newModifiers = modifiers & consideredFlags;
ITrackedNodePosition trackedFallback = null;
List<ITrackedNodePosition> trackedNodes = new ArrayList<ITrackedNodePosition>();
// remove modifiers
List<IExtendedModifier> originalList = fModifierRewrite.getOriginalList();
for (int i = 0; i < originalList.size(); i++) {
ASTNode curr = (ASTNode) originalList.get(i);
if (curr instanceof Modifier) {
int flag = ((Modifier) curr).getKeyword().toFlagValue();
if ((consideredFlags & flag) != 0) {
if ((newModifiers & flag) == 0) {
fModifierRewrite.remove(curr, editGroup);
if (trackedFallback == null)
trackedFallback = fModifierRewrite.getASTRewrite().track(curr);
}
newModifiers &= ~flag;
}
}
}
// find last annotation
IExtendedModifier lastAnnotation = null;
List<IExtendedModifier> extendedList = fModifierRewrite.getRewrittenList();
for (int i = 0; i < extendedList.size(); i++) {
IExtendedModifier curr = extendedList.get(i);
if (curr.isAnnotation())
lastAnnotation = curr;
}
// add modifiers
List<Modifier> newNodes = ASTNodeFactory.newModifiers(fAst, newModifiers);
for (int i = 0; i < newNodes.size(); i++) {
Modifier curr = newNodes.get(i);
if ((curr.getKeyword().toFlagValue() & VISIBILITY_MODIFIERS) != 0) {
if (lastAnnotation != null)
fModifierRewrite.insertAfter(curr, (ASTNode) lastAnnotation, editGroup);
else
fModifierRewrite.insertFirst(curr, editGroup);
} else {
fModifierRewrite.insertLast(curr, editGroup);
}
trackedNodes.add(fModifierRewrite.getASTRewrite().track(curr));
}
if (trackedNodes.isEmpty()) {
if (trackedFallback == null) {
// out of tricks...
trackedFallback = fModifierRewrite.getASTRewrite().track(fModifierRewrite.getParent());
}
return new LinkedProposalPositionGroup.StartPositionInformation(trackedFallback);
} else {
return new LinkedProposalPositionGroup.TrackedNodesPosition(trackedNodes);
}
}
use of org.eclipse.jdt.core.dom.Modifier in project che by eclipse.
the class StubUtility2 method getImplementationModifiers.
private static List<IExtendedModifier> getImplementationModifiers(AST ast, IMethodBinding method, boolean inInterface, ImportRewrite importRewrite, ImportRewriteContext context) throws JavaModelException {
IJavaProject javaProject = importRewrite.getCompilationUnit().getJavaProject();
int modifiers = method.getModifiers() & ~Modifier.ABSTRACT & ~Modifier.NATIVE & ~Modifier.PRIVATE;
if (inInterface) {
modifiers = modifiers & ~Modifier.PROTECTED;
if (!method.getDeclaringClass().isInterface()) {
modifiers = modifiers | Modifier.PUBLIC;
}
} else {
modifiers = modifiers & ~Modifier.DEFAULT;
}
IAnnotationBinding[] annotations = method.getAnnotations();
if (modifiers != Modifier.NONE && annotations.length > 0) {
// need an AST of the source method to preserve order of modifiers
IMethod iMethod = (IMethod) method.getJavaElement();
if (iMethod != null && JavaElementUtil.isSourceAvailable(iMethod)) {
CheASTParser parser = CheASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
parser.setSource(iMethod.getTypeRoot());
parser.setIgnoreMethodBodies(true);
CompilationUnit otherCU = (CompilationUnit) parser.createAST(null);
ASTNode otherMethod = NodeFinder.perform(otherCU, iMethod.getSourceRange());
if (otherMethod instanceof MethodDeclaration) {
MethodDeclaration otherMD = (MethodDeclaration) otherMethod;
ArrayList<IExtendedModifier> result = new ArrayList<IExtendedModifier>();
List<IExtendedModifier> otherModifiers = otherMD.modifiers();
for (IExtendedModifier otherModifier : otherModifiers) {
if (otherModifier instanceof Modifier) {
int otherFlag = ((Modifier) otherModifier).getKeyword().toFlagValue();
if ((otherFlag & modifiers) != 0) {
modifiers = ~otherFlag & modifiers;
result.addAll(ast.newModifiers(otherFlag));
}
} else {
Annotation otherAnnotation = (Annotation) otherModifier;
String n = otherAnnotation.getTypeName().getFullyQualifiedName();
for (IAnnotationBinding annotation : annotations) {
ITypeBinding otherAnnotationType = annotation.getAnnotationType();
String qn = otherAnnotationType.getQualifiedName();
if (qn.endsWith(n) && (qn.length() == n.length() || qn.charAt(qn.length() - n.length() - 1) == '.')) {
if (StubUtility2.isCopyOnInheritAnnotation(otherAnnotationType, javaProject))
result.add(importRewrite.addAnnotation(annotation, ast, context));
break;
}
}
}
}
result.addAll(ASTNodeFactory.newModifiers(ast, modifiers));
return result;
}
}
}
ArrayList<IExtendedModifier> result = new ArrayList<IExtendedModifier>();
for (IAnnotationBinding annotation : annotations) {
if (StubUtility2.isCopyOnInheritAnnotation(annotation.getAnnotationType(), javaProject))
result.add(importRewrite.addAnnotation(annotation, ast, context));
}
result.addAll(ASTNodeFactory.newModifiers(ast, modifiers));
return result;
}
use of org.eclipse.jdt.core.dom.Modifier in project che by eclipse.
the class ModifierCorrectionSubProcessor method addAbstractMethodProposals.
public static void addAbstractMethodProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(astRoot);
if (selectedNode == null) {
return;
}
MethodDeclaration decl;
if (selectedNode instanceof SimpleName) {
decl = (MethodDeclaration) selectedNode.getParent();
} else if (selectedNode instanceof MethodDeclaration) {
decl = (MethodDeclaration) selectedNode;
} else {
return;
}
ASTNode parentType = ASTResolving.findParentType(decl);
TypeDeclaration parentTypeDecl = null;
boolean parentIsAbstractClass = false;
if (parentType instanceof TypeDeclaration) {
parentTypeDecl = (TypeDeclaration) parentType;
parentIsAbstractClass = !parentTypeDecl.isInterface() && Modifier.isAbstract(parentTypeDecl.getModifiers());
}
boolean hasNoBody = decl.getBody() == null;
int id = problem.getProblemId();
if (id == IProblem.AbstractMethodInAbstractClass || id == IProblem.EnumAbstractMethodMustBeImplemented || id == IProblem.AbstractMethodInEnum || parentIsAbstractClass) {
AST ast = astRoot.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
removeModifier(decl, rewrite, Modifier.ABSTRACT);
if (hasNoBody) {
Block newBody = ast.newBlock();
rewrite.set(decl, MethodDeclaration.BODY_PROPERTY, newBody, null);
Type returnType = decl.getReturnType2();
if (returnType != null) {
Expression expr = ASTNodeFactory.newDefaultExpression(ast, returnType, decl.getExtraDimensions());
if (expr != null) {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(expr);
newBody.statements().add(returnStatement);
}
}
}
String label = CorrectionMessages.ModifierCorrectionSubProcessor_removeabstract_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_ABSTRACT_MODIFIER, image);
proposals.add(proposal);
}
if (!hasNoBody && id == IProblem.BodyForAbstractMethod) {
AST ast = decl.getAST();
{
ASTRewrite rewrite = ASTRewrite.create(ast);
rewrite.remove(decl.getBody(), null);
String label = CorrectionMessages.ModifierCorrectionSubProcessor_removebody_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_METHOD_BODY, image);
proposals.add(proposal);
}
if (JavaModelUtil.is18OrHigher(cu.getJavaProject()) && parentTypeDecl.isInterface()) {
{
// insert proposal to add static modifier
ASTRewrite rewrite = ASTRewrite.create(ast);
removeModifier(decl, rewrite, Modifier.ABSTRACT);
Modifier newModifier = ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD);
rewrite.getListRewrite(decl, MethodDeclaration.MODIFIERS2_PROPERTY).insertLast(newModifier, null);
String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertostatic_description, decl.getName());
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
int included = Modifier.STATIC;
int excluded = Modifier.ABSTRACT | Modifier.DEFAULT;
proposals.add(new ModifierChangeCorrectionProposal(label, cu, decl.resolveBinding(), decl, included, excluded, IProposalRelevance.ADD_STATIC_MODIFIER, image));
}
{
// insert proposal to add default modifier
ASTRewrite rewrite = ASTRewrite.create(ast);
removeModifier(decl, rewrite, Modifier.ABSTRACT);
Modifier newModifier = ast.newModifier(Modifier.ModifierKeyword.DEFAULT_KEYWORD);
rewrite.getListRewrite(decl, MethodDeclaration.MODIFIERS2_PROPERTY).insertLast(newModifier, null);
String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertodefault_description, decl.getName());
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
int included = Modifier.DEFAULT;
int excluded = Modifier.ABSTRACT | Modifier.STATIC;
proposals.add(new ModifierChangeCorrectionProposal(label, cu, decl.resolveBinding(), decl, included, excluded, IProposalRelevance.ADD_DEFAULT_MODIFIER, image));
}
}
}
if (id == IProblem.AbstractMethodInAbstractClass && parentTypeDecl != null) {
addMakeTypeAbstractProposal(context, parentTypeDecl, proposals);
}
}
use of org.eclipse.jdt.core.dom.Modifier in project lombok by rzwitserloot.
the class PatchValEclipse method createModifier.
public static Modifier createModifier(AST ast, ModifierKeyword keyword, int start, int end) {
Modifier modifier = null;
try {
modifier = Reflection.modifierConstructor.newInstance(ast);
} catch (InstantiationException e) {
throw Lombok.sneakyThrow(e);
} catch (IllegalAccessException e) {
throw Lombok.sneakyThrow(e);
} catch (InvocationTargetException e) {
throw Lombok.sneakyThrow(e);
}
if (modifier != null) {
modifier.setKeyword(keyword);
modifier.setSourceRange(start, end - start + 1);
}
return modifier;
}
Aggregations