- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要为方法参数添加注释。该方法之前是使用 javassist 创建的,例如:
CtClass cc2 = pool.makeClass("Dummy");
CtMethod method = CtNewMethod.make("public java.lang.String dummyMethod( java.lang.String oneparam){ return null; }", cc2);
我要添加的注解很简单:
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface Param {
/** Name of the parameter */
String name() default "";
}
1) 在方法创建抛出中写入注解
javassist.CannotCompileException: [source error] syntax error near "myMethod(@Param
2) 找到这个 solution但它基于在我的例子中返回 null 的行:
AttributeInfo paramAtrributeInfo = methodInfo.getAttribute(ParameterAnnotationsAttribute.visibleTag); // or inVisibleTag
我不知道如何完成这个;有没有人找到一种方法来创建新方法并为其参数添加任何注释?
提前致谢。
最佳答案
根据我在原始问题中提到的解决方案找到了解决方法。我无法创建类,无法向其添加方法并使用 javassist 为其参数插入注释。但是使用一个真正的类作为模板,可以找到和编辑参数注释。
1) 首先,创建一个类,其方法带有所需的注解
public class TemplateClass {
public String templateMethod(@Param String paramOne) {
return null;
}
}
2)加载它
ClassPool pool = ClassPool.getDefault();
CtClass liveClass = null;
try {
liveClass = pool.get("your.package.path.Dummyclass");
} catch (NotFoundException e) {
logger.error("Template class not found.", e);
}
3)工作
// -- Get method template
CtMethod dummyMethod = liveClass.getMethods()[2];
// -- Get the annotation
AttributeInfo parameterAttributeInfo = dummyMethod.getMethodInfo().getAttribute(ParameterAnnotationsAttribute.visibleTag);
ConstPool parameterConstPool = parameterAttributeInfo.getConstPool();
ParameterAnnotationsAttribute parameterAtrribute = ((ParameterAnnotationsAttribute) parameterAttributeInfo);
Annotation[][] paramArrays = parameterAtrribute.getAnnotations();
Annotation[] addAnno = paramArrays[0];
//-- Edit the annotation adding values
addAnno[0].addMemberValue("value", new StringMemberValue("This is the value of the annotation", parameterConstPool));
addAnno[0].addMemberValue("required", new BooleanMemberValue(Boolean.TRUE, parameterConstPool));
paramArrays[0] = addAnno;
parameterAtrribute.setAnnotations(paramArrays);
然后在构建结果类之前更改 CtClass 和/或 CtMethod 的名称。它不像预期的那样灵活,但至少可以通过这样的方法解决某些场景。欢迎任何其他解决方法!
关于java - 为使用 Javassist 创建的新方法的参数添加注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12625697/
如果有人能为我分解它,让我能理解它,我会非常感激。我知道它用于通过 apply 方法创建新对象。 Function.prototype.new = function () { var args
新版本的HADOOP中有一个方法。http://hadoop.apache.org/docs/current/api/org/apache/hadoop/fs/FileSystem.html#conc
根据支持库变更日志和 Fragment 类文档 ( https://developer.android.com/reference/android/support/v4/app/Fragment.ht
根据支持库更改日志和 Fragment 类文档 (https://developer.android.com/reference/android/support/v4/app/Fragment.htm
执行Async BigJob() 的无限运行任务的正确方法是什么?并且可以根据要求停止 提示:我正在尝试学习 [一种] 新方法来更新我现有的策略。 我有一个简单的程序(测试程序),它有一个开始 和停止
我将解释我的想法:我使用 python 作为谷歌应用程序引擎 + js + css 主项目将存储在 src 文件夹下,如下所示:\src \app <--- 这里是 gae 的所有 python 应用
我是一名优秀的程序员,十分优秀!