Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
mister_gosh
May 24, 2002

Aleksei Vasiliev posted:

Can't you just check for null || arr.length == 0 or is 0-len a valid condition?

Yes, but for a large GUI codebase previously run under Java 6, it seems odd to have to go into all classes which checked that it was null and add the extra code.

I could also Override the TreeMap method... there's ways around this, but it just seems like you shouldn't need to patch code just for the remainder of a major release.

mister_gosh fucked around with this message at 21:02 on Aug 13, 2012

Adbot
ADBOT LOVES YOU

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

mister_gosh posted:

I could also Override the TreeMap method... there's ways around this, but it just seems like you shouldn't need to patch code just for the remainder of a major release.

Which is reasonable I agree, but time has proven once again that you can't count on any new update not breaking your poo poo. There is wisdom in not updating your platform every time a major update comes along. You're lucky that you still have access to the source code of your program and can fix it.

wins32767
Mar 16, 2007

Is there a tool out there that will attach to a running Servlet container and log all of the method calls that are made? I'm trying to generate some system documentation for a framework whose developer is no longer with the team and stepping through the code manually is going to be pretty painful.

JingleBells
Jan 7, 2007

Oh what fun it is to see the Harriers win away!

wins32767 posted:

Is there a tool out there that will attach to a running Servlet container and log all of the method calls that are made? I'm trying to generate some system documentation for a framework whose developer is no longer with the team and stepping through the code manually is going to be pretty painful.

You could use AspectJ and set the aspect to match all method calls, a quick google suggests this might work.

Be careful - I did this once by setting the wrong aspect, and logged every method call in all the libraries I was using too

Paolomania
Apr 26, 2006

wins32767 posted:

Is there a tool out there that will attach to a running Servlet container and log all of the method calls that are made? I'm trying to generate some system documentation for a framework whose developer is no longer with the team and stepping through the code manually is going to be pretty painful.

Try attaching with VisualVM and doing some "CPU Profiling"

other people
Jun 27, 2004
Associate Christ
Is there a good book or online resource for some one with zero java experience? I swear half the job descriptions I read are looking for java experience, and I have none.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Kaluza-Klein posted:

Is there a good book or online resource for some one with zero java experience? I swear half the job descriptions I read are looking for java experience, and I have none.

http://docs.oracle.com/javase/tutorial/tutorialLearningPaths.html

slam5000
Aug 19, 2012
I must say, I completely agree. Unlike many languages, the best learning tools for Java are made by the people who actually make the language. Also, just generally check the documentation if you have a question. It's extremely well written and besides the syntax, it pretty much can teach you everything you need to know. Good luck with learning Java!

notMordecai
Mar 4, 2007

Gay Boy Suicide Pact?
Sucking Dick For Satan??

I am attempting to teach myself proper Unit Testing using JUnit but into something the first time and would like some help. I'm just getting into backend programming after coming from mobile so hopefully this question doesn't cause you guys to want to kill me.

I've been going through some example classes to create proper unit tests and came across this. I'll simplify it a bit for sake of SA.
code:
public void executeSomeThing(Dog dog1, Cat cat1){
.
.
.
}
How would one unit test a function like this? Not that (obviously) these example Dog and Cat classes are in other files with their own fields. Would I literally just take those existing Dog and Cat classes and fill them with values to allow passage into this public method? I also can't go any further due to the fact that the methods to be unit tested are all private and depend on this public function to dive into.

Also, can someone explain downcasting. I am having a really hard time wrapping my head around it.
code:
public class Parent{}
public class Child extends Parent(){}
 
public static void main(String args[]){
    Parent parent = new Child();              // Parent is parent class of Child, parent variable holding value of type Child
    Child child = (Child)parent;              // This is possible since parent object is currently holding value of Child class
}
This is apparently the simplest example someone put on wikipedia but without actual variable/value inside these classes, and I still can't seem to understand what downcasting does or transforms to these.

baquerd
Jul 2, 2007

by FactsAreUseless

notMordecai posted:

I am attempting to teach myself proper Unit Testing using JUnit but into something the first time and would like some help. I'm just getting into backend programming after coming from mobile so hopefully this question doesn't cause you guys to want to kill me.

