Browse Source

added Enum support to AnnotationInfo

Updated for JDK 1.5 Beta2 changes
type_index for Annotation is now UTF8 descriptor, same for all other class types


git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@108 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
tags/rel_3_17_1_ga
patriot1burke 20 years ago
parent
commit
fa03e04037

+ 1
- 1
build.xml View File



<project name="javassist" default="jar" basedir="."> <project name="javassist" default="jar" basedir=".">


<property name="dist-version" value="javassist-2.7"/>
<property name="dist-version" value="javassist-3.0beta2"/>


<property environment="env"/> <property environment="env"/>
<property name="target.jar" value="javassist.jar"/> <property name="target.jar" value="javassist.jar"/>

+ 19
- 0
src/main/javassist/bytecode/Descriptor.java View File

return classname.replace('/', '.'); return classname.replace('/', '.');
} }


/**
* Converts to a classname from a descriptor
*/
public static String fromDescriptor(String descriptor)
{
String newname = toJavaName(descriptor).substring(1);
return newname.substring(0, newname.length() - 1);
}

/**
* Converts to a descriptor from a classname
*/
public static String toDescriptor(String classname)
{
return "L" + toJvmName(classname) + ";";
}

/** /**
* Returns the internal representation of the class name in the * Returns the internal representation of the class name in the
* JVM. * JVM.
return sbuf.toString(); return sbuf.toString();
} }




private static void toDescriptor(StringBuffer desc, CtClass type) { private static void toDescriptor(StringBuffer desc, CtClass type) {
if (type.isArray()) { if (type.isArray()) {
desc.append('['); desc.append('[');

+ 9
- 4
src/main/javassist/bytecode/annotation/AnnotationInfo.java View File

package javassist.bytecode.annotation; package javassist.bytecode.annotation;


import javassist.bytecode.ConstPool; import javassist.bytecode.ConstPool;
import javassist.bytecode.Descriptor;
import javassist.CtClass; import javassist.CtClass;
import javassist.CtMethod; import javassist.CtMethod;
import javassist.CtPrimitiveType; import javassist.CtPrimitiveType;
* Comment * Comment
* *
* @author <a href="mailto:bill@jboss.org">Bill Burke</a> * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
* @version $Revision: 1.3 $
* @version $Revision: 1.4 $
* *
**/ **/
public class AnnotationInfo public class AnnotationInfo
} }
else else
{ {
throw new RuntimeException("cannot handle member type: " + returnType.getName());
// treat as enum. I know this is not typed, but JBoss has an Annotation Compiler for JDK 1.4
// and I want it to work with that. - Bill Burke
return new EnumMemberValue(returnType.getName(), cp);
} }
} }




if (!clazz.isInterface()) throw new RuntimeException("Only interfaces are allowed for AnnotationInfo creation."); if (!clazz.isInterface()) throw new RuntimeException("Only interfaces are allowed for AnnotationInfo creation.");
this.cp = cp; this.cp = cp;
type_index = (short) cp.addClassInfo(clazz);
// beta1 type_index = (short) cp.addClassInfo(clazz);
type_index = (short)cp.addUtf8Info(Descriptor.toDescriptor(clazz.getName()));
CtMethod methods[] = clazz.getDeclaredMethods(); CtMethod methods[] = clazz.getDeclaredMethods();
if (methods.length > 0) if (methods.length > 0)
{ {


public String getAnnotationType() public String getAnnotationType()
{ {
return cp.getClassInfo(type_index);
String name = Descriptor.fromDescriptor(cp.getUtf8Info(type_index));
return name;
} }


public Set getMemberNames() public Set getMemberNames()

+ 6
- 3
src/main/javassist/bytecode/annotation/ClassMemberValue.java View File

package javassist.bytecode.annotation; package javassist.bytecode.annotation;


import javassist.bytecode.ConstPool; import javassist.bytecode.ConstPool;
import javassist.bytecode.Descriptor;


import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
* Comment * Comment
* *
* @author <a href="mailto:bill@jboss.org">Bill Burke</a> * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
* @version $Revision: 1.3 $
* @version $Revision: 1.4 $
* *
**/ **/
public class ClassMemberValue extends MemberValue public class ClassMemberValue extends MemberValue


public String getClassName() public String getClassName()
{ {
return cp.getClassInfo(class_info_index);
// beta1 return cp.getClassInfo(class_info_index);
return Descriptor.fromDescriptor(cp.getUtf8Info(class_info_index));
} }


public void setClassName(String name) public void setClassName(String name)
{ {
class_info_index = (short)cp.addClassInfo(name);
// beta1 class_info_index = (short)cp.addClassInfo(name);
class_info_index = (short)cp.addUtf8Info(Descriptor.toDescriptor(name));
} }


public String toString() public String toString()

+ 33
- 2
src/main/javassist/bytecode/annotation/EnumMemberValue.java View File

package javassist.bytecode.annotation; package javassist.bytecode.annotation;


import javassist.bytecode.ConstPool; import javassist.bytecode.ConstPool;
import javassist.bytecode.Descriptor;


import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
* Comment * Comment
* *
* @author <a href="mailto:bill@jboss.org">Bill Burke</a> * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
* @version $Revision: 1.3 $
* @version $Revision: 1.4 $
* *
**/ **/
public class EnumMemberValue extends MemberValue public class EnumMemberValue extends MemberValue
this.const_name_index = cni; this.const_name_index = cni;
} }


public EnumMemberValue(String type, ConstPool cp)
{
super('e', cp);
setEnumType(type);
}

public EnumMemberValue(ConstPool cp)
{
super('e', cp);
}

/**
* @return tring representing the classname
*/
public String getEnumType() public String getEnumType()
{ {
return cp.getUtf8Info(type_name_index);
return Descriptor.fromDescriptor(cp.getUtf8Info(type_name_index));
}

/**
*
* @param classname FQN classname
*/
public void setEnumType(String classname)
{
type_name_index = (short)cp.addUtf8Info(Descriptor.toDescriptor(classname));
} }


public String getEnumVal() public String getEnumVal()
return cp.getUtf8Info(const_name_index); return cp.getUtf8Info(const_name_index);
} }


public void setEnumVal(String name)
{
const_name_index = (short)cp.addUtf8Info(name);
}



public String toString() public String toString()
{ {
return getEnumType() + "." + getEnumVal(); return getEnumType() + "." + getEnumVal();

Loading…
Cancel
Save