gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 03:55:21 25 4
gpt4 key购买 nike

给定一个 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;
}

所讨论的范围是方法调用语句 LOGGER.debug(STR1); 在 catch block 中的范围。结果:“sb4”

关于java - 有没有办法在给定范围内生成唯一的变量名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8910419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com