本文整理汇总了Java中javassist.bytecode.AnnotationsAttribute.addAnnotation方法的典型用法代码示例。如果您正苦于以下问题:Java AnnotationsAttribute.addAnnotation方法的具体用法?Java AnnotationsAttribute.addAnnotation怎么用?Java AnnotationsAttribute.addAnnotation使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javassist.bytecode.AnnotationsAttribute
的用法示例。
在下文中一共展示了AnnotationsAttribute.addAnnotation方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import javassist.bytecode.AnnotationsAttribute; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
Reflections reflections = new Reflections();
LukkitPlus.BUKKIT_EVENTS = reflections.getSubTypesOf(Event.class);
ClassPool classpath = ClassPool.getDefault();
CtClass eventClass = classpath.makeClass("online.pizzacrust.lukkitplus" +
".EventCallback");
for (Class<? extends Event> event : LukkitPlus.BUKKIT_EVENTS) {
CtMethod eventMethod = CtNewMethod.make(CtClass.voidType, "on" + event.getSimpleName
(), new CtClass[] { classpath.get(event.getName()) }, new CtClass[0], "online" +
".pizzacrust.lukkitplus.EventCallbackGenerator.call($1);", eventClass);
eventClass.addMethod(eventMethod);
AnnotationsAttribute attribute = new AnnotationsAttribute(eventClass.getClassFile()
.getConstPool(), AnnotationsAttribute.visibleTag);
Annotation eventHandlerAnnt = new Annotation(EventHandler.class.getName(), eventClass
.getClassFile().getConstPool());
attribute.addAnnotation(eventHandlerAnnt);
eventMethod.getMethodInfo().addAttribute(attribute);
}
System.out.println("Done!");
eventClass.writeFile();
}
示例2: generateClass
import javassist.bytecode.AnnotationsAttribute; //导入方法依赖的package包/类
public static Class<?> generateClass() throws NotFoundException, CannotCompileException {
ClassPool classpath = ClassPool.getDefault();
classpath.insertClassPath(new ClassClassPath(EventCallbackGenerator.class));
CtClass eventClass = classpath.makeClass("online.pizzacrust.lukkitplus" +
".EventCallback");
eventClass.addInterface(classpath.get(Listener.class.getName()));
for (Class<? extends Event> event : LukkitPlus.BUKKIT_EVENTS) {
if (containsStaticHandlerList(event)) {
CtMethod eventMethod = CtNewMethod.make(CtClass.voidType, "on" + event.getSimpleName
(), new CtClass[]{classpath.get(event.getName())}, new CtClass[0], "online" +
".pizzacrust.lukkitplus.EventCallbackGenerator.call($1);", eventClass);
eventClass.addMethod(eventMethod);
AnnotationsAttribute attribute = new AnnotationsAttribute(eventClass.getClassFile()
.getConstPool(), AnnotationsAttribute.visibleTag);
Annotation eventHandlerAnnt = new Annotation(EventHandler.class.getName(), eventClass
.getClassFile().getConstPool());
attribute.addAnnotation(eventHandlerAnnt);
eventMethod.getMethodInfo().addAttribute(attribute);
}
}
return eventClass.toClass(LukkitPlus.class.getClassLoader());
}
示例3: instrumentMethod
import javassist.bytecode.AnnotationsAttribute; //导入方法依赖的package包/类
protected void instrumentMethod(final CtMethod ctm, final ConstPool constpool) {
try {
if(ctm.getAnnotation(susAnn)==null) {
AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
javassist.bytecode.annotation.Annotation annot = new javassist.bytecode.annotation.Annotation(susAnn.getName(), constpool);
attr.addAnnotation(annot);
ctm.getMethodInfo().addAttribute(attr);
}
/*
* Install latch. Needs to be accessible in finally block, so we need a singleton registry.
* Spin up fiber and invoke this method in fiber
* -- static method
* -- non static method
* Fiber execution: accumulate all pending sub-fibers
* finally block:
* -- if running in thread, skip.
* -- send batch
* -- wait for all sub-fibers to complete
* -- drop thread latch
*
*/
} catch (Exception ex) {
ex.printStackTrace(System.err);
throw new RuntimeException(ex);
}
}
示例4: annotateMethod
import javassist.bytecode.AnnotationsAttribute; //导入方法依赖的package包/类
private AnnotationsAttribute annotateMethod(
CtMethod ctmethod,
String annotationName,
int annotationValue ) {
AnnotationsAttribute attr = new AnnotationsAttribute(
ctmethod.getMethodInfo().getConstPool(),
AnnotationsAttribute.visibleTag);
Annotation anno = new Annotation(
"java.lang.Integer",
ctmethod.getMethodInfo().getConstPool());
anno.addMemberValue(
annotationName,
new IntegerMemberValue(
ctmethod.getMethodInfo().getConstPool(),
annotationValue));
attr.addAnnotation(anno);
ctmethod.getMethodInfo().addAttribute(
attr);
return attr;
}
示例5: addAnnotation
import javassist.bytecode.AnnotationsAttribute; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void addAnnotation(IClass cls, Class<? extends Annotation> ann,
HashMap<String, Object> values)
throws InstrumenterException {
CtClass cl = ((JAClass) cls).getCtClass();
ClassFile ccFile = cl.getClassFile();
ConstPool constpool = ccFile.getConstPool();
AnnotationsAttribute attr = new AnnotationsAttribute(constpool,
AnnotationsAttribute.visibleTag);
javassist.bytecode.annotation.Annotation marker
= new javassist.bytecode.annotation.Annotation(
ann.getName(), constpool);
setValues(marker, constpool, values);
attr.addAnnotation(marker);
cl.getClassFile().addAttribute(attr);
}
示例6: addEndpointMapping
import javassist.bytecode.AnnotationsAttribute; //导入方法依赖的package包/类
@Override
protected void addEndpointMapping(CtMethod ctMethod, String method, String request) {
MethodInfo methodInfo = ctMethod.getMethodInfo();
ConstPool constPool = methodInfo.getConstPool();
AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
Annotation requestMapping = new Annotation(RequestMapping.class.getName(), constPool);
ArrayMemberValue valueVals = new ArrayMemberValue(constPool);
StringMemberValue valueVal = new StringMemberValue(constPool);
valueVal.setValue(request);
valueVals.setValue(new MemberValue[]{valueVal});
requestMapping.addMemberValue("value", valueVals);
ArrayMemberValue methodVals = new ArrayMemberValue(constPool);
EnumMemberValue methodVal = new EnumMemberValue(constPool);
methodVal.setType(RequestMethod.class.getName());
methodVal.setValue(method);
methodVals.setValue(new MemberValue[]{methodVal});
requestMapping.addMemberValue("method", methodVals);
attr.addAnnotation(requestMapping);
methodInfo.addAttribute(attr);
}
示例7: addClassAnnotation
import javassist.bytecode.AnnotationsAttribute; //导入方法依赖的package包/类
public static void addClassAnnotation(CtClass clazz, Class<?> annotationClass, Object... values) {
ClassFile ccFile = clazz.getClassFile();
ConstPool constPool = ccFile.getConstPool();
AnnotationsAttribute attr = getAnnotationsAttribute(ccFile);
Annotation annot = new Annotation(annotationClass.getName(), constPool);
for(int i = 0; i < values.length; i = i + 2) {
String valueName = (String)values[i];
Object value = values[i+1];
if (valueName != null && value != null) {
MemberValue memberValue = createMemberValue(constPool, value);
annot.addMemberValue(valueName, memberValue);
}
}
attr.addAnnotation(annot);
}
示例8: addMethodAnnotations
import javassist.bytecode.AnnotationsAttribute; //导入方法依赖的package包/类
public static void addMethodAnnotations(CtMethod ctMethod, Method method) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
if (method != null) {
MethodInfo methodInfo = ctMethod.getMethodInfo();
ConstPool constPool = methodInfo.getConstPool();
AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
methodInfo.addAttribute(attr);
for(java.lang.annotation.Annotation anno : method.getAnnotations()) {
// If it's a Transition skip
// TODO : Make this a parameterized set of Filters instead of hardcoding
//
Annotation clone = null;
if (anno instanceof Transitions || anno instanceof Transition) {
// skip
} else {
clone = cloneAnnotation(constPool, anno);
attr.addAnnotation(clone);
}
}
}
}
示例9: annotateField
import javassist.bytecode.AnnotationsAttribute; //导入方法依赖的package包/类
private void annotateField(
CtField ctfield,
String annotationName,
int annotationValue ) {
AnnotationsAttribute attr = new AnnotationsAttribute(
ctfield.getFieldInfo().getConstPool(),
AnnotationsAttribute.visibleTag);
Annotation anno = new Annotation(
"java.lang.Integer",
ctfield.getFieldInfo().getConstPool());
anno.addMemberValue(
annotationName,
new IntegerMemberValue(
ctfield.getFieldInfo().getConstPool(),
annotationValue));
attr.addAnnotation(anno);
ctfield.getFieldInfo().addAttribute(
attr);
}
示例10: addField
import javassist.bytecode.AnnotationsAttribute; //导入方法依赖的package包/类
private CtField addField(CtClass targetClass, CtClass fieldType, String fieldName, boolean makeTransient) {
final ConstPool constPool = targetClass.getClassFile().getConstPool();
final CtField theField;
try {
theField = new CtField( fieldType, fieldName, targetClass );
targetClass.addField( theField );
}
catch (CannotCompileException e) {
throw new EnhancementException(
String.format(
"Could not enhance class [%s] to add field [%s]",
targetClass.getName(),
fieldName
),
e
);
}
// make that new field (1) private, (2) transient and (3) @Transient
if ( makeTransient ) {
theField.setModifiers( theField.getModifiers() | Modifier.TRANSIENT );
}
theField.setModifiers( Modifier.setPrivate( theField.getModifiers() ) );
final AnnotationsAttribute annotationsAttribute = getVisibleAnnotations( theField.getFieldInfo() );
annotationsAttribute.addAnnotation( new Annotation( Transient.class.getName(), constPool ) );
return theField;
}
示例11: makeAnnotationAttribute
import javassist.bytecode.AnnotationsAttribute; //导入方法依赖的package包/类
protected AnnotationsAttribute makeAnnotationAttribute(ClassPool classPool, ConstPool pool, Class<?> klass) {
AnnotationsAttribute result = new AnnotationsAttribute(pool, AnnotationsAttribute.visibleTag);
for (Annotation annotation : klass.getAnnotations()) {
result.addAnnotation(makeBytecodeAnnotation(classPool, pool, annotation));
}
return result;
}
示例12: gecco
import javassist.bytecode.AnnotationsAttribute; //导入方法依赖的package包/类
@Override
public JavassistDynamicBean gecco(String[] matchUrl, String downloader, int timeout, String... pipelines) {
AnnotationsAttribute attr = new AnnotationsAttribute(cpool, AnnotationsAttribute.visibleTag);
Annotation annot = new Annotation(Gecco.class.getName(), cpool);
// matchUrl
//annot.addMemberValue("matchUrl", new StringMemberValue(matchUrl, cpool));
ArrayMemberValue arrayMemberValueMatchUrl = new ArrayMemberValue(cpool);
MemberValue[] elementMatchUrls = new StringMemberValue[matchUrl.length];
for (int i = 0; i < matchUrl.length; i++) {
elementMatchUrls[i] = new StringMemberValue(matchUrl[i], cpool);
}
arrayMemberValueMatchUrl.setValue(elementMatchUrls);
annot.addMemberValue("matchUrl", arrayMemberValueMatchUrl);
// downloader
annot.addMemberValue("downloader", new StringMemberValue(downloader, cpool));
// timeout
annot.addMemberValue("timeout", new IntegerMemberValue(cpool, timeout));
// pipelines
ArrayMemberValue arrayMemberValue = new ArrayMemberValue(cpool);
MemberValue[] elements = new StringMemberValue[pipelines.length];
for (int i = 0; i < pipelines.length; i++) {
elements[i] = new StringMemberValue(pipelines[i], cpool);
}
arrayMemberValue.setValue(elements);
annot.addMemberValue("pipelines", arrayMemberValue);
attr.addAnnotation(annot);
cfile.addAttribute(attr);
return this;
}
示例13: addClassAnnotations
import javassist.bytecode.AnnotationsAttribute; //导入方法依赖的package包/类
private void addClassAnnotations(DTODescription description, CtClass hyalineProxyClass, ConstPool constpool)
throws CannotBuildClassException {
if (description.getAnnotations() != null) {
AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
for (java.lang.annotation.Annotation annotation : description.getAnnotations()) {
Annotation annotationCopy = JavassistUtils.createJavassistAnnotation(constpool, annotation);
attr.addAnnotation(annotationCopy);
}
hyalineProxyClass.getClassFile().addAttribute(attr);
}
}
示例14: createFieldFromDescription
import javassist.bytecode.AnnotationsAttribute; //导入方法依赖的package包/类
private CtField createFieldFromDescription(ClassPool classPool, ConstPool constpool, FieldDescription field,
CtClass hyalineProxyClass) throws CannotCompileException, CannotBuildClassException {
String fieldTypeName = field.getField().getType().getCanonicalName();
CtField ctField = CtField.make("private " + fieldTypeName + " " + field.getField().getName() + ";",
hyalineProxyClass);
if (field.getAnnotations() != null && field.getAnnotations().size() > 0) {
AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
for (java.lang.annotation.Annotation annotation : field.getAnnotations()) {
Annotation annotationCopy = JavassistUtils.createJavassistAnnotation(constpool, annotation);
attr.addAnnotation(annotationCopy);
}
ctField.getFieldInfo().addAttribute(attr);
}
return ctField;
}
示例15: addAnnotation
import javassist.bytecode.AnnotationsAttribute; //导入方法依赖的package包/类
/**
* Create a new annotation to be dynamically inserted in the byte code.
*
* @param attribute attribute
* @param annotationType annotation type
* @param members members
*/
public static void addAnnotation(AnnotationsAttribute attribute, String annotationType, Map<String, MemberValue> members) {
javassist.bytecode.annotation.Annotation annotation = new javassist.bytecode.annotation.Annotation(annotationType, attribute.getConstPool());
for (Map.Entry<String, MemberValue> member : members.entrySet()) {
annotation.addMemberValue(member.getKey(), member.getValue());
}
attribute.addAnnotation(annotation);
}