Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: gen.java | .length, .class 属性需特殊处理 #133

Open
Guitenbay opened this issue Jun 23, 2022 · 0 comments
Open

bug: gen.java | .length, .class 属性需特殊处理 #133

Guitenbay opened this issue Jun 23, 2022 · 0 comments

Comments

@Guitenbay
Copy link

在以下的代码中,code2graph 会报错

int[] test = new int[3];
test[0] = 1;
test[1] = 2;
test[2] = 3;
System.out.println(test.length);

原因是 test.lengthlength 找不到它的 Class

实际上,数组的 length 属性是 Java 编译器在编译时给加上的,为其分配了 1 个字长的内存空间,
用于存储数组的长度。这个 length 属性相当于数组对象中一个 public final static int length = ?; 的属性

Java 中还有一个跟数组 length 属性类似的,也就是类的 class 属性,比如 String.class,
这个 class 也是编译器在编译时动态给加上的

.length .class 这类编译时才添加的属性,在 jdt 中是没有 Class 的
因此:

varBinding.getDeclaringClass() == null

解决方法:
在 gen.java 的 edu.pku.code2graph.gen.jdt.ExpressionVisitor.java 中 1120 行后添加判断 varBinding.getDeclaringClass() 是否为 null

if (varBinding.getDeclaringClass() != null) {
  usePool.add(
      Triple.of(
          root,
          EdgeType.REFERENCE,
          varBinding.getDeclaringClass().getQualifiedName()
              + "."
              + varBinding.getName()));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant