use of org.eclipse.jdt.core.dom.IAnnotationBinding in project che by eclipse.
the class JavadocFinder method getAnnotations.
private String getAnnotations(IJavaElement element, ITypeRoot editorInputElement, IRegion hoverRegion) throws URISyntaxException, JavaModelException {
if (!(element instanceof IPackageFragment)) {
if (!(element instanceof IAnnotatable))
return null;
if (((IAnnotatable) element).getAnnotations().length == 0)
return null;
}
IBinding binding = null;
//TODO
//getHoveredASTNode(editorInputElement, hoverRegion);
ASTNode node = null;
if (node == null) {
//todo use ast ported parser,that uses our java model
// ASTParser p = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
// p.setProject(element.getJavaProject());
// p.setBindingsRecovery(true);
// try {
// binding = p.createBindings(new IJavaElement[]{element}, null)[0];
// } catch (OperationCanceledException e) {
// return null;
// }
} else {
binding = resolveBinding(node);
}
if (binding == null)
return null;
IAnnotationBinding[] annotations = binding.getAnnotations();
if (annotations.length == 0)
return null;
StringBuffer buf = new StringBuffer();
for (int i = 0; i < annotations.length; i++) {
//TODO: skip annotations that don't have an @Documented annotation?
addAnnotation(buf, element, annotations[i]);
//$NON-NLS-1$
buf.append("<br>");
}
return buf.toString();
}
use of org.eclipse.jdt.core.dom.IAnnotationBinding in project bndtools by bndtools.
the class MemberValuePairLocationRetriever method visit.
/**
* @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.SingleMemberAnnotation)
*/
@Override
public boolean visit(SingleMemberAnnotation node) {
final IAnnotationBinding annotationBinding = node.resolveAnnotationBinding();
if (annotationBinding != null) {
final String nodeName = annotationBinding.getAnnotationType().getQualifiedName();
boolean match;
try {
match = this.annotationNameMatch.test(nodeName);
} catch (Exception e) {
match = false;
}
if (match) {
this.locatedSourceRange = new SourceRange(node.getValue().getStartPosition(), node.getValue().getLength());
}
}
return false;
}
use of org.eclipse.jdt.core.dom.IAnnotationBinding in project eclipse.jdt.ls by eclipse.
the class StubUtility2 method findNullnessDefault.
// TODO Remove this by addressing https://bugs.eclipse.org/bugs/show_bug.cgi?id=531511
private static IAnnotationBinding findNullnessDefault(IBinding contextBinding, IJavaProject javaProject) {
if (JavaCore.ENABLED.equals(javaProject.getOption(JavaCore.COMPILER_ANNOTATION_NULL_ANALYSIS, true))) {
String annotationName = javaProject.getOption(JavaCore.COMPILER_NONNULL_BY_DEFAULT_ANNOTATION_NAME, true);
while (contextBinding != null) {
for (IAnnotationBinding annotation : contextBinding.getAnnotations()) {
ITypeBinding annotationType = annotation.getAnnotationType();
if (annotationType != null && annotationType.getQualifiedName().equals(annotationName)) {
return annotation;
}
}
// travel out:
switch(contextBinding.getKind()) {
case IBinding.METHOD:
IMethodBinding methodBinding = (IMethodBinding) contextBinding;
contextBinding = methodBinding.getDeclaringMember();
if (contextBinding == null) {
contextBinding = methodBinding.getDeclaringClass();
}
break;
case IBinding.VARIABLE:
IVariableBinding variableBinding = (IVariableBinding) contextBinding;
contextBinding = variableBinding.getDeclaringMethod();
if (contextBinding == null) {
contextBinding = variableBinding.getDeclaringClass();
}
break;
case IBinding.TYPE:
ITypeBinding currentClass = (ITypeBinding) contextBinding;
contextBinding = currentClass.getDeclaringMember();
if (contextBinding == null) {
contextBinding = currentClass.getDeclaringMethod();
if (contextBinding == null) {
contextBinding = currentClass.getDeclaringClass();
if (contextBinding == null) {
contextBinding = currentClass.getPackage();
}
}
}
break;
default:
contextBinding = null;
break;
}
}
}
return null;
}
use of org.eclipse.jdt.core.dom.IAnnotationBinding 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.IAnnotationBinding in project eclipse.jdt.ls by eclipse.
the class ASTNodeFactory method newCreationType.
/**
* Create a Type suitable as the creationType in a ClassInstanceCreation expression.
* @param ast The AST to create the nodes for.
* @param typeBinding binding representing the given class type
* @param importRewrite the import rewrite to use
* @param importContext the import context used to determine which (null) annotations to consider
* @return a Type suitable as the creationType in a ClassInstanceCreation expression.
*/
public static Type newCreationType(AST ast, ITypeBinding typeBinding, ImportRewrite importRewrite, ImportRewriteContext importContext) {
if (typeBinding.isParameterizedType()) {
Type baseType = newCreationType(ast, typeBinding.getTypeDeclaration(), importRewrite, importContext);
IAnnotationBinding[] typeAnnotations = importContext.removeRedundantTypeAnnotations(typeBinding.getTypeAnnotations(), TypeLocation.NEW, typeBinding);
for (IAnnotationBinding typeAnnotation : typeAnnotations) {
((AnnotatableType) baseType).annotations().add(importRewrite.addAnnotation(typeAnnotation, ast, importContext));
}
ParameterizedType parameterizedType = ast.newParameterizedType(baseType);
for (ITypeBinding typeArgument : typeBinding.getTypeArguments()) {
typeArgument = StubUtility2.replaceWildcardsAndCaptures(typeArgument);
parameterizedType.typeArguments().add(importRewrite.addImport(typeArgument, ast, importContext, TypeLocation.TYPE_ARGUMENT));
}
return parameterizedType;
} else {
return importRewrite.addImport(typeBinding, ast, importContext, TypeLocation.NEW);
}
}
Aggregations