前言
前面講過一個簡單的class文件怎麼用javassist去生成詳見 javassist動態生成class。
運用 方面
在項目中,我們有時候會用到一些webservice 但是由於這些webservice不是在WEB項目初始化的時候就需要被建立,而是在某種特定的條件被觸發時候纔去生成。爲了減少資源的損耗,我們就要去讓其動態生成並且生成完之後就銷燬,此時就要用javassist去動態生成webservice了。
實例
/***
*
* 動態生成Webservice
* @throws CannotCompileException
* @throws IOException
*/
public static Class dynamicCreateWebservice()
throws CannotCompileException, IOException {
ClassPool classpool = ClassPool.getDefault();
//類
CtClass ctclass= classpool.makeClass("com.bsoft.DynamicHelloWordWebservice");
//添加方法
CtMethod ctMethod = CtMethod.make("public String invokeHello(String paramString){" +
" System.out.print($1);"+
"\n return \" hello,\"+$1;"+"\n"+
"}", ctclass);
ctclass.addMethod(ctMethod);
/**
* Returns a class file for this class.
*/
ClassFile cf = ctclass.getClassFile();
ConstPool constPool=cf.getConstPool();
//添加類的註釋
/**
* * 類註解和方法註解生成流程:
1、 創建註解Annotation;
2、 註解隊列AnnotationsAttribute添加註解Annotation;
3、 類ClassFile或方法信息CtMethod.getMethodInfo()添加註解隊列AnnotationsAttribute。
@WebService(name="dynamicCreateWebservice")
public class DynamicHelloWordWebservice
*/
Annotation classAttr = new Annotation("javax.jws.WebService", constPool);
classAttr.addMemberValue("name", new StringMemberValue("dynamicCreateWebservice",constPool));
classAttr.addMemberValue("targetNamespace", new StringMemberValue("com.bsoft.com", constPool));
AnnotationsAttribute classAttrBute = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
classAttrBute.addAnnotation(classAttr);
cf.addAttribute(classAttrBute);
/***
* 添加方法註解
* * * 類註解和方法註解生成流程:
1、 創建註解Annotation;
2、 註解隊列AnnotationsAttribute添加註解Annotation;
3、 類ClassFile或方法信息CtMethod.getMethodInfo()添加註解隊列AnnotationsAttribute。
* 生成的格式: @WebMethod(operationName="invokeHello")
@WebResult(name="result")
public String invokeHello
*/
Annotation methodAttr = new Annotation("javax.jws.WebMethod", constPool);
methodAttr.addMemberValue("operationName", new StringMemberValue("invokeHello", constPool));
AnnotationsAttribute methodAnnoAttrBute = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
methodAnnoAttrBute.addAnnotation(methodAttr);
Annotation resultAttr = new Annotation("javax.jws.WebResult", constPool);
resultAttr.addMemberValue("name", new StringMemberValue("result", constPool));
methodAnnoAttrBute.addAnnotation(resultAttr);
ctMethod.getMethodInfo().addAttribute(methodAnnoAttrBute);
/***
* 參數註解生成流程:
1、 創建註解二維數組Annotation[][]:第一維對應參數序列,第二維對應註解序列;
2、 參數註解屬性ParameterAnnotationsAttribute添加註解二維數組Annotation[][];
3、 方法信息CtMethod.getMethodInfo()添加參數註解屬性ParameterAnnotationsAttribute。
*/
Annotation[][] paramAnnotanArr = new Annotation[1][1];
Annotation paramAnno = new Annotation("javax.jws.WebParam", constPool);
paramAnno.addMemberValue("name", new StringMemberValue("name",constPool));
paramAnnotanArr[0][0]=paramAnno;
ParameterAnnotationsAttribute paramAnnoTan = new ParameterAnnotationsAttribute(constPool, ParameterAnnotationsAttribute.visibleTag);
paramAnnoTan.setAnnotations(paramAnnotanArr);
ctMethod.getMethodInfo().addAttribute(paramAnnoTan);
byte[] byter = ctclass.toBytecode();
FileOutputStream fos = new FileOutputStream(new File("d:\\DynamicHelloWordWebservice.class"));
fos.write(byter);
fos.close();
return ctclass.toClass();
}