List of usage examples for org.eclipse.jdt.internal.compiler.ast AbstractMethodDeclaration isAbstract
public boolean isAbstract()
From source file:com.android.tools.lint.psi.EcjPsiBuilder.java
License:Apache License
@Nullable private EcjPsiMethod toMethod(EcjPsiClass cls, AbstractMethodDeclaration method) { if (method instanceof ConstructorDeclaration) { if ((method.bits & ASTNode.IsDefaultConstructor) != 0) { return null; }//w w w. j a v a 2s .c o m } boolean isAnnotation = method instanceof AnnotationMethodDeclaration; EcjPsiMethod psiMethod; if (!isAnnotation) { psiMethod = new EcjPsiMethod(mManager, cls, method); } else { psiMethod = new EcjPsiAnnotationMethod(mManager, cls, method); } cls.adoptChild(psiMethod); EcjPsiModifierList modifierList = toModifierList(psiMethod, method); psiMethod.setModifierList(modifierList); if (cls.isInterface()) { boolean hasDefaultMethod = method.statements != null && method.statements.length > 0; int modifiers = modifierList.getModifiers(); modifiers |= (hasDefaultMethod ? 0 : Modifier.ABSTRACT) | Modifier.PUBLIC; modifiers &= ~(Modifier.PROTECTED | Modifier.PRIVATE); modifierList.setModifiers(modifiers); } else if (cls.isAnnotationType()) { int modifiers = modifierList.getModifiers(); modifiers |= Modifier.ABSTRACT | Modifier.PUBLIC; modifiers &= ~(Modifier.PROTECTED | Modifier.PRIVATE); modifierList.setModifiers(modifiers); } else if (cls.isEnum() && method.isConstructor()) { int modifiers = modifierList.getModifiers(); modifiers |= Modifier.PRIVATE; modifiers &= ~(Modifier.PUBLIC | Modifier.PROTECTED); modifierList.setModifiers(modifiers); } TypeParameter[] typeParameters = method.typeParameters(); if (typeParameters != null) { psiMethod.setTypeParameters(toTypeParameterList(psiMethod, typeParameters)); } if (method instanceof MethodDeclaration && !method.isConstructor()) { TypeReference returnType = ((MethodDeclaration) method).returnType; if (returnType != null) { psiMethod.setReturnTypeElement(toTypeElement(psiMethod, returnType)); } } psiMethod.setNameIdentifier(toIdentifier(cls, method.selector, toRange(method.sourceStart, method.sourceStart + method.selector.length))); psiMethod.setArguments(toParameterList(psiMethod, method.arguments)); PsiReferenceList psiReferenceList = toTypeReferenceList(psiMethod, method.thrownExceptions, Role.THROWS_LIST); psiMethod.setThrownExceptions(psiReferenceList); PsiCodeBlock body; if (method instanceof ConstructorDeclaration) { ExplicitConstructorCall constructorCall = ((ConstructorDeclaration) method).constructorCall; body = toBlock(psiMethod, method.statements, constructorCall, method.bodyStart - 1, method.bodyEnd + 1); } else if (method instanceof MethodDeclaration) { boolean semiColonBody = ((method.modifiers & ExtraCompilerModifiers.AccSemicolonBody) != 0); if (!method.isAbstract() && !method.isNative() && !semiColonBody) { body = toBlock(psiMethod, method.statements, null, method.bodyStart - 1, method.bodyEnd + 1); } else { body = null; } if (isAnnotation) { //noinspection CastConflictsWithInstanceof AnnotationMethodDeclaration amd = (AnnotationMethodDeclaration) method; if (amd.defaultValue != null) { PsiExpression defaultValue = toExpression(psiMethod, amd.defaultValue); ((EcjPsiAnnotationMethod) psiMethod).setValue(defaultValue); } } } else { body = toBlock(psiMethod, method.statements, null, method.bodyStart - 1, method.bodyEnd + 1); } psiMethod.setBody(body); return psiMethod; }
From source file:com.codenvy.ide.ext.java.server.internal.compiler.parser.SourceTypeConverter.java
License:Open Source License
private TypeDeclaration convert(SourceType typeHandle, CompilationResult compilationResult) throws JavaModelException { SourceTypeElementInfo typeInfo = (SourceTypeElementInfo) typeHandle.getElementInfo(); if (typeInfo.isAnonymousMember()) throw new AnonymousMemberFound(); /* create type declaration - can be member type */ TypeDeclaration type = new TypeDeclaration(compilationResult); if (typeInfo.getEnclosingType() == null) { if (typeHandle.isAnonymous()) { type.name = CharOperation.NO_CHAR; type.bits |= (ASTNode.IsAnonymousType | ASTNode.IsLocalType); } else {/*from w w w .j a v a 2 s. co m*/ if (typeHandle.isLocal()) { type.bits |= ASTNode.IsLocalType; } } } else { type.bits |= ASTNode.IsMemberType; } if ((type.bits & ASTNode.IsAnonymousType) == 0) { type.name = typeInfo.getName(); } type.name = typeInfo.getName(); int start, end; // only positions available type.sourceStart = start = typeInfo.getNameSourceStart(); type.sourceEnd = end = typeInfo.getNameSourceEnd(); type.modifiers = typeInfo.getModifiers(); type.declarationSourceStart = typeInfo.getDeclarationSourceStart(); type.declarationSourceEnd = typeInfo.getDeclarationSourceEnd(); type.bodyEnd = type.declarationSourceEnd; // convert 1.5 specific constructs only if compliance is 1.5 or above if (this.has1_5Compliance) { /* convert annotations */ type.annotations = convertAnnotations(typeHandle); } /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=324850, even in a 1.4 project, we must internalize type variables and observe any parameterization of super class and/or super interfaces in order to be able to detect overriding in the presence of generics. */ char[][] typeParameterNames = typeInfo.getTypeParameterNames(); if (typeParameterNames.length > 0) { int parameterCount = typeParameterNames.length; char[][][] typeParameterBounds = typeInfo.getTypeParameterBounds(); type.typeParameters = new TypeParameter[parameterCount]; for (int i = 0; i < parameterCount; i++) { type.typeParameters[i] = createTypeParameter(typeParameterNames[i], typeParameterBounds[i], start, end); } } /* set superclass and superinterfaces */ if (typeInfo.getSuperclassName() != null) { type.superclass = createTypeReference(typeInfo.getSuperclassName(), start, end, true /* include generics */); type.superclass.bits |= ASTNode.IsSuperType; } char[][] interfaceNames = typeInfo.getInterfaceNames(); int interfaceCount = interfaceNames == null ? 0 : interfaceNames.length; if (interfaceCount > 0) { type.superInterfaces = new TypeReference[interfaceCount]; for (int i = 0; i < interfaceCount; i++) { type.superInterfaces[i] = createTypeReference(interfaceNames[i], start, end, true /* include generics */); type.superInterfaces[i].bits |= ASTNode.IsSuperType; } } /* convert member types */ if ((this.flags & MEMBER_TYPE) != 0) { SourceType[] sourceMemberTypes = typeInfo.getMemberTypeHandles(); int sourceMemberTypeCount = sourceMemberTypes.length; type.memberTypes = new TypeDeclaration[sourceMemberTypeCount]; for (int i = 0; i < sourceMemberTypeCount; i++) { type.memberTypes[i] = convert(sourceMemberTypes[i], compilationResult); type.memberTypes[i].enclosingType = type; } } /* convert intializers and fields*/ InitializerElementInfo[] initializers = null; int initializerCount = 0; if ((this.flags & LOCAL_TYPE) != 0) { initializers = typeInfo.getInitializers(); initializerCount = initializers.length; } SourceField[] sourceFields = null; int sourceFieldCount = 0; if ((this.flags & FIELD) != 0) { sourceFields = typeInfo.getFieldHandles(); sourceFieldCount = sourceFields.length; } int length = initializerCount + sourceFieldCount; if (length > 0) { type.fields = new FieldDeclaration[length]; for (int i = 0; i < initializerCount; i++) { type.fields[i] = convert(initializers[i], compilationResult); } int index = 0; for (int i = initializerCount; i < length; i++) { type.fields[i] = convert(sourceFields[index++], type, compilationResult); } } /* convert methods - need to add default constructor if necessary */ boolean needConstructor = (this.flags & CONSTRUCTOR) != 0; boolean needMethod = (this.flags & METHOD) != 0; if (needConstructor || needMethod) { SourceMethod[] sourceMethods = typeInfo.getMethodHandles(); int sourceMethodCount = sourceMethods.length; /* source type has a constructor ? */ /* by default, we assume that one is needed. */ int extraConstructor = 0; int methodCount = 0; int kind = TypeDeclaration.kind(type.modifiers); boolean isAbstract = kind == TypeDeclaration.INTERFACE_DECL || kind == TypeDeclaration.ANNOTATION_TYPE_DECL; if (!isAbstract) { extraConstructor = needConstructor ? 1 : 0; for (int i = 0; i < sourceMethodCount; i++) { if (sourceMethods[i].isConstructor()) { if (needConstructor) { extraConstructor = 0; // Does not need the extra constructor since one constructor already exists. methodCount++; } } else if (needMethod) { methodCount++; } } } else { methodCount = needMethod ? sourceMethodCount : 0; } type.methods = new AbstractMethodDeclaration[methodCount + extraConstructor]; if (extraConstructor != 0) { // add default constructor in first position type.methods[0] = type.createDefaultConstructor(false, false); } int index = 0; boolean hasAbstractMethods = false; for (int i = 0; i < sourceMethodCount; i++) { SourceMethod sourceMethod = sourceMethods[i]; SourceMethodElementInfo methodInfo = (SourceMethodElementInfo) sourceMethod.getElementInfo(); boolean isConstructor = methodInfo.isConstructor(); if ((methodInfo.getModifiers() & ClassFileConstants.AccAbstract) != 0) { hasAbstractMethods = true; } if ((isConstructor && needConstructor) || (!isConstructor && needMethod)) { AbstractMethodDeclaration method = convert(sourceMethod, methodInfo, compilationResult); if (isAbstract || method.isAbstract()) { // fix-up flag method.modifiers |= ExtraCompilerModifiers.AccSemicolonBody; } type.methods[extraConstructor + index++] = method; } } if (hasAbstractMethods) type.bits |= ASTNode.HasAbstractMethods; } return type; }
From source file:com.codenvy.ide.ext.java.server.internal.core.BinaryTypeConverter.java
License:Open Source License
private TypeDeclaration convert(IType type, IType alreadyComputedMember, TypeDeclaration alreadyComputedMemberDeclaration) throws JavaModelException { /* create type declaration - can be member type */ TypeDeclaration typeDeclaration = new TypeDeclaration(this.compilationResult); if (type.getDeclaringType() != null) { typeDeclaration.bits |= ASTNode.IsMemberType; }//from w w w .ja v a 2s . co m typeDeclaration.name = type.getElementName().toCharArray(); typeDeclaration.modifiers = type.getFlags(); /* set superclass and superinterfaces */ if (type.getSuperclassName() != null) { TypeReference typeReference = createTypeReference(type.getSuperclassTypeSignature()); if (typeReference != null) { typeDeclaration.superclass = typeReference; typeDeclaration.superclass.bits |= ASTNode.IsSuperType; } } String[] interfaceTypes = type.getSuperInterfaceTypeSignatures(); int interfaceCount = interfaceTypes == null ? 0 : interfaceTypes.length; typeDeclaration.superInterfaces = new TypeReference[interfaceCount]; int count = 0; for (int i = 0; i < interfaceCount; i++) { TypeReference typeReference = createTypeReference(interfaceTypes[i]); if (typeReference != null) { typeDeclaration.superInterfaces[count] = typeReference; typeDeclaration.superInterfaces[count++].bits |= ASTNode.IsSuperType; } } if (count != interfaceCount) { System.arraycopy(typeDeclaration.fields, 0, typeDeclaration.superInterfaces = new TypeReference[interfaceCount], 0, interfaceCount); } // convert 1.5 specific constructs only if compliance is 1.5 or above if (this.has1_5Compliance) { /* convert type parameters */ ITypeParameter[] typeParameters = type.getTypeParameters(); if (typeParameters != null && typeParameters.length > 0) { int parameterCount = typeParameters.length; org.eclipse.jdt.internal.compiler.ast.TypeParameter[] 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); } typeDeclaration.typeParameters = typeParams; } } /* convert member types */ IType[] memberTypes = type.getTypes(); int memberTypeCount = memberTypes == null ? 0 : memberTypes.length; typeDeclaration.memberTypes = new TypeDeclaration[memberTypeCount]; for (int i = 0; i < memberTypeCount; i++) { if (alreadyComputedMember != null && alreadyComputedMember.getFullyQualifiedName() .equals(memberTypes[i].getFullyQualifiedName())) { typeDeclaration.memberTypes[i] = alreadyComputedMemberDeclaration; } else { typeDeclaration.memberTypes[i] = convert(memberTypes[i], null, null); } typeDeclaration.memberTypes[i].enclosingType = typeDeclaration; } /* convert fields */ IField[] fields = type.getFields(); int fieldCount = fields == null ? 0 : fields.length; typeDeclaration.fields = new FieldDeclaration[fieldCount]; count = 0; for (int i = 0; i < fieldCount; i++) { FieldDeclaration fieldDeclaration = convert(fields[i], type); if (fieldDeclaration != null) { typeDeclaration.fields[count++] = fieldDeclaration; } } if (count != fieldCount) { System.arraycopy(typeDeclaration.fields, 0, typeDeclaration.fields = new FieldDeclaration[count], 0, count); } /* convert methods - need to add default constructor if necessary */ IMethod[] methods = type.getMethods(); int methodCount = methods == null ? 0 : methods.length; /* source type has a constructor ? */ /* by default, we assume that one is needed. */ int neededCount = 1; for (int i = 0; i < methodCount; i++) { if (methods[i].isConstructor()) { neededCount = 0; // Does not need the extra constructor since one constructor already exists. break; } } boolean isInterface = type.isInterface(); neededCount = isInterface ? 0 : neededCount; typeDeclaration.methods = new AbstractMethodDeclaration[methodCount + neededCount]; if (neededCount != 0) { // add default constructor in first position typeDeclaration.methods[0] = typeDeclaration.createDefaultConstructor(false, false); } boolean hasAbstractMethods = false; count = 0; for (int i = 0; i < methodCount; i++) { AbstractMethodDeclaration method = convert(methods[i], type); if (method != null) { boolean isAbstract; if ((isAbstract = method.isAbstract()) || isInterface) { // fix-up flag method.modifiers |= ExtraCompilerModifiers.AccSemicolonBody; } if (isAbstract) { hasAbstractMethods = true; } typeDeclaration.methods[neededCount + (count++)] = method; } } if (count != methodCount) { System.arraycopy(typeDeclaration.methods, 0, typeDeclaration.methods = new AbstractMethodDeclaration[count + neededCount], 0, count + neededCount); } if (hasAbstractMethods) { typeDeclaration.bits |= ASTNode.HasAbstractMethods; } return typeDeclaration; }
From source file:lombok.eclipse.handlers.HandleNonNull.java
License:Open Source License
@Override public void handle(AnnotationValues<NonNull> annotation, Annotation ast, EclipseNode annotationNode) { handleFlagUsage(annotationNode, ConfigurationKeys.NON_NULL_FLAG_USAGE, "@NonNull"); if (annotationNode.up().getKind() == Kind.FIELD) { // This is meaningless unless the field is used to generate a method (@Setter, @RequiredArgsConstructor, etc), // but in that case those handlers will take care of it. However, we DO check if the annotation is applied to // a primitive, because those handlers trigger on any annotation named @NonNull and we only want the warning // behaviour on _OUR_ 'lombok.NonNull'. try {//from ww w . j a v a 2s. c o m if (isPrimitive(((AbstractVariableDeclaration) annotationNode.up().get()).type)) { annotationNode.addWarning("@NonNull is meaningless on a primitive."); } } catch (Exception ignore) { } return; } if (annotationNode.up().getKind() != Kind.ARGUMENT) return; Argument arg; AbstractMethodDeclaration declaration; try { arg = (Argument) annotationNode.up().get(); declaration = (AbstractMethodDeclaration) annotationNode.up().up().get(); } catch (Exception e) { return; } if (isGenerated(declaration)) return; if (declaration.isAbstract()) { annotationNode.addWarning("@NonNull is meaningless on a parameter of an abstract method."); return; } // Possibly, if 'declaration instanceof ConstructorDeclaration', fetch declaration.constructorCall, search it for any references to our parameter, // and if they exist, create a new method in the class: 'private static <T> T lombok$nullCheck(T expr, String msg) {if (expr == null) throw NPE; return expr;}' and // wrap all references to it in the super/this to a call to this method. Statement nullCheck = generateNullCheck(arg, annotationNode); if (nullCheck == null) { // @NonNull applied to a primitive. Kinda pointless. Let's generate a warning. annotationNode.addWarning("@NonNull is meaningless on a primitive."); return; } if (declaration.statements == null) { declaration.statements = new Statement[] { nullCheck }; } else { char[] expectedName = arg.name; /* Abort if the null check is already there, delving into try and synchronized statements */ { Statement[] stats = declaration.statements; int idx = 0; while (stats != null && stats.length > idx) { Statement stat = stats[idx++]; if (stat instanceof TryStatement) { stats = ((TryStatement) stat).tryBlock.statements; idx = 0; continue; } if (stat instanceof SynchronizedStatement) { stats = ((SynchronizedStatement) stat).block.statements; idx = 0; continue; } char[] varNameOfNullCheck = returnVarNameIfNullCheck(stat); if (varNameOfNullCheck == null) break; if (Arrays.equals(varNameOfNullCheck, expectedName)) return; } } Statement[] newStatements = new Statement[declaration.statements.length + 1]; int skipOver = 0; for (Statement stat : declaration.statements) { if (isGenerated(stat) && isNullCheck(stat)) skipOver++; else break; } System.arraycopy(declaration.statements, 0, newStatements, 0, skipOver); System.arraycopy(declaration.statements, skipOver, newStatements, skipOver + 1, declaration.statements.length - skipOver); newStatements[skipOver] = nullCheck; declaration.statements = newStatements; } annotationNode.up().up().rebuild(); }
From source file:lombok.eclipse.handlers.HandleSneakyThrows.java
License:Open Source License
public void handleMethod(EclipseNode annotation, AbstractMethodDeclaration method, List<DeclaredException> exceptions) { if (method.isAbstract()) { annotation.addError("@SneakyThrows can only be used on concrete methods."); return;//w w w.jav a 2s.c o m } if (method.statements == null || method.statements.length == 0) { boolean hasConstructorCall = false; if (method instanceof ConstructorDeclaration) { ExplicitConstructorCall constructorCall = ((ConstructorDeclaration) method).constructorCall; hasConstructorCall = constructorCall != null && !constructorCall.isImplicitSuper() && !constructorCall.isImplicitThis(); } if (hasConstructorCall) { annotation.addWarning( "Calls to sibling / super constructors are always excluded from @SneakyThrows; @SneakyThrows has been ignored because there is no other code in this constructor."); } else { annotation.addWarning("This method or constructor is empty; @SneakyThrows has been ignored."); } return; } Statement[] contents = method.statements; for (DeclaredException exception : exceptions) { contents = new Statement[] { buildTryCatchBlock(contents, exception, exception.node, method) }; } method.statements = contents; annotation.up().rebuild(); }
From source file:lombok.eclipse.handlers.HandleVersionable.java
License:Open Source License
@Override public void handle(AnnotationValues<Versionable> annotation, Annotation ast, EclipseNode annotationNode) { handleFlagUsage(annotationNode, ConfigurationKeys.VERSIONABLE_FLAG_USAGE, "@Versionable"); if (annotationNode.up().getKind() != Kind.ARGUMENT) return;/*from w w w . ja v a2 s . c o m*/ Argument arg; AbstractMethodDeclaration declaration; try { arg = (Argument) annotationNode.up().get(); declaration = (AbstractMethodDeclaration) annotationNode.up().up().get(); } catch (Exception e) { return; } if (isGenerated(declaration)) return; if (declaration.isAbstract()) { // This used to be a warning return; } // VersionableUtils.resolveVersion(version) MessageSend resolveVersionCall = new MessageSend(); resolveVersionCall.sourceStart = annotationNode.get().sourceStart; resolveVersionCall.sourceEnd = annotationNode.get().sourceEnd; setGeneratedBy(resolveVersionCall, annotationNode.get()); resolveVersionCall.receiver = HandleSetterVersionable.generateQualifiedNameRef(annotationNode.get(), "lombok".toCharArray(), VersionableUtils.class.getSimpleName().toCharArray()); resolveVersionCall.selector = "resolveVersion".toCharArray(); resolveVersionCall.arguments = new Expression[] { new SingleNameReference(arg.name, 0L) }; // org.apache.log4j.MDC.put(VersionableUtils.VERSIONABLE_KEY, VersionableUtils.resolveVersion(version)); MessageSend mdcPut = new MessageSend(); mdcPut.sourceStart = annotationNode.get().sourceStart; mdcPut.sourceEnd = annotationNode.get().sourceEnd; setGeneratedBy(mdcPut, annotationNode.get()); mdcPut.receiver = HandleSetterVersionable.generateQualifiedNameRef(annotationNode.get(), "org".toCharArray(), "apache".toCharArray(), "log4j".toCharArray(), MDC.class.getSimpleName().toCharArray()); mdcPut.selector = "put".toCharArray(); mdcPut.arguments = new Expression[] { HandleSetterVersionable.generateQualifiedNameRef(annotationNode.get(), "lombok".toCharArray(), VersionableUtils.class.getSimpleName().toCharArray(), VersionableUtils.VERSIONABLE_KEY.toCharArray()), resolveVersionCall }; if (declaration.statements == null) { declaration.statements = new Statement[] { mdcPut }; } else { Statement[] newStatements = new Statement[declaration.statements.length + 1]; int skipOver = 0; System.arraycopy(declaration.statements, 0, newStatements, 0, skipOver); System.arraycopy(declaration.statements, skipOver, newStatements, skipOver + 1, declaration.statements.length - skipOver); newStatements[skipOver] = mdcPut; declaration.statements = newStatements; } annotationNode.up().up().rebuild(); }
From source file:org.eclipse.jdt.core.dom.ASTConverter.java
License:Open Source License
public ASTNode convert(boolean isInterface, org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration methodDeclaration) { checkCanceled();// w w w . j a v a 2 s.com if (methodDeclaration instanceof org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) { return convert((org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) methodDeclaration); } MethodDeclaration methodDecl = new MethodDeclaration(this.ast); setModifiers(methodDecl, methodDeclaration); boolean isConstructor = methodDeclaration.isConstructor(); methodDecl.setConstructor(isConstructor); final SimpleName methodName = new SimpleName(this.ast); methodName.internalSetIdentifier(new String(methodDeclaration.selector)); int start = methodDeclaration.sourceStart; // GROOVY start // why does this do what it does? /* old { int end = retrieveIdentifierEndPosition(start, methodDeclaration.sourceEnd); } new */ int end = (scannerAvailable(methodDeclaration.scope) ? retrieveIdentifierEndPosition(start, methodDeclaration.sourceEnd) : methodDeclaration.sourceEnd); // GROOVY end methodName.setSourceRange(start, end - start + 1); methodDecl.setName(methodName); org.eclipse.jdt.internal.compiler.ast.TypeReference[] thrownExceptions = methodDeclaration.thrownExceptions; int methodHeaderEnd = methodDeclaration.sourceEnd; int thrownExceptionsLength = thrownExceptions == null ? 0 : thrownExceptions.length; if (thrownExceptionsLength > 0) { Name thrownException; int i = 0; do { thrownException = convert(thrownExceptions[i++]); methodDecl.thrownExceptions().add(thrownException); } while (i < thrownExceptionsLength); methodHeaderEnd = thrownException.getStartPosition() + thrownException.getLength(); } org.eclipse.jdt.internal.compiler.ast.Argument[] parameters = methodDeclaration.arguments; int parametersLength = parameters == null ? 0 : parameters.length; if (parametersLength > 0) { SingleVariableDeclaration parameter; int i = 0; do { // GROOVY start // make sure the scope is available just in case it is necessary for varargs // new code BlockScope origScope = null; if (parameters[i].binding != null) { origScope = parameters[i].binding.declaringScope; parameters[i].binding.declaringScope = methodDeclaration.scope; } // GROOVY end parameter = convert(parameters[i++]); // GROOVY start // unset the scope // new code if (parameters[i - 1].binding != null) { parameters[i - 1].binding.declaringScope = origScope; } // GROOVY end methodDecl.parameters().add(parameter); } while (i < parametersLength); if (thrownExceptionsLength == 0) { methodHeaderEnd = parameter.getStartPosition() + parameter.getLength(); } } org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall explicitConstructorCall = null; if (isConstructor) { if (isInterface) { // interface cannot have a constructor methodDecl.setFlags(methodDecl.getFlags() | ASTNode.MALFORMED); } org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration constructorDeclaration = (org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration) methodDeclaration; explicitConstructorCall = constructorDeclaration.constructorCall; switch (this.ast.apiLevel) { case AST.JLS2_INTERNAL: // set the return type to VOID PrimitiveType returnType = new PrimitiveType(this.ast); returnType.setPrimitiveTypeCode(PrimitiveType.VOID); returnType.setSourceRange(methodDeclaration.sourceStart, 0); methodDecl.internalSetReturnType(returnType); break; default: methodDecl.setReturnType2(null); } } else if (methodDeclaration instanceof org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) { org.eclipse.jdt.internal.compiler.ast.MethodDeclaration method = (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) methodDeclaration; org.eclipse.jdt.internal.compiler.ast.TypeReference typeReference = method.returnType; if (typeReference != null) { Type returnType = convertType(typeReference); // get the positions of the right parenthesis int rightParenthesisPosition = retrieveEndOfRightParenthesisPosition(end, method.bodyEnd); int extraDimensions = retrieveExtraDimension(rightParenthesisPosition, method.bodyEnd); methodDecl.setExtraDimensions(extraDimensions); setTypeForMethodDeclaration(methodDecl, returnType, extraDimensions); } else { // no return type for a method that is not a constructor methodDecl.setFlags(methodDecl.getFlags() | ASTNode.MALFORMED); switch (this.ast.apiLevel) { case AST.JLS2_INTERNAL: break; default: methodDecl.setReturnType2(null); } } } int declarationSourceStart = methodDeclaration.declarationSourceStart; int bodyEnd = methodDeclaration.bodyEnd; methodDecl.setSourceRange(declarationSourceStart, bodyEnd - declarationSourceStart + 1); int declarationSourceEnd = methodDeclaration.declarationSourceEnd; int rightBraceOrSemiColonPositionStart = bodyEnd == declarationSourceEnd ? bodyEnd : bodyEnd + 1; int closingPosition = retrieveRightBraceOrSemiColonPosition(rightBraceOrSemiColonPositionStart, declarationSourceEnd); if (closingPosition != -1) { int startPosition = methodDecl.getStartPosition(); methodDecl.setSourceRange(startPosition, closingPosition - startPosition + 1); org.eclipse.jdt.internal.compiler.ast.Statement[] statements = methodDeclaration.statements; start = retrieveStartBlockPosition(methodHeaderEnd, methodDeclaration.bodyStart); if (start == -1) start = methodDeclaration.bodyStart; // use recovery position for body start end = retrieveRightBrace(methodDeclaration.bodyEnd, declarationSourceEnd); Block block = null; if (start != -1 && end != -1) { /* * start or end can be equal to -1 if we have an interface's method. */ block = new Block(this.ast); block.setSourceRange(start, closingPosition - start + 1); methodDecl.setBody(block); } if (block != null && (statements != null || explicitConstructorCall != null)) { if (explicitConstructorCall != null && explicitConstructorCall.accessMode != org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall.ImplicitSuper) { block.statements().add(convert(explicitConstructorCall)); } int statementsLength = statements == null ? 0 : statements.length; for (int i = 0; i < statementsLength; i++) { if (statements[i] instanceof org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) { checkAndAddMultipleLocalDeclaration(statements, i, block.statements()); } else { final Statement statement = convert(statements[i]); if (statement != null) { block.statements().add(statement); } } } } if (block != null && (Modifier.isAbstract(methodDecl.getModifiers()) || Modifier.isNative(methodDecl.getModifiers()) || isInterface)) { methodDecl.setFlags(methodDecl.getFlags() | ASTNode.MALFORMED); } } else { // syntax error in this method declaration methodDecl.setFlags(methodDecl.getFlags() | ASTNode.MALFORMED); if (!methodDeclaration.isNative() && !methodDeclaration.isAbstract()) { start = retrieveStartBlockPosition(methodHeaderEnd, bodyEnd); if (start == -1) start = methodDeclaration.bodyStart; // use recovery position for body start end = methodDeclaration.bodyEnd; // try to get the best end position CategorizedProblem[] problems = methodDeclaration.compilationResult().problems; if (problems != null) { for (int i = 0, max = methodDeclaration.compilationResult().problemCount; i < max; i++) { CategorizedProblem currentProblem = problems[i]; if (currentProblem.getSourceStart() == start && currentProblem.getID() == IProblem.ParsingErrorInsertToComplete) { end = currentProblem.getSourceEnd(); break; } } } int startPosition = methodDecl.getStartPosition(); methodDecl.setSourceRange(startPosition, end - startPosition + 1); if (start != -1 && end != -1) { /* * start or end can be equal to -1 if we have an interface's method. */ Block block = new Block(this.ast); block.setSourceRange(start, end - start + 1); methodDecl.setBody(block); } } } org.eclipse.jdt.internal.compiler.ast.TypeParameter[] typeParameters = methodDeclaration.typeParameters(); if (typeParameters != null) { switch (this.ast.apiLevel) { case AST.JLS2_INTERNAL: methodDecl.setFlags(methodDecl.getFlags() | ASTNode.MALFORMED); break; default: for (int i = 0, max = typeParameters.length; i < max; i++) { methodDecl.typeParameters().add(convert(typeParameters[i])); } } } // The javadoc comment is now got from list store in compilation unit declaration convert(methodDeclaration.javadoc, methodDecl); if (this.resolveBindings) { recordNodes(methodDecl, methodDeclaration); recordNodes(methodName, methodDeclaration); methodDecl.resolveBinding(); } return methodDecl; }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.statemachine.copyinheritance.CopyInheritance.java
License:Open Source License
/** * Copy a given method to the tsub-role. * Adds the new method to AST and performs initial creation of bindings. * @param method method to copy//from ww w .j av a 2s . c o m * @param targetRoleDecl target role class */ private static void copyMethod(MethodBinding method, TypeDeclaration targetRoleDecl) { boolean wasSynthetic = false; ReferenceBinding site = null; if ((method.modifiers & AccSynthetic) != 0) { wasSynthetic = true; // some, but not all, synthetics shall be generated with strong signatures as indicated by 'site': if (!SyntheticBaseCallSurrogate.isBaseCallSurrogateName(method.selector)) site = targetRoleDecl.binding; if (SyntheticRoleBridgeMethodBinding.isPrivateBridgeSelector(method.selector)) return; // will be generated anew } if (TypeContainerMethod.isTypeContainer(method)) return; // don't copy these dummy methods if (isCreator(method) || CharOperation.equals(IOTConstants._OT_GETBASE, method.selector) || CharOperation.prefixEquals(IOTConstants.CAST_PREFIX, method.selector) || CharOperation.prefixEquals(IOTConstants.GET_CLASS_PREFIX, method.selector)) return; // create/getBase/cast/getClass-methods are generated anew in each team. // (can only happen in a team that is a role of an outer team.) // Note: we don't use AccSynthetic on creators, because // Synthetic methods are not read from byte code. // FIXME(SH): this last note is not true any more. if (targetRoleDecl.isTeam() && (ReflectionGenerator.isReflectionMethod(method) || SerializationGenerator.isSerializationMethod(method))) return; if (MethodModel.isFakedMethod(method)) return; // will be generated anew (basecall-surrogate, rolefield-bridge) if (CharOperation.equals(IOTConstants.MIGRATE_TO_TEAM, method.selector)) return; // can only be used from the exact team // avoid copying twice (see copyGeneratedFeatures()): targetRoleDecl.getRoleModel().recordCopiedFeature(method); // some useful objects: ReferenceBinding srcRole = method.declaringClass; TypeDeclaration targetTeamDecl = targetRoleDecl.enclosingType; ReferenceBinding srcTeam = TeamModel.getEnclosingTeam(srcRole); ReferenceBinding tgtTeam = targetTeamDecl.binding; MethodBinding origin = (method.copyInheritanceSrc != null) ? method.copyInheritanceSrc : method; AbstractMethodDeclaration methodFound = findMethod(srcTeam, method, tgtTeam, targetRoleDecl); if (method.isConstructor()) { if (CharOperation.equals(srcRole.compoundName, IOTConstants.ORG_OBJECTTEAMS_TEAM_OTCONFINED)) { // must add default constructor explicitly, // since if we would copy the one from Team.__OT__Confined, // it would invoke the wrong super(). ConstructorDeclaration ctor = targetRoleDecl.createDefaultConstructor(true, true); targetRoleDecl.binding.resolveGeneratedMethod(ctor, false, origin); return; } else if (targetRoleDecl.getRoleModel()._refinesExtends) { // even if we can't copy the ctor, we need to mark // the current ctor as overriding the tsuper version. if (methodFound != null) methodFound.binding.addOverriddenTSuper(method); return; // extends is covariantly redefined, don't copy ctor! // attempts to invoke this tsuper ctor are caught in ExplicitConstructorCall.resolve(). } } // If method already exists in subteam, // + adjust method in subteam to the more general signature (here) // + extend copy with marker arg (below). // else give an exact copy. boolean reuseFoundMethod = false; if (methodFound != null) { // do not touch broken methods: // (methodFound might have no binding, // e.g., due to a missing import of return-type) if (methodFound.binding == null) return; if (methodFound.binding.copyInheritanceSrc == origin) return; // diamond inheritance: copying the same method from two different sources // tsuper method trumps previously inferred callout: if (methodFound.isMappingWrapper == WrapperKind.CALLOUT) { MethodModel model = methodFound.model; if (model != null && model._inferredCallout != null) { // reset callout related stuff: methodFound.isMappingWrapper = WrapperKind.NONE; model._inferredCallout = null; methodFound.statements = null; // setup as a copied method: methodFound.isCopied = true; methodFound.binding.copyInheritanceSrc = method; methodFound.sourceMethodBinding = method; reuseFoundMethod = true; } } // do this in any case so that ConstantPoolObjectMapper won't fail: methodFound.binding.addOverriddenTSuper(method); // abstract methods have no byte code to copy; // new method silently replaces abstract version: if (method.isAbstract()) { weakenSignature(methodFound, method); return; } if (method.isFinal()) { methodFound.scope.problemReporter().finalMethodCannotBeOverridden(methodFound.binding, method); return; } weakenSignature(methodFound, method); MethodBinding foundSrc = methodFound.binding.copyInheritanceSrc; if (foundSrc != null && foundSrc != origin) { // found a copied method which has a different origin, choose the more specific: // if incommensurable prefer the new copy (assuming it comes from *implicit* super team) if (!TeamModel.isMoreSpecificThan(foundSrc.declaringClass, origin.declaringClass)) { // more specific method overwrites previous linkage: methodFound.binding.setCopyInheritanceSrc(origin); methodFound.sourceMethodBinding = origin; methodFound.isTSuper = TSuperHelper.isTSuper(method); // TODO(SH): also update CopyInheritanceSrc-Attribute! if (!method.isAbstract()) { // not abstract any more, joining abstract and implemented methods: methodFound.modifiers &= ~(AccAbstract | AccSemicolonBody); methodFound.binding.modifiers &= ~(AccAbstract | AccSemicolonBody); // TODO(SH): might need multiple copyInheritanceSrc! // TODO(SH) need to adjust copiedInContext? } } return; } } AstGenerator gen = new AstGenerator(targetRoleDecl.sourceStart, targetRoleDecl.sourceEnd); gen.replaceableEnclosingClass = tgtTeam; final AbstractMethodDeclaration newMethodDecl; if (methodFound != null && reuseFoundMethod) { newMethodDecl = methodFound; } else { newMethodDecl = AstConverter.createMethod(method, site, targetRoleDecl.compilationResult, DecapsulationState.REPORTED, gen); if (methodFound != null) TSuperHelper.addMarkerArg(newMethodDecl, srcTeam); if (newMethodDecl.isConstructor()) { // comments (SH): // other phases may depend on this field (constructorCall) being set, // although it carries no real information. ConstructorDeclaration cd = (ConstructorDeclaration) newMethodDecl; cd.constructorCall = SuperReference.implicitSuperConstructorCall(); if (Lifting.isLiftingCtor(method) && method.parameters[0].isRole()) { // if baseclass is implicitely redefined use the strong type: ReferenceBinding newBase = targetRoleDecl.binding.baseclass(); if (TypeBinding.notEquals(newBase, method.parameters[0])) newMethodDecl.arguments[0].type = gen.baseclassReference(newBase); } } AstEdit.addMethod(targetRoleDecl, newMethodDecl, wasSynthetic, false/*addToFront*/, origin); } if (method.isPrivate()) { newMethodDecl.binding.modifiers |= ExtraCompilerModifiers.AccLocallyUsed; // don't warn unused copied method } newMethodDecl.binding.copiedInContext = tgtTeam.enclosingType(); MethodModel newModel = MethodModel.getModel(newMethodDecl); newModel.addAttribute(CopyInheritanceSourceAttribute.copyInherSrcAttribute(origin, newModel)); if (wasSynthetic) targetRoleDecl.getRoleModel().addSyntheticMethodMapping(method, newMethodDecl.binding); if (method.isAnyCallin() && !method.isCallin()) // callin wrapper newMethodDecl.isMappingWrapper = WrapperKind.CALLIN; if (methodFound == null) { // copy down some more properties: if (TSuperHelper.isTSuper(method)) newMethodDecl.isTSuper = true; if (method.model != null && method.model.callinFlags != 0) MethodModel.addCallinFlag(newMethodDecl, method.model.callinFlags); if (method.isAnyCallin()) { TypeBinding inheritedSrcReturn = MethodModel.getReturnType(method); if (inheritedSrcReturn.isRole()) inheritedSrcReturn = RoleTypeCreator.maybeWrapUnqualifiedRoleType(inheritedSrcReturn, targetRoleDecl.binding); MethodModel.saveReturnType(newMethodDecl.binding, inheritedSrcReturn); } else { if (!method.isPublic() // non-public source-level class method? && !method.isConstructor() && !targetRoleDecl.isInterface() && !CharOperation.prefixEquals(IOTConstants.OT_DOLLAR_NAME, method.selector)) { MethodModel.getModel(newMethodDecl).storeModifiers(newMethodDecl.modifiers); } } // more checking: abstract method in non-abstract class? if (newMethodDecl.isAbstract() && (targetRoleDecl.modifiers & ClassFileConstants.AccAbstract) == 0) { targetRoleDecl.scope.problemReporter().setRechecker(new IProblemRechecker() { public boolean shouldBeReported(IrritantSet[] foundIrritants) { if (newMethodDecl.isAbstract()) // implemented by callout? return true; return false; // false alarm } }).abstractMethodMustBeImplemented(targetRoleDecl.binding, newMethodDecl.binding); } } }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.statemachine.copyinheritance.CopyInheritance.java
License:Open Source License
/** * For a given role method adjust its signature according to the * tsuper-role version which is provided by its binding. * For each argument type:// www . j a v a2 s . c o m * <ul> * <li> if it is a role type, make it refer explicitly to the type in the super team * <li> for each parameter whose type is adjusted insert a new local with the desired * type and cast the provided argument value to this local. * <li> replace arguments in super calls of a constructor by the renamed and * and casted argument. * </ul> * E.g., the constructor * <pre> Role (Role2 r) { * super(r); * print(r); * }</pre> * becomes: * <pre> Role (SuperTeam.Role2 __OT__r) { * super((Role2)__OT__r); * Role2 r = (Role2)__OT__r; * print(r); * </pre> * @param method method to adjust * @param template the tsuper version which should be taken as master for adjustment. * @return has weakening been performed? */ public static boolean weakenSignature(AbstractMethodDeclaration method, MethodBinding template) { MethodBinding binding = method.binding; // no weakening for constructors: if (method.isConstructor()) return false; // do not touch broken methods: if (binding == null || method.hasErrors()) return false; if (MethodModel.hasProblem(template)) { method.tagAsHavingErrors(); // propagate error return false; } MethodScope scope = method.scope; boolean changed = false; // true weakening (with weakened type binding) avoids bridge methods if (TypeBinding.notEquals(method.binding.returnType, template.returnType) && method.binding.returnType instanceof DependentTypeBinding) method.binding.returnType = WeakenedTypeBinding.makeWeakenedTypeBinding( (DependentTypeBinding) method.binding.returnType.leafComponentType(), (ReferenceBinding) template.returnType.leafComponentType(), template.returnType.dimensions()); // liftTo methods have no role arguments if (Lifting.isLiftToMethod(method.binding)) return changed; // Method parameters int paramLen = binding.parameters.length; assert (paramLen == template.parameters.length); if (paramLen == 0) return changed; if (method.isConstructor() && binding.declaringClass.isTeam()) return changed; // already processed by Lifting.prepareArgLifting() (includes a cast). // selectively share type bindings, but not the array: for (int i = 0; i < paramLen; i++) { TypeBinding param = binding.parameters[i]; TypeBinding tmplParam = template.parameters[i]; if (param.isRole()) { binding.parameters[i] = template.parameters[i]; if (param.isParameterizedType() && tmplParam.isParameterizedType()) { // keep old type arguments, just replace the type itself TypeBinding[] args = ((ParameterizedTypeBinding) param).arguments; ReferenceBinding tmplType = ((ParameterizedTypeBinding) tmplParam).genericType(); try { LookupEnvironment env = Config.getLookupEnvironment(); binding.parameters[i] = new ParameterizedTypeBinding(tmplType, args, tmplType.enclosingType(), env); } catch (NotConfiguredException e) { e.logWarning("Cannot create parameterized type"); //$NON-NLS-1$ } } } } // below: treat statements. if (method.isAbstract()) return changed; if (method.isCopied) return changed; // no statements ArrayList<Statement> newLocalStats = new ArrayList<Statement>(); for (int i = 0; i < method.arguments.length; i++) { final Argument argument = method.arguments[i]; char[] oldName = argument.name; if (RoleTypeBinding.isRoleWithExplicitAnchor(argument.type.resolvedType)) continue; TypeReference newType = TypeAnalyzer.weakenTypeReferenceFromBinding(scope, argument.type, argument.binding.type, binding.parameters[i]); if (newType != argument.type) { changed = true; newType.setBaseclassDecapsulation(argument.type.getBaseclassDecapsulation()); // local variable: newLocalStats.add(generateCastedLocal(argument, newType)); // replace arguments in super-constructor call: if (method instanceof ConstructorDeclaration) { ConstructorDeclaration ctor = (ConstructorDeclaration) method; ReplaceSingleNameVisitor.IExpressionProvider provider = new ReplaceSingleNameVisitor.IExpressionProvider() { public Expression newExpression() { return new SingleNameReference(argument.name, argument.sourceStart); } }; if (ctor.constructorCall != null) ctor.constructorCall.traverse(new ReplaceSingleNameVisitor(oldName, provider), null); // no scope } } } if (!binding.declaringClass.isDirectRole() && CharOperation.equals(binding.selector, IOTConstants.INIT_METHOD_NAME)) return changed; // no statements, not casted locals. if (!newLocalStats.isEmpty()) { if (StateHelper.hasState(binding.declaringClass, ITranslationStates.STATE_RESOLVED)) for (Statement localDeclaration : newLocalStats) localDeclaration.resolve(method.scope); // add the local variable declaration statements: MethodModel.prependStatements(method, newLocalStats); } return changed; }