[英]Eclipse debug mode changing variables
当使用 Eclipse IDE 在 Java 中编程时,我有时会使用调试 function。很多时候我喜欢在程序运行时实时更改变量。 然而,我经常发现改变一些变量实际上不会影响当前运行的程序。
我的问题是:是否有一定的调试规则? scope 中哪些变量是调试器之类的?
如果这是一个愚蠢的问题,我很抱歉。 我对 Eclipse 中的调试和一般编程还很陌生。
下面的代码是一个示例。 如果它难以阅读或其他什么,我很抱歉,但问题是:在 Ball class 中,每当我更改最终变量(如PARTICLES_PER_CLICK
或SPEED
)时,它们都会实时更新,我可以看到程序 window 中的差异,但是,当我更改RADIUS
变量时,它什么也不做,即使它与其他两个变量位于相同的 class 中。
public class Main {
public static final int WIDTH = 1280;
public static final int HEIGHT = 720;
public static Ball[] ball = new Ball[100000];
public Main() {
try {
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.create();
Display.setTitle("Game Engine");
} catch (LWJGLException e) {
e.printStackTrace();
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
boolean[] doOnce = new boolean[10];
boolean gravity = false;
while (!Display.isCloseRequested()) {
if (Mouse.isButtonDown(1) || Mouse.isButtonDown(1)) {
if (!doOnce[0]) {
Ball.createParticles();
doOnce[0]=true;
}
} else {
doOnce[0] = false;
}
glClear(GL_COLOR_BUFFER_BIT);
for (int i = 1; i <= Ball.ballAmount; i++) {
ball[i].updatePosition(gravity);
if (Mouse.isButtonDown(0)) {
gravity = true;
} else {
gravity = false;
}
if (ball[i].position.x > 0 && ball[i].position.y > 0 && ball[i].position.x < WIDTH && ball[i].position.y < HEIGHT) {
glBegin(GL_TRIANGLE_FAN);
glVertex2d(ball[i].position.x, ball[i].position.y);
for(int u=0;u<=360;u+=5){
glVertex2d(ball[i].position.x+Math.cos(u)*Ball.RADIUS, ball[i].position.y+Math.sin(u)*Ball.RADIUS);
}
glEnd();
}
}
System.out.println("Particle Amount: " + Ball.ballAmount);
Display.update();
Display.sync(60);
}
Display.destroy();
System.exit(0);
}
public static void main(String[] args) {
new Main();
}
}
class Ball {
public Vector2f position, velocity;
public static final int RADIUS = 10;
public static final int INITIAL_SPEED = 5;
public static final int SPEED = 2;
public static final int PARTICLES_PER_CLICK = 50;
public static int ballAmount = 0;
public double r, g, b;
public static Random rnd = new Random();
public Ball(double x, double y) {
int angle = rnd.nextInt(360);
position = new Vector2f((float) x, (float) y);
velocity = new Vector2f((float) Math.cos(angle) * rnd.nextFloat() * INITIAL_SPEED, (float) Math.sin(angle) * rnd.nextFloat() * INITIAL_SPEED);
this.r = rnd.nextDouble();
this.g = rnd.nextDouble();
this.b = rnd.nextDouble();
}
public void updatePosition(boolean gravity) {
this.position.x += this.velocity.x * SPEED;
this.position.y += this.velocity.y * SPEED;
if (gravity) {
double dx = this.position.x - Mouse.getX();
double dy = this.position.y - (Main.HEIGHT - Mouse.getY());
double distance = Math.sqrt(dx * dx + dy * dy);
this.velocity.x -= (this.position.x - Mouse.getX()) / distance;
this.velocity.y -= (this.position.y - (Main.HEIGHT - Mouse.getY())) / distance;
} else {
this.velocity.x *= 0.99;
this.velocity.y *= 0.99;
}
}
public static void createParticles() {
for (int i = 1; i <= PARTICLES_PER_CLICK; i++) {
ballAmount += 1;
Main.ball[ballAmount] = new Ball(Mouse.getX(),Main.HEIGHT- Mouse.getY());
}
}
}
如果优化器看到一个最终变量,它将知道它的值不会改变。 这些知识的作用取决于它。 它可能无能为力(就像在您的情况下似乎发生在PARTICLES_PER_CLICK或SPEED上一样),或者它可能只是用所有地方的实际值替换了该变量的所有出现。 除了没有更改最终变量的值之外,没有特殊的规则。
没有特殊规则,除了不改变final变量的值。
实际上,Eclipse 4.23(2022 年第二季度,七年后)现在更清楚了:
关于更改最终字段的警告
由于 Eclipse 3.1 Eclipse Java 调试器允许更改最终字段值。
虽然在技术上可行,但此类更改的后果并非微不足道,可能会影响看似无关的代码并导致各种危险后果。
因此,使用 Eclipse 4.23 Java 调试器现在显示一个新警告:
默认情况下启用此警告,可以通过首选项禁用:
此外,“
org.eclipse.debug.ui.variableValueEditors
”扩展点已更新,以允许自定义产品为现有调试模型贡献自己的“variableValueEditor
”实现,并对final
字段修改有更多控制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.