gpt4 book ai didi

java - 查看构造函数是否执行成功

转载 作者:行者123 更新时间:2023-12-02 11:30:14 25 4
gpt4 key购买 nike

所以我再次使用java,在这样做时,我遇到了一个有趣的问题。

我正在尝试为自己编写一个小注册服务。如果某个类型的对象通过其构造函数实例化,它将从我的注册服务中检索一个 ID,并将其添加到对象的服务列表中。

这是一个示例对象

public class Test {

private final Long id;

public Test() {
this.id = TestRegistration.register(this);
throw new IllegalAccessError();
}

public Long getId() {
return this.id;
}
}

这是一个示例服务

public class TestRegistration {

private final static Map<Long, Test> registration = new HashMap<>();

protected final static long register(final Test pTest) {
if (pTest.getId() != null) {
throw new IllegalStateException();
}
long freeId = 0;
while (registration.containsKey(freeId)) {
freeId = freeId + 1;
}
registration.put(freeId, pTest);
return freeId;
}

protected final static Test get(final long pId) {
return registration.get(pId);
}
}

现在如您所见,我的类 Test 的构造函数根本无法成功执行,因为它总是抛出 IllegalAccessError。但是在引发此错误之前,我的 TestRegistration 类的方法 register(...) 在构造函数中被调用。在此方法中,它被添加到注册Map中。例如,如果我运行这段代码

public static void main(final String[] args) {
try {
final Test t = new Test();
} catch (final IllegalAccessError e) {

}
final Test t2 = TestRegistration.get(0);
System.out.println(t2);
}

我的TestRegistration实际上包含调用我的Test类的构造函数时创建的对象,我可以(这里使用变量t2 )访问它,即使它最初没有成功创建。

我的问题是,我能否以某种方式从 TestRegistration 类中检测 Test 的构造函数是否成功执行,没有任何异常或其他中断?

在你问我为什么首先抛出这个异常之前,这是我的答案。测试可能有我还不知道的潜在子类,但仍将在我的 TestRegistration 类中注册。但由于我对这些子类的结构没有影响,我无法判断它们的构造函数是否会抛出异常。

最佳答案

似乎不可能。

我已经从下面的 JLS 中收集到了最好的信息。

从中可以看出,构造函数体的执行是对象创建的最后一步。

因此,我没有看到任何证据表明引发异常会导致任何不会发生的事情,您可以检查执行是否成功(除了其余的部分)显然是构造函数的主体)。

这可能看起来不是特别令人信服,尽管我不相信您会找到比这更具体的东西(毕竟,证明某些东西不存在是出了名的困难)。

<小时/>

我建议将修改外部数据的逻辑放在其他地方。

要求在构造对象后显式调用 register 方法(在 TestRegistration 类中或在对象本身中)并非完全不合理。

它也可以工作,而不是直接调用构造函数,有一个包含返回对象的方法的类(这听起来很像工厂设计模式),然后你可以在其中放置这个外部修改逻辑。

<小时/>

来自 JLS:

15.9.4. Run-Time Evaluation of Class Instance Creation Expressions

At run time, evaluation of a class instance creation expression is as follows.

First, if the class instance creation expression is a qualified class instance creation expression, the qualifying primary expression is evaluated. If the qualifying expression evaluates to null, a NullPointerException is raised, and the class instance creation expression completes abruptly. If the qualifying expression completes abruptly, the class instance creation expression completes abruptly for the same reason.

Next, space is allocated for the new class instance. If there is insufficient space to allocate the object, evaluation of the class instance creation expression completes abruptly by throwing an OutOfMemoryError.

The new object contains new instances of all the fields declared in the specified class type and all its superclasses. As each new field instance is created, it is initialized to its default value (§4.12.5).

Next, the actual arguments to the constructor are evaluated, left-to-right. If any of the argument evaluations completes abruptly, any argument expressions to its right are not evaluated, and the class instance creation expression completes abruptly for the same reason.

Next, the selected constructor of the specified class type is invoked. This results in invoking at least one constructor for each superclass of the class type. This process can be directed by explicit constructor invocation statements (§8.8) and is specified in detail in §12.5.

The value of a class instance creation expression is a reference to the newly created object of the specified class. Every time the expression is evaluated, a fresh object is created.

<小时/>

12.5. Creation of New Class Instances

...

Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:

  1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.

  2. If this constructor begins with an explicit constructor invocation (§8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.

  3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.

  4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.

  5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.

关于java - 查看构造函数是否执行成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49350193/

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