给定一个 Scope
,是否有一个函数可以生成一个唯一的变量名,以便可以在作用域中插入一个唯一名称的变量声明,并且生成的源代码/ CompilationUnitTree
仍然可以编译?
最佳答案
我最终编写了自己的实用程序函数:
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"
。关于java - 有没有办法在给定范围内生成唯一的变量名称?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8910419/