123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- /* *******************************************************************
- * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
- * All rights reserved.
- * This program and the accompanying materials are made available
- * under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * PARC initial implementation
- * ******************************************************************/
- package org.aspectj.ajdt.internal.compiler.lookup;
-
- import java.lang.reflect.Modifier;
- import java.util.Map;
-
- import org.aspectj.bridge.ISourceLocation;
- import org.aspectj.org.eclipse.jdt.core.compiler.CharOperation;
- import org.aspectj.org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
- import org.aspectj.org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
- import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding;
- import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope;
- import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
- import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
- import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
- import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
- import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TagBits;
- import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding;
- import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.UnresolvedReferenceBinding;
- import org.aspectj.weaver.ConcreteTypeMunger;
- import org.aspectj.weaver.NewConstructorTypeMunger;
- import org.aspectj.weaver.NewFieldTypeMunger;
- import org.aspectj.weaver.NewMemberClassTypeMunger;
- import org.aspectj.weaver.NewMethodTypeMunger;
- import org.aspectj.weaver.ResolvedMember;
- import org.aspectj.weaver.ResolvedType;
- import org.aspectj.weaver.ResolvedTypeMunger;
- import org.aspectj.weaver.UnresolvedType;
- import org.aspectj.weaver.World;
-
- public class EclipseTypeMunger extends ConcreteTypeMunger {
- private ResolvedType targetTypeX;
- // protected ReferenceBinding targetBinding = null;
- public AbstractMethodDeclaration sourceMethod;
- private EclipseFactory world;
- private ISourceLocation sourceLocation;
-
- public EclipseTypeMunger(EclipseFactory world, ResolvedTypeMunger munger, ResolvedType aspectType,
- AbstractMethodDeclaration sourceMethod) {
- super(munger, aspectType);
- this.world = world;
- this.sourceMethod = sourceMethod;
- // A null sourceMethod can be because of binary weaving
- if (sourceMethod != null) {
- this.sourceLocation = new EclipseSourceLocation(sourceMethod.compilationResult, sourceMethod.sourceStart,
- sourceMethod.sourceEnd);
- // Piece of magic that tells type mungers where they came from.
- // Won't be persisted unless ResolvedTypeMunger.persistSourceLocation is true.
- munger.setSourceLocation(sourceLocation);
-
- // use a different ctor for type level inter type decls I think
- // } else {
- // this.sourceLocation = aspectType.getSourceLocation();
- // munger.setSourceLocation(sourceLocation);
- }
- // was
- targetTypeX = munger.getDeclaringType().resolve(world.getWorld());
- // targetTypeX = munger.getSignature().getDeclaringType().resolve(world.getWorld());
- // AMC, needed until generic and raw have distinct sigs...
- if (targetTypeX.isParameterizedType() || targetTypeX.isRawType()) {
- targetTypeX = targetTypeX.getGenericType();
- // targetBinding = (ReferenceBinding)world.makeTypeBinding(targetTypeX);
- }
- }
-
- public static boolean supportsKind(ResolvedTypeMunger.Kind kind) {
- return kind == ResolvedTypeMunger.Field || kind == ResolvedTypeMunger.Method || kind == ResolvedTypeMunger.Constructor
- || kind == ResolvedTypeMunger.InnerClass;
- }
-
- @Override
- public String toString() {
- return "(EclipseTypeMunger " + getMunger() + ")";
- }
-
- /**
- * Modifies signatures of a TypeBinding through its ClassScope, i.e. adds Method|FieldBindings, plays with inheritance, ...
- */
- public boolean munge(SourceTypeBinding sourceType, ResolvedType onType) {
- ResolvedType rt = onType;
- if (rt.isRawType() || rt.isParameterizedType()) {
- rt = rt.getGenericType();
- }
- boolean isExactTargetType = rt.equals(targetTypeX);
- if (!isExactTargetType) {
- // might be the topmost implementor of an interface we care about
- if (munger.getKind() != ResolvedTypeMunger.Method) {
- return false;
- }
- if (onType.isInterface()) {
- return false;
- }
- if (!munger.needsAccessToTopmostImplementor()) {
- return false;
- }
- // so we do need access, and this type could be it...
- if (!onType.isTopmostImplementor(targetTypeX)) {
- return false;
- }
- // we are the topmost implementor of an interface type that needs munging
- // but we only care about public methods here (we only do this at all to
- // drive the JDT MethodVerifier correctly)
- if (!Modifier.isPublic(munger.getSignature().getModifiers())) {
- return false;
- }
- }
- // System.out.println("munging: " + sourceType);
- // System.out.println("match: " + world.fromEclipse(sourceType) +
- // " with " + targetTypeX);
- if (munger.getKind() == ResolvedTypeMunger.Field) {
- mungeNewField(sourceType, (NewFieldTypeMunger) munger);
- } else if (munger.getKind() == ResolvedTypeMunger.Method) {
- return mungeNewMethod(sourceType, onType, (NewMethodTypeMunger) munger, isExactTargetType);
- } else if (munger.getKind() == ResolvedTypeMunger.Constructor) {
- mungeNewConstructor(sourceType, (NewConstructorTypeMunger) munger);
- } else if (munger.getKind() == ResolvedTypeMunger.InnerClass) {
- mungeNewInnerClass(sourceType, onType, (NewMemberClassTypeMunger) munger, isExactTargetType);
- } else {
- throw new RuntimeException("unimplemented: " + munger.getKind());
- }
- return true;
- }
-
- private boolean mungeNewMethod(SourceTypeBinding sourceType, ResolvedType onType, NewMethodTypeMunger munger,
- boolean isExactTargetType) {
- InterTypeMethodBinding binding = new InterTypeMethodBinding(world, munger, aspectType, sourceMethod);
-
- if (!isExactTargetType) {
- // we're munging an interface ITD onto a topmost implementor
- ResolvedMember existingMember = onType.lookupMemberIncludingITDsOnInterfaces(getSignature());
- if (existingMember != null) {
- // already have an implementation, so don't do anything
- if (onType == existingMember.getDeclaringType() && Modifier.isFinal(munger.getSignature().getModifiers())) {
- // final modifier on default implementation is taken to mean that
- // no-one else can provide an implementation
- if (!(sourceType instanceof BinaryTypeBinding)) {
- // If sourceType is a BinaryTypeBinding, this can indicate we are re-applying the ITDs to the target
- // as we 'pull it in' to resolve something. This means the clash here is with itself ! So if the ITD
- // was final when initially added to the target this error logic will trigger. We can't easily
- // identify it was added via ITD, so I'm going to make this quick change to say avoid this error for
- // BinaryTypeBindings
- CompilationUnitScope cuScope = sourceType.scope.compilationUnitScope();
- MethodBinding offendingBinding = sourceType.getExactMethod(binding.selector, binding.parameters, cuScope);
- sourceType.scope.problemReporter().finalMethodCannotBeOverridden(offendingBinding, binding);
- }
- }
- // so that we find methods from our superinterfaces later on...
- findOrCreateInterTypeMemberFinder(sourceType);
- return false;
- }
- }
-
- // retain *only* the visibility modifiers and abstract when putting methods on an interface...
- if (sourceType.isInterface()) {
- boolean isAbstract = (binding.modifiers & ClassFileConstants.AccAbstract) != 0;
- binding.modifiers = (binding.modifiers & (ClassFileConstants.AccPublic | ClassFileConstants.AccProtected | ClassFileConstants.AccPrivate));
- if (isAbstract) {
- binding.modifiers |= ClassFileConstants.AccAbstract;
- }
- }
- if (munger.getSignature().isVarargsMethod()) {
- binding.modifiers |= ClassFileConstants.AccVarargs;
- }
- findOrCreateInterTypeMemberFinder(sourceType).addInterTypeMethod(binding);
- return true;
- }
-
- // very similar to code in AspectDeclaration
- private boolean mungeNewInnerClass(SourceTypeBinding sourceType, ResolvedType onType, NewMemberClassTypeMunger munger,
- boolean isExactTargetType) {
-
- SourceTypeBinding aspectTypeBinding = (SourceTypeBinding) world.makeTypeBinding(aspectType);
-
- char[] mungerMemberTypeName = ("$" + munger.getMemberTypeName()).toCharArray();
- ReferenceBinding innerTypeBinding = null;
- for (ReferenceBinding innerType : aspectTypeBinding.memberTypes) {
- char[] compounded = CharOperation.concatWith(innerType.compoundName, '.');
- if (org.aspectj.org.eclipse.jdt.core.compiler.CharOperation.endsWith(compounded, mungerMemberTypeName)) {
- innerTypeBinding = innerType;
- break;
- }
- }
- // may be unresolved if the aspect type binding was a BinaryTypeBinding
- if (innerTypeBinding instanceof UnresolvedReferenceBinding) {
- innerTypeBinding = (ReferenceBinding)BinaryTypeBinding.resolveType(innerTypeBinding, world.getLookupEnvironment(), true);
- }
- // rb = new InterTypeMemberClassBinding(world, munger, aspectType, aspectTypeBinding, onType, munger.getMemberTypeName(),
- // sourceType);
-
- // TODO adjust modifier?
- // TODO deal with itd of it onto an interface
-
- findOrCreateInterTypeMemberClassFinder(sourceType).addInterTypeMemberType(innerTypeBinding);
- return true;
- }
-
- private void mungeNewConstructor(SourceTypeBinding sourceType, NewConstructorTypeMunger munger) {
- if (shouldTreatAsPublic()) {
- MethodBinding binding = world.makeMethodBinding(munger.getSignature(), munger.getTypeVariableAliases());
- findOrCreateInterTypeMemberFinder(sourceType).addInterTypeMethod(binding);
- TypeVariableBinding[] typeVariables = binding.typeVariables;
- for (int i = 0; i < typeVariables.length; i++) {
- TypeVariableBinding tv = typeVariables[i];
- String name = new String(tv.sourceName);
- TypeVariableBinding[] tv2 = sourceMethod.binding.typeVariables;
- for (int j = 0; j < tv2.length; j++) {
- if (new String(tv2[j].sourceName).equals(name)) {
- typeVariables[i].declaringElement = binding;
- }
- }
- }
- for (int i = 0; i < typeVariables.length; i++) {
- if (typeVariables[i].declaringElement == null) {
- throw new RuntimeException("Declaring element not set");
- }
-
- }
- // classScope.referenceContext.binding.addMethod(binding);
- } else {
- InterTypeMethodBinding binding = new InterTypeMethodBinding(world, munger, aspectType, sourceMethod);
- findOrCreateInterTypeMemberFinder(sourceType).addInterTypeMethod(binding);
- }
-
- }
-
- private void mungeNewField(SourceTypeBinding sourceType, NewFieldTypeMunger munger) {
- if (shouldTreatAsPublic() && !targetTypeX.isInterface()) {
- FieldBinding binding = world.makeFieldBinding(munger);
- findOrCreateInterTypeMemberFinder(sourceType).addInterTypeField(binding);
- // classScope.referenceContext.binding.addField(binding);
- } else {
- InterTypeFieldBinding binding = new InterTypeFieldBinding(world, munger, aspectType, sourceMethod);
- InterTypeMemberFinder finder = findOrCreateInterTypeMemberFinder(sourceType);
- // Downgrade this field munger if name is already 'claimed'
- if (finder.definesField(munger.getSignature().getName())) {
- munger.version = NewFieldTypeMunger.VersionOne;
- }
- finder.addInterTypeField(binding);
- }
- }
-
- private boolean shouldTreatAsPublic() {
- // ??? this is where we could fairly easily choose to treat package-protected
- // ??? introductions like public ones when the target type and the aspect
- // ??? are in the same package
- return Modifier.isPublic(munger.getSignature().getModifiers());
- }
-
- private InterTypeMemberFinder findOrCreateInterTypeMemberFinder(SourceTypeBinding sourceType) {
- InterTypeMemberFinder finder = (InterTypeMemberFinder) sourceType.memberFinder;
- if (finder == null) {
- finder = new InterTypeMemberFinder();
- sourceType.memberFinder = finder;
- finder.sourceTypeBinding = sourceType;
- }
- return finder;
- }
-
- private IntertypeMemberTypeFinder findOrCreateInterTypeMemberClassFinder(SourceTypeBinding sourceType) {
- IntertypeMemberTypeFinder finder = (IntertypeMemberTypeFinder) sourceType.typeFinder;
- if (finder == null) {
- finder = new IntertypeMemberTypeFinder();
- sourceType.typeFinder = finder;
- finder.targetTypeBinding = sourceType;
- sourceType.tagBits &= ~TagBits.HasNoMemberTypes; // ensure it thinks it has one
- }
- return finder;
- }
-
- @Override
- public ISourceLocation getSourceLocation() {
- return sourceLocation;
- }
-
- public void setSourceLocation(ISourceLocation sourceLocation) {
- this.sourceLocation = sourceLocation;
- }
-
- /**
- * @return AbstractMethodDeclaration
- */
- public AbstractMethodDeclaration getSourceMethod() {
- return sourceMethod;
- }
-
- @Override
- public ConcreteTypeMunger parameterizedFor(ResolvedType target) {
- return new EclipseTypeMunger(world, munger.parameterizedFor(target), aspectType, sourceMethod);
- }
-
- public ConcreteTypeMunger parameterizeWith(Map<String,UnresolvedType> m, World w) {
- return new EclipseTypeMunger(world, munger.parameterizeWith(m, w), aspectType, sourceMethod);
- }
-
- }
|