From 2d0d143c0b20b814faed4428916fc06848bdca60 Mon Sep 17 00:00:00 2001 From: "kuzukami.sh" Date: Mon, 13 Jul 2015 16:55:01 +0900 Subject: [PATCH] append an new API of hasAnnotation(String annotClsName) --- src/main/javassist/CtBehavior.java | 4 ++-- src/main/javassist/CtClass.java | 11 ++++++++++ src/main/javassist/CtClassType.java | 34 +++++++++++++++++++---------- src/main/javassist/CtField.java | 4 ++-- src/main/javassist/CtMember.java | 15 +++++++++++-- src/test/javassist/JvstTest4.java | 4 ++++ 6 files changed, 54 insertions(+), 18 deletions(-) diff --git a/src/main/javassist/CtBehavior.java b/src/main/javassist/CtBehavior.java index ede73422..f82c0f2e 100644 --- a/src/main/javassist/CtBehavior.java +++ b/src/main/javassist/CtBehavior.java @@ -161,12 +161,12 @@ public abstract class CtBehavior extends CtMember { /** * Returns true if the class has the specified annotation class. * - * @param clz the annotation class. + * @param clz the name of annotation class. * @return true if the annotation is found, * otherwise false. * @since 3.11 */ - public boolean hasAnnotation(Class clz) { + public boolean hasAnnotation(String clz) { MethodInfo mi = getMethodInfo2(); AnnotationsAttribute ainfo = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag); diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java index 0fca3ff7..8c39e376 100644 --- a/src/main/javassist/CtClass.java +++ b/src/main/javassist/CtClass.java @@ -589,6 +589,17 @@ public abstract class CtClass { * @since 3.11 */ public boolean hasAnnotation(Class clz) { + return hasAnnotation(clz.getName()); + } + + /** + * Returns true if the class has the specified annotation class. + * + * @param annotClzName the name of annotation class. + * @return true if the annotation is found, otherwise false. + * @since 3.11 + */ + public boolean hasAnnotation(String annotClzName) { return false; } diff --git a/src/main/javassist/CtClassType.java b/src/main/javassist/CtClassType.java index 8aa76b82..cf171f09 100644 --- a/src/main/javassist/CtClassType.java +++ b/src/main/javassist/CtClassType.java @@ -451,17 +451,26 @@ class CtClassType extends CtClass { cf.setAccessFlags(AccessFlag.of(mod)); } - public boolean hasAnnotation(Class clz) { + //@Override + public boolean hasAnnotation(String annotClzName) { ClassFile cf = getClassFile2(); AnnotationsAttribute ainfo = (AnnotationsAttribute) - cf.getAttribute(AnnotationsAttribute.invisibleTag); + cf.getAttribute(AnnotationsAttribute.invisibleTag); AnnotationsAttribute ainfo2 = (AnnotationsAttribute) - cf.getAttribute(AnnotationsAttribute.visibleTag); - return hasAnnotationType(clz, getClassPool(), ainfo, ainfo2); + cf.getAttribute(AnnotationsAttribute.visibleTag); + return hasAnnotationType(annotClzName, getClassPool(), ainfo, ainfo2); } static boolean hasAnnotationType(Class clz, ClassPool cp, - AnnotationsAttribute a1, AnnotationsAttribute a2) + AnnotationsAttribute a1, + AnnotationsAttribute a2) + { + return hasAnnotationType(clz.getName(), cp, a1, a2); + } + + static boolean hasAnnotationType(String annotationClzNm, ClassPool cp, + AnnotationsAttribute a1, + AnnotationsAttribute a2) { Annotation[] anno1, anno2; @@ -475,16 +484,17 @@ class CtClassType extends CtClass { else anno2 = a2.getAnnotations(); - String typeName = clz.getName(); + // String typeName = clz.getName(); + String typeName = annotationClzNm; if (anno1 != null) - for (int i = 0; i < anno1.length; i++) - if (anno1[i].getTypeName().equals(typeName)) - return true; + 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; + for (int i = 0; i < anno2.length; i++) + if (anno2[i].getTypeName().equals(typeName)) + return true; return false; } diff --git a/src/main/javassist/CtField.java b/src/main/javassist/CtField.java index dff540a0..bf5125e2 100644 --- a/src/main/javassist/CtField.java +++ b/src/main/javassist/CtField.java @@ -244,11 +244,11 @@ public class CtField extends CtMember { /** * Returns true if the class has the specified annotation class. * - * @param clz the annotation class. + * @param clz the name of annotation class. * @return true if the annotation is found, otherwise false. * @since 3.11 */ - public boolean hasAnnotation(Class clz) { + public boolean hasAnnotation(String clz) { FieldInfo fi = getFieldInfo2(); AnnotationsAttribute ainfo = (AnnotationsAttribute) fi.getAttribute(AnnotationsAttribute.invisibleTag); diff --git a/src/main/javassist/CtMember.java b/src/main/javassist/CtMember.java index ae141c65..63a26b56 100644 --- a/src/main/javassist/CtMember.java +++ b/src/main/javassist/CtMember.java @@ -30,7 +30,7 @@ public abstract class CtMember { */ static class Cache extends CtMember { protected void extendToString(StringBuffer buffer) {} - public boolean hasAnnotation(Class clz) { return false; } + public boolean hasAnnotation(String clz) { return false; } public Object getAnnotation(Class clz) throws ClassNotFoundException { return null; } public Object[] getAnnotations() @@ -214,7 +214,18 @@ public abstract class CtMember { * @return true if the annotation is found, otherwise false. * @since 3.11 */ - public abstract boolean hasAnnotation(Class clz); + public boolean hasAnnotation(Class clz) { + return hasAnnotation(clz.getName()); + } + + /** + * Returns true if the class has the specified annotation class. + * + * @param annotClzName the name of annotation class. + * @return true if the annotation is found, otherwise false. + * @since 3.11 + */ + public abstract boolean hasAnnotation(String annotClzName); /** * Returns the annotation if the class has the specified annotation class. diff --git a/src/test/javassist/JvstTest4.java b/src/test/javassist/JvstTest4.java index 549102f0..ac4c1e3b 100644 --- a/src/test/javassist/JvstTest4.java +++ b/src/test/javassist/JvstTest4.java @@ -101,6 +101,10 @@ public class JvstTest4 extends JvstTestRoot { assertTrue(cc.hasAnnotation(test4.Anno1.class)); assertFalse(cc.hasAnnotation(java.lang.annotation.Documented.class)); + + assertTrue(cc.hasAnnotation(test4.Anno1.class.getName())); + assertFalse(cc.hasAnnotation(java.lang.annotation.Documented.class.getName())); + assertEquals("empty", ((test4.Anno1)cc.getAnnotation(test4.Anno1.class)).value()); assertNull(cc.getAnnotation(Deprecated.class)); -- 2.39.5