I've been going through some example classes to create proper unit tests and came across this. I'll simplify it a bit for sake of SA.
code:
public void executeSomeThing(Dog dog1, Cat cat1){
.
.
.
}
How would one unit test a function like this? Not that (obviously) these example Dog and Cat classes are in other files with their own fields. Would I literally just take those existing Dog and Cat classes and fill them with values to allow passage into this public method? I also can't go any further due to the fact that the methods to be unit tested are all private and depend on this public function to dive into.

Well, you need to document what executeSomeThing is supposed to do. Create Cat and Dog objects and pass them through, checking to make sure that executeSomeThing produces the correct result with a number of different arguments, including illegal ones if applicable to test failure conditions. Since it appears this function will likely mutate the parameters, you need to check to make sure this state manipulation was done correctly and without side effects.

quote:

Also, can someone explain downcasting. I am having a really hard time wrapping my head around it.
code:
public class Parent{}
public class Child extends Parent(){}
 
public static void main(String args[]){
    Parent parent = new Child();              // Parent is parent class of Child, parent variable holding value of type Child
    Child child = (Child)parent;              // This is possible since parent object is currently holding value of Child class
}
This is apparently the simplest example someone put on wikipedia but without actual variable/value inside these classes, and I still can't seem to understand what downcasting does or transforms to these.

Downcasting's most immediate effect is to allow compile time access to the fields and methods of the child object's class. So if Parent has method X() and Child adds method Y(), you would need to downcast to call Y().

notMordecai
Mar 4, 2007

Gay Boy Suicide Pact?
Sucking Dick For Satan??

baquerd posted:

Well, you need to document what executeSomeThing is supposed to do. Create Cat and Dog objects and pass them through...

Yeah that's what I was trying to get at. So I WOULD create and define the fields of a class in a unit test, then pass that entire class to unit test it?

It's more of a general question and less of a "this scenario" question. I've never attempted to test a method that had entire objects pass into it.

Base Emitter
Apr 1, 2012

?

notMordecai posted:

Yeah that's what I was trying to get at. So I WOULD create and define the fields of a class in a unit test, then pass that entire class to unit test it?

It's more of a general question and less of a "this scenario" question. I've never attempted to test a method that had entire objects pass into it.
You might want to take a look at mock objects, too. For example the EasyMock or PowerMock libraries. Mock objects are basically fake replacements that allow you to verify that the class under test tickled it's dependencies in all the right places.

For example if the method was supposed to call the Dog class's giveTreat method, you'd create a mock Dog object, tell it to expect a giveTreat call, then call the method you're testing, then verify the mock's method got called.

(These don't replace JUnit, you'd use them together.)

notMordecai
Mar 4, 2007

Gay Boy Suicide Pact?
Sucking Dick For Satan??

Base Emitter posted:

You might want to take a look at mock objects, too. For example the EasyMock or PowerMock libraries. Mock objects are basically fake replacements that allow you to verify that the class under test tickled it's dependencies in all the right places.

For example if the method was supposed to call the Dog class's giveTreat method, you'd create a mock Dog object, tell it to expect a giveTreat call, then call the method you're testing, then verify the mock's method got called.

(These don't replace JUnit, you'd use them together.)

yeah I got very familiar with Mockito trying to figure this out after realizing I would need to use it for this case. Still came up empty on a solution. Hopefully I can take a crack at it tomorrow.

Safe and Secure!
Jun 14, 2008

OFFICIAL SA THREAD RUINER
SPRING 2013
While we're on the topic, any favorite Java mock libraries?

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

Safe and Secure! posted:

While we're on the topic, any favorite Java mock libraries?

Mocking really pisses me off but when I have to, I use Mockito.

Jo
Jan 24, 2005

:allears:
Soiled Meat
As a general policy, if I have a constructor in Java which takes two points (for example) and produces a line, is it a good idea to use the points as a reference or should I make copies? That is...

Java code:
	public Line2DInt(Point a, Point b) {
		p1 = new Point(a);
		p2 = new Point(b);
	}
