Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Generate Get and Set methods for Java in perl UNIX
I find it a pain when writing java code to type out 20 or 30 get set methods (since you sould use get and set in java instead of referring straight to the variable) so i wrote a perl scrpt that does this for me.

Here it is.
#!/usr/bin/perl
($a, $t) = @ARGV;
unless ($a)
{
print "Nameov integer: ";
chomp($a = <STDIN>);
print "Type of integer:";
chomp($t = <STDIN>);
$b = ucfirst($a);
}
print "\npublic $t get$b()\n{\n";
print "\t return $a;\n}\n\n";
print "pubic void set$b($t val)\n{\n";
print "\t $a = val;\n}\n\n";
[Editor's note: This hint has not been tested on my machine, primarily because my skill set does not extend to Java and perl!]
    •    
  • Currently 1.67 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[8,914 views]  

Generate Get and Set methods for Java in perl | 9 comments | Create New Account
Click here to return to the 'Generate Get and Set methods for Java in perl' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
and in the spirit of Perl...
Authored by: ragnar on Aug 05, '02 05:03:21PM

There are no comments inserted into the code! I couldn't resist a little jab. ;) I'm one of the those Java programmers who writes his JavaDocs first.



[ Reply to This | # ]
Writing 20 or 30 get/set methods is bad
Authored by: a1291762 on Aug 05, '02 07:24:51PM
I find it a pain when writing java code to type out 20 or 30 get set methods (since you sould use get and set in java instead of referring straight to the variable) so i wrote a perl scrpt that does this for me.

If you have classes with 20-30 get/set methods then your design is clearly wrong. There's no reason for other classes to need access to all the internal variables of a class. You should do an actual design and only allow access to things that external classes need access too. Remeber the point of abstraction is that the external interface does not have to resemble the internal structure of a class.

/me thinks you are using the C-struct mentality, in which case you would be better off just using public variables. Making all the varables private and creating get/set methods doesn't make your code any better. Data Flow Diagrams (DFD) and Class Reponsibility Cards (CRC) are a great way to find out just what needs to be shared between classes.

Remember, the goal of OO programming is to increase cohesion in classes and decrease coupling between classes

[ Reply to This | # ]

Writing 20 or 30 get/set methods is bad
Authored by: Accura on Aug 06, '02 12:01:32AM

I do understand this, all the script does is do the basic get/set method i usually have a couple of extra lines of code that converts or changes the data in some way so i don't have to remember to do this every time. also i use them mostly for internal class' and set as private or protected. as i said, this just does the grunt work.

When i learn i was told to allways use get or set instead of going stright to the veriable, i always questioned this but did it any way because i read it in a few books. do you know if i should or shouldn't do this and why?



[ Reply to This | # ]
Writing 20 or 30 get/set methods is bad
Authored by: Anonymous on Aug 06, '02 07:56:31AM

Encapsulation (or -for others who perhaps don't know-preventing access to the internals of a class from outside and requiring accessor functions) is a good thing.
However you should think carefully before immediately implementing a large number of accessor functions. Do you really need access to all of the functions of the class? Could you perhaps bundle related values into objects of some kind?
For example
int box_height;
int box_width;
int box_length;
int box_color;
into a class called box which is itself accessed from your main class
and accessed with product.box().height()
You still have each accessor function but now you can reuse the box class elsewhere. Perhaps you have a function volume() which might compute the volume of a box. And you could use the box class in another program altogether.

removals.storageCrate().volume()

Though color() would probably always be brown in this case(?)

It isn't that good an example. :-)

If you can break up the program into smaller units they are more likely to be resuseable. Which of course is one of these goals of OO programming :-)

I once took a lecture series at university called Software Engineering-objects and components where the lecturer recommended no more than 4 main responsibilities for any class. If you had more then you were supposed to refactor the application and seperate stuff out more.
I couldn't say whether I manage this in the code that I write. :-)

However regardless of all of this, this tool might come in handy. I'm not writing so much java at the moment.More demand for other things.
Thanks for thinking it up.

Angus



[ Reply to This | # ]
Writing 20 or 30 get/set methods is bad
Authored by: buzzlabs on Sep 01, '02 04:52:45AM

Yawn.

One thing I've found in the 10 years I've been programming professionally (been programming for 20 total) is that you should ignore a lot of what people tell you, and you should not look to those with degrees and all sorts of "OO expertise" for the answers.

Especially with Java... But anyway... The Java Beans notion is that you have "getter" and "setter" methods for the attributes which are to be publically-exposed in your class, rather than setting them directly and marking them public or otherwise accessible by other classes in your package. Therefore, it is wise to have "get" and "set" methods.

The project that I wrote Beano for was an ATG Dynamo project. The way to do things there was definitely to have "getter" and "setter" methods for your bean classes, as that's how their lovey (gag) "Relational View" abstraction atop JDBC handled things when it was moving data from SQL query ResultSets into your beans, therefore...

-S



[ Reply to This | # ]
throw perl to swine
Authored by: code_monkey on Aug 06, '02 03:14:46PM

Same kind'a thing but with file input/output:

#!/usr/bin/perl -w
use strict;

if(($#ARGV) != 1)
{
print "Need java file and output file names/paths; i.e. foo.pl /home/monkey/bar.java /home/monkey/foo.log\n";
die;
}

my $java_file = $ARGV[0]; #the filename and path of the file containing the data tobe imported.
my $report_file = $ARGV[1]; #filename and path of the report file.

open (FILEHANDLE, "<$java_file") || die $!;
while (<FILEHANDLE>)
{
my $inline = $_;
if($inline =~/^\s*(private|public)\s+(?:\s*static\s*|\s*final\s*|\s*transient\s*)*\s*(\w+)\s+(\w+)\s*[\;|\=]/)
{
add_to_file($1,$2,$3);
}
}
close(FILEHANDLE);

sub add_to_file
{
my ($scope, $type, $var) = @_;
my $name = ucfirst($var);
open (FH, ">>$report_file") || die $!;
print FH "\npublic $type get$name()";
print FH "\n{";
print FH "\n return $var;";
print FH "\n}\n";
print FH "\npublic void set$name($type val)";
print FH "\n{";
print FH "\n $var = val;";
print FH "\n}\n";
close(FH);
}

prolly loads of cases i've missed in that regex; but hey, half the fun is filling in the blanks ;)



[ Reply to This | # ]
What a rubbish hint
Authored by: Anonymous on Aug 07, '02 05:19:34AM

For heaven's sake this is nonsense! This tipster clearly doesn't have much clue about what they are talking about.

a) typing out getter & setter methods isn't that hard. It is the kind of thing which seems like a chore when you're a newbie

b) WTF does "type of integer" mean???

c) If you supply a non-primitive type, e.g. String, there is no null checking

Get your act together MacOSXHints... I know you try to be all things to all people, but some of the hints are so basic as to be unreal. Occasionally others are just a load of rubbish, like this one. The quality of the site depends on the quality of filtering on incoming hints.

alexf



[ Reply to This | # ]
Beano (TM)
Authored by: buzzlabs on Sep 01, '02 04:48:53AM

I wrote a little Java-based command-line tool that has many options (but little documentation). It creates Java bean files from SQL database tables. It's hard-coded to use the mm.mysql JDBC driver right now, but this can easily be changed.

Beano will create "get" and "set" methods and the associated properties which relate to a database table for you. Saves hours and hours of typing. Unfortunately, I wrote this tool after I needed it for a large work project. If I had it at the time, it would have changed something that took weeks into a few days' work.

See: http://www.seapod.org/software/ and look for "Beano"

Steve Klingsporn
http://www.seapod.org



[ Reply to This | # ]
Generate Get and Set methods for Java in perl
Authored by: jbriscoe on Sep 08, '04 10:11:46PM

Just use Eclipse; right click in the editor area choose refactor generate getters and/or setters.



[ Reply to This | # ]