Java Code Examples for javassist.bytecode.MethodInfo#getConstPool()

The following examples show how to use javassist.bytecode.MethodInfo#getConstPool() . 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 vote down vote up
@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 vote down vote up
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: AbstractRestfulBinder.java    From statefulj with Apache License 2.0 6 votes vote down vote up
protected Annotation[] addIdParameter(
		CtMethod ctMethod, 
		Class<?> idType,
		ClassPool cp) throws NotFoundException, CannotCompileException {
	// Clone the parameter Class
	//
	CtClass ctParm = cp.get(idType.getName());
	
	// Add the parameter to the method
	//
	ctMethod.addParameter(ctParm);
	
	// Add the Parameter Annotations to the Method
	//
	MethodInfo methodInfo = ctMethod.getMethodInfo();
	ConstPool constPool = methodInfo.getConstPool();
	Annotation annot = new Annotation(getPathAnnotationClass().getName(), constPool);
	
	StringMemberValue valueVal = new StringMemberValue("id", constPool); 
	annot.addMemberValue("value", valueVal);
	
	return new Annotation[]{ annot };
}
 
Example 4
Source File: JavassistPerfTest.java    From annotation-tools with MIT License 5 votes vote down vote up
byte[] counterAdaptClass(final InputStream is, final String name)
        throws Exception
{
    CtClass cc = pool.makeClass(is);
    if (!cc.isInterface()) {
        cc.addField(new CtField(CtClass.intType, "_counter", cc));
    }
    CtMethod[] ms = cc.getDeclaredMethods();
    for (int j = 0; j < ms.length; ++j) {
        CtMethod m = ms[j];
        int modifiers = m.getModifiers();
        if (!Modifier.isStatic(modifiers)
                && !Modifier.isAbstract(modifiers)
                && !Modifier.isNative(modifiers))
        {
            if (!m.isEmpty()) {
                MethodInfo info = m.getMethodInfo();
                Bytecode bc = new Bytecode(info.getConstPool(), 1, 0);
                bc.addAload(0);
                bc.addAload(0);
                bc.addGetfield(cc, "_counter", "I");
                bc.add(Opcode.ICONST_1);
                bc.add(Opcode.IADD);
                bc.addPutfield(cc, "_counter", "I");
                CodeIterator iter = info.getCodeAttribute().iterator();
                iter.begin();
                iter.insert(bc.get());
            }
        }
    }
    return cc.toBytecode();
}
 
Example 5
Source File: JavassistPerfTest.java    From annotation-tools with MIT License 5 votes vote down vote up
byte[] counterAdaptClass(final InputStream is, final String name)
        throws Exception
{
    CtClass cc = pool.makeClass(is);
    if (!cc.isInterface()) {
        cc.addField(new CtField(CtClass.intType, "_counter", cc));
    }
    CtMethod[] ms = cc.getDeclaredMethods();
    for (int j = 0; j < ms.length; ++j) {
        CtMethod m = ms[j];
        int modifiers = m.getModifiers();
        if (!Modifier.isStatic(modifiers)
                && !Modifier.isAbstract(modifiers)
                && !Modifier.isNative(modifiers))
        {
            if (!m.isEmpty()) {
                MethodInfo info = m.getMethodInfo();
                Bytecode bc = new Bytecode(info.getConstPool(), 1, 0);
                bc.addAload(0);
                bc.addAload(0);
                bc.addGetfield(cc, "_counter", "I");
                bc.add(Opcode.ICONST_1);
                bc.add(Opcode.IADD);
                bc.addPutfield(cc, "_counter", "I");
                CodeIterator iter = info.getCodeAttribute().iterator();
                iter.begin();
                iter.insert(bc.get());
            }
        }
    }
    return cc.toBytecode();
}
 
Example 6
Source File: CamelBinder.java    From statefulj with Apache License 2.0 5 votes vote down vote up
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 7
Source File: AbstractRestfulBinder.java    From statefulj with Apache License 2.0 5 votes vote down vote up
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 8
Source File: JerseyBinder.java    From statefulj with Apache License 2.0 5 votes vote down vote up
@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 9
Source File: AbstractRestfulBinder.java    From statefulj with Apache License 2.0 4 votes vote down vote up
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);
}