How to Learn how a library works - java

This is kind of an abstract question.
In general, how do I go about learning a library? I don't mean learning how to use it, I mean learning the design and the details of the interworkings.
For some more detail about what I mean, here is the situation that prompted me to ask this question.
My service has the ability to record a call's dependent data into a json blob. I'm working on some code that reads in the json blob, reconstructs the objects in the blob, and replays the call from the blob. The issue that I'm running into is that when one of the objects is being reconstructed I'm getting an exception that says "Field X cannot be null," but the field is not null in the json blob so obviously something is wrong. The stack trace is about 15 levels deep inside the Jackson library, so it's not easy to tell what's going wrong.
I honestly don't mind learning how the library works to fix this problem. I think it will be interesting. I just don't know where to start. I feel like I was just handed a big project full of files with no direction other than that, at a high level, I can imagine how serializing and deserializing Java objects would work.

In general, how do I go about learning a library? I don't mean
learning how to use it, I mean learning the design and the details of
the interworkings.
Generally, mature libraries (like Jackson as you mentioned in your question) have good introductions in their documentation as to their design. You can then drill down deeper for "the details of the interworkings" going as far as source code if needed.
I think a good general order in learning a library is the following
1. Introductory/high level/design view
2. More specific sections
3. Source code (rarely should have to go to this level)
I generally don't find myself as low as the source code view when learning a library...after all, if you're down at that level, one could argue you could write the code yourself...libraries are there to abstract away all the code needed to accomplish a task (like JSON data-binding in the case of Jackson).
I like how the jackson-databind page lays things out nicely in a general-specific order...you can get up and running right away and drill deeper if needed further down the page.
Give yourself time to learn a library...it doesn't happen overnight...

In general, how do I go about learning a library? I don't mean learning how to use it, I mean learning the design and the details of the interworkings.
In general, you don't. That's the point. A well-written library has a stable and well-documented interface. When it comes to Java libraries, "well-documented" means exactly one thing: Javadoc. You can browse the Javadoc to get an overview of the library, and you can attach the Javadoc to your project in your IDE so you can view the documentation for a class or method just by hovering your mouse pointer over it. The library's clients (the programs that use it) and the programmer writing them should not know or care how it works internally. If you know how it works, you may come to depend on how it works, and then your programs will break when it changes.
If you want to make sure a library isn't doing something stupid or malicious, or you want to study it for educational purposes, the best thing to do is read the source.

I think the best way is reading the Library's source code not just reading reference file.I suggest you use Java Decompiler or Jad tools to get source code.

Related

How should I create a Java application, where do I place the class?

First of all, I know how to build a Java application. But I have always been puzzled about where to put my classes. There are proponents for organizing the packages in a strictly domain oriented fashion, others separate by tier.
I have a problems with
naming and placing
So,
Where do you put your domain specific constants?
Where do you place class for stuff which is both infrastructural and domain specific (for instance I have a FileStorageStrategy class, which stores the files either in the database, or alternatively in database)?
thx
Here's an excellent approach to coming up with your own style and approach to class naming and organization style:
Find a great IDE. I really like Eclipse. It provides all of the features that follow.
Identify a naming style and organizational layout. This is an inductive approach to developing your own effective best practices.
Ask yourself: does #2 help you or are you fighting with it?
Archive your system frequently. Use a great configuration management system like git or GitHub! It will make #5 easier.
Make small changes. The IDE should allow you to rename class at any point of your system and make the changes globally. Moving classes should be equally easy. Doing this will prevent getting hamstrung by analysis: analysis paralysis.
Loop to #2. Yes, infinite loop. I've been doing this loop for a very long time. (Duh, it's an infinite loop!) In fact, sometimes I thrash: I flip-flop between 2 styles.
While in your infinite loop, examine online style guides and coding standards. Google and NASA JPL have great ones. Also, look at the great open-source APIs to see how the community of developers name and organize. If someone else is going to work with your code, you don't want to confuse them in any way. Worst case, they'll ignore what you did and rewrite it.
Don't be afraid to experiment. Don't be afraid to make mistakes. Don't always rely on what others do or have done. Good luck.

How should I design a code-block editor in the style of Lego Mindstorms?

I have started a project where I need to design an application that will work as a code-block editor, much like Lego Mindstorms and Blockly, a google project.
What are the possible approaches for designing such an application in Java?
More specifically, assuming I have one block A, how can I define which other blocks can be connected with it? What I think of is using subclasses and checking the hierarchy, or another possible solution could be some kind of 2D table that will verify a connection is valid(although I think it might get too big?).
p.s: I hope this is within the scope of SO questions. If not please point me to the right direction.
Lego Mindstorms is basically a stripped-down modified version of National Instruments' LabVIEW programming language.
I think the project you are trying to accomplish will be quite involved. This will not be an easy task to do right unless you only have a very limited number of actions that the code which you can build with your editor will be able to perform.
Essentially, you are trying to develop an entirely new programming language here. And before even thinking about the details of implementing the GUI, you need to pin down the "grammar" and other semantics of the language you are designing. For example, are you going to support for loops? while loops? case/switch statements? if statements? gotos? throwing and catching exceptions?
I'm not sure what your requirements and resources are, but you might be better off trying to call National Instruments to see if they will build something like the MindStorms IDE for/with you or seeing if there are other open source languages out there that you can modify to fit your needs.
There is an open LabVIEW clone, but it's a German page: http://www.myopenlab.de/.
Here are a couple of others: http://en.wikipedia.org/wiki/Visual_programming_language.
If you are not beholden to graphical approaches, and as I am unsure how Lego MindStorms works, it is possible that ANTLR may qualify as a solution.
"ANTLR is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. It's widely used to build languages, tools, and frameworks. From a grammar, ANTLR generates a parser that can build and walk parse trees."
I am doing a similar thing my C# project. I used a tree view control, and I allowed the user to add actions which would show up in the tree view, and the program would run through the tree view nodes, calling their actions. Behind the scenes, I used this pattern. Comment if you need any more help.
Why not just use Blockly? That is what it is intended for, after all. It is available under the Apache License on gitub. If you don't want to use Blockly, please let us know how Blockly doesn't fit your needs. :-)

