169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/* 269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Javassist, a Java-bytecode translator toolkit. 369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved. 469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The contents of this file are subject to the Mozilla Public License Version 669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 1.1 (the "License"); you may not use this file except in compliance with 769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the License. Alternatively, the contents of this file may be used under 869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the terms of the GNU Lesser General Public License Version 2.1 or later. 969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 1069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Software distributed under the License is distributed on an "AS IS" basis, 1169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 1269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * for the specific language governing rights and limitations under the 1369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * License. 1469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 1569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 1669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpackage javassist; 1769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 1869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.bytecode.*; 1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.compiler.Javac; 2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.compiler.CompileError; 2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.CtMethod.ConstParameter; 2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/** 2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * A collection of static methods for creating a <code>CtMethod</code>. 2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * An instance of this class does not make any sense. 2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @see CtClass#addMethod(CtMethod) 2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpublic class CtNewMethod { 3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Compiles the given source code and creates a method. 3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The source code must include not only the method body 3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * but the whole declaration, for example, 3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <ul><pre>"public Object id(Object obj) { return obj; }"</pre></ul> 3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param src the source text. 3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param declaring the class to which the created method is added. 4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public static CtMethod make(String src, CtClass declaring) 4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal throws CannotCompileException 4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal { 4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return make(src, declaring, null, null); 4569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 4669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 4769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 4869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Compiles the given source code and creates a method. 4969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The source code must include not only the method body 5069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * but the whole declaration, for example, 5169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 5269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <ul><pre>"public Object id(Object obj) { return obj; }"</pre></ul> 5369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 5469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <p>If the source code includes <code>$proceed()</code>, then 5569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * it is compiled into a method call on the specified object. 5669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 5769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param src the source text. 5869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param declaring the class to which the created method is added. 5969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param delegateObj the source text specifying the object 6069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * that is called on by <code>$proceed()</code>. 6169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param delegateMethod the name of the method 6269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * that is called by <code>$proceed()</code>. 6369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 6469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public static CtMethod make(String src, CtClass declaring, 6569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal String delegateObj, String delegateMethod) 6669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal throws CannotCompileException 6769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal { 6869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal Javac compiler = new Javac(declaring); 6969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal try { 7069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal if (delegateMethod != null) 7169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal compiler.recordProceed(delegateObj, delegateMethod); 7269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 7369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal CtMember obj = compiler.compile(src); 7469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal if (obj instanceof CtMethod) 7569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return (CtMethod)obj; 7669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 7769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal catch (CompileError e) { 7869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal throw new CannotCompileException(e); 7969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 8069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 8169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal throw new CannotCompileException("not a method"); 8269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 8369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 8469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 8569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Creates a public (non-static) method. The created method cannot 8669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * be changed to a static method later. 8769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 8869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param returnType the type of the returned value. 8969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param mname the method name. 9069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param parameters a list of the parameter types. 9169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param exceptions a list of the exception types. 9269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param body the source text of the method body. 9369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * It must be a block surrounded by <code>{}</code>. 9469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * If it is <code>null</code>, the created method 9569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * does nothing except returning zero or null. 9669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param declaring the class to which the created method is added. 9769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @see #make(int, CtClass, String, CtClass[], CtClass[], String, CtClass) 9869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 9969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public static CtMethod make(CtClass returnType, 10069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal String mname, CtClass[] parameters, 10169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal CtClass[] exceptions, 10269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal String body, CtClass declaring) 10369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal throws CannotCompileException 10469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal { 10569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return make(Modifier.PUBLIC, returnType, mname, parameters, exceptions, 10669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal body, declaring); 10769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 10869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 10969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 11069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Creates a method. <code>modifiers</code> can contain 11169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <code>Modifier.STATIC</code>. 11269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 11369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param modifiers access modifiers. 11469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param returnType the type of the returned value. 11569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param mname the method name. 11669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param parameters a list of the parameter types. 11769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param exceptions a list of the exception types. 11869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param body the source text of the method body. 11969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * It must be a block surrounded by <code>{}</code>. 12069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * If it is <code>null</code>, the created method 12169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * does nothing except returning zero or null. 12269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param declaring the class to which the created method is added. 12369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 12469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @see Modifier 12569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 12669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public static CtMethod make(int modifiers, CtClass returnType, 12769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal String mname, CtClass[] parameters, 12869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal CtClass[] exceptions, 12969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal String body, CtClass declaring) 13069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal throws CannotCompileException 13169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal { 13269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal try { 13369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal CtMethod cm 13469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal = new CtMethod(returnType, mname, parameters, declaring); 13569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal cm.setModifiers(modifiers); 13669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal cm.setExceptionTypes(exceptions); 13769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal cm.setBody(body); 13869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return cm; 13969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 14069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal catch (NotFoundException e) { 14169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal throw new CannotCompileException(e); 14269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 14369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 14469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 14569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 14669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Creates a copy of a method. This method is provided for creating 14769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * a new method based on an existing method. 14869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * This is a convenience method for calling 14969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * {@link CtMethod#CtMethod(CtMethod, CtClass, ClassMap) this constructor}. 15069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * See the description of the constructor for particular behavior of the copying. 15169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 15269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param src the source method. 15369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param declaring the class to which the created method is added. 15469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param map the hash table associating original class names 15569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * with substituted names. 15669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * It can be <code>null</code>. 15769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 15869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @see CtMethod#CtMethod(CtMethod,CtClass,ClassMap) 15969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 16069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public static CtMethod copy(CtMethod src, CtClass declaring, 16169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal ClassMap map) throws CannotCompileException { 16269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return new CtMethod(src, declaring, map); 16369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 16469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 16569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 16669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Creates a copy of a method with a new name. 16769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * This method is provided for creating 16869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * a new method based on an existing method. 16969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * This is a convenience method for calling 17069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * {@link CtMethod#CtMethod(CtMethod, CtClass, ClassMap) this constructor}. 17169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * See the description of the constructor for particular behavior of the copying. 17269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 17369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param src the source method. 17469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param name the name of the created method. 17569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param declaring the class to which the created method is added. 17669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param map the hash table associating original class names 17769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * with substituted names. 17869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * It can be <code>null</code>. 17969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 18069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @see CtMethod#CtMethod(CtMethod,CtClass,ClassMap) 18169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 18269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public static CtMethod copy(CtMethod src, String name, CtClass declaring, 18369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal ClassMap map) throws CannotCompileException { 18469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal CtMethod cm = new CtMethod(src, declaring, map); 18569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal cm.setName(name); 18669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return cm; 18769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 18869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 18969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 19069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Creates a public abstract method. 19169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 19269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param returnType the type of the returned value 19369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param mname the method name 19469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param parameters a list of the parameter types 19569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param exceptions a list of the exception types 19669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param declaring the class to which the created method is added. 19769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 19869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @see CtMethod#CtMethod(CtClass,String,CtClass[],CtClass) 19969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 20069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public static CtMethod abstractMethod(CtClass returnType, 20169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal String mname, 20269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal CtClass[] parameters, 20369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal CtClass[] exceptions, 20469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal CtClass declaring) 20569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal throws NotFoundException 20669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal { 20769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal CtMethod cm = new CtMethod(returnType, mname, parameters, declaring); 20869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal cm.setExceptionTypes(exceptions); 20969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return cm; 21069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 21169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 21269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 21369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Creates a public getter method. The getter method returns the value 21469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * of the specified field in the class to which this method is added. 21569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The created method is initially not static even if the field is 21669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * static. Change the modifiers if the method should be static. 21769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 21869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param methodName the name of the getter 21969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param field the field accessed. 22069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 22169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public static CtMethod getter(String methodName, CtField field) 22269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal throws CannotCompileException 22369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal { 22469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal FieldInfo finfo = field.getFieldInfo2(); 22569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal String fieldType = finfo.getDescriptor(); 22669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal String desc = "()" + fieldType; 22769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal ConstPool cp = finfo.getConstPool(); 22869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal MethodInfo minfo = new MethodInfo(cp, methodName, desc); 22969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal minfo.setAccessFlags(AccessFlag.PUBLIC); 23069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 23169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal Bytecode code = new Bytecode(cp, 2, 1); 23269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal try { 23369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal String fieldName = finfo.getName(); 23469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal if ((finfo.getAccessFlags() & AccessFlag.STATIC) == 0) { 23569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code.addAload(0); 23669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code.addGetfield(Bytecode.THIS, fieldName, fieldType); 23769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 23869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal else 23969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code.addGetstatic(Bytecode.THIS, fieldName, fieldType); 24069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 24169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code.addReturn(field.getType()); 24269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 24369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal catch (NotFoundException e) { 24469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal throw new CannotCompileException(e); 24569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 24669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 24769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal minfo.setCodeAttribute(code.toCodeAttribute()); 24869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return new CtMethod(minfo, field.getDeclaringClass()); 24969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 25069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 25169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 25269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Creates a public setter method. The setter method assigns the 25369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * value of the first parameter to the specified field 25469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * in the class to which this method is added. 25569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The created method is not static even if the field is 25669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * static. You may not change it to be static 25769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * by <code>setModifiers()</code> in <code>CtBehavior</code>. 25869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 25969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param methodName the name of the setter 26069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param field the field accessed. 26169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 26269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public static CtMethod setter(String methodName, CtField field) 26369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal throws CannotCompileException 26469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal { 26569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal FieldInfo finfo = field.getFieldInfo2(); 26669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal String fieldType = finfo.getDescriptor(); 26769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal String desc = "(" + fieldType + ")V"; 26869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal ConstPool cp = finfo.getConstPool(); 26969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal MethodInfo minfo = new MethodInfo(cp, methodName, desc); 27069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal minfo.setAccessFlags(AccessFlag.PUBLIC); 27169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 27269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal Bytecode code = new Bytecode(cp, 3, 3); 27369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal try { 27469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal String fieldName = finfo.getName(); 27569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal if ((finfo.getAccessFlags() & AccessFlag.STATIC) == 0) { 27669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code.addAload(0); 27769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code.addLoad(1, field.getType()); 27869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code.addPutfield(Bytecode.THIS, fieldName, fieldType); 27969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 28069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal else { 28169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code.addLoad(1, field.getType()); 28269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code.addPutstatic(Bytecode.THIS, fieldName, fieldType); 28369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 28469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 28569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code.addReturn(null); 28669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 28769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal catch (NotFoundException e) { 28869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal throw new CannotCompileException(e); 28969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 29069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 29169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal minfo.setCodeAttribute(code.toCodeAttribute()); 29269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return new CtMethod(minfo, field.getDeclaringClass()); 29369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 29469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 29569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 29669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Creates a method forwarding to a delegate in 29769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * a super class. The created method calls a method specified 29869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * by <code>delegate</code> with all the parameters passed to the 29969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * created method. If the delegate method returns a value, 30069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the created method returns that value to the caller. 30169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The delegate method must be declared in a super class. 30269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 30369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <p>The following method is an example of the created method. 30469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 30569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <ul><pre>int f(int p, int q) { 30669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * return super.f(p, q); 30769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * }</pre></ul> 30869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 30969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <p>The name of the created method can be changed by 31069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <code>setName()</code>. 31169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 31269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param delegate the method that the created method forwards to. 31369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param declaring the class to which the created method is 31469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * added. 31569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 31669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public static CtMethod delegator(CtMethod delegate, CtClass declaring) 31769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal throws CannotCompileException 31869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal { 31969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal try { 32069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return delegator0(delegate, declaring); 32169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 32269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal catch (NotFoundException e) { 32369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal throw new CannotCompileException(e); 32469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 32569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 32669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 32769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal private static CtMethod delegator0(CtMethod delegate, CtClass declaring) 32869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal throws CannotCompileException, NotFoundException 32969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal { 33069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal MethodInfo deleInfo = delegate.getMethodInfo2(); 33169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal String methodName = deleInfo.getName(); 33269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal String desc = deleInfo.getDescriptor(); 33369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal ConstPool cp = declaring.getClassFile2().getConstPool(); 33469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal MethodInfo minfo = new MethodInfo(cp, methodName, desc); 33569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal minfo.setAccessFlags(deleInfo.getAccessFlags()); 33669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 33769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal ExceptionsAttribute eattr = deleInfo.getExceptionsAttribute(); 33869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal if (eattr != null) 33969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal minfo.setExceptionsAttribute( 34069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal (ExceptionsAttribute)eattr.copy(cp, null)); 34169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 34269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal Bytecode code = new Bytecode(cp, 0, 0); 34369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal boolean isStatic = Modifier.isStatic(delegate.getModifiers()); 34469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal CtClass deleClass = delegate.getDeclaringClass(); 34569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal CtClass[] params = delegate.getParameterTypes(); 34669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal int s; 34769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal if (isStatic) { 34869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal s = code.addLoadParameters(params, 0); 34969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code.addInvokestatic(deleClass, methodName, desc); 35069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 35169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal else { 35269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code.addLoad(0, deleClass); 35369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal s = code.addLoadParameters(params, 1); 35469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code.addInvokespecial(deleClass, methodName, desc); 35569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 35669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 35769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code.addReturn(delegate.getReturnType()); 35869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code.setMaxLocals(++s); 35969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal code.setMaxStack(s < 2 ? 2 : s); // for a 2-word return value 36069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal minfo.setCodeAttribute(code.toCodeAttribute()); 36169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return new CtMethod(minfo, declaring); 36269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 36369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 36469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 36569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Creates a wrapped method. The wrapped method receives parameters 36669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * in the form of an array of <code>Object</code>. 36769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 36869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <p>The body of the created method is a copy of the body of the method 36969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * specified by <code>body</code>. However, it is wrapped in 37069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * parameter-conversion code. 37169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 37269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <p>The method specified by <code>body</code> must have this singature: 37369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 37469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <ul><code>Object method(Object[] params, <type> cvalue) 37569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * </code></ul> 37669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 37769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <p>The type of the <code>cvalue</code> depends on 37869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <code>constParam</code>. 37969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * If <code>constParam</code> is <code>null</code>, the signature 38069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * must be: 38169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 38269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <ul><code>Object method(Object[] params)</code></ul> 38369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 38469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <p>The method body copied from <code>body</code> is wrapped in 38569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * parameter-conversion code, which converts parameters specified by 38669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <code>parameterTypes</code> into an array of <code>Object</code>. 38769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The returned value is also converted from the <code>Object</code> 38869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * type to the type specified by <code>returnType</code>. Thus, 38969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the resulting method body is as follows: 39069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 39169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <ul><pre>Object[] params = new Object[] { p0, p1, ... }; 39269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <<i>type</i>> cvalue = <<i>constant-value</i>>; 39369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <i>... copied method body ...</i> 39469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Object result = <<i>returned value</i>> 39569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * return (<i><returnType></i>)result; 39669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * </pre></ul> 39769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 39869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <p>The variables <code>p0</code>, <code>p2</code>, ... represent 39969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * formal parameters of the created method. 40069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The value of <code>cvalue</code> is specified by 40169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <code>constParam</code>. 40269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 40369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <p>If the type of a parameter or a returned value is a primitive 40469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * type, then the value is converted into a wrapper object such as 40569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <code>java.lang.Integer</code>. If the type of the returned value 40669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * is <code>void</code>, the returned value is discarded. 40769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 40869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <p><i>Example:</i> 40969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 41069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <ul><pre>ClassPool pool = ... ; 41169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * CtClass vec = pool.makeClass("intVector"); 41269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * vec.setSuperclass(pool.get("java.util.Vector")); 41369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * CtMethod addMethod = pool.getMethod("Sample", "add0"); 41469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 41569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * CtClass[] argTypes = { CtClass.intType }; 41669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * CtMethod m = CtNewMethod.wrapped(CtClass.voidType, "add", argTypes, 41769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * null, addMethod, null, vec); 41869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * vec.addMethod(m);</pre></ul> 41969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 42069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <p>where the class <code>Sample</code> is as follows: 42169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 42269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <ul><pre>public class Sample extends java.util.Vector { 42369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * public Object add0(Object[] args) { 42469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * super.addElement(args[0]); 42569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * return null; 42669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * } 42769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * }</pre></ul> 42869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 42969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <p>This program produces a class <code>intVector</code>: 43069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 43169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <ul><pre>public class intVector extends java.util.Vector { 43269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * public void add(int p0) { 43369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Object[] args = new Object[] { p0 }; 43469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * // begin of the copied body 43569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * super.addElement(args[0]); 43669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Object result = null; 43769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * // end 43869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * } 43969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * }</pre></ul> 44069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 44169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <p>Note that the type of the parameter to <code>add()</code> depends 44269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * only on the value of <code>argTypes</code> passed to 44369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <code>CtNewMethod.wrapped()</code>. Thus, it is easy to 44469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * modify this program to produce a 44569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <code>StringVector</code> class, which is a vector containing 44669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * only <code>String</code> objects, and other vector classes. 44769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 44869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param returnType the type of the returned value. 44969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param mname the method name. 45069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param parameterTypes a list of the parameter types. 45169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param exceptionTypes a list of the exception types. 45269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param body the method body 45369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * (must not be a static method). 45469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param constParam the constant parameter 45569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * (maybe <code>null</code>). 45669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param declaring the class to which the created method is 45769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * added. 45869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 45969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public static CtMethod wrapped(CtClass returnType, 46069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal String mname, 46169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal CtClass[] parameterTypes, 46269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal CtClass[] exceptionTypes, 46369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal CtMethod body, ConstParameter constParam, 46469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal CtClass declaring) 46569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal throws CannotCompileException 46669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal { 46769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return CtNewWrappedMethod.wrapped(returnType, mname, parameterTypes, 46869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal exceptionTypes, body, constParam, declaring); 46969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 47069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal} 471