Auto-Generate Getter and Setter

This commit is contained in:
nora 2020-11-22 14:57:51 +01:00
parent 16e254b73a
commit 1db367b9bd
6 changed files with 89 additions and 5 deletions

View file

@ -71,10 +71,19 @@ public class UMLClass {
for (UMLMethod method : methods) { for (UMLMethod method : methods) {
s.append(method.toString()); s.append(method.toString());
} }
s.append("}");
} }
if(manager.isGetSetAuto()){
System.out.println("hallo");
for(UMLField field : fields){
s.append(field.getter());
s.append(field.setter());
}
}
s.append("}");
return s.toString(); return s.toString();
} }

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="UMLClassView"> <form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="UMLClassView">
<grid id="27dc6" binding="panel1" default-binding="true" layout-manager="GridLayoutManager" row-count="4" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="27dc6" binding="panel1" default-binding="true" layout-manager="GridLayoutManager" row-count="5" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="3" left="3" bottom="3" right="3"/> <margin top="3" left="3" bottom="3" right="3"/>
<constraints> <constraints>
<xy x="20" y="20" width="500" height="400"/> <xy x="20" y="20" width="500" height="400"/>
@ -123,9 +123,17 @@
</component> </component>
<vspacer id="5f3c8"> <vspacer id="5f3c8">
<constraints> <constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/> <grid row="4" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
</vspacer> </vspacer>
<component id="fe6ca" class="javax.swing.JCheckBox" binding="generateGetSetButton">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Generate Getter/Setter"/>
</properties>
</component>
</children> </children>
</grid> </grid>
</form> </form>

View file

@ -3,6 +3,8 @@ import java.awt.datatransfer.DataFlavor;
import java.awt.dnd.DnDConstants; import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget; import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDropEvent; import java.awt.dnd.DropTargetDropEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter; import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.io.File; import java.io.File;
@ -16,6 +18,7 @@ public class UMLClassView {
private JButton convertFileButton; private JButton convertFileButton;
private JTextField packagePathField; private JTextField packagePathField;
private JCheckBox watermarkBox; private JCheckBox watermarkBox;
private JCheckBox generateGetSetButton;
private UMLManager manager; private UMLManager manager;
@ -67,6 +70,14 @@ public class UMLClassView {
manager.setShowWatermark(watermarkBox.isSelected()); manager.setShowWatermark(watermarkBox.isSelected());
refreshTextArea(); refreshTextArea();
}); });
generateGetSetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
manager.setGetSetAuto(generateGetSetButton.isSelected());
refreshTextArea();
}
});
} }
private void refreshTextArea(){ private void refreshTextArea(){
@ -86,4 +97,8 @@ public class UMLClassView {
public boolean isWatermarkSelected(){ public boolean isWatermarkSelected(){
return watermarkBox.isSelected(); return watermarkBox.isSelected();
} }
public boolean isGetSetAutoSelected() {
return generateGetSetButton.isSelected();
}
} }

View file

@ -40,4 +40,24 @@ public class UMLField {
return " " + encapsulation + dataType + " " + name + ";\n"; return " " + encapsulation + dataType + " " + name + ";\n";
} }
public String setter() {
String nameCapital = name.toUpperCase();
String nameCC = "set" + nameCapital.charAt(0) + name.substring(1);
UMLMethod setter = new UMLMethod("void ", nameCC, "public ");
setter.addArg(name, dataType);
setter.addBodyLine("this." + name + " = " + name + ";");
return setter.toString();
}
public String getter() {
String nameCapital = name.toUpperCase();
String nameCC = "get" + nameCapital.charAt(0) + name.substring(1);
UMLMethod setter = new UMLMethod(dataType + " ", nameCC, "public ");
setter.addBodyLine("return " + name + ";");
return setter.toString();
}
} }

View file

@ -10,10 +10,12 @@ public class UMLManager {
private UMLClassView view; private UMLClassView view;
private boolean showWatermark; private boolean showWatermark;
private boolean getSetAuto;
public UMLManager(UMLClassView view) { public UMLManager(UMLClassView view) {
this.view = view; this.view = view;
this.showWatermark = view.isWatermarkSelected(); this.showWatermark = view.isWatermarkSelected();
this.getSetAuto = view.isGetSetAutoSelected();
view.setManager(this); view.setManager(this);
} }
@ -56,4 +58,12 @@ public class UMLManager {
public void setShowWatermark(boolean showWatermark) { public void setShowWatermark(boolean showWatermark) {
this.showWatermark = showWatermark; this.showWatermark = showWatermark;
} }
public void setGetSetAuto(boolean getSetAuto) {
this.getSetAuto = getSetAuto;
}
public boolean isGetSetAuto() {
return getSetAuto;
}
} }

View file

@ -12,6 +12,8 @@ public class UMLMethod {
private final ArrayList<String> argsNames = new ArrayList<>(); private final ArrayList<String> argsNames = new ArrayList<>();
private final ArrayList<String> argsTypes = new ArrayList<>(); private final ArrayList<String> argsTypes = new ArrayList<>();
private String methodBody = "";
/** /**
* Create a new method/constructor from the UML representation of that method * Create a new method/constructor from the UML representation of that method
* *
@ -56,6 +58,11 @@ public class UMLMethod {
} }
} }
public UMLMethod(String returnType, String name, String encapsulation) {
this.returnType = returnType;
this.name = name;
this.encapsulation = encapsulation;
}
/** /**
* Returns the method as the Java code representation * Returns the method as the Java code representation
@ -74,8 +81,23 @@ public class UMLMethod {
} }
} }
returnString.append(") {\n \n }\n"); returnString.append(") {\n ");
returnString.append(methodBody);
returnString.append("\n }\n");
return returnString.toString(); return returnString.toString();
} }
public void addArg(String name, String dataType) {
argsNames.add(name);
argsTypes.add(dataType);
}
public void addBodyLine(String line){
if(methodBody.contains("\n")) {
methodBody += "\n " + line;
} else {
methodBody += " " + line;
}
}
} }