OR

Java code:
	public Line2DInt(Point a, Point b) {
		p1 = a;
		p2 = b;
	}
On the one hand, people might be expecting to fiddle around with the values after the creation of the object. On the other, perhaps a static copy is desired? Or maybe I'm just overthinking this.

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.
Always make a copy of mutable data you're given and going to store unless a) it's internal to the implementation (not public), or b) you document it properly and can just yell at the users if they gently caress it up. (this is in Effective Java)
e: or c) you're doing weird poo poo where best practices are wrong

Jo posted:

On the one hand, people might be expecting to fiddle around with the values after the creation of the object.
This is what setters are for.

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
Jo: Well, Points are mutable. It depends on what you're trying to do. In some situations it can be very useful to have composed objects update under your feet, but if you mutate the Points that are passed in it may seem like "spooky action at a distance" from the caller's perspective. At any rate, it is indeed an important issue to consider, and worth documenting.

Jo
Jan 24, 2005

:allears:
Soiled Meat

Internet Janitor posted:

Jo: Well, Points are mutable. It depends on what you're trying to do. In some situations it can be very useful to have composed objects update under your feet, but if you mutate the Points that are passed in it may seem like "spooky action at a distance" from the caller's perspective. At any rate, it is indeed an important issue to consider, and worth documenting.

I was fiddling with a reimplementation of Ken Silverman's BUILD engine. Part of my reasoning was, if someone dicks with a vertex, I'd like to see the lines and sectors update automatically without having to iterate through each line and sector. At the same time, perhaps that's the correct way to do it on update. What if someone hoses a vertex? We'd need to update all the lines and sectors anyways to account for the loss.

pigdog
Apr 23, 2004

by Smythe
Also check out Flyweight pattern if it's applicable in your context.

Jo
Jan 24, 2005

:allears:
Soiled Meat

pigdog posted:

Also check out Flyweight pattern if it's applicable in your context.

Thank you. I think this will be beneficial.

Contra Duck
Nov 4, 2004

#1 DAD

Safe and Secure! posted:

While we're on the topic, any favorite Java mock libraries?

I like JMockit. It takes a while to learn but it's absurdly powerful and quick once you get the hang of it.

Volguus
Mar 3, 2009
I personally prefer Mockito. I found it to allow me to write cleaner and easier to maintain code, and it's extremely powerful.

Tres Burritos
Sep 3, 2009

Easiest way to parse xml? I'm this close to just using regexes.

Doctor w-rw-rw-
Jun 24, 2008

Tres Burritos posted:

Easiest way to parse xml? I'm this close to just using regexes.

Any library that gives you XPath?

Blacknose
Jul 28, 2006

Meet frustration face to face
A point of view creates more waves
So lose some sleep and say you tried
Yeah there's no good reason to code that from scratch.

bartkusa
Sep 25, 2005

Air, Fire, Earth, Hope

Tres Burritos posted:

Easiest way to parse xml? I'm this close to just using regexes.

Funny, was just passing this StackOverflow link around the office today.

Tres Burritos
Sep 3, 2009

bartkusa posted:

Funny, was just passing this StackOverflow link around the office today.

Yeah I did some googling and found that too. Is dom4j an acceptable answer or are the cool kids not using that?

Volguus
Mar 3, 2009

Tres Burritos posted:

Yeah I did some googling and found that too. Is dom4j an acceptable answer or are the cool kids not using that?

Whatever works for you is acceptable. If you have big XMLs to parse and/or memory is tight, use SAX. It's a bit more work, but it's relatively fast. A DOM parser is nice and easy, but it takes up a tad bit more memory to do it's job. If you only need parts of the XML (the value of all the test elements for example, how many times does the person tag appears, etc.), don't parse that thing, use XPath.

