Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How to detect initialization order of fields?
How to detect initialization order of fields? [message #1794235] Mon, 27 August 2018 19:03 Go to next message
Sergey Toshin is currently offline Sergey ToshinFriend
Messages: 56
Registered: May 2015
Member
Hi, I have a testing class like it

class Test {
private Container c1 = new Container(this.c2);
private Container c2 = new Container();
}

In the provided example c2 should be initialized before c1 (doesn't matter of their declaration order), but are there any default Eclipse JDT methods/APIs that can provide me that order? Or such order doesn't exists, but it's created in runtime (e.g. c1 is being initialized, but stopped until c2 is initialized)?
Re: How to detect initialization order of fields? [message #1794238 is a reply to message #1794235] Mon, 27 August 2018 22:00 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Field initialization is performed in the order of declarations in the source code (see JLS §12.5 step 4).

Note, that removing the "this." prefix from the forward reference to c2, yields this error:
        private Container c1 = new Container(c2);
                                             ^^
Cannot reference a field before it is defined


IOW, using "this.c2" is a trick to bypass the rules from JLS §8.3.3.
Re: How to detect initialization order of fields? [message #1794277 is a reply to message #1794238] Tue, 28 August 2018 12:11 Go to previous messageGo to next message
Sergey Toshin is currently offline Sergey ToshinFriend
Messages: 56
Registered: May 2015
Member
Thanks, but it's description how it should work anywhere, but not how it really works in Eclipse. I'm using Eclipse JDT in my own project, and I have FieldDeclaration[] for a class, so I want to resolve order of initializations (because previously I initialized fields by ascending of their array indexes, but it appeared to be wrong, because some fields are referenced before they were initialized in my tool). That's why I'm trying to find a solution how using JDT I can resolve that order
Re: How to detect initialization order of fields? [message #1794279 is a reply to message #1794277] Tue, 28 August 2018 12:16 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Sergey Toshin wrote on Tue, 28 August 2018 14:11
Thanks, but it's description how it should work anywhere, but not how it really works in Eclipse.


What exactly is not working that way in Eclipse?
Do we generate wrong byte code?

Quote:

I'm using Eclipse JDT in my own project, and I have FieldDeclaration[] for a class, so I want to resolve order of initializations


Are you interested in the correct order according to JLS (textual source code order), or the semantically meaningful order (with no dereference before initialization)?

As mentioned, when saying "this.c2" this is not guaranteed to be the same order, but when saying "c2", differences between both orders will raise a compile error.
Re: How to detect initialization order of fields? [message #1794280 is a reply to message #1794279] Tue, 28 August 2018 12:19 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
IOW: Eclipse doesn't compute the meaningful order of fields, it only checks if the actual textual order violates that order wrt simple name references (where "this.c2" is *not* a simple name reference).
Re: How to detect initialization order of fields? [message #1794282 is a reply to message #1794280] Tue, 28 August 2018 12:32 Go to previous messageGo to next message
Sergey Toshin is currently offline Sergey ToshinFriend
Messages: 56
Registered: May 2015
Member
Quote:
the semantically meaningful order (with no dereference before initialization)

I believe I want that. So in the provided example
class Test {
    private Container c1 = new Container(this.c2);
    private Container c2 = new Container();
}

Field declarations should be sorted to
1. private Container c2 = new Container();
2. private Container c1 = new Container(this.c2);
Re: How to detect initialization order of fields? [message #1794284 is a reply to message #1794282] Tue, 28 August 2018 12:40 Go to previous messageGo to next message
Sergey Toshin is currently offline Sergey ToshinFriend
Messages: 56
Registered: May 2015
Member
I had idea to retrieve all SimpleNames (which are variable names) in initializers using a visitor, and then sort them to ensure that dependencies are already initialized. But it seems to be monkey codes, so I wanted to know is there a better approach
Re: How to detect initialization order of fields? [message #1794285 is a reply to message #1794282] Tue, 28 August 2018 12:41 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Sergey Toshin wrote on Tue, 28 August 2018 14:32

Field declarations should be sorted to
1. private Container c2 = new Container();
2. private Container c1 = new Container(this.c2);


There is no unique order fulfilling your criteria, and in case of cycles there may not even exist any solution.

To implement your own strategy you'd need to get a resolved AST (i.e., with bindings) and search for field references inside initializers by yourself (using bindings is more precise than relying on names). This is the preferred way if you want to solve this automatically.

A human should simply remove the "this." prefix and change order until it's accepted by the compiler. At that point you will have an order that fulfills your criteria.

Re: How to detect initialization order of fields? [message #1794296 is a reply to message #1794285] Tue, 28 August 2018 14:03 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

I think you are assuming that Java is going to be very clever when it only requires you the programmer to be competent.

In general "private Container c1 = new Container(this.c2);" does not work since "this" has yet to be fully initialized. For non-trivial initializations you need to sequence them as constructor statements rather than field initializers. It is then very obvious what has been subtly sequenced. Usually attempts to use "this" during construction are discouraged/inhibited since use of a partially constructed "this" is dangerous. However in some circumstances, particularly with the aid of a static method you can use a partially constructed "this" that violates OO integrity but may be useful when you know what you are doing. If Java prohibited such well-designed hazardous usage that would be painful. e.g. A has a final @NonNull reference to B and vice-versa.

Regards

Ed Willink
Re: How to detect initialization order of fields? [message #1794371 is a reply to message #1794296] Wed, 29 August 2018 16:18 Go to previous message
Sergey Toshin is currently offline Sergey ToshinFriend
Messages: 56
Registered: May 2015
Member
Thanks! Now I'm resolving dependencies in the initializer part, so it seems to be a solved problem
Previous Topic:Installing Java on Windows for all users
Next Topic:Changing VM Arguments field doesn't offer to save run configuration
Goto Forum:
  


Current Time: Thu Apr 25 17:38:08 GMT 2024

Powered by FUDForum. Page generated in 0.03472 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top