use of org.eclipse.jdt.core.dom.IExtendedModifier 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.IExtendedModifier in project eclipse-pmd by acanda.
the class SuppressWarningsQuickFix method findPosition.
/**
* @return The position after the last existing annotation.
*/
private int findPosition(final List<IExtendedModifier> modifiers) {
int position = 0;
int index = 0;
for (final IExtendedModifier modifier : modifiers) {
index++;
if (modifier.isAnnotation()) {
position = index;
}
}
return position;
}
use of org.eclipse.jdt.core.dom.IExtendedModifier 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.IExtendedModifier in project eclipse.jdt.ls by eclipse.
the class StubUtility2 method findAnnotation.
public static Annotation findAnnotation(String qualifiedTypeName, List<IExtendedModifier> modifiers) {
for (int i = 0; i < modifiers.size(); i++) {
IExtendedModifier curr = modifiers.get(i);
if (curr instanceof Annotation) {
Annotation annot = (Annotation) curr;
ITypeBinding binding = annot.getTypeName().resolveTypeBinding();
if (binding != null && qualifiedTypeName.equals(binding.getQualifiedName())) {
return annot;
}
}
}
return null;
}
use of org.eclipse.jdt.core.dom.IExtendedModifier 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;
}
Aggregations