我想知道当使用反射来获取某些字段值时,垃圾回收是如何工作的。在没有使用正式语言语法访问它们的情况下,JVM如何知道这些字段引用的值是可访问的,因此当前不符合垃圾回收的条件?
以下是描述该问题的小片段(虽然这里过分强调了反射):
以下是描述该问题的小片段(虽然这里过分强调了反射):
/**
*
*/
import java.lang.reflect.Field;
public class B {
protected B previous = null, next = null;
/**
*
*/
public B(B from) {
this.previous = from;
}
public void transition(B to) {
this.next = to;
}
public B next() {
try {
Field f = getClass().getField("next");
f.setAccessible(true);
try {
return (B)f.get(this);
} finally {
f.setAccessible(false);
}
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
public B previous() {
try {
Field f = getClass().getField("previous");
f.setAccessible(true);
try {
return (B)f.get(this);
} finally {
f.setAccessible(false);
}
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
祝福您,
克里斯