Browse Source

fixed JASSIST-84


git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@484 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
tags/rel_3_17_1_ga
chiba 15 years ago
parent
commit
a690c8bf87

+ 3
- 1
Readme.html View File

@@ -283,7 +283,9 @@ see javassist.Dump.

<p>-version 3.11
<ul>
<li>JIRA JASSIST-67, 68, 74, 75, 76, 83 were fixed.
<li>JIRA JASSIST-67, 68, 74, 75, 76, 81, 83, 84, 85 were fixed.
<li>Now javassist.bytecode.CodeIterator can insert a gap into
a large method body more than 32KB. (JIRA JASSIST-79, 80)
</ul>

<p>-version 3.10 on March 5, 2009

+ 41
- 0
src/main/javassist/CtBehavior.java View File

@@ -145,6 +145,47 @@ public abstract class CtBehavior extends CtMember {
methodInfo.setAccessFlags(AccessFlag.of(mod));
}

/**
* Returns true if the class has the specified annotation class.
*
* @param clz the annotation class.
* @return <code>true</code> if the annotation is found,
* otherwise <code>false</code>.
* @since 3.11
*/
public boolean hasAnnotation(Class clz) {
MethodInfo mi = getMethodInfo2();
AnnotationsAttribute ainfo = (AnnotationsAttribute)
mi.getAttribute(AnnotationsAttribute.invisibleTag);
AnnotationsAttribute ainfo2 = (AnnotationsAttribute)
mi.getAttribute(AnnotationsAttribute.visibleTag);
return CtClassType.hasAnnotationType(clz,
getDeclaringClass().getClassPool(),
ainfo, ainfo2);
}

/**
* Returns the annotation if the class has the specified annotation class.
* For example, if an annotation <code>@Author</code> is associated
* with this method/constructor, an <code>Author</code> object is returned.
* The member values can be obtained by calling methods on
* the <code>Author</code> object.
*
* @param clz the annotation class.
* @return the annotation if found, otherwise <code>null</code>.
* @since 3.11
*/
public Object getAnnotation(Class clz) throws ClassNotFoundException {
MethodInfo mi = getMethodInfo2();
AnnotationsAttribute ainfo = (AnnotationsAttribute)
mi.getAttribute(AnnotationsAttribute.invisibleTag);
AnnotationsAttribute ainfo2 = (AnnotationsAttribute)
mi.getAttribute(AnnotationsAttribute.visibleTag);
return CtClassType.getAnnotationType(clz,
getDeclaringClass().getClassPool(),
ainfo, ainfo2);
}

/**
* Returns the annotations associated with this method or constructor.
*

+ 26
- 0
src/main/javassist/CtClass.java View File

@@ -474,6 +474,32 @@ public abstract class CtClass {
return 0;
}

/**
* Returns true if the class has the specified annotation class.
*
* @param clz the annotation class.
* @return <code>true</code> if the annotation is found, otherwise <code>false</code>.
* @since 3.11
*/
public boolean hasAnnotation(Class clz) {
return false;
}

/**
* Returns the annotation if the class has the specified annotation class.
* For example, if an annotation <code>@Author</code> is associated
* with this class, an <code>Author</code> object is returned.
* The member values can be obtained by calling methods on
* the <code>Author</code> object.
*
* @param clz the annotation class.
* @return the annotation if found, otherwise <code>null</code>.
* @since 3.11
*/
public Object getAnnotation(Class clz) throws ClassNotFoundException {
return null;
}

/**
* Returns the annotations associated with this class.
* For example, if an annotation <code>@Author</code> is associated

+ 77
- 0
src/main/javassist/CtClassType.java View File

@@ -434,6 +434,83 @@ class CtClassType extends CtClass {
cf.setAccessFlags(AccessFlag.of(mod));
}

public boolean hasAnnotation(Class clz) {
ClassFile cf = getClassFile2();
AnnotationsAttribute ainfo = (AnnotationsAttribute)
cf.getAttribute(AnnotationsAttribute.invisibleTag);
AnnotationsAttribute ainfo2 = (AnnotationsAttribute)
cf.getAttribute(AnnotationsAttribute.visibleTag);
return hasAnnotationType(clz, getClassPool(), ainfo, ainfo2);
}

static boolean hasAnnotationType(Class clz, ClassPool cp,
AnnotationsAttribute a1, AnnotationsAttribute a2)
{
Annotation[] anno1, anno2;

if (a1 == null)
anno1 = null;
else
anno1 = a1.getAnnotations();

if (a2 == null)
anno2 = null;
else
anno2 = a2.getAnnotations();

String typeName = clz.getName();
if (anno1 != null)
for (int i = 0; i < anno1.length; i++)
if (anno1[i].getTypeName().equals(typeName))
return true;

if (anno2 != null)
for (int i = 0; i < anno2.length; i++)
if (anno2[i].getTypeName().equals(typeName))
return true;

return false;
}

public Object getAnnotation(Class clz) throws ClassNotFoundException {
ClassFile cf = getClassFile2();
AnnotationsAttribute ainfo = (AnnotationsAttribute)
cf.getAttribute(AnnotationsAttribute.invisibleTag);
AnnotationsAttribute ainfo2 = (AnnotationsAttribute)
cf.getAttribute(AnnotationsAttribute.visibleTag);
return getAnnotationType(clz, getClassPool(), ainfo, ainfo2);
}

static Object getAnnotationType(Class clz, ClassPool cp,
AnnotationsAttribute a1, AnnotationsAttribute a2)
throws ClassNotFoundException
{
Annotation[] anno1, anno2;

if (a1 == null)
anno1 = null;
else
anno1 = a1.getAnnotations();

if (a2 == null)
anno2 = null;
else
anno2 = a2.getAnnotations();

String typeName = clz.getName();
if (anno1 != null)
for (int i = 0; i < anno1.length; i++)
if (anno1[i].getTypeName().equals(typeName))
return toAnnoType(anno1[i], cp);

if (anno2 != null)
for (int i = 0; i < anno2.length; i++)
if (anno2[i].getTypeName().equals(typeName))
return toAnnoType(anno2[i], cp);

return null;
}

public Object[] getAnnotations() throws ClassNotFoundException {
return getAnnotations(false);
}

+ 38
- 0
src/main/javassist/CtField.java View File

@@ -240,6 +240,44 @@ public class CtField extends CtMember {
fieldInfo.setAccessFlags(AccessFlag.of(mod));
}

/**
* Returns true if the class has the specified annotation class.
*
* @param clz the annotation class.
* @return <code>true</code> if the annotation is found, otherwise <code>false</code>.
* @since 3.11
*/
public boolean hasAnnotation(Class clz) {
FieldInfo fi = getFieldInfo2();
AnnotationsAttribute ainfo = (AnnotationsAttribute)
fi.getAttribute(AnnotationsAttribute.invisibleTag);
AnnotationsAttribute ainfo2 = (AnnotationsAttribute)
fi.getAttribute(AnnotationsAttribute.visibleTag);
return CtClassType.hasAnnotationType(clz, getDeclaringClass().getClassPool(),
ainfo, ainfo2);
}

/**
* Returns the annotation if the class has the specified annotation class.
* For example, if an annotation <code>@Author</code> is associated
* with this field, an <code>Author</code> object is returned.
* The member values can be obtained by calling methods on
* the <code>Author</code> object.
*
* @param clz the annotation class.
* @return the annotation if found, otherwise <code>null</code>.
* @since 3.11
*/
public Object getAnnotation(Class clz) throws ClassNotFoundException {
FieldInfo fi = getFieldInfo2();
AnnotationsAttribute ainfo = (AnnotationsAttribute)
fi.getAttribute(AnnotationsAttribute.invisibleTag);
AnnotationsAttribute ainfo2 = (AnnotationsAttribute)
fi.getAttribute(AnnotationsAttribute.visibleTag);
return CtClassType.getAnnotationType(clz, getDeclaringClass().getClassPool(),
ainfo, ainfo2);
}

/**
* Returns the annotations associated with this field.
*

+ 25
- 0
src/main/javassist/CtMember.java View File

@@ -29,6 +29,9 @@ public abstract class CtMember {
*/
static class Cache extends CtMember {
protected void extendToString(StringBuffer buffer) {}
public boolean hasAnnotation(Class clz) { return false; }
public Object getAnnotation(Class clz)
throws ClassNotFoundException { return null; }
public Object[] getAnnotations()
throws ClassNotFoundException { return null; }
public byte[] getAttribute(String name) { return null; }
@@ -201,6 +204,28 @@ public abstract class CtMember {
*/
public abstract void setModifiers(int mod);

/**
* Returns true if the class has the specified annotation class.
*
* @param clz the annotation class.
* @return <code>true</code> if the annotation is found, otherwise <code>false</code>.
* @since 3.11
*/
public abstract boolean hasAnnotation(Class clz);

/**
* Returns the annotation if the class has the specified annotation class.
* For example, if an annotation <code>@Author</code> is associated
* with this member, an <code>Author</code> object is returned.
* The member values can be obtained by calling methods on
* the <code>Author</code> object.
*
* @param clz the annotation class.
* @return the annotation if found, otherwise <code>null</code>.
* @since 3.11
*/
public abstract Object getAnnotation(Class clz) throws ClassNotFoundException;

/**
* Returns the annotations associated with this member.
* For example, if an annotation <code>@Author</code> is associated

Loading…
Cancel
Save