Sign in to follow this  
Followers 0
andrew.arnott

Handy Setter/Getter Makey (java)

5 posts in this topic

#1 ·  Posted (edited)

Woot! It works now B)

-- Any issues/suggestions?

This hotkey function will take whatever variable declarations you have highlighted in your IDE, and then create appropriate getter/setter methods (upon pressing ctrl + shift + d).

For example:

Say you have highlighted:

private int monkey;

public Chimpanzee andrew;

protected Monkey kingKong;

Would put into the clipboard for you to paste (upon pressing ctrl + shift + d)

public int getMonkey() {

return this.monkey;

}

public void setMonkey(int monkey) {

this.monkey = monkey;

}

public Chimpanzee getAndrew() {

return this.andrew;

}

public void setAndrew(Chimpanzee andrew) {

this.andrew = andrew;

}

public Monkey getKingKong() {

return this.kingKong;

}

public void setKingKong(Monkey kingKong) {

this.kingKong = kingKong;

}

HotKeySet("^+d", "CreateGetterSetter")

#Include <string.au3>
#Include <array.au3>

While 1
    Sleep(0x7FFFFFFF)
WEnd

Func CreateGetterSetter()
    Send("{CTRLDOWN}c{CTRLUP}")
    $text = ClipGet()
    $text = StringReplace($text,@CRLF," ")
    $text = StringReplace($text,@LF," ")
    $text = StringReplace($text,@CR," ")
    $text = StringReplace($text,@TAB,"")
    Local $split = StringSplit($text, " ")
    $count = 1
    While($count < UBound($split))
        If($split[$count] = "") Then
            _ArrayDelete($split, $count)
            $split[0] = $split[0] - 1
        Else
            $count = UBound($split)
        EndIf
    WEnd
    $count = UBound($split) - 1
    While($count > 0)
        If($split[$count] = "") Then
            _ArrayDelete($split, $count)
            $count = $count - 1
            $split[0] = $split[0] - 1
        Else
            $count = 0
        EndIf
    WEnd
    Local $return = ""
    If (Mod($split[0], 3) = 0) Then
        For $i = 1 To ($split[0] / 3)
            $split[$i*3] = StringReplace($split[$i*3],";",'')
            $return = $return & "public " & $split[$i*3 - 1] & " get" & StringReplace($split[$i*3], StringMid($split[$i*3], 1, 1), StringUpper(StringMid($split[$i*3], 1, 1)), 1) & "() {" & @CRLF
            $return = $return & @TAB & "return this." & $split[$i*3] & ";" & @CRLF
            $return = $return & "}" & @CRLF  & @CRLF
            $return = $return & "public void set" & StringReplace($split[$i*3], StringMid($split[$i*3], 1, 1), StringUpper(StringMid($split[$i*3], 1, 1)), 1) & "(" & $split[$i*3 - 1] & " " & $split[$i*3] & ") {" & @CRLF
            $return = $return & @TAB & "this." & $split[$i*3] & " = " & $split[$i*3] & ";" & @CRLF
            $return = $return & "}" & @CRLF &  @CRLF
            ClipPut($return)
        Next
        MsgBox(0, "Done!", "Getters and Setters successfully created", 0)
    EndIf
EndFunc

:graduated: Hurray for lazy coding! :o

Edited by andrew.arnott

Share this post


Link to post
Share on other sites



What a fool I was doing those by hand in C#, sweet script B)


"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

Updates complete -- now allows for multiple setter/getters at once B)

Share this post


Link to post
Share on other sites

Well really you don't need any control over setter and getter methods. They should never be any different than the template. And as far as naming conventions, this preserves the java standard (I'm not sure what other languages have for their standards). I'm probably going to add some more lazy-coder hotkeys when I see myself doing repetitive tasks. I'm pretty sure this one has already saved me some time (lets just say I make a boatload of POJOs on a regular basis).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0