Java question

This forum is for anything that doesn't specifically have to do with Better Than Wolves
Post Reply
User avatar
FlowerChild
Site Admin
Posts: 18753
Joined: Mon Jul 04, 2011 7:24 pm

Java question

Post by FlowerChild »

Hey guys,

In a bit of a rarity, I wanted to throw a Java programming question out there. This represents a problem I've repeatedly encountered in trying to limit the number of base-class mods BTW makes, and I've dug around a bit online to see if there's a solution in the past to no avail.

What I'm wondering is if Java provides a means to call a super method *two levels* (or more) above the current one in the inheritance hierarchy. In other words, something along the lines of the following:

super.super();

This is entirely possible in C++, but I've yet to figure out a way to do it in Java, and I don't think there may even be one. I can obviously copy the code directly from two levels above, but this is less than optimal as it means that if there are any changes to the vanilla code I'm copying in the future (bug fixes or what have you), those won't be reflected in my copies unless I take specific note of them.

Anyways, if anyone knows of a way to do this, I'd be much obliged. After getting rid of most of the base class mods I was doing to blocks and items, I'd like to tackle entities next, but the above is making it far more annoying than it needs to be.
User avatar
FlowerChild
Site Admin
Posts: 18753
Joined: Mon Jul 04, 2011 7:24 pm

Re: Java question

Post by FlowerChild »

Sigh...I really doubt this is at all possible in Java. Just came across this:

http://stackoverflow.com/questions/5863 ... ed-in-java

Yeah...it's all well and good that it's not "proper" from an academic standpoint, but in real-world situations it's still sometimes the best solution. The example someone provides of working with an existing library where you can't change the code in the library yourself and need to bypass a level in the inheritance hierarchy is pretty much the same thing I'm running into here with the mod.

Sometimes Java's apparent obsession with forcing the "proper" approach as opposed to the realities of programming in various situations really pisses me off. This is definitely another +1 for C++ in my books as the language provides you with an abundance of power and it's left to the good judgement of the programmer to know when it is appropriate to use it.
User avatar
Syruse
Posts: 338
Joined: Mon Oct 03, 2011 4:15 pm

Re: Java question

Post by Syruse »

DogLove currently doesn't have access to his forum account, so, over in the IRC had this to say:
<DogLove>
In java, no, that is not possible using the language. However if you get access to the classloader at MC loading time then there is a way, such as how a Forge CoreMod works, but otherwise no.
Not sure if this was the answer you were looking for, but re requested that someone post it.
An IRC Conversation wrote: [19:24] <Ribky> on the bright side, i did my first modifications with the MCP today... :)
[19:24] <Horizon> what's it do?
[19:25] <Ribky> it eats your goddamn animals
User avatar
FlowerChild
Site Admin
Posts: 18753
Joined: Mon Jul 04, 2011 7:24 pm

Re: Java question

Post by FlowerChild »

Syruse wrote:Not sure if this was the answer you were looking for, but re requested that someone post it.
Fair enough. I think I answered my own question with the above, but I appreciate the response.

Actually, I think I went through this same process a couple of months ago and answered it for myself again then, but it's the kind of thing that pops up from time to time that leaves me wondering once again if I might have overlooked something and that it really is possible.

Kinda like passing by reference. It persistently gets under my skin that I can't explicitly state when I want to pass parameters by reference or value in Java, and it often leaves me saying "for fuck sake...I just want to pass a reference or pointer to this bloody int and be done with it", and instead end up wasting time (and CPU cycles) creating encapsulating classes to get the variables to a function in the form I require.

I REALLY don't like the way Java obfuscates what's actually a reference and what's a copy. Way too much hand-holding going on in this language for my taste.
User avatar
Syruse
Posts: 338
Joined: Mon Oct 03, 2011 4:15 pm

Re: Java question

Post by Syruse »

FlowerChild wrote: Fair enough. I think I answered my own question with the above, but I appreciate the response.
No problem, I really don't know jack shit about code so I was a little hesitant to post after your followup. :P
An IRC Conversation wrote: [19:24] <Ribky> on the bright side, i did my first modifications with the MCP today... :)
[19:24] <Horizon> what's it do?
[19:25] <Ribky> it eats your goddamn animals
User avatar
FlowerChild
Site Admin
Posts: 18753
Joined: Mon Jul 04, 2011 7:24 pm

Re: Java question

Post by FlowerChild »

While I'm ranting about Java, I may as well round it off with a complaint about the total lack of precompiler directives.

In particular, the inability to perform any kind of conditional compilation is something I personally offer thanks up to the Java gods for every time I perform a client to server port, the need for which could be almost entirely eliminated with a few simple "#ifdef" statements.

