use of org.eclipse.jdt.core.dom.Modifier in project AutoRefactor by JnRouvignac.
the class RemoveUselessModifiersRefactoring method removeStaticModifier.
private boolean removeStaticModifier(List<IExtendedModifier> modifiers) {
boolean result = VISIT_SUBTREE;
for (Modifier m : getModifiersOnly(modifiers)) {
if (m.isStatic()) {
ctx.getRefactorings().remove(m);
result = DO_NOT_VISIT_SUBTREE;
}
}
return result;
}
use of org.eclipse.jdt.core.dom.Modifier in project AutoRefactor by JnRouvignac.
the class RemoveUselessModifiersRefactoring method removePublicAbstractModifiers.
private boolean removePublicAbstractModifiers(BodyDeclaration node) {
boolean result = VISIT_SUBTREE;
for (Modifier m : getModifiersOnly(modifiers(node))) {
if (m.isPublic() || m.isAbstract()) {
ctx.getRefactorings().remove(m);
result = DO_NOT_VISIT_SUBTREE;
}
}
return result;
}
use of org.eclipse.jdt.core.dom.Modifier in project eclipse.jdt.ls by eclipse.
the class ModifierCorrectionSubProcessor method findVisibilityModifier.
private static Modifier findVisibilityModifier(List<IExtendedModifier> modifiers) {
for (int i = 0; i < modifiers.size(); i++) {
IExtendedModifier curr = modifiers.get(i);
if (curr instanceof Modifier) {
Modifier modifier = (Modifier) curr;
ModifierKeyword keyword = modifier.getKeyword();
if (keyword == ModifierKeyword.PUBLIC_KEYWORD || keyword == ModifierKeyword.PROTECTED_KEYWORD || keyword == ModifierKeyword.PRIVATE_KEYWORD) {
return modifier;
}
}
}
return null;
}
use of org.eclipse.jdt.core.dom.Modifier in project eclipse.jdt.ls by eclipse.
the class StubUtility2 method getImplementationModifiers.
// public static DelegateEntry[] getDelegatableMethods(ITypeBinding binding) {
// final List<DelegateEntry> tuples= new ArrayList<>();
// final List<IMethodBinding> declared= new ArrayList<>();
// IMethodBinding[] typeMethods= binding.getDeclaredMethods();
// for (int index= 0; index < typeMethods.length; index++) {
// declared.add(typeMethods[index]);
// }
// IVariableBinding[] typeFields= binding.getDeclaredFields();
// for (int index= 0; index < typeFields.length; index++) {
// IVariableBinding fieldBinding= typeFields[index];
// if (fieldBinding.isField() && !fieldBinding.isEnumConstant() && !fieldBinding.isSynthetic()) {
// getDelegatableMethods(new ArrayList<>(declared), fieldBinding, fieldBinding.getType(), binding, tuples);
// }
// }
// // list of tuple<IVariableBinding, IMethodBinding>
// return tuples.toArray(new DelegateEntry[tuples.size()]);
// }
// private static void getDelegatableMethods(List<IMethodBinding> methods, IVariableBinding fieldBinding, ITypeBinding typeBinding, ITypeBinding binding, List<DelegateEntry> result) {
// boolean match= false;
// if (typeBinding.isTypeVariable()) {
// ITypeBinding[] typeBounds= typeBinding.getTypeBounds();
// if (typeBounds.length > 0) {
// for (int i= 0; i < typeBounds.length; i++) {
// getDelegatableMethods(methods, fieldBinding, typeBounds[i], binding, result);
// }
// } else {
// ITypeBinding objectBinding= Bindings.findTypeInHierarchy(binding, "java.lang.Object"); //$NON-NLS-1$
// if (objectBinding != null) {
// getDelegatableMethods(methods, fieldBinding, objectBinding, binding, result);
// }
// }
// } else {
// IMethodBinding[] candidates= getDelegateCandidates(typeBinding, binding);
// for (int index= 0; index < candidates.length; index++) {
// match= false;
// final IMethodBinding methodBinding= candidates[index];
// for (int offset= 0; offset < methods.size() && !match; offset++) {
// if (Bindings.areOverriddenMethods(methods.get(offset), methodBinding)) {
// match= true;
// }
// }
// if (!match) {
// result.add(new DelegateEntry(methodBinding, fieldBinding));
// methods.add(methodBinding);
// }
// }
// final ITypeBinding superclass= typeBinding.getSuperclass();
// if (superclass != null) {
// getDelegatableMethods(methods, fieldBinding, superclass, binding, result);
// }
// ITypeBinding[] superInterfaces= typeBinding.getInterfaces();
// for (int offset= 0; offset < superInterfaces.length; offset++) {
// getDelegatableMethods(methods, fieldBinding, superInterfaces[offset], binding, result);
// }
// }
// }
// private static IMethodBinding[] getDelegateCandidates(ITypeBinding binding, ITypeBinding hierarchy) {
// List<IMethodBinding> allMethods= new ArrayList<>();
// boolean isInterface= binding.isInterface();
// IMethodBinding[] typeMethods= binding.getDeclaredMethods();
// for (int index= 0; index < typeMethods.length; index++) {
// final int modifiers= typeMethods[index].getModifiers();
// if (!typeMethods[index].isConstructor() && !Modifier.isStatic(modifiers) && (isInterface || Modifier.isPublic(modifiers))) {
// IMethodBinding result= Bindings.findOverriddenMethodInHierarchy(hierarchy, typeMethods[index]);
// if (result != null && Flags.isFinal(result.getModifiers())) {
// continue;
// }
// ITypeBinding[] parameterBindings= typeMethods[index].getParameterTypes();
// boolean upper= false;
// for (int offset= 0; offset < parameterBindings.length; offset++) {
// if (parameterBindings[offset].isWildcardType() && parameterBindings[offset].isUpperbound()) {
// upper= true;
// }
// }
// if (!upper) {
// allMethods.add(typeMethods[index]);
// }
// }
// }
// return allMethods.toArray(new IMethodBinding[allMethods.size()]);
// }
private static List<IExtendedModifier> getImplementationModifiers(AST ast, IMethodBinding method, boolean inInterface, ImportRewrite importRewrite, ImportRewriteContext context, IAnnotationBinding defaultNullness) throws JavaModelException {
IJavaProject javaProject = importRewrite.getCompilationUnit().getJavaProject();
int modifiers = method.getModifiers();
if (inInterface) {
modifiers = modifiers & ~Modifier.PROTECTED & ~Modifier.PUBLIC;
if (Modifier.isAbstract(modifiers) && JavaModelUtil.is18OrHigher(javaProject)) {
modifiers = modifiers | Modifier.DEFAULT;
}
} else {
modifiers = modifiers & ~Modifier.DEFAULT;
}
modifiers = modifiers & ~Modifier.ABSTRACT & ~Modifier.NATIVE & ~Modifier.PRIVATE;
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 && isSourceAvailable(iMethod)) {
ASTParser parser = ASTParser.newParser(IASTSharedValues.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<>();
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, defaultNullness)) {
result.add(importRewrite.addAnnotation(annotation, ast, context));
}
break;
}
}
}
}
result.addAll(ASTNodeFactory.newModifiers(ast, modifiers));
return result;
}
}
}
ArrayList<IExtendedModifier> result = new ArrayList<>();
for (IAnnotationBinding annotation : annotations) {
if (StubUtility2.isCopyOnInheritAnnotation(annotation.getAnnotationType(), javaProject, defaultNullness)) {
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 flux by eclipse.
the class ModifierCorrectionSubProcessor method addMethodRequiresBodyProposals.
public static void addMethodRequiresBodyProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ICompilationUnit cu = context.getCompilationUnit();
AST ast = context.getASTRoot().getAST();
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (!(selectedNode instanceof MethodDeclaration)) {
return;
}
MethodDeclaration decl = (MethodDeclaration) selectedNode;
Modifier modifierNode;
{
ASTRewrite rewrite = ASTRewrite.create(ast);
modifierNode = removeModifier(decl, rewrite, Modifier.ABSTRACT);
Block body = ast.newBlock();
rewrite.set(decl, MethodDeclaration.BODY_PROPERTY, body, null);
if (!decl.isConstructor()) {
Type returnType = decl.getReturnType2();
if (returnType != null) {
Expression expression = ASTNodeFactory.newDefaultExpression(ast, returnType, decl.getExtraDimensions());
if (expression != null) {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(expression);
body.statements().add(returnStatement);
}
}
}
String label = CorrectionMessages.ModifierCorrectionSubProcessor_addmissingbody_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.ADD_MISSING_BODY);
proposals.add(proposal);
}
IMethodBinding binding = decl.resolveBinding();
if (modifierNode == null && binding != null) {
String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertoabstract_description, getMethodLabel(binding));
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
int included = binding.getDeclaringClass().isInterface() ? Modifier.NONE : Modifier.ABSTRACT;
int excluded = Modifier.STATIC | Modifier.DEFAULT;
ModifierChangeCorrectionProposal proposal = new ModifierChangeCorrectionProposal(label, cu, binding, decl, included, excluded, IProposalRelevance.ADD_ABSTRACT_MODIFIER);
proposals.add(proposal);
}
}
Aggregations