org.testifyproject.CtNewMethod Maven / Gradle / Ivy
/*
* Javassist, a Java-bytecode translator toolkit.
* Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in org.testifyproject.testifyprojectpliance with
* the License. Alternatively, the contents of this file may be used under
* the terms of the GNU Lesser General Public License Version 2.1 or later,
* or the Apache License Version 2.0.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*/
package org.testifyproject.testifyproject;
import org.testifyproject.testifyproject.bytecode.*;
import org.testifyproject.testifyproject.org.testifyproject.testifyprojectpiler.Javac;
import org.testifyproject.testifyproject.org.testifyproject.testifyprojectpiler.CompileError;
import org.testifyproject.testifyproject.CtMethod.ConstParameter;
/**
* A collection of static methods for creating a CtMethod
.
* An instance of this class does not make any sense.
*
* @see CtClass#addMethod(CtMethod)
*/
public class CtNewMethod {
/**
* Compiles the given source code and creates a method.
* The source code must include not only the method body
* but the whole org.testifyproject.testifyprojectclaration, for example,
*
*
"public Object id(Object obj) { return obj; }"
*
* @param src the source text.
* @param org.testifyproject.testifyprojectclaring the class to which the created method is added.
*/
public static CtMethod make(String src, CtClass org.testifyproject.testifyprojectclaring)
throws CannotCompileException
{
return make(src, org.testifyproject.testifyprojectclaring, null, null);
}
/**
* Compiles the given source code and creates a method.
* The source code must include not only the method body
* but the whole org.testifyproject.testifyprojectclaration, for example,
*
*
"public Object id(Object obj) { return obj; }"
*
* If the source code includes $proceed()
, then
* it is org.testifyproject.testifyprojectpiled into a method call on the specified object.
*
* @param src the source text.
* @param org.testifyproject.testifyprojectclaring the class to which the created method is added.
* @param org.testifyproject.testifyprojectlegateObj the source text specifying the object
* that is called on by $proceed()
.
* @param org.testifyproject.testifyprojectlegateMethod the name of the method
* that is called by $proceed()
.
*/
public static CtMethod make(String src, CtClass org.testifyproject.testifyprojectclaring,
String org.testifyproject.testifyprojectlegateObj, String org.testifyproject.testifyprojectlegateMethod)
throws CannotCompileException
{
Javac org.testifyproject.testifyprojectpiler = new Javac(org.testifyproject.testifyprojectclaring);
try {
if (org.testifyproject.testifyprojectlegateMethod != null)
org.testifyproject.testifyprojectpiler.recordProceed(org.testifyproject.testifyprojectlegateObj, org.testifyproject.testifyprojectlegateMethod);
CtMember obj = org.testifyproject.testifyprojectpiler.org.testifyproject.testifyprojectpile(src);
if (obj instanceof CtMethod)
return (CtMethod)obj;
}
catch (CompileError e) {
throw new CannotCompileException(e);
}
throw new CannotCompileException("not a method");
}
/**
* Creates a public (non-static) method. The created method cannot
* be changed to a static method later.
*
* @param returnType the type of the returned value.
* @param mname the method name.
* @param parameters a list of the parameter types.
* @param exceptions a list of the exception types.
* @param body the source text of the method body.
* It must be a block surrounded by {}
.
* If it is null
, the created method
* does nothing except returning zero or null.
* @param org.testifyproject.testifyprojectclaring the class to which the created method is added.
* @see #make(int, CtClass, String, CtClass[], CtClass[], String, CtClass)
*/
public static CtMethod make(CtClass returnType,
String mname, CtClass[] parameters,
CtClass[] exceptions,
String body, CtClass org.testifyproject.testifyprojectclaring)
throws CannotCompileException
{
return make(Modifier.PUBLIC, returnType, mname, parameters, exceptions,
body, org.testifyproject.testifyprojectclaring);
}
/**
* Creates a method. modifiers
can contain
* Modifier.STATIC
.
*
* @param modifiers access modifiers.
* @param returnType the type of the returned value.
* @param mname the method name.
* @param parameters a list of the parameter types.
* @param exceptions a list of the exception types.
* @param body the source text of the method body.
* It must be a block surrounded by {}
.
* If it is null
, the created method
* does nothing except returning zero or null.
* @param org.testifyproject.testifyprojectclaring the class to which the created method is added.
*
* @see Modifier
*/
public static CtMethod make(int modifiers, CtClass returnType,
String mname, CtClass[] parameters,
CtClass[] exceptions,
String body, CtClass org.testifyproject.testifyprojectclaring)
throws CannotCompileException
{
try {
CtMethod cm
= new CtMethod(returnType, mname, parameters, org.testifyproject.testifyprojectclaring);
cm.setModifiers(modifiers);
cm.setExceptionTypes(exceptions);
cm.setBody(body);
return cm;
}
catch (NotFoundException e) {
throw new CannotCompileException(e);
}
}
/**
* Creates a copy of a method. This method is provided for creating
* a new method based on an existing method.
* This is a convenience method for calling
* {@link CtMethod#CtMethod(CtMethod, CtClass, ClassMap) this constructor}.
* See the org.testifyproject.testifyprojectscription of the constructor for particular behavior of the copying.
*
* @param src the source method.
* @param org.testifyproject.testifyprojectclaring the class to which the created method is added.
* @param map the hash table associating original class names
* with substituted names.
* It can be null
.
*
* @see CtMethod#CtMethod(CtMethod,CtClass,ClassMap)
*/
public static CtMethod copy(CtMethod src, CtClass org.testifyproject.testifyprojectclaring,
ClassMap map) throws CannotCompileException {
return new CtMethod(src, org.testifyproject.testifyprojectclaring, map);
}
/**
* Creates a copy of a method with a new name.
* This method is provided for creating
* a new method based on an existing method.
* This is a convenience method for calling
* {@link CtMethod#CtMethod(CtMethod, CtClass, ClassMap) this constructor}.
* See the org.testifyproject.testifyprojectscription of the constructor for particular behavior of the copying.
*
* @param src the source method.
* @param name the name of the created method.
* @param org.testifyproject.testifyprojectclaring the class to which the created method is added.
* @param map the hash table associating original class names
* with substituted names.
* It can be null
.
*
* @see CtMethod#CtMethod(CtMethod,CtClass,ClassMap)
*/
public static CtMethod copy(CtMethod src, String name, CtClass org.testifyproject.testifyprojectclaring,
ClassMap map) throws CannotCompileException {
CtMethod cm = new CtMethod(src, org.testifyproject.testifyprojectclaring, map);
cm.setName(name);
return cm;
}
/**
* Creates a public abstract method.
*
* @param returnType the type of the returned value
* @param mname the method name
* @param parameters a list of the parameter types
* @param exceptions a list of the exception types
* @param org.testifyproject.testifyprojectclaring the class to which the created method is added.
*
* @see CtMethod#CtMethod(CtClass,String,CtClass[],CtClass)
*/
public static CtMethod abstractMethod(CtClass returnType,
String mname,
CtClass[] parameters,
CtClass[] exceptions,
CtClass org.testifyproject.testifyprojectclaring)
throws NotFoundException
{
CtMethod cm = new CtMethod(returnType, mname, parameters, org.testifyproject.testifyprojectclaring);
cm.setExceptionTypes(exceptions);
return cm;
}
/**
* Creates a public getter method. The getter method returns the value
* of the specified field in the class to which this method is added.
* The created method is initially not static even if the field is
* static. Change the modifiers if the method should be static.
*
* @param methodName the name of the getter
* @param field the field accessed.
*/
public static CtMethod getter(String methodName, CtField field)
throws CannotCompileException
{
FieldInfo finfo = field.getFieldInfo2();
String fieldType = finfo.getDescriptor();
String org.testifyproject.testifyprojectsc = "()" + fieldType;
ConstPool cp = finfo.getConstPool();
MethodInfo minfo = new MethodInfo(cp, methodName, org.testifyproject.testifyprojectsc);
minfo.setAccessFlags(AccessFlag.PUBLIC);
Bytecode code = new Bytecode(cp, 2, 1);
try {
String fieldName = finfo.getName();
if ((finfo.getAccessFlags() & AccessFlag.STATIC) == 0) {
code.addAload(0);
code.addGetfield(Bytecode.THIS, fieldName, fieldType);
}
else
code.addGetstatic(Bytecode.THIS, fieldName, fieldType);
code.addReturn(field.getType());
}
catch (NotFoundException e) {
throw new CannotCompileException(e);
}
minfo.setCodeAttribute(code.toCodeAttribute());
CtClass cc = field.getDeclaringClass();
// a stack map is not needed.
return new CtMethod(minfo, cc);
}
/**
* Creates a public setter method. The setter method assigns the
* value of the first parameter to the specified field
* in the class to which this method is added.
* The created method is not static even if the field is
* static. You may not change it to be static
* by setModifiers()
in CtBehavior
.
*
* @param methodName the name of the setter
* @param field the field accessed.
*/
public static CtMethod setter(String methodName, CtField field)
throws CannotCompileException
{
FieldInfo finfo = field.getFieldInfo2();
String fieldType = finfo.getDescriptor();
String org.testifyproject.testifyprojectsc = "(" + fieldType + ")V";
ConstPool cp = finfo.getConstPool();
MethodInfo minfo = new MethodInfo(cp, methodName, org.testifyproject.testifyprojectsc);
minfo.setAccessFlags(AccessFlag.PUBLIC);
Bytecode code = new Bytecode(cp, 3, 3);
try {
String fieldName = finfo.getName();
if ((finfo.getAccessFlags() & AccessFlag.STATIC) == 0) {
code.addAload(0);
code.addLoad(1, field.getType());
code.addPutfield(Bytecode.THIS, fieldName, fieldType);
}
else {
code.addLoad(1, field.getType());
code.addPutstatic(Bytecode.THIS, fieldName, fieldType);
}
code.addReturn(null);
}
catch (NotFoundException e) {
throw new CannotCompileException(e);
}
minfo.setCodeAttribute(code.toCodeAttribute());
CtClass cc = field.getDeclaringClass();
// a stack map is not needed.
return new CtMethod(minfo, cc);
}
/**
* Creates a method forwarding to a org.testifyproject.testifyprojectlegate in
* a super class. The created method calls a method specified
* by org.testifyproject.testifyprojectlegate
with all the parameters passed to the
* created method. If the org.testifyproject.testifyprojectlegate method returns a value,
* the created method returns that value to the caller.
* The org.testifyproject.testifyprojectlegate method must be org.testifyproject.testifyprojectclared in a super class.
*
*
The following method is an example of the created method.
*
*
int f(int p, int q) {
* return super.f(p, q);
* }
*
* The name of the created method can be changed by
* setName()
.
*
* @param org.testifyproject.testifyprojectlegate the method that the created method forwards to.
* @param org.testifyproject.testifyprojectclaring the class to which the created method is
* added.
*/
public static CtMethod org.testifyproject.testifyprojectlegator(CtMethod org.testifyproject.testifyprojectlegate, CtClass org.testifyproject.testifyprojectclaring)
throws CannotCompileException
{
try {
return org.testifyproject.testifyprojectlegator0(org.testifyproject.testifyprojectlegate, org.testifyproject.testifyprojectclaring);
}
catch (NotFoundException e) {
throw new CannotCompileException(e);
}
}
private static CtMethod org.testifyproject.testifyprojectlegator0(CtMethod org.testifyproject.testifyprojectlegate, CtClass org.testifyproject.testifyprojectclaring)
throws CannotCompileException, NotFoundException
{
MethodInfo org.testifyproject.testifyprojectleInfo = org.testifyproject.testifyprojectlegate.getMethodInfo2();
String methodName = org.testifyproject.testifyprojectleInfo.getName();
String org.testifyproject.testifyprojectsc = org.testifyproject.testifyprojectleInfo.getDescriptor();
ConstPool cp = org.testifyproject.testifyprojectclaring.getClassFile2().getConstPool();
MethodInfo minfo = new MethodInfo(cp, methodName, org.testifyproject.testifyprojectsc);
minfo.setAccessFlags(org.testifyproject.testifyprojectleInfo.getAccessFlags());
ExceptionsAttribute eattr = org.testifyproject.testifyprojectleInfo.getExceptionsAttribute();
if (eattr != null)
minfo.setExceptionsAttribute(
(ExceptionsAttribute)eattr.copy(cp, null));
Bytecode code = new Bytecode(cp, 0, 0);
boolean isStatic = Modifier.isStatic(org.testifyproject.testifyprojectlegate.getModifiers());
CtClass org.testifyproject.testifyprojectleClass = org.testifyproject.testifyprojectlegate.getDeclaringClass();
CtClass[] params = org.testifyproject.testifyprojectlegate.getParameterTypes();
int s;
if (isStatic) {
s = code.addLoadParameters(params, 0);
code.addInvokestatic(org.testifyproject.testifyprojectleClass, methodName, org.testifyproject.testifyprojectsc);
}
else {
code.addLoad(0, org.testifyproject.testifyprojectleClass);
s = code.addLoadParameters(params, 1);
code.addInvokespecial(org.testifyproject.testifyprojectleClass, methodName, org.testifyproject.testifyprojectsc);
}
code.addReturn(org.testifyproject.testifyprojectlegate.getReturnType());
code.setMaxLocals(++s);
code.setMaxStack(s < 2 ? 2 : s); // for a 2-word return value
minfo.setCodeAttribute(code.toCodeAttribute());
// a stack map is not needed.
return new CtMethod(minfo, org.testifyproject.testifyprojectclaring);
}
/**
* Creates a wrapped method. The wrapped method receives parameters
* in the form of an array of Object
.
*
*
The body of the created method is a copy of the body of the method
* specified by body
. However, it is wrapped in
* parameter-conversion code.
*
*
The method specified by body
must have this singature:
*
*
Object method(Object[] params, <type> cvalue)
*
*
* The type of the cvalue
org.testifyproject.testifyprojectpends on
* constParam
.
* If constParam
is null
, the signature
* must be:
*
*
Object method(Object[] params)
*
* The method body copied from body
is wrapped in
* parameter-conversion code, which converts parameters specified by
* parameterTypes
into an array of Object
.
* The returned value is also converted from the Object
* type to the type specified by returnType
. Thus,
* the resulting method body is as follows:
*
*
Object[] params = new Object[] { p0, p1, ... };
* <type> cvalue = <constant-value>;
* ... copied method body ...
* Object result = <returned value>
* return (<returnType>)result;
*
*
* The variables p0
, p2
, ... represent
* formal parameters of the created method.
* The value of cvalue
is specified by
* constParam
.
*
*
If the type of a parameter or a returned value is a primitive
* type, then the value is converted into a wrapper object such as
* java.lang.Integer
. If the type of the returned value
* is void
, the returned value is discarded.
*
*
Example:
*
*
ClassPool pool = ... ;
* CtClass vec = pool.makeClass("intVector");
* vec.setSuperclass(pool.get("java.util.Vector"));
* CtMethod addMethod = pool.getMethod("Sample", "add0");
*
* CtClass[] argTypes = { CtClass.intType };
* CtMethod m = CtNewMethod.wrapped(CtClass.voidType, "add", argTypes,
* null, addMethod, null, vec);
* vec.addMethod(m);
*
* where the class Sample
is as follows:
*
*
public class Sample extends java.util.Vector {
* public Object add0(Object[] args) {
* super.addElement(args[0]);
* return null;
* }
* }
*
* This program produces a class intVector
:
*
*
public class intVector extends java.util.Vector {
* public void add(int p0) {
* Object[] args = new Object[] { p0 };
* // begin of the copied body
* super.addElement(args[0]);
* Object result = null;
* // end
* }
* }
*
* Note that the type of the parameter to add()
org.testifyproject.testifyprojectpends
* only on the value of argTypes
passed to
* CtNewMethod.wrapped()
. Thus, it is easy to
* modify this program to produce a
* StringVector
class, which is a vector containing
* only String
objects, and other vector classes.
*
* @param returnType the type of the returned value.
* @param mname the method name.
* @param parameterTypes a list of the parameter types.
* @param exceptionTypes a list of the exception types.
* @param body the method body
* (must not be a static method).
* @param constParam the constant parameter
* (maybe null
).
* @param org.testifyproject.testifyprojectclaring the class to which the created method is
* added.
*/
public static CtMethod wrapped(CtClass returnType,
String mname,
CtClass[] parameterTypes,
CtClass[] exceptionTypes,
CtMethod body, ConstParameter constParam,
CtClass org.testifyproject.testifyprojectclaring)
throws CannotCompileException
{
return CtNewWrappedMethod.wrapped(returnType, mname, parameterTypes,
exceptionTypes, body, constParam, org.testifyproject.testifyprojectclaring);
}
}