Again, it may not be "proper" but real world programming often isn't.
User avatar
thekyz
Posts: 266
Joined: Wed Jul 20, 2011 5:52 am
Location: Rennes, France

Re: Java question

Post by thekyz »

You can create a protected method in class B which calls A's method and can thus be called from C. Not perfect but I don't think what you want to do is achievable and it may be better than copying code ...
User avatar
FlowerChild
Site Admin
Posts: 18753
Joined: Mon Jul 04, 2011 7:24 pm

Re: Java question

Post by FlowerChild »

thekyz wrote:You can create a protected method in class B which calls A's method and can thus be called from C. Not perfect but I don't think what you want to do is achievable and it may be better than copying code ...
Not without modifying B, which is what I'm trying to avoid with this in the first place.

That's pretty much the problem with all such workarounds that I've seen.
User avatar
thekyz
Posts: 266
Joined: Wed Jul 20, 2011 5:52 am
Location: Rennes, France

Re: Java question

Post by thekyz »

FlowerChild wrote:Not without modifying B, which is what I'm trying to avoid with this in the first place.

That's pretty much the problem with all such workarounds that I've seen.
I guessed that was the problem :(
FlowerChild wrote:While I'm ranting about Java, I may as well round it off with a complaint about the total lack of precompiler directives.

In particular, the inability to perform any kind of conditional compilation is something I personally offer thanks up to the Java gods for every time I perform a client to server port, the need for which could be almost entirely eliminated with a few simple "#ifdef" statements.
I use those kind of things when coding java to test some bits of code:
http://stackoverflow.com/questions/1344 ... eprocessor

It's nowhere near the power of the C preprocessor alas, but the compiler will actually leave the code out.

Sorry if i'm being mister obvious there, I have never looked at your code so I don't know how much Java you actually know.
User avatar
FlowerChild
Site Admin
Posts: 18753
Joined: Mon Jul 04, 2011 7:24 pm

Re: Java question

Post by FlowerChild »

thekyz wrote:It's nowhere near the power of the C preprocessor alas, but the compiler will actually leave the code out.

Sorry if i'm being mister obvious there, I have never looked at your code so I don't know how much Java you actually know.
No, that will still generate compile errors when the same code is used in different contexts, and even cause crashes if the code contained within the if is invalid at run-time, which is what I'm trying to avoid.

For example, I just tried the following:

Code: Select all

private static final boolean TEST = false;

if ( TEST )
{
    dahsgsdjkh();
}
and it resulted in both a compiler error, and a crash when I ignored the error and ran the code regardless.

I'm primarily a C++ programmer, having only picked up Java when I started coding BTW, so I certainly don't consider myself a Java guru, but the above is a tad obvious, and not what I was looking for ;)
Dr. Kylstein
Posts: 67
Joined: Thu Feb 09, 2012 8:05 pm

Re: Java question

Post by Dr. Kylstein »

Perhaps you could modify the build script to run the files through gcc before compiling.
whitechaos35
Posts: 205
Joined: Wed Nov 30, 2011 4:19 am

Re: Java question

Post by whitechaos35 »

I have a python script that will handle some basic preprocessor commands for Java files. It will detect if there are any changes to the working copy, and if not, will modify the java files based on input values, compile the files, then revert the working copy back. There may be something online that you can find for your environment. If not, I can send you my script. It was written with mercurial in mind, but you can change it to whatever your environment is.
User avatar
BinoAl
Posts: 2552
Joined: Mon Jul 04, 2011 9:39 pm
Location: Everywhere.

Re: Java question

Post by BinoAl »

FlowerChild wrote:+1 for C++
You mean a... C++++?
*Badum-Tsss*
Image
ialdbaoloth
Posts: 137
Joined: Sat Nov 12, 2011 2:46 am

Re: Java question

Post by ialdbaoloth »

FlowerChild wrote:What I'm wondering is if Java provides a means to call a super method *two levels* (or more) above the current one in the inheritance hierarchy.
In case you might be willing to mess with bytecode, the JVM actually supports this without classloaders or runtime bytecode transformers. For backwards compatibility, invokespecial can call methods on deeper ancestors if your class file has the ACC_SUPER flag cleared (longer nicer explanation). Javac won't do that, but I put together a little example where class B (compiled by jasmin and postprocessed) extends A extends Object, A overrides toString, and B calls Object::toString on itself. I haven't heard of anyone using this in anger, but some signs are good - that test jar works for me on windows and a few linux JVMs, a modlet with this sort of modified class file survives MCPs reobfuscate, and the behavior is promised in the spec (for backwards compatibility, which Java people seem pretty serious about).
Post Reply