Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javassist复制带有注释的方法_Java_Annotations_Decompiling - Fatal编程技术网

javassist复制带有注释的方法

javassist复制带有注释的方法,java,annotations,decompiling,Java,Annotations,Decompiling,这是最初的方法: @GET @Produces({"application/json"}) public Response getTermClouds(@Context SecurityContext secCtxt, @Context UriInfo ui) { return null } 我想复制这个方法,但是添加了一个新的字符串参数,新方法的注释和以前一样,如下所示: @GET @Produces({"application/json"}) pub

这是最初的方法:

  @GET
  @Produces({"application/json"})
  public Response getTermClouds(@Context SecurityContext secCtxt, @Context UriInfo ui)
  {
    return null
  }
我想复制这个方法,但是添加了一个新的字符串参数,新方法的注释和以前一样,如下所示:

  @GET
  @Produces({"application/json"})
  public Response getTermClouds(@Context SecurityContext secCtxt, @Context UriInfo ui,String newParam)
  {
    return null
  }
//Create a new method with the same name as the old one
CtMethod mNew = CtNewMethod.copy(mOrig, curClass, null);

//Copy all attributes from the old method to the new one. This includes annotations
for(Object attribute: mOrig.getMethodInfo().getAttributes()) {
    m.getMethodInfo().addAttribute((AttributeInfo)attribute);
}

//Remove the method and parameter annotations from the old method
mOrig.getMethodInfo().removeAttribute(AnnotationsAttribute.visibleTag);
mOrig.getMethodInfo().removeAttribute(ParameterAnnotationsAttribute.visibleTag);

//Add the new String parameter to the new method
m.addParameter(cp.getOrNull("java.lang.String"));

//Add a new empty annotation entry for the new parameter (not sure this part is necessary for you. 
//In my case, I was adding a new parameter to the beginning, so the old ones needed to be offset.
ParameterAnnotationsAttribute paa = (ParameterAnnotationsAttribute)m.getMethodInfo().getAttribute(ParameterAnnotationsAttribute.visibleTag);
Annotation[][] oldAnnos = paa.getAnnotations();
Annotation[][] newAnnos = new Annotation[oldAnnos.length + 1][];
newAnnos[oldAnnos.length] = new Annotation[] {};
System.arraycopy(oldAnnos, 0, newAnnos, 0, oldAnnos.length);
paa.setAnnotations(newAnnos);

//Rename the old method and add the new one to the class
mOrig.setName(mOrig.getName() + "_replaced");
curClass.addMethod(m);

我使用javassist来做这件事,我不想先添加一个“get”注释,然后再添加一个“products”注释,因为可能还有很多其他注释是未知的。如何作为一种常见的方法来做呢

尝试向方法添加新参数时,Javassist不允许向现有方法添加额外参数,而是将接收额外参数和其他参数的新方法添加到同一类中

CtMethod对象的副本可以通过CtNewMethod.copy()获得


尝试创建以前方法的副本。你能准确地解释一下你想用注释做什么吗

我意识到这已经过时了,但我在尝试向SpringWeb处理程序方法添加参数时遇到了同样的问题,我已经解决了。您需要将旧类的属性复制到新类。您可能还希望从旧版本中删除它们,以防止可能的冲突。代码如下所示:

  @GET
  @Produces({"application/json"})
  public Response getTermClouds(@Context SecurityContext secCtxt, @Context UriInfo ui,String newParam)
  {
    return null
  }
//Create a new method with the same name as the old one
CtMethod mNew = CtNewMethod.copy(mOrig, curClass, null);

//Copy all attributes from the old method to the new one. This includes annotations
for(Object attribute: mOrig.getMethodInfo().getAttributes()) {
    m.getMethodInfo().addAttribute((AttributeInfo)attribute);
}

//Remove the method and parameter annotations from the old method
mOrig.getMethodInfo().removeAttribute(AnnotationsAttribute.visibleTag);
mOrig.getMethodInfo().removeAttribute(ParameterAnnotationsAttribute.visibleTag);

//Add the new String parameter to the new method
m.addParameter(cp.getOrNull("java.lang.String"));

//Add a new empty annotation entry for the new parameter (not sure this part is necessary for you. 
//In my case, I was adding a new parameter to the beginning, so the old ones needed to be offset.
ParameterAnnotationsAttribute paa = (ParameterAnnotationsAttribute)m.getMethodInfo().getAttribute(ParameterAnnotationsAttribute.visibleTag);
Annotation[][] oldAnnos = paa.getAnnotations();
Annotation[][] newAnnos = new Annotation[oldAnnos.length + 1][];
newAnnos[oldAnnos.length] = new Annotation[] {};
System.arraycopy(oldAnnos, 0, newAnnos, 0, oldAnnos.length);
paa.setAnnotations(newAnnos);

//Rename the old method and add the new one to the class
mOrig.setName(mOrig.getName() + "_replaced");
curClass.addMethod(m);

实际上,它是一个jax rs类,带有一个名为“Path”的注释。我没有源代码,我想在该方法中添加一个参数“@context-HttpSevletRequest”,这样我就可以从中获得真正的路径。虽然我可以复制新方法,但它没有注释。或者从字符串解析新方法,也没有注释,也没有泛型类型。@user6630815您可以在运行时向方法添加注释。