Jump to content

Handy Setter/Getter Makey (java)


andrew.arnott
 Share

Recommended Posts

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
Link to comment
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.
Link to comment
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).

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...