本文整理匯總了Java中javassist.bytecode.annotation.Annotation類的典型用法代碼示例。如果您正苦於以下問題:Java Annotation類的具體用法?Java Annotation怎麽用?Java Annotation使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Annotation類屬於javassist.bytecode.annotation包,在下文中一共展示了Annotation類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import javassist.bytecode.annotation.Annotation; //導入依賴的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.annotation.Annotation; //導入依賴的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: addAnnotation
import javassist.bytecode.annotation.Annotation; //導入依賴的package包/類
/**
* Adds an annotation. If there is an annotation with the same type,
* it is removed before the new annotation is added.
*
* @param annotation the added annotation.
*/
public void addAnnotation(Annotation annotation) {
String type = annotation.getTypeName();
Annotation[] annotations = getAnnotations();
for (int i = 0; i < annotations.length; i++) {
if (annotations[i].getTypeName().equals(type)) {
annotations[i] = annotation;
setAnnotations(annotations);
return;
}
}
Annotation[] newlist = new Annotation[annotations.length + 1];
System.arraycopy(annotations, 0, newlist, 0, annotations.length);
newlist[annotations.length] = annotation;
setAnnotations(newlist);
}
示例4: setAnnotations
import javassist.bytecode.annotation.Annotation; //導入依賴的package包/類
/**
* Changes the annotations represented by this object according to
* the given array of <code>Annotation</code> objects.
*
* @param annotations the data structure representing the
* new annotations.
*/
public void setAnnotations(Annotation[] annotations) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
AnnotationsWriter writer = new AnnotationsWriter(output, constPool);
try {
int n = annotations.length;
writer.numAnnotations(n);
for (int i = 0; i < n; ++i)
annotations[i].write(writer);
writer.close();
}
catch (IOException e) {
throw new RuntimeException(e); // should never reach here.
}
set(output.toByteArray());
}
示例5: getEntityListeners
import javassist.bytecode.annotation.Annotation; //導入依賴的package包/類
protected Annotation getEntityListeners(ConstPool constantPool, Annotation existingEntityListeners, Annotation templateEntityListeners) {
Annotation listeners = new Annotation(EntityListeners.class.getName(), constantPool);
ArrayMemberValue listenerArray = new ArrayMemberValue(constantPool);
Set<MemberValue> listenerMemberValues = new HashSet<MemberValue>();
{
ArrayMemberValue templateListenerValues = (ArrayMemberValue) templateEntityListeners.getMemberValue("value");
listenerMemberValues.addAll(Arrays.asList(templateListenerValues.getValue()));
logger.debug("Adding template values to new EntityListeners");
}
if (existingEntityListeners != null) {
ArrayMemberValue oldListenerValues = (ArrayMemberValue) existingEntityListeners.getMemberValue("value");
listenerMemberValues.addAll(Arrays.asList(oldListenerValues.getValue()));
logger.debug("Adding previous values to new EntityListeners");
}
listenerArray.setValue(listenerMemberValues.toArray(new MemberValue[listenerMemberValues.size()]));
listeners.addMemberValue("value", listenerArray);
return listeners;
}
示例6: getParameterNames
import javassist.bytecode.annotation.Annotation; //導入依賴的package包/類
private String[] getParameterNames(final CtBehavior behavior) throws NotFoundException {
final MethodInfo methodInfo = behavior.getMethodInfo();
final ParameterAnnotationsAttribute attribute =
(ParameterAnnotationsAttribute) methodInfo.getAttribute(ParameterAnnotationsAttribute.visibleTag);
if (attribute == null) {
return null;
}
final int numParameters = behavior.getParameterTypes().length;
final String[] parameterNames = new String[numParameters];
final Annotation[][] annotationsArray = attribute.getAnnotations();
if (annotationsArray == null || annotationsArray.length != numParameters) {
return null;
}
for (int i = 0; i < numParameters; ++i) {
final String parameterName = getParameterName(annotationsArray[i]);
if (parameterName == null) {
return null;
}
parameterNames[i] = parameterName;
}
return parameterNames;
}
示例7: addAnnotation
import javassist.bytecode.annotation.Annotation; //導入依賴的package包/類
public void addAnnotation(Class<?> type, Class<?>... values) {
ClassFile cf = cc.getClassFile();
ConstPool cp = cf.getConstPool();
ClassMemberValue[] elements = new ClassMemberValue[values.length];
for (int i = 0; i < values.length; i++) {
elements[i] = cb.createClassMemberValue(values[i], cp);
}
ArrayMemberValue value = new ArrayMemberValue(cp);
value.setValue(elements);
AnnotationsAttribute ai = (AnnotationsAttribute) cf
.getAttribute(visibleTag);
if (ai == null) {
ai = new AnnotationsAttribute(cp, visibleTag);
cf.addAttribute(ai);
}
try {
Annotation annotation = new Annotation(cp, get(type));
annotation.addMemberValue("value", value);
ai.addAnnotation(annotation);
} catch (NotFoundException e) {
throw new AssertionError(e);
}
}
示例8: copyParameterAnnotations
import javassist.bytecode.annotation.Annotation; //導入依賴的package包/類
private void copyParameterAnnotations(Method method, MethodInfo info)
throws NotFoundException {
for (CtMethod e : getAll(method)) {
MethodInfo em = e.getMethodInfo();
ParameterAnnotationsAttribute ai = (ParameterAnnotationsAttribute) em
.getAttribute(ParameterAnnotationsAttribute.visibleTag);
if (ai == null)
continue;
Annotation[][] anns = ai.getAnnotations();
for (int i = 0, n = anns.length; i < n; i++) {
if (anns[i].length > 0) {
info.addAttribute(ai.copy(info.getConstPool(),
Collections.EMPTY_MAP));
return;
}
}
}
}
示例9: discoverAndIntimateForClassAnnotations
import javassist.bytecode.annotation.Annotation; //導入依賴的package包/類
/**
* Discovers Class Annotations
*
* @param classFile
*/
private void discoverAndIntimateForClassAnnotations (ClassFile classFile) {
Set<Annotation> annotations = new HashSet<Annotation>();
AnnotationsAttribute visible = (AnnotationsAttribute) classFile.getAttribute(AnnotationsAttribute.visibleTag);
AnnotationsAttribute invisible = (AnnotationsAttribute) classFile.getAttribute(AnnotationsAttribute.invisibleTag);
if (visible != null) {
annotations.addAll(Arrays.asList(visible.getAnnotations()));
}
if (invisible != null) {
annotations.addAll(Arrays.asList(invisible.getAnnotations()));
}
// now tell listeners
for (Annotation annotation : annotations) {
Set<ClassAnnotationDiscoveryListener> listeners = classAnnotationListeners.get(annotation.getTypeName());
if (null == listeners) {
continue;
}
for (ClassAnnotationDiscoveryListener listener : listeners) {
listener.discovered(classFile.getName(), annotation.getTypeName());
}
}
}
示例10: href
import javassist.bytecode.annotation.Annotation; //導入依賴的package包/類
@Override
public DynamicField href(boolean click, String... value) {
Annotation annot = new Annotation(Href.class.getName(), cpool);
annot.addMemberValue("click", new BooleanMemberValue(click, cpool));
ArrayMemberValue arrayMemberValue = new ArrayMemberValue(cpool);
MemberValue[] memberValues = new StringMemberValue[value.length];
for(int i = 0; i < value.length; i++) {
memberValues[i] = new StringMemberValue(value[i], cpool);
}
arrayMemberValue.setValue(memberValues);
annot.addMemberValue("value", arrayMemberValue);
attr.addAnnotation(annot);
return this;
}
示例11: image
import javassist.bytecode.annotation.Annotation; //導入依賴的package包/類
@Override
public DynamicField image(String download, String... value) {
Annotation annot = new Annotation(Image.class.getName(), cpool);
annot.addMemberValue("download", new StringMemberValue(download, cpool));
ArrayMemberValue arrayMemberValue = new ArrayMemberValue(cpool);
MemberValue[] memberValues = new StringMemberValue[value.length];
for(int i = 0; i < value.length; i++) {
memberValues[i] = new StringMemberValue(value[i], cpool);
}
arrayMemberValue.setValue(memberValues);
annot.addMemberValue("value", arrayMemberValue);
attr.addAnnotation(annot);
return this;
}
示例12: scanClass
import javassist.bytecode.annotation.Annotation; //導入依賴的package包/類
/**
* Scans a class representation for annotations.
* @param cf The ClassFile
* @return A lit of annotations
*/
private List<Annotation> scanClass(final ClassFile cf) {
AnnotationsAttribute visible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.visibleTag);
AnnotationsAttribute invisible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.invisibleTag);
ArrayList<Annotation> list = new ArrayList<Annotation>();
if (visible != null) {
list.addAll(Arrays.asList(visible.getAnnotations()));
}
if (invisible != null) {
list.addAll(Arrays.asList(invisible.getAnnotations()));
}
return list;
}
示例13: addMimicAnnotation
import javassist.bytecode.annotation.Annotation; //導入依賴的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();
}
示例14: createJavassistAnnotation
import javassist.bytecode.annotation.Annotation; //導入依賴的package包/類
public static Annotation createJavassistAnnotation(ConstPool constpool,
java.lang.annotation.Annotation annotation)
throws CannotBuildClassException {
Annotation annotationCopy = new Annotation(annotation.annotationType()
.getName(), constpool);
for (Method m : annotation.annotationType().getDeclaredMethods()) {
try {
Object value = m.invoke(annotation);
annotationCopy.addMemberValue(m.getName(),
JavassistUtils.createMemberValue(value, constpool));
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
e.printStackTrace();
throw new CannotBuildClassException(
"Could not copy info from annotation "
+ annotation.annotationType().getName());
}
}
return annotationCopy;
}
示例15: addEndpointMapping
import javassist.bytecode.annotation.Annotation; //導入依賴的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);
}