From cb1cfd4ed35ed8cfa206ff984aecb0dad3092973 Mon Sep 17 00:00:00 2001 From: aclement Date: Mon, 14 Sep 2009 17:58:52 +0000 Subject: [PATCH] refactoring --- .../aspectj/weaver/bcel/AtAjAttributes.java | 10 ++-- .../org/aspectj/weaver/bcel/BcelField.java | 22 +++++--- .../org/aspectj/weaver/bcel/BcelMethod.java | 53 ++++++++----------- .../aspectj/weaver/bcel/BcelTypeMunger.java | 44 ++++++++------- 4 files changed, 66 insertions(+), 63 deletions(-) diff --git a/weaver/src/org/aspectj/weaver/bcel/AtAjAttributes.java b/weaver/src/org/aspectj/weaver/bcel/AtAjAttributes.java index 898a5f458..1629c6f1b 100644 --- a/weaver/src/org/aspectj/weaver/bcel/AtAjAttributes.java +++ b/weaver/src/org/aspectj/weaver/bcel/AtAjAttributes.java @@ -367,10 +367,10 @@ public class AtAjAttributes { * @param msgHandler * @return list of AjAttributes */ - public static List readAj5MethodAttributes(Method method, BcelMethod bMethod, ResolvedType type, + public static List readAj5MethodAttributes(Method method, BcelMethod bMethod, ResolvedType type, ResolvedPointcutDefinition preResolvedPointcut, ISourceContext context, IMessageHandler msgHandler) { if (method.getName().startsWith(NameMangler.PREFIX)) - return Collections.EMPTY_LIST; // already dealt with by ajc... + return Collections.emptyList(); // already dealt with by ajc... AjAttributeMethodStruct struct = new AjAttributeMethodStruct(method, bMethod, type, context, msgHandler); Attribute[] attributes = method.getAttributes(); @@ -775,8 +775,7 @@ public class AtAjAttributes { // then iterate on field interface hierarchy (not object) boolean hasAtLeastOneMethod = false; - ResolvedMember[] methods = (ResolvedMember[]) fieldType.getMethodsWithoutIterator(true, false).toArray( - new ResolvedMember[0]); + ResolvedMember[] methods = fieldType.getMethodsWithoutIterator(true, false).toArray(new ResolvedMember[0]); for (int i = 0; i < methods.length; i++) { ResolvedMember method = methods[i]; if (method.isAbstract()) { @@ -967,8 +966,7 @@ public class AtAjAttributes { ResolvedType typeForDelegation = (ResolvedType) iterator.next(); // TODO check for overlapping interfaces. Eg. A implements I, I extends J - if they specify interfaces={I,J} we dont // want to do any methods twice - ResolvedMember[] methods = (ResolvedMember[]) typeForDelegation.getMethodsWithoutIterator(true, false).toArray( - new ResolvedMember[0]); + ResolvedMember[] methods = typeForDelegation.getMethodsWithoutIterator(true, false).toArray(new ResolvedMember[0]); for (int i = 0; i < methods.length; i++) { ResolvedMember method = methods[i]; if (method.isAbstract()) { diff --git a/weaver/src/org/aspectj/weaver/bcel/BcelField.java b/weaver/src/org/aspectj/weaver/bcel/BcelField.java index 73756ac31..e888154dd 100644 --- a/weaver/src/org/aspectj/weaver/bcel/BcelField.java +++ b/weaver/src/org/aspectj/weaver/bcel/BcelField.java @@ -14,7 +14,6 @@ package org.aspectj.weaver.bcel; import java.util.Collections; import java.util.HashSet; -import java.util.Iterator; import java.util.List; import org.aspectj.apache.bcel.classfile.Attribute; @@ -103,24 +102,28 @@ final class BcelField extends ResolvedMemberImpl { } + @Override public boolean isAjSynthetic() { return isAjSynthetic; // || getName().startsWith(NameMangler.PREFIX); } + @Override public boolean isSynthetic() { return isSynthetic; } + @Override public boolean hasAnnotation(UnresolvedType ofType) { ensureAnnotationTypesRetrieved(); - for (Iterator iter = annotationTypes.iterator(); iter.hasNext();) { - ResolvedType aType = (ResolvedType) iter.next(); - if (aType.equals(ofType)) + for (ResolvedType aType : annotationTypes) { + if (aType.equals(ofType)) { return true; + } } return false; } + @Override public ResolvedType[] getAnnotationTypes() { ensureAnnotationTypesRetrieved(); ResolvedType[] ret = new ResolvedType[annotationTypes.size()]; @@ -128,11 +131,13 @@ final class BcelField extends ResolvedMemberImpl { return ret; } + @Override public AnnotationAJ[] getAnnotations() { ensureAnnotationTypesRetrieved(); return annotations; } + @Override public AnnotationAJ getAnnotationOfType(UnresolvedType ofType) { ensureAnnotationTypesRetrieved(); for (int i = 0; i < annotations.length; i++) { @@ -146,10 +151,10 @@ final class BcelField extends ResolvedMemberImpl { if (annotationTypes == null) { AnnotationGen annos[] = field.getAnnotations(); if (annos == null || annos.length == 0) { - annotationTypes = Collections.EMPTY_SET; + annotationTypes = Collections.emptySet(); annotations = AnnotationAJ.EMPTY_ARRAY; } else { - annotationTypes = new HashSet(); + annotationTypes = new HashSet(); annotations = new AnnotationAJ[annos.length]; for (int i = 0; i < annos.length; i++) { AnnotationGen annotation = annos[i]; @@ -160,6 +165,7 @@ final class BcelField extends ResolvedMemberImpl { } } + @Override public void addAnnotation(AnnotationAJ annotation) { ensureAnnotationTypesRetrieved(); // Add it to the set of annotations @@ -170,7 +176,7 @@ final class BcelField extends ResolvedMemberImpl { annotations = ret; if (annotationTypes == Collections.EMPTY_SET) { - annotationTypes = new HashSet(); + annotationTypes = new HashSet(); } // Add it to the set of annotation types String typename = annotation.getTypeSignature(); @@ -182,6 +188,7 @@ final class BcelField extends ResolvedMemberImpl { * Unpack the generic signature attribute if there is one and we haven't already done so, then find the true field type of this * field (eg. List). */ + @Override public UnresolvedType getGenericReturnType() { unpackGenericSignature(); return genericFieldType; @@ -252,6 +259,7 @@ final class BcelField extends ResolvedMemberImpl { } } + @Override public void evictWeavingState() { if (field != null) { unpackGenericSignature(); diff --git a/weaver/src/org/aspectj/weaver/bcel/BcelMethod.java b/weaver/src/org/aspectj/weaver/bcel/BcelMethod.java index 5e66b63ea..c002cedf0 100644 --- a/weaver/src/org/aspectj/weaver/bcel/BcelMethod.java +++ b/weaver/src/org/aspectj/weaver/bcel/BcelMethod.java @@ -16,7 +16,6 @@ import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; -import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; @@ -183,34 +182,29 @@ class BcelMethod extends ResolvedMemberImpl { private void unpackAjAttributes(World world) { associatedShadowMunger = null; - List as = Utility.readAjAttributes(getDeclaringType().getClassName(), method.getAttributes(), getSourceContext(world), - world, bcelObjectType.getWeaverVersionAttribute()); + List as = Utility.readAjAttributes(getDeclaringType().getClassName(), method.getAttributes(), + getSourceContext(world), world, bcelObjectType.getWeaverVersionAttribute()); processAttributes(world, as); as = AtAjAttributes.readAj5MethodAttributes(method, this, world.resolve(getDeclaringType()), preResolvedPointcut, getSourceContext(world), world.getMessageHandler()); processAttributes(world, as); } - private void processAttributes(World world, List as) { - for (Iterator iter = as.iterator(); iter.hasNext();) { - AjAttribute a = (AjAttribute) iter.next(); - if (a instanceof AjAttribute.MethodDeclarationLineNumberAttribute) { - declarationLineNumber = (AjAttribute.MethodDeclarationLineNumberAttribute) a; - } else if (a instanceof AjAttribute.AdviceAttribute) { - associatedShadowMunger = ((AjAttribute.AdviceAttribute) a).reify(this, world); - // return; - } else if (a instanceof AjAttribute.AjSynthetic) { + private void processAttributes(World world, List as) { + for (AjAttribute attr : as) { + if (attr instanceof AjAttribute.MethodDeclarationLineNumberAttribute) { + declarationLineNumber = (AjAttribute.MethodDeclarationLineNumberAttribute) attr; + } else if (attr instanceof AjAttribute.AdviceAttribute) { + associatedShadowMunger = ((AjAttribute.AdviceAttribute) attr).reify(this, world); + } else if (attr instanceof AjAttribute.AjSynthetic) { bitflags |= IS_AJ_SYNTHETIC; - // isAjSynthetic = true; - } else if (a instanceof AjAttribute.EffectiveSignatureAttribute) { - // System.out.println("found effective: " + this); - effectiveSignature = (AjAttribute.EffectiveSignatureAttribute) a; - } else if (a instanceof AjAttribute.PointcutDeclarationAttribute) { - // this is an @AspectJ annotated advice method, with pointcut - // pre-resolved by ajc - preResolvedPointcut = ((AjAttribute.PointcutDeclarationAttribute) a).reify(); + } else if (attr instanceof AjAttribute.EffectiveSignatureAttribute) { + effectiveSignature = (AjAttribute.EffectiveSignatureAttribute) attr; + } else if (attr instanceof AjAttribute.PointcutDeclarationAttribute) { + // this is an @AspectJ annotated advice method, with pointcut pre-resolved by ajc + preResolvedPointcut = ((AjAttribute.PointcutDeclarationAttribute) attr).reify(); } else { - throw new BCException("weird method attribute " + a); + throw new BCException("weird method attribute " + attr); } } } @@ -267,8 +261,6 @@ class BcelMethod extends ResolvedMemberImpl { return (bitflags & IS_AJ_SYNTHETIC) != 0; } - // FIXME ??? needs an isSynthetic method - @Override public ShadowMunger getAssociatedShadowMunger() { return associatedShadowMunger; @@ -325,10 +317,10 @@ class BcelMethod extends ResolvedMemberImpl { @Override public boolean hasAnnotation(UnresolvedType ofType) { ensureAnnotationsRetrieved(); - for (Iterator iter = annotationTypes.iterator(); iter.hasNext();) { - ResolvedType aType = (ResolvedType) iter.next(); - if (aType.equals(ofType)) + for (ResolvedType aType : annotationTypes) { + if (aType.equals(ofType)) { return true; + } } return false; } @@ -380,8 +372,9 @@ class BcelMethod extends ResolvedMemberImpl { bitflags |= HAS_ANNOTATIONS; // Add it to the set of annotation types - if (annotationTypes == Collections.EMPTY_SET) - annotationTypes = new HashSet(); + if (annotationTypes == Collections.EMPTY_SET) { + annotationTypes = new HashSet(); + } annotationTypes.add(UnresolvedType.forName(annotation.getTypeName()).resolve(bcelObjectType.getWorld())); // FIXME asc looks like we are managing two 'bunches' of annotations, // one @@ -422,7 +415,7 @@ class BcelMethod extends ResolvedMemberImpl { AnnotationGen annos[] = method.getAnnotations(); if (annos.length != 0) { - annotationTypes = new HashSet(); + annotationTypes = new HashSet(); annotations = new AnnotationAJ[annos.length]; for (int i = 0; i < annos.length; i++) { AnnotationGen annotation = annos[i]; @@ -431,7 +424,7 @@ class BcelMethod extends ResolvedMemberImpl { } bitflags |= HAS_ANNOTATIONS; } else { - annotationTypes = Collections.EMPTY_SET; + annotationTypes = Collections.emptySet(); } } diff --git a/weaver/src/org/aspectj/weaver/bcel/BcelTypeMunger.java b/weaver/src/org/aspectj/weaver/bcel/BcelTypeMunger.java index e26fd58df..f408d68d2 100644 --- a/weaver/src/org/aspectj/weaver/bcel/BcelTypeMunger.java +++ b/weaver/src/org/aspectj/weaver/bcel/BcelTypeMunger.java @@ -939,8 +939,7 @@ public class BcelTypeMunger extends ConcreteTypeMunger { annotationsOnRealMember = realMember.getAnnotations(); if (annotationsOnRealMember != null) { - for (int i = 0; i < annotationsOnRealMember.length; i++) { - AnnotationAJ annotationX = annotationsOnRealMember[i]; + for (AnnotationAJ annotationX : annotationsOnRealMember) { AnnotationGen a = ((BcelAnnotation) annotationX).getBcelAnnotation(); AnnotationGen ag = new AnnotationGen(a, classWeaver.getLazyClassGen().getConstantPool(), true); mg.addAnnotation(new BcelAnnotation(ag, classWeaver.getWorld())); @@ -999,24 +998,8 @@ public class BcelTypeMunger extends ConcreteTypeMunger { // Work out if we need a bridge method for the new method added to the topmostimplementor. // Check if the munger being processed is a parameterized form of the original munger - if (munger.getDeclaredSignature() != null) { - boolean needsbridging = false; - ResolvedMember mungerSignature = munger.getSignature(); - ResolvedMember toBridgeTo = munger.getDeclaredSignature().parameterizedWith(null, - mungerSignature.getDeclaringType().resolve(getWorld()), false, munger.getTypeVariableAliases()); - if (!toBridgeTo.getReturnType().getErasureSignature().equals( - mungerSignature.getReturnType().getErasureSignature())) - needsbridging = true; - UnresolvedType[] originalParams = toBridgeTo.getParameterTypes(); - UnresolvedType[] newParams = mungerSignature.getParameterTypes(); - for (int ii = 0; ii < originalParams.length; ii++) { - if (!originalParams[ii].getErasureSignature().equals(newParams[ii].getErasureSignature())) - needsbridging = true; - } - if (needsbridging) { - createBridge(classWeaver, unMangledInterMethod, classGen, toBridgeTo); - } - } + createBridgeIfNecessary(classWeaver, munger, unMangledInterMethod, classGen); + return true; } } else { @@ -1024,6 +1007,27 @@ public class BcelTypeMunger extends ConcreteTypeMunger { } } + private void createBridgeIfNecessary(BcelClassWeaver classWeaver, NewMethodTypeMunger munger, + ResolvedMember unMangledInterMethod, LazyClassGen classGen) { + if (munger.getDeclaredSignature() != null) { + boolean needsbridging = false; + ResolvedMember mungerSignature = munger.getSignature(); + ResolvedMember toBridgeTo = munger.getDeclaredSignature().parameterizedWith(null, + mungerSignature.getDeclaringType().resolve(getWorld()), false, munger.getTypeVariableAliases()); + if (!toBridgeTo.getReturnType().getErasureSignature().equals(mungerSignature.getReturnType().getErasureSignature())) + needsbridging = true; + UnresolvedType[] originalParams = toBridgeTo.getParameterTypes(); + UnresolvedType[] newParams = mungerSignature.getParameterTypes(); + for (int ii = 0; ii < originalParams.length; ii++) { + if (!originalParams[ii].getErasureSignature().equals(newParams[ii].getErasureSignature())) + needsbridging = true; + } + if (needsbridging) { + createBridge(classWeaver, unMangledInterMethod, classGen, toBridgeTo); + } + } + } + private void copyOverParameterAnnotations(LazyMethodGen receiverMethod, ResolvedMember donorMethod) { AnnotationAJ[][] pAnnos = donorMethod.getParameterAnnotations(); if (pAnnos != null) { -- 2.39.5