Java Code Examples for javassist.bytecode.annotation.ArrayMemberValue#setValue()
The following examples show how to use
javassist.bytecode.annotation.ArrayMemberValue#setValue() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClassTemplate.java From anno4j with Apache License 2.0 | 6 votes |
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); } }
Example 2
Source File: JavassistDynamicField.java From gecco with MIT License | 6 votes |
@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; }
Example 3
Source File: JavassistDynamicField.java From gecco with MIT License | 6 votes |
@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; }
Example 4
Source File: TransformationUtil.java From jpmml-model with BSD 3-Clause "New" or "Revised" License | 6 votes |
static private void removeValue(ArrayMemberValue memberValue, String value){ List<MemberValue> values = new ArrayList<>(Arrays.asList(memberValue.getValue())); boolean removed = false; Iterator<MemberValue> it = values.iterator(); while(it.hasNext()){ StringMemberValue stringValue = (StringMemberValue)it.next(); if((value).equals(stringValue.getValue())){ it.remove(); removed = true; } } if(!removed){ throw new RuntimeException(value + " not in " + values); } memberValue.setValue(values.toArray(new MemberValue[values.size()])); }
Example 5
Source File: SpringMVCBinder.java From statefulj with Apache License 2.0 | 6 votes |
@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); }
Example 6
Source File: AbstractMethodCreator.java From minnal with Apache License 2.0 | 6 votes |
/** * Returns the api parameter annotations * * @return */ protected Annotation getApiParamAnnotations() { Annotation implicitParams = new Annotation(ApiImplicitParams.class.getCanonicalName(), ctClass.getClassFile().getConstPool()); List<AnnotationMemberValue> annotationMemberValues = new ArrayList<AnnotationMemberValue>(); List<Annotation> annotations = Lists.newArrayList(); annotations.addAll(getApiPathParamAnnotations()); annotations.addAll(getApiQueryParamAnnotations()); annotations.addAll(getApiAdditionalParamAnnotations()); for (Annotation annotation : annotations) { annotationMemberValues.add(new AnnotationMemberValue(annotation, ctClass.getClassFile().getConstPool())); } ArrayMemberValue values = new ArrayMemberValue(ctClass.getClassFile().getConstPool()); values.setValue(annotationMemberValues.toArray(new AnnotationMemberValue[0])); implicitParams.addMemberValue("value", values); return implicitParams; }
Example 7
Source File: AbstractMethodCreator.java From minnal with Apache License 2.0 | 6 votes |
/** * Returns the security annotation * * @return */ protected Annotation getSecurityAnnotation() { Set<String> permissions = getPermissions(); if (permissions == null || permissions.isEmpty()) { return null; } Annotation rolesAllowed = new Annotation(RolesAllowed.class.getCanonicalName(), ctClass.getClassFile().getConstPool()); ArrayMemberValue values = new ArrayMemberValue(ctClass.getClassFile().getConstPool()); List<StringMemberValue> memberValues = new ArrayList<StringMemberValue>(); for (String permission : permissions) { memberValues.add(new StringMemberValue(permission, ctClass.getClassFile().getConstPool())); } values.setValue(memberValues.toArray(new StringMemberValue[0])); rolesAllowed.addMemberValue("value", values); return rolesAllowed; }
Example 8
Source File: ClassGenerator.java From flower with Apache License 2.0 | 5 votes |
private MemberValue getMemberValue(Object obj, ConstPool cp) { if (obj == null) { return null; } if (obj instanceof Integer) { return new IntegerMemberValue(cp, (Integer) obj); } else if (obj instanceof Boolean) { return new BooleanMemberValue((Boolean) obj, cp); } else if (obj instanceof Double) { return new DoubleMemberValue((Double) obj, cp); } else if (obj instanceof Float) { return new FloatMemberValue((Float) obj, cp); } else if (obj instanceof Short) { return new ShortMemberValue((Short) obj, cp); } else if (obj instanceof String) { return new StringMemberValue((String) obj, cp); } else if (obj instanceof String[]) { String[] oo = (String[]) obj; MemberValue[] memberValues = new MemberValue[oo.length]; ArrayMemberValue value = new ArrayMemberValue(cp); for (int i = 0; i < oo.length; i++) { memberValues[i] = getMemberValue(oo[i], cp); } value.setValue(memberValues); return value; } else if (obj instanceof Byte) { return new ByteMemberValue((Byte) obj, cp); } else if (obj instanceof Annotation) { return new AnnotationMemberValue((Annotation) obj, cp); } else if (obj instanceof ArrayMemberValue) { return new ArrayMemberValue((MemberValue) obj, cp); } else if (obj instanceof Character) { return new CharMemberValue((char) obj, cp); } else if (obj instanceof Long) { return new LongMemberValue((Long) obj, cp); } return null; }
Example 9
Source File: JavassistDynamicBean.java From gecco with MIT License | 5 votes |
@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; }
Example 10
Source File: AbstractMethodCreator.java From minnal with Apache License 2.0 | 5 votes |
protected Annotation getApiResponsesAnnotation() { Annotation apiResponses = new Annotation(ApiResponses.class.getCanonicalName(), ctClass.getClassFile().getConstPool()); ArrayMemberValue values = new ArrayMemberValue(ctClass.getClassFile().getConstPool()); List<AnnotationMemberValue> memberValues = new ArrayList<AnnotationMemberValue>(); for (Annotation annotation : getApiResponseAnnotations()) { memberValues.add(new AnnotationMemberValue(annotation, ctClass.getClassFile().getConstPool())); } values.setValue(memberValues.toArray(new AnnotationMemberValue[0])); apiResponses.addMemberValue("value", values); return apiResponses; }
Example 11
Source File: AbstractMethodCreator.java From minnal with Apache License 2.0 | 5 votes |
protected Annotation getProducesAnnotation() { Annotation annotation = new Annotation(Produces.class.getCanonicalName(), getCtClass().getClassFile().getConstPool()); ArrayMemberValue values = new ArrayMemberValue(getCtClass().getClassFile().getConstPool()); StringMemberValue json = new StringMemberValue(MediaType.APPLICATION_JSON, getCtClass().getClassFile().getConstPool()); StringMemberValue xml = new StringMemberValue(MediaType.APPLICATION_XML, getCtClass().getClassFile().getConstPool()); values.setValue(new StringMemberValue[]{json, xml}); annotation.addMemberValue("value", values); return annotation; }
Example 12
Source File: ServiceDelegateBuilder.java From jweb-cms with GNU Affero General Public License v3.0 | 4 votes |
private MemberValue memberValue(Class<?> type, Object value) { if (type.isArray()) { Object[] array = (Object[]) value; MemberValue[] memberValues = new MemberValue[array.length]; Class<?> componentType = type.getComponentType(); for (int i = 0; i < array.length; i++) { memberValues[i] = memberValue(componentType, array[i]); } ArrayMemberValue arrayMemberValue = new ArrayMemberValue(constPool); arrayMemberValue.setValue(memberValues); return arrayMemberValue; } else { if (type == int.class) { return new IntegerMemberValue(constPool, (int) value); } else if (long.class.equals(type)) { return new LongMemberValue((long) value, constPool); } else if (short.class.equals(type)) { return new ShortMemberValue((short) value, constPool); } else if (byte.class.equals(type)) { return new ByteMemberValue((byte) value, constPool); } else if (char.class.equals(type)) { return new CharMemberValue((char) value, constPool); } else if (float.class.equals(type)) { return new FloatMemberValue((float) value, constPool); } else if (double.class.equals(type)) { return new DoubleMemberValue((double) value, constPool); } else if (String.class.equals(type)) { return new StringMemberValue((String) value, constPool); } else if (boolean.class.equals(type)) { return new BooleanMemberValue((boolean) value, constPool); } else if (Class.class.equals(type)) { return new ClassMemberValue(((Class) value).getName(), constPool); } else if (Enum.class.isAssignableFrom(type)) { EnumMemberValue enumMemberValue = new EnumMemberValue(constPool); enumMemberValue.setType(type.getName()); enumMemberValue.setValue(((Enum) value).name()); return enumMemberValue; } else { throw new ApplicationException("unsupported annotation method type, type={}", type); } } }
Example 13
Source File: AsmRest.java From fastquery with Apache License 2.0 | 4 votes |
/** * 自动生成Repository接口的实现类并以字节的形式返回. * * @param repositoryClazz repository class * @return 生成的类字节码 */ public static byte[] generateBytes(Class<?> repositoryClazz) { // 生成类 ClassPool pool = ClassPool.getDefault(); // web容器中的repository 需要增加classPath ClassClassPath classClassPath = new ClassClassPath(repositoryClazz); pool.removeClassPath(classClassPath); pool.insertClassPath(classClassPath); String className = repositoryClazz.getName() + Placeholder.REST_SUF; CtClass ctClass = pool.makeClass(className); ClassFile ccFile = ctClass.getClassFile(); ConstPool constpool = ccFile.getConstPool(); try { // 增加接口 ctClass.setInterfaces(new CtClass[] { pool.get(repositoryClazz.getName()) }); // 增加字段 CtClass executor = pool.get(repositoryClazz.getName()); CtField field = new CtField(executor, "d", ctClass); field.setModifiers(Modifier.PRIVATE); FieldInfo fieldInfo = field.getFieldInfo(); // 标识属性注解 AnnotationsAttribute fieldAttr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag); javassist.bytecode.annotation.Annotation autowired = new javassist.bytecode.annotation.Annotation( "javax.inject.Inject", constpool); fieldAttr.addAnnotation(autowired); fieldInfo.addAttribute(fieldAttr); ctClass.addField(field); AsmRepository.addGetInterfaceClassMethod(repositoryClazz, ctClass); // 标识类注解 AnnotationsAttribute classAttr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag); javassist.bytecode.annotation.Annotation singAnn = new javassist.bytecode.annotation.Annotation( "javax.inject.Singleton", constpool); classAttr.addAnnotation(singAnn); ccFile.addAttribute(classAttr); // 实现抽象方法 Method[] methods = repositoryClazz.getMethods(); for (Method method : methods) { if (!method.isDefault()) { CtMethod cm = CtMethod.make(AsmRepository.getMethodDef(method) + "{return d."+method.getName()+"($$);}", ctClass); // 标识方法注解 AnnotationsAttribute methodAttr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag); javassist.bytecode.annotation.Annotation extendsAnn = new javassist.bytecode.annotation.Annotation( "org.fastquery.core.Extends", constpool); ArrayMemberValue arrayMemberValue = new ArrayMemberValue(constpool); Annotation[] mas = method.getAnnotations(); ClassMemberValue[] cmvs = new ClassMemberValue[mas.length]; for (int i = 0; i < mas.length; i++) { cmvs[i] = new ClassMemberValue(mas[i].annotationType().getName(), constpool); } arrayMemberValue.setValue(cmvs); extendsAnn.addMemberValue("value", arrayMemberValue); methodAttr.addAnnotation(extendsAnn); MethodInfo info = cm.getMethodInfo(); info.addAttribute(methodAttr); ctClass.addMethod(cm); } } return ctClass.toBytecode(); } catch (Exception e) { throw new ExceptionInInitializerError(e); } }
Example 14
Source File: JCommanderTranslationMap.java From geowave with Apache License 2.0 | 4 votes |
/** * Iterate the annotations, look for a 'names' parameter, and override it to prepend the given * prefix. */ private void overrideParameterPrefixes(final CtField field, final String[] names) { // This is the JCommander package name final String packageName = JCommander.class.getPackage().getName(); final AnnotationsAttribute fieldAttributes = (AnnotationsAttribute) field.getFieldInfo().getAttribute(AnnotationsAttribute.visibleTag); // Look for annotations that have a 'names' attribute, and whose package // starts with the expected JCommander package. for (final Annotation annotation : fieldAttributes.getAnnotations()) { if (annotation.getTypeName().startsWith(packageName)) { // See if it has a 'names' member variable. final MemberValue namesMember = annotation.getMemberValue(NAMES_MEMBER); // We have a names member!!! if (namesMember != null) { final ArrayMemberValue arrayNamesMember = (ArrayMemberValue) namesMember; // Iterate and transform each item in 'names()' list and // transform it. final MemberValue[] newMemberValues = new MemberValue[names.length]; for (int i = 0; i < names.length; i++) { newMemberValues[i] = new StringMemberValue(names[i], field.getFieldInfo2().getConstPool()); } // Override the member values in nameMember with the new // one's we've generated arrayNamesMember.setValue(newMemberValues); // This is KEY! For some reason, the existing annotation // will not be modified unless // you call 'setAnnotation' here. I'm guessing // 'getAnnotation()' creates a copy. fieldAttributes.setAnnotation(annotation); // Finished processing names. break; } } } }