use of org.eclipse.jdt.core.dom.IAnnotationBinding in project xtext-eclipse by eclipse.
the class JdtBasedTypeFactory method setParameterNamesAndAnnotations.
private void setParameterNamesAndAnnotations(IMethodBinding method, ITypeBinding[] parameterTypes, String[] parameterNames, JvmExecutable result) {
InternalEList<JvmFormalParameter> parameters = (InternalEList<JvmFormalParameter>) result.getParameters();
for (int i = 0; i < parameterTypes.length; i++) {
IAnnotationBinding[] parameterAnnotations;
try {
parameterAnnotations = method.getParameterAnnotations(i);
} catch (AbortCompilation aborted) {
parameterAnnotations = null;
}
ITypeBinding parameterType = parameterTypes[i];
String parameterName = parameterNames == null ? null : /* lazy */
i < parameterNames.length ? parameterNames[i] : "arg" + i;
JvmFormalParameter formalParameter = createFormalParameter(parameterType, parameterName, parameterAnnotations);
parameters.addUnique(formalParameter);
}
}
use of org.eclipse.jdt.core.dom.IAnnotationBinding in project eclipse.jdt.ls 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) {
// return createParameters(project, imports, context, ast, binding, paramNames, decl, null);
// }
private static List<SingleVariableDeclaration> createParameters(IJavaProject project, ImportRewrite imports, ImportRewriteContext context, AST ast, IMethodBinding binding, String[] paramNames, MethodDeclaration decl, IAnnotationBinding defaultNullness) {
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];
type = replaceWildcardsAndCaptures(type);
if (!is50OrHigher) {
type = type.getErasure();
var.setType(imports.addImport(type, ast, context, TypeLocation.PARAMETER));
} 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<>();
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, TypeLocation.PARAMETER));
}
var.setName(ast.newSimpleName(paramNames[i]));
IAnnotationBinding[] annotations = binding.getParameterAnnotations(i);
for (IAnnotationBinding annotation : annotations) {
if (StubUtility2.isCopyOnInheritAnnotation(annotation.getAnnotationType(), project, defaultNullness)) {
var.modifiers().add(imports.addAnnotation(annotation, ast, context));
}
}
parameters.add(var);
}
return parameters;
}
use of org.eclipse.jdt.core.dom.IAnnotationBinding in project eclipse.jdt.ls by eclipse.
the class StubUtility2 method createImplementationStub.
public static MethodDeclaration createImplementationStub(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, ImportRewriteContext context, IMethodBinding binding, String[] parameterNames, ITypeBinding targetType, CodeGenerationSettings settings, boolean inInterface, IBinding contextBinding, boolean snippetStringSupport) throws CoreException {
Assert.isNotNull(imports);
Assert.isNotNull(rewrite);
AST ast = rewrite.getAST();
String type = Bindings.getTypeQualifiedName(targetType);
IJavaProject javaProject = unit.getJavaProject();
IAnnotationBinding nullnessDefault = null;
if (contextBinding != null && JavaCore.ENABLED.equals(javaProject.getOption(JavaCore.COMPILER_ANNOTATION_NULL_ANALYSIS, true))) {
nullnessDefault = findNullnessDefault(contextBinding, javaProject);
}
MethodDeclaration decl = ast.newMethodDeclaration();
decl.modifiers().addAll(getImplementationModifiers(ast, binding, inInterface, imports, context, nullnessDefault));
decl.setName(ast.newSimpleName(binding.getName()));
decl.setConstructor(false);
ITypeBinding bindingReturnType = binding.getReturnType();
bindingReturnType = StubUtility2.replaceWildcardsAndCaptures(bindingReturnType);
if (JavaModelUtil.is50OrHigher(javaProject)) {
createTypeParameters(imports, context, ast, binding, decl);
} else {
bindingReturnType = bindingReturnType.getErasure();
}
decl.setReturnType2(imports.addImport(bindingReturnType, ast, context, TypeLocation.RETURN_TYPE));
List<SingleVariableDeclaration> parameters = createParameters(javaProject, imports, context, ast, binding, parameterNames, decl, nullnessDefault);
createThrownExceptions(decl, binding, imports, context, ast);
String delimiter = unit.findRecommendedLineSeparator();
int modifiers = binding.getModifiers();
ITypeBinding declaringType = binding.getDeclaringClass();
// $NON-NLS-1$
ITypeBinding typeObject = ast.resolveWellKnownType("java.lang.Object");
if (!inInterface || (declaringType != typeObject && JavaModelUtil.is18OrHigher(javaProject))) {
// generate a method body
Map<String, String> options = javaProject.getOptions(true);
Block body = ast.newBlock();
decl.setBody(body);
// $NON-NLS-1$
String bodyStatement = "";
if (Modifier.isAbstract(modifiers)) {
Expression expression = ASTNodeFactory.newDefaultExpression(ast, decl.getReturnType2(), decl.getExtraDimensions());
if (expression != null) {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(expression);
bodyStatement = ASTNodes.asFormattedString(returnStatement, 0, delimiter, options);
}
} else {
SuperMethodInvocation invocation = ast.newSuperMethodInvocation();
if (declaringType.isInterface()) {
ITypeBinding supertype = Bindings.findImmediateSuperTypeInHierarchy(targetType, declaringType.getTypeDeclaration().getQualifiedName());
if (supertype == null) {
// should not happen, but better use the type we have rather than failing
supertype = declaringType;
}
if (supertype.isInterface()) {
String qualifier = imports.addImport(supertype.getTypeDeclaration(), context);
Name name = ASTNodeFactory.newName(ast, qualifier);
invocation.setQualifier(name);
}
}
invocation.setName(ast.newSimpleName(binding.getName()));
for (SingleVariableDeclaration varDecl : parameters) {
invocation.arguments().add(ast.newSimpleName(varDecl.getName().getIdentifier()));
}
Expression expression = invocation;
Type returnType = decl.getReturnType2();
if (returnType instanceof PrimitiveType && ((PrimitiveType) returnType).getPrimitiveTypeCode().equals(PrimitiveType.VOID)) {
bodyStatement = ASTNodes.asFormattedString(ast.newExpressionStatement(expression), 0, delimiter, options);
} else {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(expression);
bodyStatement = ASTNodes.asFormattedString(returnStatement, 0, delimiter, options);
}
}
if (bodyStatement != null) {
StringBuilder placeHolder = new StringBuilder();
if (snippetStringSupport) {
placeHolder.append("${0");
if (!bodyStatement.isEmpty()) {
placeHolder.append(":");
}
bodyStatement = CompletionUtils.sanitizeCompletion(bodyStatement);
}
placeHolder.append(bodyStatement);
if (snippetStringSupport) {
placeHolder.append("}");
}
ReturnStatement todoNode = (ReturnStatement) rewrite.createStringPlaceholder(placeHolder.toString(), ASTNode.RETURN_STATEMENT);
body.statements().add(todoNode);
}
}
/* jdt.ls doesn't create comments at that point
if (settings != null && settings.createComments) {
String string= CodeGeneration.getMethodComment(unit, type, decl, binding, delimiter);
if (string != null) {
Javadoc javadoc= (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
decl.setJavadoc(javadoc);
}
}
*/
// According to JLS8 9.2, an interface doesn't implicitly declare non-public members of Object,
// and JLS8 9.6.4.4 doesn't allow @Override for these methods (clone and finalize).
boolean skipOverride = inInterface && declaringType == typeObject && !Modifier.isPublic(modifiers);
if (!skipOverride) {
addOverrideAnnotation(settings, javaProject, rewrite, imports, decl, binding.getDeclaringClass().isInterface(), null);
}
return decl;
}
use of org.eclipse.jdt.core.dom.IAnnotationBinding in project eclipse.jdt.ls by eclipse.
the class RedundantNullnessTypeAnnotationsFilter method determineNonNullByDefaultLocations.
public static EnumSet<TypeLocation> determineNonNullByDefaultLocations(ASTNode astNode, String nonNullByDefaultName) {
// look for first @NonNullByDefault
while (astNode != null) {
IAnnotationBinding annot = getNNBDAnnotation(astNode, nonNullByDefaultName);
if (annot != null) {
return determineNNBDValue(annot);
}
astNode = astNode.getParent();
}
return EnumSet.noneOf(TypeLocation.class);
}
use of org.eclipse.jdt.core.dom.IAnnotationBinding in project eclipse.jdt.ls by eclipse.
the class RedundantNullnessTypeAnnotationsFilter method getNNBDAnnotation.
// based on org.eclipse.jdt.apt.core.internal.declaration.ASTBasedDeclarationImpl.getAnnotationInstancesFromAST()
private static /* @Nullable */
IAnnotationBinding getNNBDAnnotation(ASTNode astNode, String nonNullByDefaultName) {
List<IExtendedModifier> extendsMods = null;
switch(astNode.getNodeType()) {
case ASTNode.COMPILATION_UNIT:
{
// special case: when reaching the root of the ast, check the package annotations.
PackageDeclaration packageDeclaration = ((CompilationUnit) astNode).getPackage();
if (packageDeclaration != null) {
IPackageBinding packageBinding = packageDeclaration.resolveBinding();
if (packageBinding != null) {
for (IAnnotationBinding annotationBinding : packageBinding.getAnnotations()) {
ITypeBinding annotationType = annotationBinding.getAnnotationType();
if (annotationType != null && annotationType.getQualifiedName().equals(nonNullByDefaultName)) {
return annotationBinding;
}
}
}
}
return null;
}
case ASTNode.TYPE_DECLARATION:
case ASTNode.ANNOTATION_TYPE_DECLARATION:
case ASTNode.ENUM_DECLARATION:
case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
case ASTNode.METHOD_DECLARATION:
case ASTNode.FIELD_DECLARATION:
case ASTNode.ENUM_CONSTANT_DECLARATION:
extendsMods = ((BodyDeclaration) astNode).modifiers();
break;
case ASTNode.VARIABLE_DECLARATION_STATEMENT:
extendsMods = ((VariableDeclarationStatement) astNode).modifiers();
break;
case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
extendsMods = ((VariableDeclarationExpression) astNode).modifiers();
break;
case ASTNode.SINGLE_VARIABLE_DECLARATION:
extendsMods = ((SingleVariableDeclaration) astNode).modifiers();
break;
case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
final ASTNode parent = ((VariableDeclarationFragment) astNode).getParent();
if (parent instanceof BodyDeclaration) {
extendsMods = ((BodyDeclaration) parent).modifiers();
}
break;
default:
return null;
}
if (extendsMods != null) {
for (IExtendedModifier extMod : extendsMods) {
if (extMod.isAnnotation()) {
Annotation annotation = (Annotation) extMod;
IAnnotationBinding annotationBinding = annotation.resolveAnnotationBinding();
if (annotationBinding != null) {
ITypeBinding annotationType = annotationBinding.getAnnotationType();
if (annotationType != null && annotationType.getQualifiedName().equals(nonNullByDefaultName)) {
return annotationBinding;
}
}
}
}
}
return null;
}
Aggregations