Java Monkey Engine Tutorial

I've worked my way through the tutorial series of jme and feel like I've understood the basics. Now I would like to create a first very simple game. Nothing fancy just a simple test scenario: The player should be able to move an object. Then I would like to expand it. However I don't know how to start. It's hard to explain but I just don't know where to put my code. The manual says that there are "controls" (implemented as interfaces) that let you hook your code up to frequent update calls or other events (that's what I understood) but I can't grasp how to use them.
More detailed description. You could skip this:
To get the point clear - I'm coming from the engine Unity3d. The usage was quite pleasant, the community helpful and the documentation great. But you have to spend about 1000$ to get the full version and be allowed to use pro features (shadows, custom shaders, some advanced tools,...). After a while the temptation of open-source was simply to big. In unity3d you basically have an advanced 3d editor and a scene graph. You can place objects in the scene and access and modify them over the scene graph. You can use predefined objects such as lights or particle systems and change them to suit your needs but most things you use are custom made and puzzled together by different components: a mesh, a collider, a few scripts. And this is where your code comes to work. In unity3d your program is split into small scripts and these are added to the objects of each scene. The scripts have access to the underlying engine, the properties of their object and can use a special framework for math, networking,... Each script can also define functions to be called in certain events: update(), onCollisionEnter(Collider other), onTriggerEnter(Collision other), ...
This was extremely easy to use. Admittedly huge architectures can be difficult to implement but this setback was complemented by very high productivity.
Frankly: I need advice how to structure a simple game with JME.
What architecture do you recommend ?
How are controls used ?
I would appreciate a link to a tutorial (I already read the flagrush series) or any other resource.
Thank you for your help
I'm not sure how well you read the tutorials but they quite literally explain the things you asked for.
Anyway, you code everything so you don't add an object and put code in it you instance an object trough code.
Input is handled by the InputManager, as long as you still use SimpleApplication as foundation you don't have to think about it and use it the way you learned in this tutorial

Java API interface

Ever since I started playing around with Scala, I have had one big question concerning the Java API: why does Oracle keep the same old HTML page with "frameset" tags and no search function at all? It looks like they haven't made it to the Web 2.0...
The Scala API documentation on the other hand, while not the best website in the web history, is several orders of magnitude more usable.
Anyways, if anybody knows why that is and, more importantly, if there exists a Java API documentation with a better interface, please let me know!
Recently, for Java 7, JavaDoc was improved so it could use custom CSS. Here are the first results: http://download.java.net/jdk7/docs/api/. The work continues and I think we'll see more when new updates come out. I do agree that ScalaDoc is superior, but they didn't have to deal with 15 years of legacy.
Javadocs provides the output in that format and its published at that address, I guess no one really saw the need for improvement, but now that you mention it, it makes for an interesting side-project. I googled around to find if there was any "better" interface but no luck.
You could run javadoc -h to see what extra options are available if you want to re-generate the javadocs. Some interesting ones are to provide custom header/footer and linking to the source, but nothing to the effect that you are asking.
Those HTML pages were made using the Javadoc tools, a standard way to build documentation in Java.
I don't know if there are other webpages with a better formatting of the API, but if it helps you with anything, and you are using an IDE and the SDK, you can see the source code for most of the files there.
JavaDoc was designed to be the lowest common denominator. Virtually any web browser can display it, even without JavaScript support.
If you are looking for quicker access and search capabilities, you can access JavaDoc from within an IDE such as Eclipse.

what is the best way to transform a java Class into a php class?

I'd like to transform my java Class file intp a php class file. Do you know a good open source tool to do this?
Thanks you very much,
Bat
As far as i know there isn't any good automated scripts out there that converts java code to php, best practice would be think through what you want to have done read the php documentation which is very well documented. And try to accomplish the same thing. If you hit any obstacles feel free to come back here and ask whatever it might be.
I don't know any tool able to convert Java to PHP, but you may want to seize the opportunity of having a well structured source program in Java and keep a similar structure in PHP - as much as possible (by using classes, keeping the files segmentation, it'll help the conversion anyway)
Unless you have a pretty complex Java code (synchronized methods, Spring transactions, multi threaded access, data isolation...), or with heavy subclassing, or using an exotic framework, the logic itself should translate well from Java to PHP.
The templates should not be a real pain.
Regarding data structure, PHP usually makes the programmer life rather easy - basically you can make an array() from a Set, a Map, a List/Array[].
Regarding data types, you have to be careful when PHP (not typed) automatically converts a fraction to a double if necessary (in Java (int)4/3 is 1, and 1.333... in PHP).
Strings should be ok (implement mbstring in PHP - Java is utf8 by default)
And PHP offers so many functions that you should be able to find most of the necessary Java equivalent features.
Interesting project - I would be glad if you could edit your question and post the progress and how you finally could deal with the challenge.
Using a conversion script will result in bad or garbage code. To achieve a usable result you should read Java class carefully and implement in PHP.

Categories