Skip to content

Commit

Permalink
release 0.4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
misakuo committed Mar 7, 2016
1 parent 1f604c2 commit bc618bb
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 55 deletions.
Binary file modified ModelGenerator.zip
Binary file not shown.
Binary file added ModelGenerator_0.4.1.zip
Binary file not shown.
15 changes: 8 additions & 7 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<idea-plugin version="2">
<id>com.moxun.generetor</id>
<name>JSON Model Generator</name>
<version>0.4.1</version>
<version>0.4.2</version>
<vendor email="furmint@foxmail.com">moxun</vendor>

<description><![CDATA[
A JSON model generator, get json string from URL and transforming it to Java class<br>
Tool to covert JSON string to Java class.<br>
<br>
* Support get JSON string from URL or user's input.<br>
* Support data conversion library like fastjson.<br>
* Support auto implementation Java interface.<br>
* Support field type inference.<br><br>
Expand All @@ -17,9 +18,9 @@
]]></description>

<change-notes><![CDATA[
<b>NEXT</b><br>
Support parsing JSON Array to List<T>;<br>
<br><br>
<b>0.4.2</b><br>
Replaced with a new icon;<br>
Support parsing JSON Array to List<E>;<br>
<b>0.4.1</b><br>
Fixed conflicts of the same name parameter elements,you can using <a href="http://www.mrtao.net/wp-json/posts">this case</a> to testing;<br>
<b>0.4.0</b><br>
Expand Down Expand Up @@ -64,8 +65,8 @@

<actions>
<!-- Add your actions here -->
<action class="com.moxun.generator.Generator" id="moxun.generator" text="Model Generator"
description="Model Generator doge doge doge" icon="/icons/icon.gif">
<action class="com.moxun.generator.Generator" id="moxun.generator" text="JSON Model Generator"
description="Tool for convert JSON string to Java class." icon="/icons/icon.png">
<add-to-group group-id="ToolsMenu" anchor="last"/>
<add-to-group group-id="MainToolBar" anchor="last"/>
</action>
Expand Down
23 changes: 23 additions & 0 deletions src/com/moxun/generator/ClassNameUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.moxun.generator;

/**
* Created by moxun on 16/3/7.
*/
public class ClassNameUtil {
public static String getName(String name) {
char[] chars = name.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if (!Character.isLetter(c)) {
chars[i] = '_';
if (i + 1 < chars.length) {
chars[i + 1] = Character.toUpperCase(chars[i + 1]);
}
}
}

return String.valueOf(chars).replaceAll("_","");
}


}
39 changes: 35 additions & 4 deletions src/com/moxun/generator/GeneratorEnginer.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,46 @@ public void run() {
protected void run(@NotNull Result result) throws Throwable {
PsiClass dist = dataSet.get(clsName);
if (s.startsWith("//")) {
// TODO: issues here.
//PsiComment comment = factory.createCommentFromText(s, null);
//dist.add(comment);
Logger.error("Class " + dist.getQualifiedName() + " has no fields, you need checking it maybe.");
PsiElement comment = factory.createCommentFromText(s, dist);
dist.addBefore(comment,dist.getRBrace());
} else {
String r = s.replaceAll("-", "_");
PsiField field = factory.createFieldFromText(r, dist);
dist.add(field);
}

if (s.contains("public List<")) {
PsiImportStatement[] imports = ((PsiJavaFile) dist.getContainingFile()).getImportList().getImportStatements();
if (imports != null) {
boolean isAdded = false;
for (PsiImportStatement importStatement : imports) {
if (importStatement.getQualifiedName().equals("java.util.List")) {
isAdded = true;
}
}
if (!isAdded) {
GlobalSearchScope searchScope = GlobalSearchScope.allScope(project);
PsiClass[] psiClasses = PsiShortNamesCache.getInstance(project).getClassesByName("List", searchScope);
for (PsiClass psiClass : psiClasses) {
if (psiClass.getQualifiedName().equals("java.util.List")) {
PsiImportStatement importStatement = factory.createImportStatement(psiClass);
((PsiJavaFile) dist.getContainingFile()).getImportList().add(importStatement);
break;
}
}
}
} else {
GlobalSearchScope searchScope = GlobalSearchScope.allScope(project);
PsiClass[] psiClasses = PsiShortNamesCache.getInstance(project).getClassesByName("List", searchScope);
for (PsiClass psiClass : psiClasses) {
if (psiClass.getQualifiedName().equals("java.util.List")) {
PsiImportStatement importStatement = factory.createImportStatement(psiClass);
((PsiJavaFile) dist.getContainingFile()).getImportList().add(importStatement);
break;
}
}
}
}
}
}.execute();
}
Expand Down
150 changes: 120 additions & 30 deletions src/com/moxun/generator/GeneratorForm.form
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.moxun.generator.GeneratorForm">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="7" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="9" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="20" y="20" width="570" height="402"/>
<xy x="20" y="20" width="746" height="402"/>
</constraints>
<properties/>
<border type="none"/>
Expand Down Expand Up @@ -62,14 +62,6 @@
<text value=""/>
</properties>
</component>
<component id="82a56" class="javax.swing.JButton" binding="generatorButton" default-binding="true">
<constraints>
<grid row="6" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Generate"/>
</properties>
</component>
<component id="7c35" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
Expand Down Expand Up @@ -131,26 +123,6 @@
</constraints>
<properties/>
</component>
<component id="af71d" class="javax.swing.JCheckBox" binding="generatorSampleCheckBox" default-binding="true">
<constraints>
<grid row="6" column="2" 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 sample"/>
</properties>
</component>
<component id="f8c79" class="javax.swing.JButton" binding="gitButton" default-binding="true">
<constraints>
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="32" height="32"/>
<maximum-size width="32" height="32"/>
</grid>
</constraints>
<properties>
<icon value="icons/github.png"/>
<text value=""/>
</properties>
</component>
<component id="e6751" class="javax.swing.JComboBox" binding="source">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="1" indent="0" use-parent-layout="false">
Expand All @@ -165,6 +137,124 @@
</model>
</properties>
</component>
<grid id="af320" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="6" column="1" row-span="2" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="etched">
<font/>
<title-color color="-1579033"/>
</border>
<children>
<component id="63418" class="javax.swing.JRadioButton" binding="arrayRadioButton" default-binding="true">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<selected value="true"/>
<text value="Array"/>
</properties>
</component>
<component id="41b53" class="javax.swing.JRadioButton" binding="listERadioButton" default-binding="true">
<constraints>
<grid row="0" column="2" 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="List&lt;E&gt;"/>
</properties>
</component>
<component id="bc7a1" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=" Parsing JSONArray As"/>
</properties>
</component>
</children>
</grid>
<grid id="8d51f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="8" column="1" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="empty"/>
<children>
<component id="af71d" class="javax.swing.JCheckBox" binding="generatorSampleCheckBox" default-binding="true">
<constraints>
<grid row="0" 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 sample"/>
</properties>
</component>
<scrollpane id="3bcb2">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="true"/>
<foreground color="-1250068"/>
</properties>
<border type="etched">
<title-color color="-1579033"/>
</border>
<children>
<component id="467ff" class="javax.swing.JLabel" binding="status">
<constraints/>
<properties>
<background color="-1579033"/>
<foreground color="-13074177"/>
<text value=""/>
</properties>
</component>
</children>
</scrollpane>
</children>
</grid>
<grid id="89311" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="6" column="0" row-span="3" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="f8c79" class="javax.swing.JButton" binding="gitButton" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="32" height="32"/>
<maximum-size width="32" height="32"/>
</grid>
</constraints>
<properties>
<icon value="icons/github.png"/>
<text value=""/>
</properties>
</component>
</children>
</grid>
<grid id="86b80" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="6" column="3" row-span="3" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="82a56" class="javax.swing.JButton" binding="generatorButton" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Generate"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>
24 changes: 21 additions & 3 deletions src/com/moxun/generator/GeneratorForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public class GeneratorForm {
private JPanel panel;
private JButton gitButton;
private JComboBox source;
private JRadioButton arrayRadioButton;
private JRadioButton listERadioButton;
private JLabel status;

private Project project;
private List<JTextField> textFields = new ArrayList<JTextField>();
Expand All @@ -64,6 +67,10 @@ public GeneratorForm(Project p_project) {
textFields.add(urlText);
textFields.add(pkgText);

ButtonGroup group = new ButtonGroup();
group.add(arrayRadioButton);
group.add(listERadioButton);

parser = new JSONParser();
}

Expand Down Expand Up @@ -124,6 +131,8 @@ public void actionPerformed(ActionEvent e) {
generatorButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
status.setText("Start generating……");
status.paintImmediately(status.getBounds());
if (check()) {
if (pkgText.getText().split("\\.").length > 0) {
String[] tmp = pkgText.getText().split("\\.");
Expand All @@ -135,7 +144,11 @@ public void actionPerformed(ActionEvent e) {

JSONObject response;
if (source.getSelectedIndex() == 0) {
status.setText("Start fetching URL……");
status.paintImmediately(status.getBounds());
response = parseString(HttpHelper.getResponse(urlText.getText()));
status.setText("Fetch URL complete.");
status.paintImmediately(status.getBounds());
} else {
response = parseString(jsonString);
}
Expand All @@ -154,13 +167,18 @@ public void actionPerformed(ActionEvent e) {
dist = response;
}


processImplements();

parser.init(pkgText.getText(), implement);
status.setText("Start parsing JSON……");
status.paintImmediately(status.getBounds());

parser.init(pkgText.getText(), implement, listERadioButton.isSelected());
parser.setGenSample(generatorSampleCheckBox.isSelected());
parser.decodeJSONObject(dist);

status.setText("Generating complete.");
status.paintImmediately(status.getBounds());

Messages.showInfoMessage(project, "Generating success!", "Success");
frame.dispose();
File file = new File(basePath + mainClassName + ".java");
Expand Down Expand Up @@ -198,7 +216,7 @@ public void onCommit(String src) {
jsonString = src;
String snapshot;
if (src.length() > 10) {
snapshot = src.substring(0,10);
snapshot = src.substring(0, 10);
} else {
snapshot = src;
}
Expand Down
Loading

0 comments on commit bc618bb

Please sign in to comment.