本文整理汇总了Java中javassist.bytecode.ClassFile.addAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java ClassFile.addAttribute方法的具体用法?Java ClassFile.addAttribute怎么用?Java ClassFile.addAttribute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javassist.bytecode.ClassFile
的用法示例。
在下文中一共展示了ClassFile.addAttribute方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addMimicAnnotation
import javassist.bytecode.ClassFile; //导入方法依赖的package包/类
private void addMimicAnnotation(CtClass dst, String sourceClassName,
boolean isMimicingInterfaces, boolean isMimicingFields,
boolean isMimicingConstructors, boolean isMimicingMethods) {
ClassFile cf = dst.getClassFile();
ConstPool cp = cf.getConstPool();
AnnotationsAttribute attr = new AnnotationsAttribute(cp,
AnnotationsAttribute.visibleTag);
Annotation a = new Annotation(Mimic.class.getName(), cp);
a.addMemberValue("sourceClass", new ClassMemberValue(sourceClassName,
cp));
a.addMemberValue("isMimicingInterfaces", new BooleanMemberValue(
isMimicingInterfaces, cp));
a.addMemberValue("isMimicingFields", new BooleanMemberValue(
isMimicingFields, cp));
a.addMemberValue("isMimicingConstructors", new BooleanMemberValue(
isMimicingConstructors, cp));
a.addMemberValue("isMimicingMethods", new BooleanMemberValue(
isMimicingMethods, cp));
attr.setAnnotation(a);
cf.addAttribute(attr);
cf.setVersionToJava5();
}
示例2: generate
import javassist.bytecode.ClassFile; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
Class<? extends T> generate(Class<? extends Repository> baseImpl) {
try {
CtClass cc = createClass(
getClassName(baseImpl, getCounter(repositoryInterface).incrementAndGet()),
baseImpl
);
ClassFile cf = cc.getClassFile();
ConstPool constPool = cf.getConstPool();
cc.setModifiers(Modifier.PUBLIC);
cc.setInterfaces(new CtClass[]{classPool.getCtClass(repositoryInterface.getName())});
if (hasGenericConstructor(baseImpl)) {
cc.addConstructor(createConstructor(constPool, cc));
} else {
cc.addConstructor(createDefaultConstructor(cc));
}
cf.addAttribute(createQualifierAttribute(constPool, getQualifier(baseImpl)
.orElseThrow(() -> new NotFoundException("Qualifier annotation not found"))));
return cc.toClass(
classLoader,
DefaultRepositoryCollector.class.getProtectionDomain()
);
} catch (NoSuchMethodException | CannotCompileException | NotFoundException e) {
throw BusinessException.wrap(e, BusinessErrorCode.UNABLE_TO_CREATE_DEFAULT_IMPLEMENTATION)
.put("interface", repositoryInterface)
.put("base", baseImpl);
}
}
示例3: setAttribute
import javassist.bytecode.ClassFile; //导入方法依赖的package包/类
public void setAttribute(String name, byte[] data) {
checkModify();
ClassFile cf = getClassFile2();
cf.addAttribute(new AttributeInfo(cf.getConstPool(), name, data));
}
示例4: copyAnnotations
import javassist.bytecode.ClassFile; //导入方法依赖的package包/类
protected void copyAnnotations(ClassPool pool, Class<?> runnerClass, CtClass newRunnerCtClass) {
ClassFile classFile = newRunnerCtClass.getClassFile();
classFile.addAttribute(makeAnnotationAttribute(pool, classFile.getConstPool(), runnerClass));
}
示例5: createMixInAnnotation
import javassist.bytecode.ClassFile; //导入方法依赖的package包/类
/**
* 创建jackson的代理注解接口类 <br>
* 2013-10-25 上午11:59:50
*
* @param names 要生成的字段
* @return 代理接口类
*/
private Class<?> createMixInAnnotation(String[] names) {
Class<?> clazz = null;
clazz = proxyMixInAnnotationMap.get(StringHelper.hashCodeOfStringArray(names));
if (clazz != null) {
return clazz;
}
ClassPool pool = ClassPool.getDefault();
// 创建代理接口
CtClass cc = pool.makeInterface("ProxyMixInAnnotation" + System.currentTimeMillis()
+ proxyIndex++);
ClassFile ccFile = cc.getClassFile();
ConstPool constpool = ccFile.getConstPool();
// create the annotation
AnnotationsAttribute attr = new AnnotationsAttribute(constpool,
AnnotationsAttribute.visibleTag);
// 创建JsonIgnoreProperties注解
Annotation jsonIgnorePropertiesAnnotation = new Annotation(
JsonIgnoreProperties.class.getName(), constpool);
BooleanMemberValue ignoreUnknownMemberValue = new BooleanMemberValue(false, constpool);
ArrayMemberValue arrayMemberValue = new ArrayMemberValue(constpool);// value的数组成员
Collection<MemberValue> memberValues = new HashSet<MemberValue>();
for (int i = 0; i < names.length; i++) {
String name = names[i];
StringMemberValue memberValue = new StringMemberValue(constpool);// 将name值设入注解内
memberValue.setValue(name);
memberValues.add(memberValue);
}
arrayMemberValue.setValue(memberValues.toArray(new MemberValue[]{}));
jsonIgnorePropertiesAnnotation.addMemberValue("value", arrayMemberValue);
jsonIgnorePropertiesAnnotation.addMemberValue("ignoreUnknown", ignoreUnknownMemberValue);
attr.addAnnotation(jsonIgnorePropertiesAnnotation);
ccFile.addAttribute(attr);
// generate the class
try {
clazz = cc.toClass();
proxyMixInAnnotationMap.put(StringHelper.hashCodeOfStringArray(names), clazz);
// JsonIgnoreProperties ignoreProperties = (JsonIgnoreProperties)
// clazz
// .getAnnotation(JsonIgnoreProperties.class);
// EntityHelper.print(ignoreProperties);
//
// EntityHelper.print(clazz);
// try {
// Object instance = clazz.newInstance();
// EntityHelper.print(instance);
//
// } catch (InstantiationException e) {
// e.printStackTrace();
// } catch (IllegalAccessException e) {
// e.printStackTrace();
// }
} catch (CannotCompileException e) {
e.printStackTrace();
}
// right
// mthd.getMethodInfo().addAttribute(attr);
return clazz;
}
示例6: setGenericSignature
import javassist.bytecode.ClassFile; //导入方法依赖的package包/类
public void setGenericSignature(String sig) {
ClassFile cf = getClassFile();
SignatureAttribute sa = new SignatureAttribute(cf.getConstPool(), sig);
cf.addAttribute(sa);
}