Java Code Examples for javassist.bytecode.MethodInfo#addAttribute()
The following examples show how to use
javassist.bytecode.MethodInfo#addAttribute() .
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: 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 2
Source File: JavassistUtils.java From statefulj with Apache License 2.0 | 6 votes |
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); } } } }
Example 3
Source File: CamelBinder.java From statefulj with Apache License 2.0 | 5 votes |
private void addConsumeAnnotation(CtMethod ctMethod, String uri) { MethodInfo methodInfo = ctMethod.getMethodInfo(); ConstPool constPool = methodInfo.getConstPool(); Annotation consume = new Annotation(Consume.class.getName(), constPool); StringMemberValue valueVal = new StringMemberValue(constPool); valueVal.setValue(uri); consume.addMemberValue("uri", valueVal); AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); attr.addAnnotation(consume); methodInfo.addAttribute(attr); }
Example 4
Source File: AbstractRestfulBinder.java From statefulj with Apache License 2.0 | 5 votes |
protected void copyParameters(CtMethod ctMethod, Method method, ClassPool cp) throws NotFoundException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, CannotCompileException { String[] parmNames = (method != null) ? parmDiscover.getParameterNames(method) : null; MethodInfo methodInfo = ctMethod.getMethodInfo(); ParameterAnnotationsAttribute paramAtrributeInfo = new ParameterAnnotationsAttribute( methodInfo.getConstPool(), ParameterAnnotationsAttribute.visibleTag); Annotation[][] paramArrays = new Annotation[method.getParameterTypes().length][]; java.lang.annotation.Annotation[][] parmAnnotations = method.getParameterAnnotations(); int parmIndex = 0; for(Class<?> parm : method.getParameterTypes()) { // Clone the parameter Class // CtClass ctParm = cp.get(parm.getName()); // Add the parameter to the method // ctMethod.addParameter(ctParm); // Add the Parameter Annotations to the Method // String parmName = (parmNames != null && parmNames.length > parmIndex) ? parmNames[parmIndex] : null; paramArrays[parmIndex] = createParameterAnnotations( parmName, ctMethod.getMethodInfo(), parmAnnotations[parmIndex], paramAtrributeInfo.getConstPool()); parmIndex++; } paramAtrributeInfo.setAnnotations(paramArrays); methodInfo.addAttribute(paramAtrributeInfo); }
Example 5
Source File: JerseyBinder.java From statefulj with Apache License 2.0 | 5 votes |
@Override protected void addEndpointMapping( CtMethod ctMethod, String method, String request) { // Add Path Annotation // MethodInfo methodInfo = ctMethod.getMethodInfo(); ConstPool constPool = methodInfo.getConstPool(); AnnotationsAttribute annoAttr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); Annotation pathMapping = new Annotation(Path.class.getName(), constPool); StringMemberValue valueVal = new StringMemberValue(constPool); valueVal.setValue(request); pathMapping.addMemberValue("value", valueVal); annoAttr.addAnnotation(pathMapping); // Add Verb Annotation (GET|POST|PUT|DELETE) // String verbClassName = "javax.ws.rs." + method; Annotation verb = new Annotation(verbClassName, constPool); annoAttr.addAnnotation(verb); methodInfo.addAttribute(annoAttr); }
Example 6
Source File: Javassists.java From ProjectAres with GNU Affero General Public License v3.0 | 4 votes |
public static AttributeInfo addAttribute(MethodInfo to, AttributeInfo attr) { attr = inPool(attr, to.getConstPool()); to.addAttribute(attr); return attr; }
Example 7
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 8
Source File: AbstractRestfulBinder.java From statefulj with Apache License 2.0 | 4 votes |
protected void addRequestParameters( boolean referencesId, Class<?> idType, boolean isDomainEntity, CtMethod ctMethod, Method method, ClassPool cp) throws NotFoundException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, CannotCompileException { int fixedParmCnt = (isDomainEntity) ? 1 : 2; String[] parmNames = (method != null) ? parmDiscover.getParameterNames(method) : null; MethodInfo methodInfo = ctMethod.getMethodInfo(); ParameterAnnotationsAttribute paramAtrributeInfo = new ParameterAnnotationsAttribute( methodInfo.getConstPool(), ParameterAnnotationsAttribute.visibleTag); Annotation[][] paramArrays = null; if (method != null) { int parmIndex = 0; // Does this event reference the stateful object? // int additionalParmCnt = (referencesId) ? 2 : 1; int annotationCnt = method.getParameterTypes().length + additionalParmCnt - fixedParmCnt; annotationCnt = Math.max(annotationCnt, additionalParmCnt); // Pull the Parameter Annotations from the StatefulController - we're going to skip // over the first one (DomainEntity) or two (Controller) - but then we're going to // add a parameter for the HttpServletRequest and "id" parameter // java.lang.annotation.Annotation[][] parmAnnotations = method.getParameterAnnotations(); paramArrays = new Annotation[annotationCnt][]; // Add an Id parameter at the beginning of the method - this will be // used by the Harness to fetch the object // if (referencesId) { paramArrays[parmIndex] = addIdParameter(ctMethod, idType, cp); parmIndex++; } // Add an HttpServletRequest - this will be passed in as a context to the finder/factory methods // paramArrays[parmIndex] = addHttpRequestParameter(ctMethod, cp); parmIndex++; int parmCnt = 0; for(Class<?> parm : method.getParameterTypes()) { // Skip first two parameters - they are the Stateful Object and event String. // if (parmCnt < fixedParmCnt) { parmCnt++; continue; } // Clone the parameter Class // CtClass ctParm = cp.get(parm.getName()); // Add the parameter to the method // ctMethod.addParameter(ctParm); // Add the Parameter Annotations to the Method // String parmName = (parmNames != null && parmNames.length > parmCnt) ? parmNames[parmCnt] : null; paramArrays[parmIndex] = createParameterAnnotations( parmName, ctMethod.getMethodInfo(), parmAnnotations[parmCnt], paramAtrributeInfo.getConstPool()); parmCnt++; parmIndex++; } } else { // NOOP transitions always a require an object Id // paramArrays = new Annotation[2][]; paramArrays[0] = addIdParameter(ctMethod, idType, cp); paramArrays[1] = addHttpRequestParameter(ctMethod, cp); } paramAtrributeInfo.setAnnotations(paramArrays); methodInfo.addAttribute(paramAtrributeInfo); }