List of usage examples for org.eclipse.jdt.internal.compiler.ast AnnotationMethodDeclaration AnnotationMethodDeclaration
public AnnotationMethodDeclaration(CompilationResult compilationResult)
From source file:com.codenvy.ide.ext.java.server.internal.compiler.parser.SourceTypeConverter.java
License:Open Source License
private AbstractMethodDeclaration convert(SourceMethod methodHandle, SourceMethodElementInfo methodInfo, CompilationResult compilationResult) throws JavaModelException { AbstractMethodDeclaration method;//from w w w .ja va2s . c o m /* only source positions available */ int start = methodInfo.getNameSourceStart(); int end = methodInfo.getNameSourceEnd(); /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=324850, Even when this type is being constructed on behalf of a 1.4 project we must internalize type variables properly in order to be able to recognize usages of them in the method signature, to apply substitutions and thus to be able to detect overriding in the presence of generics. If we simply drop them, when the method signature refers to the type parameter, we won't know it should be bound to the type parameter and perform incorrect lookup and may mistakenly end up with missing types */ TypeParameter[] typeParams = null; char[][] typeParameterNames = methodInfo.getTypeParameterNames(); if (typeParameterNames != null) { int parameterCount = typeParameterNames.length; if (parameterCount > 0) { // method's type parameters must be null if no type parameter char[][][] typeParameterBounds = methodInfo.getTypeParameterBounds(); typeParams = new TypeParameter[parameterCount]; for (int i = 0; i < parameterCount; i++) { typeParams[i] = createTypeParameter(typeParameterNames[i], typeParameterBounds[i], start, end); } } } int modifiers = methodInfo.getModifiers(); if (methodInfo.isConstructor()) { ConstructorDeclaration decl = new ConstructorDeclaration(compilationResult); decl.bits &= ~ASTNode.IsDefaultConstructor; method = decl; decl.typeParameters = typeParams; } else { MethodDeclaration decl; if (methodInfo.isAnnotationMethod()) { AnnotationMethodDeclaration annotationMethodDeclaration = new AnnotationMethodDeclaration( compilationResult); /* conversion of default value */ SourceAnnotationMethodInfo annotationMethodInfo = (SourceAnnotationMethodInfo) methodInfo; boolean hasDefaultValue = annotationMethodInfo.defaultValueStart != -1 || annotationMethodInfo.defaultValueEnd != -1; if ((this.flags & FIELD_INITIALIZATION) != 0) { if (hasDefaultValue) { char[] defaultValueSource = CharOperation.subarray(getSource(), annotationMethodInfo.defaultValueStart, annotationMethodInfo.defaultValueEnd + 1); if (defaultValueSource != null) { Expression expression = parseMemberValue(defaultValueSource); if (expression != null) { annotationMethodDeclaration.defaultValue = expression; } } else { // could not retrieve the default value hasDefaultValue = false; } } } if (hasDefaultValue) modifiers |= ClassFileConstants.AccAnnotationDefault; decl = annotationMethodDeclaration; } else { decl = new MethodDeclaration(compilationResult); } // convert return type decl.returnType = createTypeReference(methodInfo.getReturnTypeName(), start, end); // type parameters decl.typeParameters = typeParams; method = decl; } method.selector = methodHandle.getElementName().toCharArray(); boolean isVarargs = (modifiers & ClassFileConstants.AccVarargs) != 0; method.modifiers = modifiers & ~ClassFileConstants.AccVarargs; method.sourceStart = start; method.sourceEnd = end; method.declarationSourceStart = methodInfo.getDeclarationSourceStart(); method.declarationSourceEnd = methodInfo.getDeclarationSourceEnd(); // convert 1.5 specific constructs only if compliance is 1.5 or above if (this.has1_5Compliance) { /* convert annotations */ method.annotations = convertAnnotations(methodHandle); } /* convert arguments */ String[] argumentTypeSignatures = methodHandle.getParameterTypes(); char[][] argumentNames = methodInfo.getArgumentNames(); int argumentCount = argumentTypeSignatures == null ? 0 : argumentTypeSignatures.length; if (argumentCount > 0) { ILocalVariable[] parameters = methodHandle.getParameters(); long position = ((long) start << 32) + end; method.arguments = new Argument[argumentCount]; for (int i = 0; i < argumentCount; i++) { TypeReference typeReference = createTypeReference(argumentTypeSignatures[i], start, end); if (isVarargs && i == argumentCount - 1) { typeReference.bits |= ASTNode.IsVarArgs; } method.arguments[i] = new Argument(argumentNames[i], position, typeReference, ClassFileConstants.AccDefault); // do not care whether was final or not // convert 1.5 specific constructs only if compliance is 1.5 or above if (this.has1_5Compliance) { /* convert annotations */ method.arguments[i].annotations = convertAnnotations(parameters[i]); } } } /* convert thrown exceptions */ char[][] exceptionTypeNames = methodInfo.getExceptionTypeNames(); int exceptionCount = exceptionTypeNames == null ? 0 : exceptionTypeNames.length; if (exceptionCount > 0) { method.thrownExceptions = new TypeReference[exceptionCount]; for (int i = 0; i < exceptionCount; i++) { method.thrownExceptions[i] = createTypeReference(exceptionTypeNames[i], start, end); } } /* convert local and anonymous types */ if ((this.flags & LOCAL_TYPE) != 0) { IJavaElement[] children = methodInfo.getChildren(); int typesLength = children.length; if (typesLength != 0) { Statement[] statements = new Statement[typesLength]; for (int i = 0; i < typesLength; i++) { SourceType type = (SourceType) children[i]; TypeDeclaration localType = convert(type, compilationResult); if ((localType.bits & ASTNode.IsAnonymousType) != 0) { QualifiedAllocationExpression expression = new QualifiedAllocationExpression(localType); expression.type = localType.superclass; localType.superclass = null; localType.superInterfaces = null; localType.allocation = expression; statements[i] = expression; } else { statements[i] = localType; } } method.statements = statements; } } return method; }
From source file:com.codenvy.ide.ext.java.server.internal.core.BinaryTypeConverter.java
License:Open Source License
private AbstractMethodDeclaration convert(IMethod method, IType type) throws JavaModelException { AbstractMethodDeclaration methodDeclaration; org.eclipse.jdt.internal.compiler.ast.TypeParameter[] typeParams = null; // convert 1.5 specific constructs only if compliance is 1.5 or above if (this.has1_5Compliance) { /* convert type parameters */ ITypeParameter[] typeParameters = method.getTypeParameters(); if (typeParameters != null && typeParameters.length > 0) { int parameterCount = typeParameters.length; typeParams = new org.eclipse.jdt.internal.compiler.ast.TypeParameter[parameterCount]; for (int i = 0; i < parameterCount; i++) { ITypeParameter typeParameter = typeParameters[i]; typeParams[i] = createTypeParameter(typeParameter.getElementName().toCharArray(), stringArrayToCharArray(typeParameter.getBounds()), 0, 0); }// ww w .j a v a2 s. c o m } } if (method.isConstructor()) { ConstructorDeclaration decl = new ConstructorDeclaration(this.compilationResult); decl.bits &= ~ASTNode.IsDefaultConstructor; decl.typeParameters = typeParams; methodDeclaration = decl; } else { MethodDeclaration decl = type.isAnnotation() ? new AnnotationMethodDeclaration(this.compilationResult) : new MethodDeclaration(this.compilationResult); /* convert return type */ TypeReference typeReference = createTypeReference(method.getReturnType()); if (typeReference == null) return null; decl.returnType = typeReference; decl.typeParameters = typeParams; methodDeclaration = decl; } methodDeclaration.selector = method.getElementName().toCharArray(); int flags = method.getFlags(); boolean isVarargs = Flags.isVarargs(flags); methodDeclaration.modifiers = flags & ~Flags.AccVarargs; /* convert arguments */ String[] argumentTypeNames = method.getParameterTypes(); String[] argumentNames = method.getParameterNames(); int argumentCount = argumentTypeNames == null ? 0 : argumentTypeNames.length; // Ignore synthetic arguments (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=212224) int startIndex = (method.isConstructor() && type.isMember() && !Flags.isStatic(type.getFlags())) ? 1 : 0; argumentCount -= startIndex; methodDeclaration.arguments = new Argument[argumentCount]; for (int i = 0; i < argumentCount; i++) { String argumentTypeName = argumentTypeNames[startIndex + i]; TypeReference typeReference = createTypeReference(argumentTypeName); if (typeReference == null) return null; if (isVarargs && i == argumentCount - 1) { typeReference.bits |= ASTNode.IsVarArgs; } methodDeclaration.arguments[i] = new Argument(argumentNames[i].toCharArray(), 0, typeReference, ClassFileConstants.AccDefault); // do not care whether was final or not } /* convert thrown exceptions */ String[] exceptionTypeNames = method.getExceptionTypes(); int exceptionCount = exceptionTypeNames == null ? 0 : exceptionTypeNames.length; if (exceptionCount > 0) { methodDeclaration.thrownExceptions = new TypeReference[exceptionCount]; for (int i = 0; i < exceptionCount; i++) { TypeReference typeReference = createTypeReference(exceptionTypeNames[i]); if (typeReference == null) return null; methodDeclaration.thrownExceptions[i] = typeReference; } } return methodDeclaration; }
From source file:org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration.java
License:Open Source License
/** * Create a JDT MethodDeclaration that represents a groovy MethodNode *//*w w w .ja v a2 s . c o m*/ private MethodDeclaration createMethodDeclaration(ClassNode classNode, MethodNode methodNode, boolean isEnum, CompilationResult compilationResult) { if (classNode.isAnnotationDefinition()) { AnnotationMethodDeclaration methodDeclaration = new AnnotationMethodDeclaration(compilationResult); int modifiers = methodNode.getModifiers(); modifiers &= ~(ClassFileConstants.AccSynthetic | ClassFileConstants.AccTransient); methodDeclaration.annotations = transformAnnotations(methodNode.getAnnotations()); methodDeclaration.modifiers = modifiers; if (methodNode.hasAnnotationDefault()) { methodDeclaration.modifiers |= ClassFileConstants.AccAnnotationDefault; } methodDeclaration.selector = methodNode.getName().toCharArray(); fixupSourceLocationsForMethodDeclaration(methodDeclaration, methodNode); ClassNode returnType = methodNode.getReturnType(); methodDeclaration.returnType = createTypeReferenceForClassNode(returnType); return methodDeclaration; } else { MethodDeclaration methodDeclaration = new MethodDeclaration(compilationResult); // TODO refactor - extract method GenericsType[] generics = methodNode.getGenericsTypes(); // generic method if (generics != null && generics.length != 0) { methodDeclaration.typeParameters = new TypeParameter[generics.length]; for (int tp = 0; tp < generics.length; tp++) { TypeParameter typeParameter = new TypeParameter(); typeParameter.name = generics[tp].getName().toCharArray(); ClassNode[] upperBounds = generics[tp].getUpperBounds(); if (upperBounds != null) { // FIXASC Positional info for these references? typeParameter.type = createTypeReferenceForClassNode(upperBounds[0]); typeParameter.bounds = (upperBounds.length > 1 ? new TypeReference[upperBounds.length - 1] : null); for (int b = 1, max = upperBounds.length; b < max; b++) { typeParameter.bounds[b - 1] = createTypeReferenceForClassNode(upperBounds[b]); typeParameter.bounds[b - 1].bits |= ASTNode.IsSuperType; } } methodDeclaration.typeParameters[tp] = typeParameter; } } boolean isMain = false; // Note: modifiers for the MethodBinding constructed for this declaration will be created marked with // AccVarArgs if the bitset for the type reference in the final argument is marked IsVarArgs int modifiers = methodNode.getModifiers(); modifiers &= ~(ClassFileConstants.AccSynthetic | ClassFileConstants.AccTransient); methodDeclaration.annotations = transformAnnotations(methodNode.getAnnotations()); methodDeclaration.modifiers = modifiers; methodDeclaration.selector = methodNode.getName().toCharArray(); // Need to capture the rule in Verifier.adjustTypesIfStaticMainMethod(MethodNode node) // if (node.getName().equals("main") && node.isStatic()) { // Parameter[] params = node.getParameters(); // if (params.length == 1) { // Parameter param = params[0]; // if (param.getType() == null || param.getType()==ClassHelper.OBJECT_TYPE) { // param.setType(ClassHelper.STRING_TYPE.makeArray()); // ClassNode returnType = node.getReturnType(); // if(returnType == ClassHelper.OBJECT_TYPE) { // node.setReturnType(ClassHelper.VOID_TYPE); // } // } // } Parameter[] params = methodNode.getParameters(); ClassNode returnType = methodNode.getReturnType(); // source of 'static main(args)' would become 'static Object main(Object args)' - so transform here if ((modifiers & ClassFileConstants.AccStatic) != 0 && params != null && params.length == 1 && methodNode.getName().equals("main")) { Parameter p = params[0]; if (p.getType() == null || p.getType().getName().equals(ClassHelper.OBJECT)) { String name = p.getName(); params = new Parameter[1]; params[0] = new Parameter(ClassHelper.STRING_TYPE.makeArray(), name); if (returnType.getName().equals(ClassHelper.OBJECT)) { returnType = ClassHelper.VOID_TYPE; } } } methodDeclaration.arguments = createArguments(params, isMain); methodDeclaration.returnType = createTypeReferenceForClassNode(returnType); methodDeclaration.thrownExceptions = createTypeReferencesForClassNodes(methodNode.getExceptions()); fixupSourceLocationsForMethodDeclaration(methodDeclaration, methodNode); return methodDeclaration; } }
From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java
License:Open Source License
protected void consumeMethodHeaderName(boolean isAnnotationMethod) { // MethodHeaderName ::= Modifiersopt Type 'Identifier' '(' // AnnotationMethodHeaderName ::= Modifiersopt Type 'Identifier' '(' // RecoveryMethodHeaderName ::= Modifiersopt Type 'Identifier' '(' MethodDeclaration md = null;// w w w . j a v a 2s . co m if (isAnnotationMethod) { md = new AnnotationMethodDeclaration(this.compilationUnit.compilationResult); this.recordStringLiterals = false; } else { md = new MethodDeclaration(this.compilationUnit.compilationResult); } //name md.selector = this.identifierStack[this.identifierPtr]; long selectorSource = this.identifierPositionStack[this.identifierPtr--]; this.identifierLengthPtr--; //type md.returnType = getTypeReference(this.intStack[this.intPtr--]); //modifiers md.declarationSourceStart = this.intStack[this.intPtr--]; md.modifiers = this.intStack[this.intPtr--]; // consume annotations int length; if ((length = this.expressionLengthStack[this.expressionLengthPtr--]) != 0) { System.arraycopy(this.expressionStack, (this.expressionPtr -= length) + 1, md.annotations = new Annotation[length], 0, length); } // javadoc md.javadoc = this.javadoc; this.javadoc = null; //highlight starts at selector start md.sourceStart = (int) (selectorSource >>> 32); pushOnAstStack(md); md.sourceEnd = this.lParenPos; md.bodyStart = this.lParenPos + 1; this.listLength = 0; // initialize this.listLength before reading parameters/throws // recovery if (this.currentElement != null) { if (this.currentElement instanceof RecoveredType //|| md.modifiers != 0 || (Util.getLineNumber(md.returnType.sourceStart, this.scanner.lineEnds, 0, this.scanner.linePtr) == Util.getLineNumber(md.sourceStart, this.scanner.lineEnds, 0, this.scanner.linePtr))) { this.lastCheckPoint = md.bodyStart; this.currentElement = this.currentElement.add(md, 0); this.lastIgnoredToken = -1; } else { this.lastCheckPoint = md.sourceStart; this.restartRecovery = true; } } }
From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java
License:Open Source License
protected void consumeMethodHeaderNameWithTypeParameters(boolean isAnnotationMethod) { // MethodHeaderName ::= Modifiersopt TypeParameters Type 'Identifier' '(' // AnnotationMethodHeaderName ::= Modifiersopt TypeParameters Type 'Identifier' '(' // RecoveryMethodHeaderName ::= Modifiersopt TypeParameters Type 'Identifier' '(' MethodDeclaration md = null;//from ww w .j a v a 2s . co m if (isAnnotationMethod) { md = new AnnotationMethodDeclaration(this.compilationUnit.compilationResult); this.recordStringLiterals = false; } else { md = new MethodDeclaration(this.compilationUnit.compilationResult); } //name md.selector = this.identifierStack[this.identifierPtr]; long selectorSource = this.identifierPositionStack[this.identifierPtr--]; this.identifierLengthPtr--; //type md.returnType = getTypeReference(this.intStack[this.intPtr--]); // consume type parameters int length = this.genericsLengthStack[this.genericsLengthPtr--]; this.genericsPtr -= length; System.arraycopy(this.genericsStack, this.genericsPtr + 1, md.typeParameters = new TypeParameter[length], 0, length); //modifiers md.declarationSourceStart = this.intStack[this.intPtr--]; md.modifiers = this.intStack[this.intPtr--]; // consume annotations if ((length = this.expressionLengthStack[this.expressionLengthPtr--]) != 0) { System.arraycopy(this.expressionStack, (this.expressionPtr -= length) + 1, md.annotations = new Annotation[length], 0, length); } // javadoc md.javadoc = this.javadoc; this.javadoc = null; //highlight starts at selector start md.sourceStart = (int) (selectorSource >>> 32); pushOnAstStack(md); md.sourceEnd = this.lParenPos; md.bodyStart = this.lParenPos + 1; this.listLength = 0; // initialize this.listLength before reading parameters/throws // recovery if (this.currentElement != null) { boolean isType; if ((isType = this.currentElement instanceof RecoveredType) //|| md.modifiers != 0 || (Util.getLineNumber(md.returnType.sourceStart, this.scanner.lineEnds, 0, this.scanner.linePtr) == Util.getLineNumber(md.sourceStart, this.scanner.lineEnds, 0, this.scanner.linePtr))) { if (isType) { ((RecoveredType) this.currentElement).pendingTypeParameters = null; } this.lastCheckPoint = md.bodyStart; this.currentElement = this.currentElement.add(md, 0); this.lastIgnoredToken = -1; } else { this.lastCheckPoint = md.sourceStart; this.restartRecovery = true; } } }
From source file:org.eclipse.jdt.internal.compiler.parser.SourceTypeConverter.java
License:Open Source License
private AbstractMethodDeclaration convert(SourceMethod methodHandle, SourceMethodElementInfo methodInfo, CompilationResult compilationResult) throws JavaModelException { AbstractMethodDeclaration method;//from www . j ava 2 s. co m /* only source positions available */ int start = methodInfo.getNameSourceStart(); int end = methodInfo.getNameSourceEnd(); /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=324850, Even when this type is being constructed on behalf of a 1.4 project we must internalize type variables properly in order to be able to recognize usages of them in the method signature, to apply substitutions and thus to be able to detect overriding in the presence of generics. If we simply drop them, when the method signature refers to the type parameter, we won't know it should be bound to the type parameter and perform incorrect lookup and may mistakenly end up with missing types */ TypeParameter[] typeParams = null; char[][] typeParameterNames = methodInfo.getTypeParameterNames(); if (typeParameterNames != null) { int parameterCount = typeParameterNames.length; if (parameterCount > 0) { // method's type parameters must be null if no type parameter char[][][] typeParameterBounds = methodInfo.getTypeParameterBounds(); typeParams = new TypeParameter[parameterCount]; for (int i = 0; i < parameterCount; i++) { typeParams[i] = createTypeParameter(typeParameterNames[i], typeParameterBounds[i], start, end); } } } int modifiers = methodInfo.getModifiers(); if (methodInfo.isConstructor()) { ConstructorDeclaration decl = new ConstructorDeclaration(compilationResult); decl.bits &= ~ASTNode.IsDefaultConstructor; method = decl; decl.typeParameters = typeParams; } else { MethodDeclaration decl; if (methodInfo.isAnnotationMethod()) { AnnotationMethodDeclaration annotationMethodDeclaration = new AnnotationMethodDeclaration( compilationResult); /* conversion of default value */ SourceAnnotationMethodInfo annotationMethodInfo = (SourceAnnotationMethodInfo) methodInfo; boolean hasDefaultValue = annotationMethodInfo.defaultValueStart != -1 || annotationMethodInfo.defaultValueEnd != -1; if ((this.flags & FIELD_INITIALIZATION) != 0) { if (hasDefaultValue) { char[] defaultValueSource = CharOperation.subarray(getSource(), annotationMethodInfo.defaultValueStart, annotationMethodInfo.defaultValueEnd + 1); if (defaultValueSource != null) { Expression expression = parseMemberValue(defaultValueSource); if (expression != null) { annotationMethodDeclaration.defaultValue = expression; } } else { // could not retrieve the default value hasDefaultValue = false; } } } if (hasDefaultValue) modifiers |= ClassFileConstants.AccAnnotationDefault; decl = annotationMethodDeclaration; } else { decl = new MethodDeclaration(compilationResult); } // convert return type decl.returnType = createTypeReference(methodInfo.getReturnTypeName(), start, end); // type parameters decl.typeParameters = typeParams; method = decl; } method.selector = methodHandle.getElementName().toCharArray(); boolean isVarargs = (modifiers & ClassFileConstants.AccVarargs) != 0; method.modifiers = modifiers & ~ClassFileConstants.AccVarargs; method.sourceStart = start; method.sourceEnd = end; method.declarationSourceStart = methodInfo.getDeclarationSourceStart(); method.declarationSourceEnd = methodInfo.getDeclarationSourceEnd(); // convert 1.5 specific constructs only if compliance is 1.5 or above if (this.has1_5Compliance) { /* convert annotations */ method.annotations = convertAnnotations(methodHandle); } /* convert arguments */ String[] argumentTypeSignatures = methodHandle.getParameterTypes(); char[][] argumentNames = methodInfo.getArgumentNames(); int argumentCount = argumentTypeSignatures == null ? 0 : argumentTypeSignatures.length; if (argumentCount > 0) { long position = ((long) start << 32) + end; method.arguments = new Argument[argumentCount]; for (int i = 0; i < argumentCount; i++) { TypeReference typeReference = createTypeReference(argumentTypeSignatures[i], start, end); if (isVarargs && i == argumentCount - 1) { typeReference.bits |= ASTNode.IsVarArgs; } method.arguments[i] = new Argument(argumentNames[i], position, typeReference, ClassFileConstants.AccDefault); // do not care whether was final or not } } /* convert thrown exceptions */ char[][] exceptionTypeNames = methodInfo.getExceptionTypeNames(); int exceptionCount = exceptionTypeNames == null ? 0 : exceptionTypeNames.length; if (exceptionCount > 0) { method.thrownExceptions = new TypeReference[exceptionCount]; for (int i = 0; i < exceptionCount; i++) { method.thrownExceptions[i] = createTypeReference(exceptionTypeNames[i], start, end); } } /* convert local and anonymous types */ if ((this.flags & LOCAL_TYPE) != 0) { IJavaElement[] children = methodInfo.getChildren(); int typesLength = children.length; if (typesLength != 0) { Statement[] statements = new Statement[typesLength]; for (int i = 0; i < typesLength; i++) { SourceType type = (SourceType) children[i]; TypeDeclaration localType = convert(type, compilationResult); if ((localType.bits & ASTNode.IsAnonymousType) != 0) { QualifiedAllocationExpression expression = new QualifiedAllocationExpression(localType); expression.type = localType.superclass; localType.superclass = null; localType.superInterfaces = null; localType.allocation = expression; statements[i] = expression; } else { statements[i] = localType; } } method.statements = statements; } } return method; }