/**
* Creates a new public nested class.
- * This method is called by CtClassType.makeNestedClass().
+ * This method is called by {@link CtClassType#makeNestedClass()}.
*
* @param classname a fully-qualified class name.
* @return the nested class.
return clazz;
}
+ /**
+ * Creates a new annotation.
+ * If there already exists a class/interface with the same name,
+ * the new interface overwrites that previous one.
+ *
+ * @param name a fully-qualified interface name.
+ * Or null if the annotation has no super interface.
+ * @param superclass the super interface.
+ * @throws RuntimeException if the existing interface is frozen.
+ * @since 3.19
+ */
+ public CtClass makeAnnotation(String name, CtClass superclass) throws RuntimeException {
+ CtClass cc = makeInterface(name, superclass);
+ cc.setModifiers(cc.getModifiers() | Modifier.ANNOTATION);
+ return cc;
+ }
+
/**
* Appends the system search path to the end of the
* search path. The system search path
import java.net.URL;
import java.security.ProtectionDomain;
import java.util.Collection;
+
import javassist.bytecode.ClassFile;
import javassist.bytecode.Descriptor;
import javassist.bytecode.Opcode;
-import javassist.bytecode.SignatureAttribute;
import javassist.expr.ExprEditor;
/* Note:
throw new NotFoundException(name);
}
+ /**
+ * Retrieves methods with the specified name among the methods
+ * declared in the class. Multiple methods with different parameters
+ * may be returned.
+ *
+ * <p>Note: this method does not search the superclasses.</p>
+ *
+ * @param name method name.
+ * @since 3.19
+ */
+ public CtMethod[] getDeclaredMethods(String name) throws NotFoundException {
+ throw new NotFoundException(name);
+ }
+
/**
* Retrieves the method with the specified name among the methods
* declared in the class. If there are multiple methods with
return cms;
}
+ public CtMethod[] getDeclaredMethods(String name) throws NotFoundException {
+ CtMember.Cache memCache = getMembers();
+ CtMember mth = memCache.methodHead();
+ CtMember mthTail = memCache.lastMethod();
+ ArrayList<CtMethod> methods = new ArrayList<CtMethod>();
+ while (mth != mthTail) {
+ mth = mth.next();
+ if (mth.getName().equals(name))
+ methods.add((CtMethod)mth);
+ }
+
+ return methods.toArray(new CtMethod[methods.size()]);
+ }
+
public CtMethod getDeclaredMethod(String name) throws NotFoundException {
CtMember.Cache memCache = getMembers();
CtMember mth = memCache.methodHead();
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashSet;
+import java.util.List;
+
import javassist.bytecode.*;
+import javassist.bytecode.annotation.Annotation;
import javassist.expr.*;
public class JvstTest4 extends JvstTestRoot {
assertEquals("test4.AnnoArg$B", a.value().getName());
System.out.println(a.value().getName());
}
+
+ public void testDeclaredMethods() throws Exception {
+ CtClass cc = sloader.get("test4.DeclMethodsList");
+ CtMethod[] meth = cc.getDeclaredMethods("foo");
+ assertEquals(2, meth.length);
+ assertEquals("()V", meth[0].getSignature());
+ assertEquals("(I)I", meth[1].getSignature());
+ meth = cc.getDeclaredMethods("bar");
+ assertEquals(1, meth.length);
+ assertEquals("()V", meth[0].getSignature());
+ meth = cc.getDeclaredMethods("baz");
+ assertEquals(0, meth.length);
+ }
+
+ public void testAnnotationLoader() throws Exception {
+ CtClass anno = sloader.makeAnnotation("test4.AnnoLoadAnno", null);
+ anno.debugWriteFile();
+ CtClass cc = sloader.get("test4.AnnoLoad");
+ CtMethod m = cc.getDeclaredMethod("foo");
+ ClassFile cf = cc.getClassFile();
+ ConstPool cp = cf.getConstPool();
+ AnnotationsAttribute attr
+ = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag);
+ Annotation a = new Annotation(anno.getName(), cp);
+ attr.setAnnotation(a);
+ m.getMethodInfo().addAttribute(attr);
+ cc.writeFile();
+ Object obj = m.getAnnotations()[0];
+ String name = obj.getClass().getName();
+ assertEquals(anno.getName(), name);
+ }
}
--- /dev/null
+package test4;
+
+public class AnnoLoad {
+ public int foo() { return 0; }
+}
--- /dev/null
+package test4;
+
+public class DeclMethodsList {
+ public void foo() {}
+ int foo(int i) { return i; }
+ public void bar() {}
+}