One decent and relatively fast library for java is XOM (http://www.xom.nu/), but there are many others (including the ones that come with the jdk). Which one you use is just a matter of taste and your requirements. Personally I prefer the jdk bundled libraries.

ZombieIsland
Jan 21, 2011

The name is Sherlock Holmes and the address is 221B Baker Street
What is the recommended book or video series of java with no previous experience?

Volguus
Mar 3, 2009

ZombieIsland posted:

What is the recommended book or video series of java with no previous experience?

Start with the tutorial and go from there: http://docs.oracle.com/javase/tutorial/ .

After that get a java book that has decent reviews, that is focused on whatever you're interested in.

Lord Of Texas
Dec 26, 2006

trex eaterofcadrs posted:

Mocking really pisses me off but when I have to, I use Mockito.

Have you never had to test code that depends on an external service? Mocks are extremely useful for when you are trying to actually create unit tests instead of integration tests. I suppose they could be overdone, but at my shop they're underutilized.

quote:

Yeah I did some googling and found that too. Is dom4j an acceptable answer or are the cool kids not using that?

Either DOM or SAX will be fine.

Whatever you do, for the love of god please don't try to write a regex for parsing. I can empathize with the sentiment that many Java libraries are poorly documented and hard to understand, but you'll still save yourself a ton of time by not rolling your own solutions for common tasks like consuming XML.

quote:

What is the recommended book or video series of java with no previous experience?

Head First Java is frequently recommended. Sierra's SCJP 6 is good for learning the syntax and basic libraries, but won't teach you good practice.

Once you have some experience under your belt, I strongly recommend Effective Java and Head First Design Patterns. They tackle pattern reuse, which is just as important as code reuse.

Lord Of Texas fucked around with this message at 21:35 on Aug 26, 2012

Luminous
May 19, 2004

Girls
Games
Gains

Lord Of Texas posted:

Have you never had to test code that depends on an external service? Mocks are extremely useful for when you are trying to actually create unit tests, and integration tests. I suppose they could be overdone, but at my shop they're underutilized.

I think this is a big point, and if I recall, has been debated in this thread before. But the lack of understanding about the different types of testing that code can and should undergo can lead to excessive or improper use of helpful technology. Even in testing, it's still about using the right tool for the job - but you need to know what the job is first.

Doctor Malaver
May 23, 2007

Ce qui s'est passé t'a rendu plus fort
Anyone interested in the review of the manuscript of the fourth edition of Spring in Action? If you are interested in reviewing, check out the main thread.

DGK2000
May 3, 2007

Hotel Soap is super proud of his little perfumed balls that never get dirty or stinky
This might be a dumb question, but my CS190 class has us using jGRASP. I like the Netbeans interface better, how do I go about writing in Netbeans and then moving it over to jGRASP for the Professor?

Star War Sex Parrot
Oct 2, 2003

I can't imagine you need to do anything IDE-specific, especially for a simple Java course. I did all of my Java course work in Sublime Text 2, and the only thing I can recall my instructor's Netbeans-generated code having was a package line that I didn't need.

DGK2000
May 3, 2007

Hotel Soap is super proud of his little perfumed balls that never get dirty or stinky

Star War Sex Parrot posted:

I can't imagine you need to do anything IDE-specific, especially for a simple Java course. I did all of my Java course work in Sublime Text 2, and the only thing I can recall my instructor's Netbeans-generated code having was a package line that I didn't need.

I don't really. I'm writing my first project right now in jGrasp, it was more of an aesthetic thing.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

There shouldn't be anything special, even the package declaration is a part of the Java language. I'm sure if you had some issues showing up in NetBeans that didn't happen in JGrasp we could help figure it out.

Lord Of Texas
Dec 26, 2006

Here's a design question. It has members of my development team on opposite sides, so I thought I'd get the opinions of the thread.
-----

Say you're calling a method that returns some business resource from an external service.

code:
public ThingBean getThingBean(params){}
The requirements state that if the requested resource is not found, the application should recover (i.e. throwing an unchecked exception is out).

Your options when the resource is not found are:

1) return null
2) throw a checked exception for the client code to handle
3) return an empty ThingBean with no fields populated

Adbot
ADBOT LOVES YOU

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.
Option 3 is the solution I'd go with if I was asked to come up with the worst solution.

If throwing an unchecked exception is out, then so is returning null, as they can fail to handle that (and cause a NPE).

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply