Skip to content

Commit

Permalink
adding jackson support
Browse files Browse the repository at this point in the history
  • Loading branch information
vinay committed Dec 29, 2017
1 parent 17441d2 commit 3914a00
Show file tree
Hide file tree
Showing 10 changed files with 349 additions and 81 deletions.
282 changes: 218 additions & 64 deletions .idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion DTOnator.iml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/.idea" />
</content>
<orderEntry type="jdk" jdkName="IntelliJ IDEA Community Edition IC-171.4249.39" jdkType="IDEA JDK" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="org" level="project" />
<orderEntry type="library" exported="" name="gson-2.2.4" level="project" />
Expand Down
4 changes: 1 addition & 3 deletions META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
on how to target different products -->
<!--uncomment to enable plugin in all products-->
<depends>com.intellij.modules.lang</depends>
<depends>org.jetbrains.kotlin</depends>

<actions>
<!-- Add your actions here -->
Expand All @@ -59,7 +60,4 @@
<add-to-group group-id="GenerateGroup" anchor="first"/>
</action>
</actions>
<extensions defaultExtensionNs="com.intellij">
<fileTypeFactory implementation="com.intellij.json.JsonFileTypeFactory"/>
</extensions>
</idea-plugin>
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.nvinayshetty.DTOnator.Ui.InputWindow;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.asJava.elements.KtLightElement;
import org.jetbrains.kotlin.idea.KotlinLanguage;
import org.jetbrains.kotlin.idea.internal.Location;
import org.jetbrains.kotlin.psi.KtClass;

/**
Expand All @@ -37,25 +41,62 @@ public UserActionListener() {
super();
}

public static KtClass getKtClassForElement(@NotNull PsiElement psiElement) {
if (psiElement instanceof KtLightElement) {
PsiElement origin = ((KtLightElement) psiElement).getKotlinOrigin();
if (origin != null) {
return getKtClassForElement(origin);
} else {
return null;
}

} else if (psiElement instanceof KtClass && !((KtClass) psiElement).isEnum() &&
!((KtClass) psiElement).isInterface() &&
!((KtClass) psiElement).isAnnotation() &&
!((KtClass) psiElement).isSealed()) {
return (KtClass) psiElement;

} else {
PsiElement parent = psiElement.getParent();
if (parent == null) {
return null;
} else {
return getKtClassForElement(parent);
}
}
}

public void actionPerformed(AnActionEvent event) {
mClass = getPsiClassFromContext(event);
ktClass = getKtClassFromContext(event);
InputWindow dialog;

InputWindow dialog = new InputWindow(mClass);
if (ktClass != null) {
dialog = new InputWindow((PsiClass) ktClass);
} else {
dialog = new InputWindow(mClass);
}
dialog.setVisible(true);
}

@Override
public void update(AnActionEvent event) {
PsiClass psiClass = getPsiClassFromContext(event);
KtClass ktClass = getKtClassFromContext(event);
Presentation presentation = event.getPresentation();
presentation.setEnabled(isGenerateDtoOptionEnabled(psiClass));
if (psiClass != null) {
presentation.setEnabled(isGenerateDtoOptionEnabled(psiClass));
}

if (ktClass != null) {
presentation.setEnabled(isGenerateDtoOptionForKtEnabled(ktClass));
}
}

private boolean isGenerateDtoOptionEnabled(PsiClass psiClass) {
return psiClass != null;
}


private PsiClass getPsiClassFromContext(AnActionEvent e) {
PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
Editor editor = e.getData(PlatformDataKeys.EDITOR);
Expand All @@ -67,4 +108,26 @@ private PsiClass getPsiClassFromContext(AnActionEvent e) {
PsiClass psiClass = PsiTreeUtil.getParentOfType(elementAt, PsiClass.class);
return psiClass;
}

private KtClass getKtClassFromContext(AnActionEvent e) {

final PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
final Editor editor = e.getData(PlatformDataKeys.EDITOR);
if (psiFile == null || editor == null) {
return null;
}

final Location location = Location.fromEditor(editor, editor.getProject());
final PsiElement psiElement = psiFile.findElementAt(location.getStartOffset());
if (psiFile.getLanguage() != KotlinLanguage.INSTANCE) return null;
KtClass ktClass = getKtClassForElement(psiElement);
return ktClass;

}

private boolean isGenerateDtoOptionForKtEnabled(KtClass ktClass) {
return ktClass != null && !ktClass.isEnum() && !ktClass.isInterface() && !ktClass.isAnnotation() && !ktClass.isSealed();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
* Created by vinay on 6/6/15.
*/
public enum FieldType {
GSON, POJO,GSON_EXPOSE
GSON, POJO,GSON_EXPOSE,JACKSON
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public static FieldCreationStrategy getFieldCreatorFor(FieldType fieldType) {
return new SimpleFieldCreator();
case GSON_EXPOSE:
return new ExposedGsonFieldCreator();
case JACKSON:
return new JacksonFieldCreator();


}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2015 Vinaya Prasad N
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.nvinayshetty.DTOnator.FieldCreator;

import com.nvinayshetty.DTOnator.FieldRepresentors.FieldRepresentor;
import com.nvinayshetty.DTOnator.NameConventionCommands.FieldNameParser;
import com.nvinayshetty.DTOnator.nameConflictResolvers.NameConflictResolver;

public class JacksonFieldCreator implements FieldCreationStrategy {
@Override
public String getFieldFor(FieldRepresentor type, AccessModifier accessModifier, String key, FieldNameParser parser, NameConflictResolver nameConflictResolver) {
return type.jacksonFieldRepresentationTemplate(accessModifier, key, parser, nameConflictResolver);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package com.nvinayshetty.DTOnator.FieldRepresentors;

import com.google.gson.annotations.Expose;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.project.Project;
Expand All @@ -33,13 +32,18 @@
* Created by vinay on 6/6/15.
*/
public abstract class FieldRepresentor {
public static final String JACKSON_ANNOTAION_PREFIX = "@com.fasterxml.jackson.annotation.JsonProperty(\"";
protected static final String GSON_ANNOTATION_PREFIX = "@com.google.gson.annotations.SerializedName(\"";
protected static final String ANNOTATION_SUFFIX = "\")\n";
private static final String SPACE = " ";
private static final String CLASS_FIELD_SUFFIX = ";";
private Project project;
private KeywordClassifier keywordClassifier = new KeywordClassifier();

{

}

protected static String suffix(String key) {
return new StringBuilder().append(SPACE).append(key).append(CLASS_FIELD_SUFFIX).toString();
}
Expand All @@ -48,6 +52,10 @@ private static String getGsonAnnotationFor(String key) {
return GSON_ANNOTATION_PREFIX + key + ANNOTATION_SUFFIX;
}

private static String getJacksonAnnotationFor(String key) {
return JACKSON_ANNOTAION_PREFIX + key + ANNOTATION_SUFFIX;
}

public final String fieldCreationTemplate(AccessModifier AccessModifier, String key, FieldNameParser parser, NameConflictResolver nameConflictResolver, KeywordClassifier keywordClassifier) {
String parsedFieldName = parse(key, parser, nameConflictResolver, keywordClassifier);
return getFieldRepresentationFor(AccessModifier, parsedFieldName);
Expand All @@ -58,7 +66,11 @@ public final String gsonFieldRepresentationTemplate(AccessModifier AccessModifie
}

public final String gsonFieldWithExposeAnnotationTemplate(AccessModifier AccessModifier, String key, FieldNameParser parser, NameConflictResolver nameConflictResolver) {
return getExposeAnnotation() +"\n"+getGsonAnnotationFor(key) + fieldCreationTemplate(AccessModifier, key, parser, nameConflictResolver, keywordClassifier);
return getExposeAnnotation() + "\n" + getGsonAnnotationFor(key) + fieldCreationTemplate(AccessModifier, key, parser, nameConflictResolver, keywordClassifier);
}

public final String jacksonFieldRepresentationTemplate(AccessModifier AccessModifier, String key, FieldNameParser parser, NameConflictResolver nameConflictResolver) {
return getJacksonAnnotationFor(key) + fieldCreationTemplate(AccessModifier, key, parser, nameConflictResolver, keywordClassifier);
}

@NotNull
Expand Down
16 changes: 12 additions & 4 deletions src/com/nvinayshetty/DTOnator/Ui/InputWindow.form
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
</component>
</children>
</grid>
<grid id="e2af8" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="e2af8" layout-manager="GridLayoutManager" row-count="4" 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="2" column="1" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
Expand All @@ -116,28 +116,36 @@
<children>
<component id="f0ae3" class="javax.swing.JRadioButton" binding="pojoRadioButton" default-binding="true">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="POJO"/>
</properties>
</component>
<component id="fddf5" class="javax.swing.JRadioButton" binding="gsonRadioButton" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Gson"/>
</properties>
</component>
<component id="d94e5" class="javax.swing.JRadioButton" binding="exposeRadioButton" default-binding="true">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="2" 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="@Expose"/>
</properties>
</component>
<component id="938c3" class="javax.swing.JRadioButton" binding="jacksonRadioButton">
<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="Jackson"/>
</properties>
</component>
</children>
</grid>
<scrollpane id="40739" binding="exceptionLoggerPane" default-binding="true">
Expand Down
8 changes: 5 additions & 3 deletions src/com/nvinayshetty/DTOnator/Ui/InputWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
public class InputWindow extends JFrame {
private PsiClass mClass;
private Project project;
private PsiFile mFile;

private JPanel contentPane;
private JButton buttonCancel;
Expand All @@ -61,7 +60,6 @@ public class InputWindow extends JFrame {
private JRadioButton provideSetter;
private JRadioButton provideGetter;
private JRadioButton exposeRadioButton;

private ButtonGroup classTypeButtonGroup;
private ButtonGroup feedTypeButtonGroup;
private JScrollPane exceptionLoggerPane;
Expand All @@ -70,14 +68,14 @@ public class InputWindow extends JFrame {
private JRadioButton OnCOnflictPrefixFieldNameRadioButton;
private JTextField onConflictprefixString;
private JTextField nameConventionPrefix;
private JRadioButton jacksonRadioButton;
private HashSet<NameConflictResolverCommand> nameConflictResolverCommands;
private HashSet<NameParserCommand> fieldNameParser = new LinkedHashSet<>();


public InputWindow(PsiClass mClass) {
this.mClass = mClass;
project = mClass.getProject();
mFile = mClass.getContainingFile();
setContentPane(contentPane);
inputFeedText.getRootPane().setSize(750, 400);
setSize(1000, 600);
Expand Down Expand Up @@ -159,6 +157,7 @@ public void actionPerformed(ActionEvent e) {
exposeRadioButton.setVisible(false);
}
});

}

private void SetEncapsulationOptionsSelected(boolean condition) {
Expand Down Expand Up @@ -188,6 +187,7 @@ public void actionPerformed(ActionEvent e) {
feedTypeButtonGroup = new ButtonGroup();
feedTypeButtonGroup.add(pojoRadioButton);
feedTypeButtonGroup.add(gsonRadioButton);
feedTypeButtonGroup.add(jacksonRadioButton);
}


Expand Down Expand Up @@ -221,6 +221,8 @@ private HashSet<NameConflictResolverCommand> getNameConflictResolvers() {
}

private FieldType getFieldTYpe() {
if (jacksonRadioButton.isSelected())
return FieldType.JACKSON;
if (pojoRadioButton.isSelected())
return FieldType.POJO;
if (exposeRadioButton.isSelected())
Expand Down

0 comments on commit 3914a00

Please sign in to comment.