From 79fdd0c8e7290b12ecca0b12aa62faa8b35a5709 Mon Sep 17 00:00:00 2001 From: aclement Date: Wed, 22 Feb 2006 15:11:07 +0000 Subject: [PATCH] optimizationasm: new BCI independant Annotation representation. --- .../src/org/aspectj/weaver/AnnotationAJ.java | 88 +++++++++++++++++++ .../weaver/AnnotationAnnotationValue.java | 35 ++++++++ .../weaver/AnnotationNameValuePair.java | 38 ++++++++ .../org/aspectj/weaver/AnnotationValue.java | 72 +++++++++++++++ .../src/org/aspectj/weaver/AnnotationX.java | 46 +++++++--- .../aspectj/weaver/ArrayAnnotationValue.java | 57 ++++++++++++ .../aspectj/weaver/ClassAnnotationValue.java | 31 +++++++ .../aspectj/weaver/EnumAnnotationValue.java | 37 ++++++++ 8 files changed, 390 insertions(+), 14 deletions(-) create mode 100644 weaver/src/org/aspectj/weaver/AnnotationAJ.java create mode 100644 weaver/src/org/aspectj/weaver/AnnotationAnnotationValue.java create mode 100644 weaver/src/org/aspectj/weaver/AnnotationNameValuePair.java create mode 100644 weaver/src/org/aspectj/weaver/AnnotationValue.java create mode 100644 weaver/src/org/aspectj/weaver/ArrayAnnotationValue.java create mode 100644 weaver/src/org/aspectj/weaver/ClassAnnotationValue.java create mode 100644 weaver/src/org/aspectj/weaver/EnumAnnotationValue.java diff --git a/weaver/src/org/aspectj/weaver/AnnotationAJ.java b/weaver/src/org/aspectj/weaver/AnnotationAJ.java new file mode 100644 index 000000000..aed97377c --- /dev/null +++ b/weaver/src/org/aspectj/weaver/AnnotationAJ.java @@ -0,0 +1,88 @@ +/* ******************************************************************* + * Copyright (c) 2006 Contributors + * 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: + * Andy Clement IBM initial implementation + * ******************************************************************/ +package org.aspectj.weaver; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +/** + * This type represents the weavers abstraction of an annotation - it is + * not tied to any underlying BCI toolkit. The weaver actualy handles these + * through AnnotationX wrapper objects - until we start transforming the + * BCEL annotations into this form (expensive) or offer a clever + * visitor mechanism over the BCEL annotation stuff that builds these + * annotation types directly. + * + * @author AndyClement + */ +public class AnnotationAJ { + + private String type; + private boolean isRuntimeVisible; + + private List /*of AnnotationNVPair*/ nvPairs = null; + + public AnnotationAJ(String type,boolean isRuntimeVisible) { + this.type = type; + this.isRuntimeVisible = isRuntimeVisible; + } + + public String getTypeSignature() { + return type; + } + + public List getNameValuePairs() { + return nvPairs; + } + + public boolean hasNameValuePairs() { + return nvPairs!=null && nvPairs.size()!=0; + } + + public boolean isRuntimeVisible() { + return isRuntimeVisible; + } + + public String stringify() { + return "xxxxxxxxxxx"; + } + + public String getStringValueOf(Object name) { + if (!hasNameValuePairs()) return null; + for (Iterator iter = nvPairs.iterator(); iter.hasNext();) { + AnnotationNameValuePair nvpair = (AnnotationNameValuePair) iter.next(); + if (nvpair.getName().equals(name)) return nvpair.getValue().stringify(); + } + return null; + } + + public void addNameValuePair(AnnotationNameValuePair pair) { + if (nvPairs==null) nvPairs=new ArrayList(); + nvPairs.add(pair); + } + + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append("ANNOTATION ["+getTypeSignature()+"] ["+ + (isRuntimeVisible?"runtimeVisible":"runtimeInvisible")+"] ["); + if (nvPairs!=null) { + for (Iterator iter = nvPairs.iterator(); iter.hasNext();) { + AnnotationNameValuePair element = (AnnotationNameValuePair) iter.next(); + sb.append(element.toString()); + if (iter.hasNext()) sb.append(","); + } + } + sb.append("]"); + return sb.toString(); + } +} diff --git a/weaver/src/org/aspectj/weaver/AnnotationAnnotationValue.java b/weaver/src/org/aspectj/weaver/AnnotationAnnotationValue.java new file mode 100644 index 000000000..8002371ca --- /dev/null +++ b/weaver/src/org/aspectj/weaver/AnnotationAnnotationValue.java @@ -0,0 +1,35 @@ +/* ******************************************************************* + * Copyright (c) 2006 Contributors + * 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: + * Andy Clement IBM initial implementation + * ******************************************************************/ + package org.aspectj.weaver; + +public class AnnotationAnnotationValue extends AnnotationValue { + + private AnnotationAJ value; + + public AnnotationAnnotationValue(AnnotationAJ value) { + super(AnnotationValue.ANNOTATION); + this.value = value; + } + + public AnnotationAJ getAnnotation() { + return value; + } + + public String stringify() { + return value.stringify(); + } + + public String toString() { + return value.toString(); + } + +} diff --git a/weaver/src/org/aspectj/weaver/AnnotationNameValuePair.java b/weaver/src/org/aspectj/weaver/AnnotationNameValuePair.java new file mode 100644 index 000000000..3f2e61b1d --- /dev/null +++ b/weaver/src/org/aspectj/weaver/AnnotationNameValuePair.java @@ -0,0 +1,38 @@ +/* ******************************************************************* + * Copyright (c) 2006 Contributors + * 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: + * Andy Clement IBM initial implementation + * ******************************************************************/ + package org.aspectj.weaver; + +public class AnnotationNameValuePair { + + private String name; + + private AnnotationValue val; + + public AnnotationNameValuePair(String name,AnnotationValue val) { + this.name = name; + this.val = val; + } + + public String getName() { + return name; + } + + public AnnotationValue getValue() { + return val; + } + + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append(name+"="+val.toString()); + return sb.toString(); + } +} diff --git a/weaver/src/org/aspectj/weaver/AnnotationValue.java b/weaver/src/org/aspectj/weaver/AnnotationValue.java new file mode 100644 index 000000000..9c7a4b916 --- /dev/null +++ b/weaver/src/org/aspectj/weaver/AnnotationValue.java @@ -0,0 +1,72 @@ +/* ******************************************************************* + * Copyright (c) 2006 Contributors + * 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: + * Andy Clement IBM initial implementation + * ******************************************************************/ + package org.aspectj.weaver; + + +public abstract class AnnotationValue { + + protected int valueKind; + + public static final int STRING = 's'; + public static final int ENUM_CONSTANT = 'e'; + public static final int CLASS = 'c'; + public static final int ANNOTATION = '@'; + public static final int ARRAY = '['; + + public static final int PRIMITIVE_INT = 'I'; + public static final int PRIMITIVE_BYTE = 'B'; + public static final int PRIMITIVE_CHAR = 'C'; + public static final int PRIMITIVE_DOUBLE = 'D'; + public static final int PRIMITIVE_FLOAT = 'F'; + public static final int PRIMITIVE_LONG = 'J'; + public static final int PRIMITIVE_SHORT = 'S'; + public static final int PRIMITIVE_BOOLEAN= 'Z'; + + public abstract String stringify(); + + public AnnotationValue(int kind) { + valueKind = kind; + } + + public static String whatKindIsThis(int kind) { + switch (kind) { + case PRIMITIVE_BYTE: // byte + return "byte"; + case PRIMITIVE_CHAR: // char + return "char"; + case PRIMITIVE_DOUBLE: // double + return "double"; + case PRIMITIVE_FLOAT: // float + return "float"; + case PRIMITIVE_INT: // int + return "int"; + case PRIMITIVE_LONG: // long + return "long"; + case PRIMITIVE_SHORT: // short + return "short"; + case PRIMITIVE_BOOLEAN: // boolean + return "boolean"; + case 's': // String + return "string"; + case 'e': // Enum constant + return "enum"; + case 'c': // Class + return "class"; + case '@': // Annotation + return "annotation"; + case '[': // Array + return "array"; + default: + throw new RuntimeException("Dont know what this is : "+kind); + } + } +} diff --git a/weaver/src/org/aspectj/weaver/AnnotationX.java b/weaver/src/org/aspectj/weaver/AnnotationX.java index 2582b2c47..17e06aa61 100644 --- a/weaver/src/org/aspectj/weaver/AnnotationX.java +++ b/weaver/src/org/aspectj/weaver/AnnotationX.java @@ -20,15 +20,23 @@ import org.aspectj.apache.bcel.classfile.annotation.ArrayElementValue; import org.aspectj.apache.bcel.classfile.annotation.ElementNameValuePair; import org.aspectj.apache.bcel.classfile.annotation.ElementValue; import org.aspectj.apache.bcel.classfile.annotation.EnumElementValue; +import org.aspectj.apache.bcel.classfile.Utility; /** - * An AnnotationX is the 'holder' for a BCEL annotation - we have this holder - * so that types about the bcel weaver package can work with something not - * BCEL specific. + * AnnotationX instances are holders for an annotation from either Bcel or + * ASM. We have this holder so that types about the bcel weaver package + * can work with something not bytecode toolkit specific. */ public class AnnotationX { - private Annotation theRealAnnotation; + public static final AnnotationX[] NONE = new AnnotationX[0]; + + private Annotation theRealBcelAnnotation; + private AnnotationAJ theRealASMAnnotation; + private int mode = -1; + private final static int MODE_ASM = 1; + private final static int MODE_BCEL = 2; + private ResolvedType signature = null; // @target meta-annotation related stuff, built lazily @@ -37,12 +45,19 @@ public class AnnotationX { private Set supportedTargets = null; public AnnotationX(Annotation a,World world) { - theRealAnnotation = a; - signature = UnresolvedType.forSignature(theRealAnnotation.getTypeSignature()).resolve(world); + theRealBcelAnnotation = a; + signature = UnresolvedType.forSignature(theRealBcelAnnotation.getTypeSignature()).resolve(world); + mode = MODE_BCEL; + } + + public AnnotationX(AnnotationAJ a,World world) { + theRealASMAnnotation = a; + signature = UnresolvedType.forSignature(theRealASMAnnotation.getTypeSignature()).resolve(world); + mode= MODE_ASM; } public Annotation getBcelAnnotation() { - return theRealAnnotation; + return theRealBcelAnnotation; } public UnresolvedType getSignature() { @@ -50,16 +65,19 @@ public class AnnotationX { } public String toString() { - return theRealAnnotation.toString(); + if (mode==MODE_BCEL) return theRealBcelAnnotation.toString(); + else return theRealASMAnnotation.toString(); } public String getTypeName() { - return theRealAnnotation.getTypeName(); + if (mode==MODE_BCEL) return theRealBcelAnnotation.getTypeName(); + else return Utility.signatureToString(theRealASMAnnotation.getTypeSignature()); } public String getTypeSignature() { - return theRealAnnotation.getTypeSignature(); + if (mode==MODE_BCEL) return theRealBcelAnnotation.getTypeSignature(); + else return theRealASMAnnotation.getTypeSignature(); } @@ -68,9 +86,9 @@ public class AnnotationX { * return true if this annotation can target an annotation type */ public boolean allowedOnAnnotationType() { - ensureAtTargetInitialized(); - if (atTargetAnnotation == null) return true; // if no target specified, then return true - return supportedTargets.contains("ANNOTATION_TYPE"); + ensureAtTargetInitialized(); + if (atTargetAnnotation == null) return true; // if no target specified, then return true + return supportedTargets.contains("ANNOTATION_TYPE"); } /** @@ -159,7 +177,7 @@ public class AnnotationX { } public boolean isRuntimeVisible() { - return theRealAnnotation.isRuntimeVisible(); + return theRealBcelAnnotation.isRuntimeVisible(); } } \ No newline at end of file diff --git a/weaver/src/org/aspectj/weaver/ArrayAnnotationValue.java b/weaver/src/org/aspectj/weaver/ArrayAnnotationValue.java new file mode 100644 index 000000000..8b63eaa8f --- /dev/null +++ b/weaver/src/org/aspectj/weaver/ArrayAnnotationValue.java @@ -0,0 +1,57 @@ +/* ******************************************************************* + * Copyright (c) 2006 Contributors + * 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: + * Andy Clement IBM initial implementation + * ******************************************************************/ + package org.aspectj.weaver; + +public class ArrayAnnotationValue extends AnnotationValue { + + private AnnotationValue[] values; + + public ArrayAnnotationValue() { + super(AnnotationValue.ARRAY); + } + + public void setValues(AnnotationValue[] values) { + this.values = values; + } + + public ArrayAnnotationValue(AnnotationValue[] values) { + super(AnnotationValue.ARRAY); + this.values = values; + } + + public AnnotationValue[] getValues() { + return values; + } + + public String stringify() { + StringBuffer sb = new StringBuffer(); + sb.append("["); + for (int i = 0; i < values.length; i++) { + sb.append(values[i].stringify()); + if (i+1