使用java代码构造字符串的简洁方法是什么?

时间:2015-08-02 05:45:39

标签: java arrays reflection

我正在尝试使用Java代理为每个方法注入一些额外的代码。到目前为止,我正在编写类似这样的代码:

String signature = method.getSignature();
method.insertBefore("System.err.println(\"" + signature + "\");");

是否有更简洁的Java语法编写方式?类似于available for SQL的东西会很有用。

更新:例如,创建System.err.println,类似于createClass(" System")。createField(" out")。createMethod ("的println&#34)。

2 个答案:

答案 0 :(得分:1)

我相信你正在寻找String.format(String, Object...)之类的东西,比如

method.insertBefore(String.format("System.err.println(\"%s\");", signature));

答案 1 :(得分:1)

你可以自己做一个实用工具:

package ramin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import com.google.common.base.Joiner;

public class AraniUtil{

    private StringBuilder fullyQualifiedName = new StringBuilder("");//fully qualified name

    /*
     * Parameters :
     * List classes : a list of classes to build in String type
     * List fields : a list of fields to build in String type
     * HashMap<String, List> methodsAndSignatures (or "mas") : a hashmap where maps method name to a list of arguments in String Type
     */
    public AraniUtil(List classes, List fields, HashMap<String, List> mas)
    {
        //list of manipulated methods with their arguments
        List methods = new ArrayList();
        Iterator iterator = mas.keySet().iterator();//method names iterator
        for (Iterator i = iterator; iterator.hasNext(); ) {

            Object next = i.next();
            StringBuilder mapped = new StringBuilder("");
            mapped.append((String) next);


            Iterator signatures = mas.get(next).iterator();
            List tempSignature = new ArrayList();
            for (Iterator j = signatures; j.hasNext(); ) {
                next = j.next();
                if (next.getClass().equals(String.class))
                    //mapped.append("\"" + next + "\"");
                    tempSignature.add("\"" + next + "\"");
                else
                    tempSignature.add(next);
                    //mapped.append(next);
            }
            mapped.append("(" + Joiner.on(",").join(tempSignature) + ")");
            methods.add(mapped.toString());
        }
        fullyQualifiedName.append(Joiner.on(".").join(classes) + ".");
        fullyQualifiedName.append(Joiner.on(".").join(fields) + ".");
        fullyQualifiedName.append(Joiner.on(".").join(methods));
    }

    @Override
    public String toString(){
        return this.fullyQualifiedName.toString();
    }

    public static void main(String[] args) {
        //initializing classes to create
        List classes = new ArrayList<String>();
        classes.add("System");
        classes.add("System2");
        //initializing fields to create
        List fields = new ArrayList<String>();
        fields.add("out");
        fields.add("out2");
        HashMap methodsAndSignatures = new HashMap();
        //creating method signatures
        List tempList = new ArrayList();
        tempList.add("arg1");
        tempList.add(6956);
        //map a method to array of arguments
        methodsAndSignatures.put("println", tempList);
        //creating method signatures
        List tempList2 = new ArrayList();
        tempList2.add("arg2");
        tempList2.add(6956555);
        //map a method to array of arguments
        methodsAndSignatures.put("println2", tempList2);
        //actual utility function
        AraniUtil araniUtil = new AraniUtil(classes, fields, methodsAndSignatures);
        //and the result
        System.out.println(araniUtil);
    }
}

我写了这个我的自我,并鼓励我的朋友重构/编辑它。

这会输出System.System2.out.out2.println("arg1",6956).println("arg2",6956555)

这非常有效:)