我遇到了一个问题,我必须修改package-info。
package-info.java
@javax.xml.bind.annotation.XmlSchema(namespace = "http://some.url/soap/style/document_literal")
package org.example.wsdl.wsdl;
以下代码适用于1.7.0_45。
// do not load any classes before, this could break the following code.
Class<?> pkgInfo = Class.forName("org.example.wsdl.package-info", true, NameSpaceModifier.class.getClassLoader());
Field field = Class.class.getDeclaredField("annotations");
field.setAccessible(true);
final XmlSchema oldAnnotation = (XmlSchema) pkgInfo.getAnnotations()[0];
logger.debug("Old Annotation namespace value was: " + oldAnnotation.namespace());
XmlSchema newAnnotation = new XmlSchema() {
@Override
public XmlNs[] xmlns() {
return oldAnnotation.xmlns();
}
@Override
public String namespace() {
return "newNs";
}
@Override
public XmlNsForm elementFormDefault() {
return oldAnnotation.elementFormDefault();
}
@Override
public XmlNsForm attributeFormDefault() {
return oldAnnotation.attributeFormDefault();
}
@Override
public String location() {
return oldAnnotation.location();
}
@Override
public Class<? extends Annotation> annotationType() {
return oldAnnotation.annotationType();
}
};
@SuppressWarnings("unchecked")
Map<Class<? extends Annotation>, Annotation> annotations = (Map<Class<? extends Annotation>, Annotation>) field.get(pkgInfo);
annotations.put(XmlSchema.class, newAnnotation);
XmlSchema modifiedAnnotation = (XmlSchema) pkgInfo.getAnnotations()[0];
使用1.8.0_05编译并执行相同的代码时,出现此错误消息:
java.lang.NoSuchFieldException: annotations
at java.lang.Class.getDeclaredField(Class.java:2057)
我知道它是一个哈克,至少它看起来像一个。但Java 8是否按预期工作? 我如何更改它与Java 8一起使用的代码呢?
也欢迎Javassist的答案;)
答案 0 :(得分:4)
Java 8改变了内部存储注释的方式。由于您使用带有硬编码字段名称的讨厌反射黑客,因此任何Java更新都有可能重新破解您的代码。
java.lang.Class
:
/**
* @since 1.5
*/
public Annotation[] getAnnotations() {
return AnnotationParser.toArray(annotationData().annotations);
}
private volatile transient AnnotationData annotationData;
private AnnotationData annotationData() {
while (true) { // retry loop
AnnotationData annotationData = this.annotationData;
int classRedefinedCount = this.classRedefinedCount;
if (annotationData != null &&
annotationData.redefinedCount == classRedefinedCount) {
return annotationData;
}
// null or stale annotationData -> optimistically create new instance
AnnotationData newAnnotationData = createAnnotationData(classRedefinedCount);
// try to install it
if (Atomic.casAnnotationData(this, annotationData, newAnnotationData)) {
// successfully installed new AnnotationData
return newAnnotationData;
}
}
}
private static class AnnotationData {
final Map<Class<? extends Annotation>, Annotation> annotations;
final Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
// Value of classRedefinedCount when we created this AnnotationData instance
final int redefinedCount;
AnnotationData(Map<Class<? extends Annotation>, Annotation> annotations,
Map<Class<? extends Annotation>, Annotation> declaredAnnotations,
int redefinedCount) {
this.annotations = annotations;
this.declaredAnnotations = declaredAnnotations;
this.redefinedCount = redefinedCount;
}
}
我想您可以使用新字段值来临时修复代码。永久修复是更改注释本身,而不是在运行时动态修改它。
Field annotationDataField = Class.class.getDeclaredField("annotationData");
annotationDataField.setAccessible(true);
Object annotationData = annotationDataField.get(pkgInfo);
Field annotationsField = annotationData.getClass().getDeclaredField("annotations");
annotationsField.setAccessible(true);
Map<Class<? extends Annotation>, Annotation> annotations = (Map<Class<? extends Annotation>, Annotation>) annotationsField
.get(annotationData);
annotations.put(XmlSchema.class, newAnnotation);
XmlSchema modifiedAnnotation = (XmlSchema) pkgInfo.getAnnotations()[0];
答案 1 :(得分:0)
我遇到了需要外化namespace
注释的XmlSchema
属性的情况。因为它需要一个恒定的价值,我转向反思并找到了这篇文章。但这对我来说太过于苛刻,而且op也欢迎Javassist的回答,这里有一个:
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.get("org.example.wsdl.package-info");
ClassFile ccFile = cc.getClassFile();
ConstPool cp = ccFile.getConstPool();
AnnotationsAttribute attr = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag);
Annotation as = new Annotation("javax.xml.bind.annotation.XmlSchema", cp);
as.addMemberValue("namespace", new StringMemberValue("newNs", cp));
EnumMemberValue emv = new EnumMemberValue(cp);
emv.setType("javax.xml.bind.annotation.XmlNsForm");
emv.setValue("QUALIFIED");
as.addMemberValue("elementFormDefault", emv);
// Others member values can be added manually or cloned from the original annotation like in the op's example
attr.addAnnotation(as);
ccFile.addAttribute(attr);
cc.toClass();