use of org.eclipse.jdt.core.dom.IAnnotationBinding in project xtext-eclipse by eclipse.
the class JdtBasedTypeFactory method createAnnotationValues.
protected void createAnnotationValues(IBinding annotated, JvmAnnotationTarget result) {
try {
resolveAnnotations.start();
IAnnotationBinding[] annotationBindings = annotated.getAnnotations();
if (annotationBindings.length != 0) {
InternalEList<JvmAnnotationReference> annotations = (InternalEList<JvmAnnotationReference>) result.getAnnotations();
for (IAnnotationBinding annotation : annotationBindings) {
annotations.addUnique(createAnnotationReference(annotation));
}
}
} catch (AbortCompilation aborted) {
if (aborted.problem.getID() == IProblem.IsClassPathCorrect) {
// ignore
} else {
log.info("Couldn't resolve annotations of " + annotated, aborted);
}
} finally {
resolveAnnotations.stop();
}
}
use of org.eclipse.jdt.core.dom.IAnnotationBinding in project j2objc by google.
the class BindingConverter method getPackageElement.
/**
* JDT package bindings do not include annotations, so add them from the
* package's AST node.
*/
public static JdtPackageElement getPackageElement(PackageDeclaration pkg) {
IPackageBinding binding = pkg.resolveBinding();
JdtPackageElement pkgElement = (JdtPackageElement) getElement(binding);
if (pkgElement.getAnnotationMirrors().isEmpty() && pkg.annotations().size() > 0) {
for (Object modifier : pkg.annotations()) {
IAnnotationBinding annotation = ((org.eclipse.jdt.core.dom.Annotation) modifier).resolveAnnotationBinding();
pkgElement.addAnnotation(new JdtAnnotationMirror(annotation));
}
}
return pkgElement;
}
use of org.eclipse.jdt.core.dom.IAnnotationBinding in project che by eclipse.
the class ASTNodeFactory method newCreationType.
public static Type newCreationType(AST ast, ITypeBinding typeBinding, ImportRewrite importRewrite, ImportRewriteContext importContext) {
if (typeBinding.isParameterizedType()) {
Type baseType = newCreationType(ast, typeBinding.getTypeDeclaration(), importRewrite, importContext);
ParameterizedType parameterizedType = ast.newParameterizedType(baseType);
for (ITypeBinding typeArgument : typeBinding.getTypeArguments()) {
parameterizedType.typeArguments().add(newCreationType(ast, typeArgument, importRewrite, importContext));
}
return parameterizedType;
} else if (typeBinding.isParameterizedType()) {
Type elementType = newCreationType(ast, typeBinding.getElementType(), importRewrite, importContext);
ArrayType arrayType = ast.newArrayType(elementType, 0);
while (typeBinding.isArray()) {
Dimension dimension = ast.newDimension();
IAnnotationBinding[] typeAnnotations = typeBinding.getTypeAnnotations();
for (IAnnotationBinding typeAnnotation : typeAnnotations) {
dimension.annotations().add(importRewrite.addAnnotation(typeAnnotation, ast, importContext));
}
arrayType.dimensions().add(dimension);
typeBinding = typeBinding.getComponentType();
}
return arrayType;
} else if (typeBinding.isWildcardType()) {
ITypeBinding bound = typeBinding.getBound();
typeBinding = (bound != null) ? bound : typeBinding.getErasure();
return newCreationType(ast, typeBinding, importRewrite, importContext);
} else {
return importRewrite.addImport(typeBinding, ast, importContext);
}
}
use of org.eclipse.jdt.core.dom.IAnnotationBinding in project che by eclipse.
the class StubUtility2 method createParameters.
private static List<SingleVariableDeclaration> createParameters(IJavaProject project, ImportRewrite imports, ImportRewriteContext context, AST ast, IMethodBinding binding, String[] paramNames, MethodDeclaration decl) {
boolean is50OrHigher = JavaModelUtil.is50OrHigher(project);
List<SingleVariableDeclaration> parameters = decl.parameters();
ITypeBinding[] params = binding.getParameterTypes();
if (paramNames == null || paramNames.length < params.length) {
paramNames = StubUtility.suggestArgumentNames(project, binding);
}
for (int i = 0; i < params.length; i++) {
SingleVariableDeclaration var = ast.newSingleVariableDeclaration();
ITypeBinding type = params[i];
if (type.isWildcardType()) {
ITypeBinding bound = type.getBound();
type = (bound != null) ? bound : type.getErasure();
}
if (!is50OrHigher) {
type = type.getErasure();
var.setType(imports.addImport(type, ast, context));
} else if (binding.isVarargs() && type.isArray() && i == params.length - 1) {
var.setVarargs(true);
/*
* Varargs annotations are special.
* Example:
* foo(@O Object @A [] @B ... arg)
* => @B is not an annotation on the array dimension that constitutes the vararg.
* It's the type annotation of the *innermost* array dimension.
*/
int dimensions = type.getDimensions();
@SuppressWarnings("unchecked") List<Annotation>[] dimensionAnnotations = (List<Annotation>[]) new List<?>[dimensions];
for (int dim = 0; dim < dimensions; dim++) {
dimensionAnnotations[dim] = new ArrayList<Annotation>();
for (IAnnotationBinding annotation : type.getTypeAnnotations()) {
dimensionAnnotations[dim].add(imports.addAnnotation(annotation, ast, context));
}
type = type.getComponentType();
}
Type elementType = imports.addImport(type, ast, context);
if (dimensions == 1) {
var.setType(elementType);
} else {
ArrayType arrayType = ast.newArrayType(elementType, dimensions - 1);
List<Dimension> dimensionNodes = arrayType.dimensions();
for (int dim = 0; dim < dimensions - 1; dim++) {
// all except the innermost dimension
Dimension dimension = dimensionNodes.get(dim);
dimension.annotations().addAll(dimensionAnnotations[dim]);
}
var.setType(arrayType);
}
List<Annotation> varargTypeAnnotations = dimensionAnnotations[dimensions - 1];
var.varargsAnnotations().addAll(varargTypeAnnotations);
} else {
var.setType(imports.addImport(type, ast, context));
}
var.setName(ast.newSimpleName(paramNames[i]));
IAnnotationBinding[] annotations = binding.getParameterAnnotations(i);
for (IAnnotationBinding annotation : annotations) {
if (StubUtility2.isCopyOnInheritAnnotation(annotation.getAnnotationType(), project))
var.modifiers().add(imports.addAnnotation(annotation, ast, context));
}
parameters.add(var);
}
return parameters;
}
use of org.eclipse.jdt.core.dom.IAnnotationBinding 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;
}
Aggregations