有没有办法在给定范围内生成唯一的变量名称?

时间:2012-01-18 12:55:17

标签: java java-compiler-api

给定Scope,是否有一个函数可以生成一个唯一的变量名,以便可以在范围中插入唯一名称的变量声明,结果源代码/ CompilationUnitTree仍然可以编译?

1 个答案:

答案 0 :(得分:0)

我最终编写了自己的效用函数:

import com.sun.source.tree.Scope;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import static javax.lang.model.element.ElementKind.*;
import javax.lang.model.element.Name;
import javax.lang.model.util.Elements;
import org.apache.commons.lang3.StringUtils;

public class ScopeUtils {
    private Elements elements;

    public ScopeUtils(Elements elements) {
        this.elements = elements;
    }

    public Name generateUniqueName(Scope scope, CharSequence prefixCs) {
        String prefix = prefixCs.toString(); // https://issues.apache.org/jira/browse/LANG-786
        int i = 0, j = 0;

        Scope enclosingScope;
        for (; scope != null && (enclosingScope = scope.getEnclosingScope()) != null; scope = enclosingScope) {
            for (Element e : scope.getLocalElements()) {
                ElementKind kind = e.getKind();
                String simpleName = e.getSimpleName().toString();
                if (kind == LOCAL_VARIABLE ||
                        kind == PARAMETER ||
                        kind == EXCEPTION_PARAMETER ||
                        kind == TYPE_PARAMETER ||

                        kind == CLASS ||
                        kind == INTERFACE ||
                        kind == ENUM ||
                        kind == ANNOTATION_TYPE) {
                    if (StringUtils.startsWith(simpleName, prefix)) {
                        if (StringUtils.equals(simpleName, prefix)) {
                            i = Math.max(i, j + 1);
                        } else {
                            try {
                                j = Math.max(j, Integer.parseInt(simpleName.subSequence(prefix.length(), simpleName.length()).toString(), 10));
                            } catch (NumberFormatException ex) {
                                continue;
                            }

                            if (i > 0) {
                                i = Math.max(i, j + 1);
                            }
                        }
                    }
                } else {
                    assert kind == FIELD && (StringUtils.equals(simpleName, "super") || StringUtils.equals(simpleName, "this"));
                }
            }
        }

        return elements.getName(i <= 0 ? prefix : String.format("%s%d", prefix, i));
    }
}

使用以下测试源文件测试:

package test.mytest;

import org.slf4j.Logger/*INTERFACE*/;
import org.slf4j.LoggerFactory/*CLASS*/;
import test.mytest.MyEnum/*ENUM*/;
import static test.mytest.MyEnum.*;

@interface MyAnnotation/*ANNOTATION_TYPE*/ { }

interface MyInterface/*INTERFACE*/ { }

class MyClass/*CLASS*/ { }

public class App/*CLASS*/ {
    private static final Logger LOGGER = LoggerFactory.getLogger(App.class);

    private static final String STR1 = "test";

    private StringBuilder sb10 = new StringBuilder();

    //super/*FIELD*/
    //this/*FIELD*/

    public <T/*TYPE_PARAMETER*/> void test(T obj/*PARAMETER*/) {
        MyEnum e/*LOCAL_VARIABLE*/ = TEST1;
        switch (e) {
            case TEST1:
                StringBuilder sb/*LOCAL_VARIABLE*/ = new StringBuilder();
                StringBuilder sb1/*LOCAL_VARIABLE*/ = new StringBuilder();
                obj = null;
                if (obj == TEST1) { }
                try {
                    obj.toString();
                } catch (NullPointerException npe/*EXCEPTION_PARAMETER*/) {
                    StringBuilder sb3/*LOCAL_VARIABLE*/ = new StringBuilder();
                    final String str/*LOCAL_VARIABLE*/ = "hi!";
                    LOGGER.debug(STR1);
                }
            default:
                break;
        }
    }

    public static void main(String[] args) {
        new App().<String>test("test");
    }
}

其中MyEnum被声明为:

package test.mytest;

public enum MyEnum {
    TEST1, TEST2;
}

有问题的范围是catch块中方法调用语句LOGGER.debug(STR1);的范围。结果:"sb4"