├── core ├── src │ ├── test │ │ ├── resources │ │ │ ├── json │ │ │ │ ├── empty_object.json │ │ │ │ ├── array.json │ │ │ │ ├── object.json │ │ │ │ └── complex_array.json │ │ │ └── java │ │ │ │ ├── EmptyObjectTest.java │ │ │ │ ├── ArrayTest.java │ │ │ │ ├── ComplexArrayTest.java │ │ │ │ └── ObjectTest.java │ │ └── java │ │ │ ├── io │ │ │ └── t28 │ │ │ │ └── json2java │ │ │ │ └── core │ │ │ │ ├── json │ │ │ │ ├── JsonNullTest.java │ │ │ │ ├── JsonBooleanTest.java │ │ │ │ ├── JsonNumberTest.java │ │ │ │ ├── JsonStringTest.java │ │ │ │ ├── JsonArrayTest.java │ │ │ │ └── JsonObjectTest.java │ │ │ │ ├── io │ │ │ │ └── JavaBuilderImplTest.java │ │ │ │ ├── annotation │ │ │ │ ├── SuppressWarningsAnnotationPolicyTest.java │ │ │ │ └── GeneratedAnnotationPolicyTest.java │ │ │ │ ├── Assertions.java │ │ │ │ └── StyleTest.java │ │ │ └── com │ │ │ └── squareup │ │ │ └── javapoet │ │ │ ├── TypeSpecAssert.java │ │ │ └── FieldSpecAssert.java │ └── main │ │ └── java │ │ └── io │ │ └── t28 │ │ └── json2java │ │ └── core │ │ ├── annotation │ │ ├── AnnotationPolicy.java │ │ ├── SuppressWarningsAnnotationPolicy.java │ │ └── GeneratedAnnotationPolicy.java │ │ ├── naming │ │ ├── NamePolicy.java │ │ └── DefaultNamePolicy.java │ │ ├── io │ │ ├── JsonParser.java │ │ ├── exception │ │ │ ├── JavaBuildException.java │ │ │ └── JsonParseException.java │ │ ├── JavaBuilder.java │ │ ├── JavaBuilderImpl.java │ │ └── JsonParserImpl.java │ │ ├── json │ │ ├── JsonNull.java │ │ ├── JsonBoolean.java │ │ ├── JsonString.java │ │ ├── JsonNumber.java │ │ ├── JsonArray.java │ │ ├── JsonObject.java │ │ └── JsonValue.java │ │ ├── builder │ │ ├── ModelClassBuilder.java │ │ ├── MoshiClassBuilder.java │ │ ├── GsonClassBuilder.java │ │ ├── JacksonClassBuilder.java │ │ └── ClassBuilder.java │ │ └── Style.java └── build.gradle ├── demo.gif ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .idea ├── copyright │ ├── profiles_settings.xml │ └── Apache_License_2_0.xml ├── encodings.xml ├── vcs.xml ├── misc.xml ├── codeStyleSettings.xml ├── modules │ ├── json2java4idea.iml │ ├── core │ │ ├── core.iml │ │ ├── core_main.iml │ │ └── core_test.iml │ ├── idea │ │ └── idea.iml │ ├── json2java4idea_main.iml │ └── json2java4idea_test.iml ├── compiler.xml ├── runConfigurations │ └── Plugin.xml ├── inspectionProfiles │ └── Project_Default.xml └── modules.xml ├── gradle.properties ├── idea ├── build.gradle └── src │ ├── main │ ├── java │ │ └── io │ │ │ └── t28 │ │ │ └── json2java │ │ │ └── idea │ │ │ ├── exception │ │ │ ├── ClassAlreadyExistsException.java │ │ │ ├── InvalidDirectoryException.java │ │ │ └── ClassCreationException.java │ │ │ ├── validator │ │ │ ├── NullValidator.java │ │ │ ├── NameValidator.java │ │ │ ├── ClassSuffixValidator.java │ │ │ ├── ClassPrefixValidator.java │ │ │ └── JsonValidator.java │ │ │ ├── command │ │ │ ├── CommandActionFactory.java │ │ │ └── NewClassCommandAction.java │ │ │ ├── Json2JavaBundle.java │ │ │ ├── util │ │ │ ├── Extensions.java │ │ │ ├── Formatter.java │ │ │ └── PsiTypeConverter.java │ │ │ ├── naming │ │ │ ├── FieldNamePolicy.java │ │ │ ├── ParameterNamePolicy.java │ │ │ ├── ClassNamePolicy.java │ │ │ └── MethodNamePolicy.java │ │ │ ├── inject │ │ │ ├── GuiceManager.java │ │ │ └── JavaConverterFactory.java │ │ │ ├── setting │ │ │ ├── Json2JavaSettings.java │ │ │ └── TemporaryJson2JavaSettings.java │ │ │ └── view │ │ │ └── NewClassDialog.form │ └── resources │ │ ├── messages │ │ └── Json2Java4IdeaBundle.properties │ │ └── META-INF │ │ └── plugin.xml │ └── test │ └── java │ ├── io │ └── t28 │ │ └── json2java │ │ └── idea │ │ ├── Assertions.java │ │ ├── validator │ │ ├── NullValidatorTest.java │ │ ├── ClassSuffixValidatorTest.java │ │ ├── ClassPrefixValidatorTest.java │ │ └── NameValidatorTest.java │ │ ├── util │ │ ├── FormatterTest.java │ │ ├── ExtensionsTest.java │ │ └── PsiTypeConverterTest.java │ │ ├── naming │ │ ├── ParameterNamePolicyTest.java │ │ ├── FieldNamePolicyTest.java │ │ ├── ClassNamePolicyTest.java │ │ └── MethodNamePolicyTest.java │ │ ├── IdeaProjectTest.java │ │ └── setting │ │ └── TemporaryJson2JavaSettingsTest.java │ └── org │ └── jdom │ └── ElementAssert.java ├── circle.yml ├── .gitignore ├── gradlew.bat └── README.md /core/src/test/resources/json/empty_object.json: -------------------------------------------------------------------------------- 1 | {} -------------------------------------------------------------------------------- /demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/t28hub/json2java4idea/HEAD/demo.gif -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'json2java4idea' 2 | include 'core' 3 | include 'idea' 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/t28hub/json2java4idea/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /core/src/test/resources/json/array.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "name": "foo" 4 | }, 5 | { 6 | "name": "bar" 7 | }, 8 | { 9 | "name": "baz" 10 | } 11 | ] -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | org.gradle.daemon=true 2 | org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 3 | org.gradle.parallel=true 4 | org.gradle.workers.max=9 5 | org.gradle.configureondemand=true 6 | -------------------------------------------------------------------------------- /core/src/test/resources/json/object.json: -------------------------------------------------------------------------------- 1 | { 2 | "null_value": null, 3 | "number_value": 1024, 4 | "string_value": "string", 5 | "array_value": [ 6 | "foo", 7 | "bar", 8 | "baz" 9 | ], 10 | "object_value": { 11 | "name": "value" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Jan 18 23:57:17 JST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-bin.zip 7 | -------------------------------------------------------------------------------- /core/src/test/resources/java/EmptyObjectTest.java: -------------------------------------------------------------------------------- 1 | package io.t28.test; 2 | 3 | import javax.annotation.Generated; 4 | 5 | @SuppressWarnings("all") 6 | @Generated("io.t28.json2java.core.JavaConverter") 7 | public class EmptyObjectTest { 8 | public EmptyObjectTest() { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /core/build.gradle: -------------------------------------------------------------------------------- 1 | dependencies { 2 | compile 'org.jooq:jool:0.9.12' 3 | compile 'com.google.guava:guava:21.0' 4 | compile 'com.google.code.findbugs:jsr305:3.0.1' 5 | compile 'com.google.code.gson:gson:2.8.0' 6 | compile 'com.squareup:javapoet:1.8.0' 7 | compile 'com.squareup.moshi:moshi:1.4.0' 8 | compile 'com.fasterxml.jackson.core:jackson-databind:2.8.6' 9 | } 10 | -------------------------------------------------------------------------------- /core/src/test/resources/java/ArrayTest.java: -------------------------------------------------------------------------------- 1 | package io.t28.test; 2 | 3 | import javax.annotation.Generated; 4 | 5 | @SuppressWarnings("all") 6 | @Generated("io.t28.json2java.core.JavaConverter") 7 | public class ArrayTest { 8 | private final String name; 9 | 10 | public ArrayTest(String name) { 11 | this.name = name; 12 | } 13 | 14 | public String getName() { 15 | return name; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /.idea/codeStyleSettings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 15 | -------------------------------------------------------------------------------- /.idea/modules/json2java4idea.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/modules/core/core.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/modules/idea/idea.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /core/src/test/resources/json/complex_array.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "name": "foo", 4 | "array": [ 5 | 1, 6 | 2 7 | ], 8 | "objects": [ 9 | { 10 | "key": "qux" 11 | } 12 | ], 13 | "nested": [ 14 | [ 15 | null 16 | ] 17 | ] 18 | }, 19 | { 20 | "name": "bar", 21 | "array": [ 22 | 3, 23 | 4 24 | ], 25 | "objects": [ 26 | { 27 | "key": "quux" 28 | } 29 | ], 30 | "nested": [ 31 | [ 32 | null 33 | ] 34 | ] 35 | }, 36 | { 37 | "name": "baz", 38 | "array": [ 39 | 5, 40 | 6 41 | ], 42 | "objects": [ 43 | { 44 | "key": "corge" 45 | } 46 | ], 47 | "nested": [ 48 | [ 49 | null 50 | ] 51 | ] 52 | } 53 | ] -------------------------------------------------------------------------------- /.idea/copyright/Apache_License_2_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /idea/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | maven { 4 | url 'http://dl.bintray.com/jetbrains/intellij-plugin-service' 5 | } 6 | } 7 | } 8 | 9 | plugins { 10 | id 'org.jetbrains.intellij' version '0.2.5' 11 | } 12 | 13 | intellij { 14 | version 'IC-2017.2' 15 | pluginName 'Json2Java4Idea' 16 | updateSinceUntilBuild false 17 | sameSinceUntilBuild false 18 | 19 | publish { 20 | if (project.hasProperty('INTELLIJ_USERNAME')) { 21 | username INTELLIJ_USERNAME 22 | password INTELLIJ_PASSWORD 23 | pluginId INTELLIJ_PLUGIN_ID 24 | } 25 | } 26 | } 27 | 28 | dependencies { 29 | compile project(':core') 30 | compile 'com.google.inject:guice:4.1.0' 31 | compile 'com.google.inject.extensions:guice-assistedinject:4.1.0' 32 | 33 | testCompile 'org.assertj:assertj-swing:3.5.0' 34 | } 35 | -------------------------------------------------------------------------------- /.idea/runConfigurations/Plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 12 | 17 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /.idea/modules/json2java4idea_main.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /core/src/main/java/io/t28/json2java/core/annotation/AnnotationPolicy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.annotation; 18 | 19 | import com.squareup.javapoet.TypeSpec; 20 | 21 | import javax.annotation.Nonnull; 22 | 23 | public interface AnnotationPolicy { 24 | void apply(@Nonnull TypeSpec.Builder builder); 25 | } 26 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 14 | -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/exception/ClassAlreadyExistsException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.exception; 18 | 19 | import javax.annotation.Nonnull; 20 | 21 | public class ClassAlreadyExistsException extends ClassCreationException { 22 | public ClassAlreadyExistsException(@Nonnull String message) { 23 | super(message); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /core/src/main/java/io/t28/json2java/core/naming/NamePolicy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.naming; 18 | 19 | import com.squareup.javapoet.TypeName; 20 | 21 | import javax.annotation.CheckReturnValue; 22 | import javax.annotation.Nonnull; 23 | 24 | public interface NamePolicy { 25 | @Nonnull 26 | @CheckReturnValue 27 | String convert(@Nonnull String name, @Nonnull TypeName type); 28 | } 29 | -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/exception/InvalidDirectoryException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.exception; 18 | 19 | import javax.annotation.Nonnull; 20 | 21 | public class InvalidDirectoryException extends ClassCreationException { 22 | private static final long serialVersionUID = 8602840574745874176L; 23 | 24 | public InvalidDirectoryException(@Nonnull String message) { 25 | super(message); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /core/src/main/java/io/t28/json2java/core/io/JsonParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.io; 18 | 19 | import io.t28.json2java.core.io.exception.JsonParseException; 20 | import io.t28.json2java.core.json.JsonValue; 21 | 22 | import javax.annotation.CheckReturnValue; 23 | import javax.annotation.Nonnull; 24 | 25 | public interface JsonParser { 26 | @Nonnull 27 | @CheckReturnValue 28 | JsonValue parse(@Nonnull String json) throws JsonParseException; 29 | } 30 | -------------------------------------------------------------------------------- /.idea/modules/json2java4idea_test.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /core/src/main/java/io/t28/json2java/core/io/exception/JavaBuildException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.io.exception; 18 | 19 | import javax.annotation.Nonnull; 20 | import java.io.IOException; 21 | 22 | public class JavaBuildException extends IOException { 23 | private static final long serialVersionUID = 5642915429649238000L; 24 | 25 | public JavaBuildException(@Nonnull String message, @Nonnull Throwable cause) { 26 | super(message, cause); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /core/src/main/java/io/t28/json2java/core/io/exception/JsonParseException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.io.exception; 18 | 19 | import javax.annotation.Nonnull; 20 | import java.io.IOException; 21 | 22 | public class JsonParseException extends IOException { 23 | private static final long serialVersionUID = 1477109671450199015L; 24 | 25 | public JsonParseException(@Nonnull String message, @Nonnull Throwable cause) { 26 | super(message, cause); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/exception/ClassCreationException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.exception; 18 | 19 | import javax.annotation.Nonnull; 20 | 21 | public class ClassCreationException extends Exception { 22 | public ClassCreationException(@Nonnull String message) { 23 | super(message); 24 | } 25 | 26 | public ClassCreationException(@Nonnull String message, @Nonnull Throwable cause) { 27 | super(message, cause); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /core/src/main/java/io/t28/json2java/core/io/JavaBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.io; 18 | 19 | import com.squareup.javapoet.TypeSpec; 20 | import io.t28.json2java.core.io.exception.JavaBuildException; 21 | 22 | import javax.annotation.CheckReturnValue; 23 | import javax.annotation.Nonnull; 24 | 25 | public interface JavaBuilder { 26 | @Nonnull 27 | @CheckReturnValue 28 | String build(@Nonnull String packageName, @Nonnull TypeSpec typeSpec) throws JavaBuildException; 29 | } 30 | -------------------------------------------------------------------------------- /core/src/main/java/io/t28/json2java/core/json/JsonNull.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.json; 18 | 19 | import com.squareup.javapoet.TypeName; 20 | 21 | import javax.annotation.Nonnull; 22 | import javax.annotation.Nullable; 23 | 24 | public class JsonNull extends JsonValue { 25 | @Nonnull 26 | @Override 27 | public TypeName getType() { 28 | return TypeName.OBJECT; 29 | } 30 | 31 | @Nullable 32 | @Override 33 | public Object getValue() { 34 | return null; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/validator/NullValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.validator; 18 | 19 | import com.intellij.openapi.ui.InputValidator; 20 | import org.jetbrains.annotations.Nullable; 21 | 22 | public class NullValidator implements InputValidator { 23 | @Override 24 | public boolean checkInput(@Nullable String text) { 25 | return false; 26 | } 27 | 28 | @Override 29 | public boolean canClose(@Nullable String text) { 30 | return false; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /idea/src/test/java/io/t28/json2java/idea/Assertions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea; 18 | 19 | import org.jdom.Element; 20 | import org.jdom.ElementAssert; 21 | 22 | import javax.annotation.CheckReturnValue; 23 | import javax.annotation.Nonnull; 24 | import javax.annotation.Nullable; 25 | 26 | public class Assertions { 27 | private Assertions() { 28 | } 29 | 30 | @Nonnull 31 | @CheckReturnValue 32 | public static ElementAssert assertThat(@Nullable Element actual) { 33 | return new ElementAssert(actual); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /core/src/main/java/io/t28/json2java/core/json/JsonBoolean.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.json; 18 | 19 | import com.squareup.javapoet.TypeName; 20 | 21 | import javax.annotation.Nonnull; 22 | 23 | public class JsonBoolean extends JsonValue { 24 | private final boolean value; 25 | 26 | public JsonBoolean(boolean value) { 27 | this.value = value; 28 | } 29 | 30 | @Nonnull 31 | @Override 32 | public TypeName getType() { 33 | return TypeName.BOOLEAN; 34 | } 35 | 36 | @Nonnull 37 | @Override 38 | public Boolean getValue() { 39 | return value; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /core/src/main/java/io/t28/json2java/core/json/JsonString.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.json; 18 | 19 | import com.squareup.javapoet.ClassName; 20 | import com.squareup.javapoet.TypeName; 21 | 22 | import javax.annotation.Nonnull; 23 | 24 | public class JsonString extends JsonValue { 25 | private final String value; 26 | 27 | public JsonString(@Nonnull String value) { 28 | this.value = value; 29 | } 30 | 31 | @Nonnull 32 | @Override 33 | public TypeName getType() { 34 | return ClassName.get(String.class); 35 | } 36 | 37 | @Nonnull 38 | @Override 39 | public String getValue() { 40 | return value; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /core/src/test/resources/java/ComplexArrayTest.java: -------------------------------------------------------------------------------- 1 | package io.t28.test; 2 | 3 | import java.util.List; 4 | import javax.annotation.Generated; 5 | 6 | @SuppressWarnings("all") 7 | @Generated("io.t28.json2java.core.JavaConverter") 8 | public class ComplexArrayTest { 9 | private final String name; 10 | 11 | private final List array; 12 | 13 | private final List objects; 14 | 15 | private final List> nested; 16 | 17 | public ComplexArrayTest(String name, List array, List objects, 18 | List> nested) { 19 | this.name = name; 20 | this.array = array; 21 | this.objects = objects; 22 | this.nested = nested; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public List getArray() { 30 | return array; 31 | } 32 | 33 | public List getObjects() { 34 | return objects; 35 | } 36 | 37 | public List> getNested() { 38 | return nested; 39 | } 40 | 41 | public static class Objects { 42 | private final String key; 43 | 44 | public Objects(String key) { 45 | this.key = key; 46 | } 47 | 48 | public String getKey() { 49 | return key; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/command/CommandActionFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.command; 18 | 19 | import com.google.inject.assistedinject.Assisted; 20 | import com.intellij.psi.PsiDirectory; 21 | import io.t28.json2java.core.JavaConverter; 22 | 23 | import javax.annotation.CheckReturnValue; 24 | import javax.annotation.Nonnull; 25 | 26 | public interface CommandActionFactory { 27 | @Nonnull 28 | @CheckReturnValue 29 | NewClassCommandAction create( 30 | @Nonnull @Assisted("Name") String name, 31 | @Nonnull @Assisted("Json") String json, 32 | @Nonnull PsiDirectory directory, 33 | @Nonnull JavaConverter converter 34 | ); 35 | } 36 | -------------------------------------------------------------------------------- /core/src/main/java/io/t28/json2java/core/json/JsonNumber.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.json; 18 | 19 | import com.squareup.javapoet.TypeName; 20 | 21 | import javax.annotation.Nonnull; 22 | import java.lang.reflect.Type; 23 | 24 | public class JsonNumber extends JsonValue { 25 | private final Type type; 26 | private final Number value; 27 | 28 | public JsonNumber(@Nonnull Type type, @Nonnull Number value) { 29 | this.type = type; 30 | this.value = value; 31 | } 32 | 33 | @Nonnull 34 | @Override 35 | public TypeName getType() { 36 | return TypeName.get(type); 37 | } 38 | 39 | @Nonnull 40 | @Override 41 | public Number getValue() { 42 | return value; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /circle.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | jobs: 3 | build: 4 | working_directory: ~/json2java 5 | docker: 6 | - image: openjdk:8 7 | steps: 8 | - type: shell 9 | name: Set up the build environment 10 | command: apt-get -qq update; apt-get -y install git 11 | - type: checkout 12 | - type: shell 13 | name: Set up the Gradle build 14 | command: chmod +x ./gradlew; ./gradlew --version 15 | - type: shell 16 | name: Assemble the outputs of this project 17 | command: ./gradlew :core:build --full-stacktrace; ./gradlew :idea:buildPlugin --full-stacktrace 18 | - type: shell 19 | name: Run all checks including unit test and static code analysis 20 | command: ./gradlew check --full-stacktrace 21 | - type: shell 22 | name: Upload code coverage report to codacy 23 | command: ./gradlew sendCoverageToCodacy 24 | - type: test-results-store 25 | path: core/build/test-results/ 26 | - type: test-results-store 27 | path: idea/build/test-results/ 28 | - type: artifacts-store 29 | path: core/build/reports/jacoco/test 30 | destination: core/jacoco 31 | - type: artifacts-store 32 | path: idea/build/reports/jacoco/test 33 | destination: idea/jacoco 34 | - type: artifacts-store 35 | path: idea/build/distributions 36 | destination: idea 37 | -------------------------------------------------------------------------------- /core/src/main/java/io/t28/json2java/core/annotation/SuppressWarningsAnnotationPolicy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.annotation; 18 | 19 | import com.squareup.javapoet.AnnotationSpec; 20 | import com.squareup.javapoet.TypeSpec; 21 | 22 | import javax.annotation.Nonnull; 23 | 24 | public class SuppressWarningsAnnotationPolicy implements AnnotationPolicy { 25 | private final String value; 26 | 27 | public SuppressWarningsAnnotationPolicy(@Nonnull String value) { 28 | this.value = value; 29 | } 30 | 31 | @Override 32 | public void apply(@Nonnull TypeSpec.Builder builder) { 33 | builder.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class) 34 | .addMember("value", "$S", value) 35 | .build()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /core/src/main/java/io/t28/json2java/core/annotation/GeneratedAnnotationPolicy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.annotation; 18 | 19 | import com.squareup.javapoet.AnnotationSpec; 20 | import com.squareup.javapoet.TypeSpec; 21 | 22 | import javax.annotation.Generated; 23 | import javax.annotation.Nonnull; 24 | 25 | public class GeneratedAnnotationPolicy implements AnnotationPolicy { 26 | private final Class klass; 27 | 28 | public GeneratedAnnotationPolicy(@Nonnull Class klass) { 29 | this.klass = klass; 30 | } 31 | 32 | @Override 33 | public void apply(@Nonnull TypeSpec.Builder builder) { 34 | builder.addAnnotation(AnnotationSpec.builder(Generated.class) 35 | .addMember("value", "$S", klass.getCanonicalName()) 36 | .build()); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /core/src/main/java/io/t28/json2java/core/io/JavaBuilderImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.io; 18 | 19 | import com.squareup.javapoet.JavaFile; 20 | import com.squareup.javapoet.TypeSpec; 21 | import io.t28.json2java.core.io.JavaBuilder; 22 | import io.t28.json2java.core.io.exception.JavaBuildException; 23 | 24 | import javax.annotation.Nonnull; 25 | 26 | public class JavaBuilderImpl implements JavaBuilder { 27 | private static final String INDENT = " "; 28 | 29 | @Nonnull 30 | @Override 31 | public String build(@Nonnull String packageName, @Nonnull TypeSpec typeSpec) throws JavaBuildException { 32 | return JavaFile.builder(packageName, typeSpec) 33 | .indent(INDENT) 34 | .skipJavaLangImports(true) 35 | .build() 36 | .toString(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/Json2JavaBundle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea; 18 | 19 | import com.intellij.AbstractBundle; 20 | import org.jetbrains.annotations.PropertyKey; 21 | 22 | import javax.annotation.CheckReturnValue; 23 | import javax.annotation.Nonnull; 24 | 25 | public class Json2JavaBundle extends AbstractBundle { 26 | private static final String BUNDLE = "messages.Json2Java4IdeaBundle"; 27 | 28 | public Json2JavaBundle() { 29 | super(BUNDLE); 30 | } 31 | 32 | @Nonnull 33 | public static Json2JavaBundle getInstance() { 34 | return new Json2JavaBundle(); 35 | } 36 | 37 | @Nonnull 38 | @CheckReturnValue 39 | public String message(@Nonnull @PropertyKey(resourceBundle = BUNDLE) String key, @Nonnull Object... objects) { 40 | return getMessage(key, objects); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /core/src/test/resources/java/ObjectTest.java: -------------------------------------------------------------------------------- 1 | package io.t28.test; 2 | 3 | import java.util.List; 4 | import javax.annotation.Generated; 5 | 6 | @SuppressWarnings("all") 7 | @Generated("io.t28.json2java.core.JavaConverter") 8 | public class ObjectTest { 9 | private final Object nullValue; 10 | 11 | private final int numberValue; 12 | 13 | private final String stringValue; 14 | 15 | private final List arrayValue; 16 | 17 | private final ObjectValue objectValue; 18 | 19 | public ObjectTest(Object nullValue, int numberValue, String stringValue, 20 | List arrayValue, ObjectValue objectValue) { 21 | this.nullValue = nullValue; 22 | this.numberValue = numberValue; 23 | this.stringValue = stringValue; 24 | this.arrayValue = arrayValue; 25 | this.objectValue = objectValue; 26 | } 27 | 28 | public Object getNullValue() { 29 | return nullValue; 30 | } 31 | 32 | public int getNumberValue() { 33 | return numberValue; 34 | } 35 | 36 | public String getStringValue() { 37 | return stringValue; 38 | } 39 | 40 | public List getArrayValue() { 41 | return arrayValue; 42 | } 43 | 44 | public ObjectValue getObjectValue() { 45 | return objectValue; 46 | } 47 | 48 | public static class ObjectValue { 49 | private final String name; 50 | 51 | public ObjectValue(String name) { 52 | this.name = name; 53 | } 54 | 55 | public String getName() { 56 | return name; 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /core/src/test/java/io/t28/json2java/core/json/JsonNullTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.json; 18 | 19 | import com.squareup.javapoet.TypeName; 20 | import org.junit.Before; 21 | import org.junit.Test; 22 | 23 | import static io.t28.json2java.core.Assertions.assertThat; 24 | 25 | public class JsonNullTest { 26 | private JsonNull underTest; 27 | 28 | @Before 29 | public void setUp() throws Exception { 30 | underTest = new JsonNull(); 31 | } 32 | 33 | @Test 34 | public void getType() throws Exception { 35 | // exercise 36 | final TypeName actual = underTest.getType(); 37 | 38 | // verify 39 | assertThat(actual) 40 | .isEqualTo(TypeName.OBJECT); 41 | } 42 | 43 | @Test 44 | public void getValue() throws Exception { 45 | // exercise 46 | final Object actual = underTest.getValue(); 47 | 48 | // verify 49 | assertThat(actual) 50 | .isNull(); 51 | } 52 | } -------------------------------------------------------------------------------- /core/src/test/java/io/t28/json2java/core/json/JsonBooleanTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.json; 18 | 19 | import com.squareup.javapoet.TypeName; 20 | import org.junit.Before; 21 | import org.junit.Test; 22 | 23 | import static io.t28.json2java.core.Assertions.assertThat; 24 | 25 | public class JsonBooleanTest { 26 | private JsonBoolean underTest; 27 | 28 | @Before 29 | public void setUp() throws Exception { 30 | underTest = new JsonBoolean(true); 31 | } 32 | 33 | @Test 34 | public void getType() throws Exception { 35 | // exercise 36 | final TypeName actual = underTest.getType(); 37 | 38 | // verify 39 | assertThat(actual) 40 | .isEqualTo(TypeName.BOOLEAN); 41 | } 42 | 43 | @Test 44 | public void getValue() throws Exception { 45 | // exercise 46 | final Boolean actual = underTest.getValue(); 47 | 48 | // verify 49 | assertThat(actual) 50 | .isNotNull() 51 | .isTrue(); 52 | } 53 | } -------------------------------------------------------------------------------- /core/src/test/java/io/t28/json2java/core/io/JavaBuilderImplTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.io; 18 | 19 | import com.squareup.javapoet.TypeSpec; 20 | import org.junit.Before; 21 | import org.junit.Test; 22 | 23 | import javax.lang.model.element.Modifier; 24 | 25 | import static io.t28.json2java.core.Assertions.assertThat; 26 | 27 | public class JavaBuilderImplTest { 28 | private JavaBuilderImpl underTest; 29 | 30 | @Before 31 | public void setUp() throws Exception { 32 | underTest = new JavaBuilderImpl(); 33 | } 34 | 35 | @Test 36 | public void buildShouldReturnJavaCode() throws Exception { 37 | final TypeSpec typeSpec = TypeSpec.classBuilder("Test") 38 | .addModifiers(Modifier.PUBLIC) 39 | .build(); 40 | 41 | // exercise 42 | final String actual = underTest.build("io.t28.example", typeSpec); 43 | 44 | // verify 45 | assertThat(actual) 46 | .isEqualTo("package io.t28.example;\n\npublic class Test {\n}\n"); 47 | } 48 | } -------------------------------------------------------------------------------- /core/src/test/java/io/t28/json2java/core/json/JsonNumberTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.json; 18 | 19 | import com.squareup.javapoet.TypeName; 20 | import org.junit.Before; 21 | import org.junit.Test; 22 | 23 | import static io.t28.json2java.core.Assertions.assertThat; 24 | 25 | public class JsonNumberTest { 26 | private JsonNumber underTest; 27 | 28 | @Before 29 | public void setUp() throws Exception { 30 | underTest = new JsonNumber(int.class, 42); 31 | } 32 | 33 | @Test 34 | public void getType() throws Exception { 35 | // exercise 36 | final TypeName actual = underTest.getType(); 37 | 38 | // verify 39 | assertThat(actual) 40 | .isEqualTo(TypeName.INT); 41 | } 42 | 43 | @Test 44 | public void getValue() throws Exception { 45 | // exercise 46 | final Number actual = underTest.getValue(); 47 | 48 | // verify 49 | assertThat(actual) 50 | .isInstanceOf(Integer.class) 51 | .isEqualTo(42); 52 | } 53 | } -------------------------------------------------------------------------------- /core/src/test/java/io/t28/json2java/core/json/JsonStringTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.json; 18 | 19 | import com.squareup.javapoet.ClassName; 20 | import com.squareup.javapoet.TypeName; 21 | import org.junit.Before; 22 | import org.junit.Test; 23 | 24 | import static io.t28.json2java.core.Assertions.assertThat; 25 | 26 | public class JsonStringTest { 27 | private JsonString underTest; 28 | 29 | @Before 30 | public void setUp() throws Exception { 31 | underTest = new JsonString("foo"); 32 | } 33 | 34 | @Test 35 | public void getType() throws Exception { 36 | // exercise 37 | final TypeName actual = underTest.getType(); 38 | 39 | // verify 40 | assertThat(actual) 41 | .isEqualTo(ClassName.get(String.class)); 42 | } 43 | 44 | @Test 45 | public void getValue() throws Exception { 46 | // exercise 47 | final String actual = underTest.getValue(); 48 | 49 | // verify 50 | assertThat(actual) 51 | .isEqualTo("foo"); 52 | } 53 | } -------------------------------------------------------------------------------- /core/src/main/java/io/t28/json2java/core/json/JsonArray.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.json; 18 | 19 | import com.google.common.collect.ImmutableList; 20 | import com.squareup.javapoet.ParameterizedTypeName; 21 | import com.squareup.javapoet.TypeName; 22 | 23 | import javax.annotation.CheckReturnValue; 24 | import javax.annotation.Nonnull; 25 | import java.util.LinkedList; 26 | import java.util.List; 27 | import java.util.stream.Stream; 28 | 29 | public class JsonArray extends JsonValue { 30 | private final List value; 31 | 32 | public JsonArray(@Nonnull List value) { 33 | this.value = new LinkedList<>(value); 34 | } 35 | 36 | @Nonnull 37 | @Override 38 | public TypeName getType() { 39 | return ParameterizedTypeName.get(List.class, Object.class); 40 | } 41 | 42 | @Nonnull 43 | @Override 44 | public List getValue() { 45 | return ImmutableList.copyOf(value); 46 | } 47 | 48 | @Nonnull 49 | @CheckReturnValue 50 | public Stream stream() { 51 | return value.stream().map(JsonValue::wrap); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /core/src/main/java/io/t28/json2java/core/io/JsonParserImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.io; 18 | 19 | import com.fasterxml.jackson.databind.ObjectMapper; 20 | import com.google.common.annotations.VisibleForTesting; 21 | import io.t28.json2java.core.io.exception.JsonParseException; 22 | import io.t28.json2java.core.json.JsonValue; 23 | 24 | import javax.annotation.Nonnull; 25 | import java.io.IOException; 26 | 27 | public class JsonParserImpl implements JsonParser { 28 | private final ObjectMapper mapper; 29 | 30 | public JsonParserImpl() { 31 | this(new ObjectMapper()); 32 | } 33 | 34 | @VisibleForTesting 35 | JsonParserImpl(@Nonnull ObjectMapper mapper) { 36 | this.mapper = mapper; 37 | } 38 | 39 | @Nonnull 40 | @Override 41 | public JsonValue parse(@Nonnull String json) throws JsonParseException { 42 | try { 43 | final Object parsed = mapper.readValue(json, Object.class); 44 | return JsonValue.wrap(parsed); 45 | } catch (IOException e) { 46 | throw new JsonParseException("Unable to parse a JSON string(" + json + ")", e); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /core/src/test/java/io/t28/json2java/core/annotation/SuppressWarningsAnnotationPolicyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.annotation; 18 | 19 | import com.squareup.javapoet.AnnotationSpec; 20 | import com.squareup.javapoet.TypeSpec; 21 | import org.junit.Before; 22 | import org.junit.Test; 23 | 24 | import static io.t28.json2java.core.Assertions.assertThat; 25 | 26 | public class SuppressWarningsAnnotationPolicyTest { 27 | private SuppressWarningsAnnotationPolicy underTest; 28 | 29 | @Before 30 | public void setUp() throws Exception { 31 | underTest = new SuppressWarningsAnnotationPolicy("all"); 32 | } 33 | 34 | @Test 35 | public void applyShouldAddGeneratedAnnotation() throws Exception { 36 | // setup 37 | final TypeSpec.Builder builder = TypeSpec.classBuilder("Test"); 38 | 39 | // exercise 40 | underTest.apply(builder); 41 | 42 | // verify 43 | assertThat(builder.build()) 44 | .hasName("Test") 45 | .hasAnnotation(AnnotationSpec.builder(SuppressWarnings.class) 46 | .addMember("value", "$S", "all") 47 | .build()); 48 | } 49 | } -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/util/Extensions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.util; 18 | 19 | import com.intellij.openapi.fileTypes.FileType; 20 | 21 | import javax.annotation.CheckReturnValue; 22 | import javax.annotation.Nonnull; 23 | 24 | public final class Extensions { 25 | private static final String DELIMITER = "."; 26 | 27 | private Extensions() { 28 | } 29 | 30 | @Nonnull 31 | @CheckReturnValue 32 | public static String remove(@Nonnull String fileName, @Nonnull FileType fileType) { 33 | final String extension = fileType.getDefaultExtension(); 34 | if (fileName.endsWith(DELIMITER + extension)) { 35 | return fileName.substring(0, fileName.length() - (extension.length() + 1)); 36 | } 37 | return fileName; 38 | } 39 | 40 | @Nonnull 41 | @CheckReturnValue 42 | public static String append(@Nonnull String fileName, @Nonnull FileType fileType) { 43 | final String extension = fileType.getDefaultExtension(); 44 | if (fileName.endsWith(DELIMITER + extension)) { 45 | return fileName; 46 | } 47 | return fileName + DELIMITER + extension; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /core/src/test/java/io/t28/json2java/core/annotation/GeneratedAnnotationPolicyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.annotation; 18 | 19 | import com.squareup.javapoet.AnnotationSpec; 20 | import com.squareup.javapoet.TypeSpec; 21 | import org.junit.Before; 22 | import org.junit.Test; 23 | 24 | import javax.annotation.Generated; 25 | 26 | import static io.t28.json2java.core.Assertions.assertThat; 27 | 28 | public class GeneratedAnnotationPolicyTest { 29 | private GeneratedAnnotationPolicy underTest; 30 | 31 | @Before 32 | public void setUp() throws Exception { 33 | underTest = new GeneratedAnnotationPolicy(this.getClass()); 34 | } 35 | 36 | @Test 37 | public void applyShouldAddGeneratedAnnotation() throws Exception { 38 | // setup 39 | final TypeSpec.Builder builder = TypeSpec.classBuilder("Test"); 40 | 41 | // exercise 42 | underTest.apply(builder); 43 | 44 | // verify 45 | assertThat(builder.build()) 46 | .hasName("Test") 47 | .hasAnnotation(AnnotationSpec.builder(Generated.class) 48 | .addMember("value", "$S", this.getClass().getCanonicalName()) 49 | .build()); 50 | } 51 | } -------------------------------------------------------------------------------- /idea/src/test/java/io/t28/json2java/idea/validator/NullValidatorTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.validator; 18 | 19 | import org.junit.Before; 20 | import org.junit.Test; 21 | 22 | import static org.assertj.core.api.Assertions.assertThat; 23 | 24 | public class NullValidatorTest { 25 | private NullValidator underTest; 26 | 27 | @Before 28 | public void setUp() throws Exception { 29 | underTest = new NullValidator(); 30 | } 31 | 32 | @Test 33 | public void checkInputShouldAlwaysReturnFalse() throws Exception { 34 | // exercise 35 | final boolean actual = underTest.checkInput("text"); 36 | 37 | // verify 38 | assertThat(actual) 39 | .overridingErrorMessage("Expected value returned by checkInput is false but was true") 40 | .isFalse(); 41 | } 42 | 43 | @Test 44 | public void canCloseShouldAlwaysReturnFalse() throws Exception { 45 | // exercise 46 | final boolean actual = underTest.canClose("text"); 47 | 48 | // verify 49 | assertThat(actual) 50 | .overridingErrorMessage("Expected value returned by canClose is false but was true") 51 | .isFalse(); 52 | } 53 | } -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/util/Formatter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.util; 18 | 19 | import com.intellij.openapi.fileTypes.FileType; 20 | import com.intellij.psi.PsiFile; 21 | import com.intellij.psi.PsiFileFactory; 22 | import com.intellij.psi.codeStyle.CodeStyleManager; 23 | import org.jetbrains.annotations.NotNull; 24 | 25 | import javax.annotation.CheckReturnValue; 26 | import javax.annotation.Nonnull; 27 | 28 | public class Formatter { 29 | private static final String NAME = "temporary"; 30 | 31 | private final PsiFileFactory fileFactory; 32 | private final FileType fileType; 33 | 34 | public Formatter(@Nonnull PsiFileFactory fileFactory, @Nonnull FileType fileType) { 35 | this.fileFactory = fileFactory; 36 | this.fileType = fileType; 37 | } 38 | 39 | @NotNull 40 | @CheckReturnValue 41 | public String format(@Nonnull String text) { 42 | final String name = Extensions.append(NAME, fileType); 43 | final PsiFile file = fileFactory.createFileFromText(name, fileType, text); 44 | final CodeStyleManager styleManager = CodeStyleManager.getInstance(file.getProject()); 45 | styleManager.reformat(file); 46 | return file.getText(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### OS X template 2 | *.DS_Store 3 | .AppleDouble 4 | .LSOverride 5 | 6 | # Icon must end with two \r 7 | Icon 8 | 9 | # Thumbnails 10 | ._* 11 | 12 | # Files that might appear in the root of a volume 13 | .DocumentRevisions-V100 14 | .fseventsd 15 | .Spotlight-V100 16 | .TemporaryItems 17 | .Trashes 18 | .VolumeIcon.icns 19 | .com.apple.timemachine.donotpresent 20 | 21 | # Directories potentially created on remote AFP share 22 | .AppleDB 23 | .AppleDesktop 24 | Network Trash Folder 25 | Temporary Items 26 | .apdisk 27 | 28 | 29 | ### Vim template 30 | # swap 31 | [._]*.s[a-w][a-z] 32 | [._]s[a-w][a-z] 33 | # session 34 | Session.vim 35 | # temporary 36 | .netrwhist 37 | *~ 38 | # auto-generated tag files 39 | tags 40 | 41 | 42 | ### JetBrains template 43 | # User-specific stuff: 44 | .idea/workspace.xml 45 | .idea/tasks.xml 46 | .idea/dictionaries/* 47 | 48 | # Sensitive or high-churn files: 49 | .idea/dataSources/ 50 | .idea/dataSources.ids 51 | .idea/dataSources.xml 52 | .idea/dataSources.local.xml 53 | .idea/sqlDataSources.xml 54 | .idea/dynamic.xml 55 | .idea/uiDesigner.xml 56 | 57 | # Gradle: 58 | .idea/gradle.xml 59 | .idea/libraries 60 | 61 | ## File-based project format: 62 | *.iws 63 | 64 | ## Plugin-specific files: 65 | # IntelliJ 66 | /out/ 67 | 68 | # mpeltonen/sbt-idea plugin 69 | .idea_modules/ 70 | 71 | 72 | ### Java template 73 | *.class 74 | 75 | # BlueJ files 76 | *.ctxt 77 | 78 | # Mobile Tools for Java (J2ME) 79 | .mtj.tmp/ 80 | 81 | # Package Files # 82 | *.jar 83 | *.war 84 | *.ear 85 | 86 | 87 | ### Gradle template 88 | .gradle 89 | build/ 90 | */build/ 91 | 92 | # Ignore Gradle GUI config 93 | gradle-app.setting 94 | 95 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 96 | !gradle-wrapper.jar 97 | 98 | # Cache of project 99 | .gradletasknamecache 100 | -------------------------------------------------------------------------------- /idea/src/test/java/io/t28/json2java/idea/util/FormatterTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.util; 18 | 19 | import com.intellij.json.JsonFileType; 20 | import com.intellij.openapi.project.Project; 21 | import com.intellij.psi.PsiFileFactory; 22 | import io.t28.json2java.idea.IdeaProjectTest; 23 | import org.junit.Before; 24 | import org.junit.Test; 25 | 26 | import static org.assertj.core.api.Assertions.assertThat; 27 | 28 | public class FormatterTest extends IdeaProjectTest { 29 | private PsiFileFactory fileFactory; 30 | 31 | @Before 32 | @Override 33 | public void setUp() throws Exception { 34 | super.setUp(); 35 | final Project project = getProject(); 36 | fileFactory = PsiFileFactory.getInstance(project); 37 | } 38 | 39 | @Test 40 | public void formatShouldReformatText() throws Exception { 41 | getApplication().invokeAndWait(() -> { 42 | // exercise 43 | final Formatter underTest = new Formatter(fileFactory, JsonFileType.INSTANCE); 44 | final String actual = underTest.format("{\"key\":\"value\",\"array\":[\"foo\",\"bar\"]}"); 45 | 46 | // verify 47 | assertThat(actual) 48 | .isEqualTo("{\n \"key\": \"value\",\n \"array\": [\n \"foo\",\n \"bar\"\n ]\n}"); 49 | }); 50 | } 51 | } -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/validator/NameValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.validator; 18 | 19 | import com.intellij.openapi.ui.InputValidatorEx; 20 | import com.intellij.psi.PsiNameHelper; 21 | import io.t28.json2java.idea.Json2JavaBundle; 22 | 23 | import javax.annotation.Nonnull; 24 | import javax.annotation.Nullable; 25 | import javax.inject.Inject; 26 | 27 | public class NameValidator implements InputValidatorEx { 28 | private final Json2JavaBundle bundle; 29 | private final PsiNameHelper nameHelper; 30 | 31 | @Inject 32 | public NameValidator(@Nonnull Json2JavaBundle bundle, @Nonnull PsiNameHelper nameHelper) { 33 | this.bundle = bundle; 34 | this.nameHelper = nameHelper; 35 | } 36 | 37 | @Nullable 38 | @Override 39 | public String getErrorText(@Nullable String name) { 40 | if (nameHelper.isQualifiedName(name)) { 41 | return null; 42 | } 43 | return bundle.message("error.message.validator.name.invalid"); 44 | } 45 | 46 | @Override 47 | public boolean checkInput(@Nullable String name) { 48 | return true; 49 | } 50 | 51 | @Override 52 | @SuppressWarnings("RedundantIfStatement") 53 | public boolean canClose(@Nullable String name) { 54 | return nameHelper.isQualifiedName(name); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /core/src/test/java/com/squareup/javapoet/TypeSpecAssert.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.squareup.javapoet; 18 | 19 | import org.assertj.core.api.AbstractAssert; 20 | import org.assertj.core.api.Assertions; 21 | 22 | import javax.annotation.Nonnull; 23 | import javax.annotation.Nullable; 24 | import java.util.List; 25 | 26 | public class TypeSpecAssert extends AbstractAssert { 27 | public TypeSpecAssert(@Nullable TypeSpec actual) { 28 | super(actual, TypeSpecAssert.class); 29 | } 30 | 31 | @Nonnull 32 | public TypeSpecAssert hasName(@Nonnull String expected) { 33 | isNotNull(); 34 | 35 | final String actual = this.actual.name; 36 | Assertions.assertThat(actual) 37 | .overridingErrorMessage("Expected name to be <%s>, but was <%s>", expected, actual) 38 | .isEqualTo(expected); 39 | 40 | return this; 41 | } 42 | 43 | @Nonnull 44 | public TypeSpecAssert hasAnnotation(@Nonnull AnnotationSpec expected) { 45 | isNotNull(); 46 | 47 | final List actual = this.actual.annotations; 48 | Assertions.assertThat(actual) 49 | .overridingErrorMessage("Expecting <%s> to contain but could not find <%s>", actual, expected) 50 | .contains(expected); 51 | 52 | return this; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/naming/FieldNamePolicy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.naming; 18 | 19 | import com.google.common.base.CaseFormat; 20 | import com.google.common.base.Strings; 21 | import com.google.inject.Inject; 22 | import com.intellij.psi.codeStyle.JavaCodeStyleManager; 23 | import com.intellij.psi.codeStyle.VariableKind; 24 | import com.squareup.javapoet.TypeName; 25 | import io.t28.json2java.core.naming.DefaultNamePolicy; 26 | import io.t28.json2java.core.naming.NamePolicy; 27 | 28 | import javax.annotation.Nonnull; 29 | 30 | public class FieldNamePolicy implements NamePolicy { 31 | private final JavaCodeStyleManager codeStyleManager; 32 | 33 | @Inject 34 | public FieldNamePolicy(@Nonnull JavaCodeStyleManager codeStyleManager) { 35 | this.codeStyleManager = codeStyleManager; 36 | } 37 | 38 | @Nonnull 39 | @Override 40 | public String convert(@Nonnull String name, @Nonnull TypeName type) { 41 | final String propertyName = DefaultNamePolicy.format(name, CaseFormat.LOWER_CAMEL); 42 | final String fieldName = codeStyleManager.propertyNameToVariableName(propertyName, VariableKind.FIELD); 43 | if (Strings.isNullOrEmpty(fieldName)) { 44 | throw new IllegalArgumentException("Cannot convert '" + name + "' to a field name"); 45 | } 46 | return fieldName; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/naming/ParameterNamePolicy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.naming; 18 | 19 | import com.google.common.base.CaseFormat; 20 | import com.google.common.base.Strings; 21 | import com.google.inject.Inject; 22 | import com.intellij.psi.codeStyle.JavaCodeStyleManager; 23 | import com.intellij.psi.codeStyle.VariableKind; 24 | import com.squareup.javapoet.TypeName; 25 | import io.t28.json2java.core.naming.DefaultNamePolicy; 26 | import io.t28.json2java.core.naming.NamePolicy; 27 | 28 | import javax.annotation.Nonnull; 29 | 30 | public class ParameterNamePolicy implements NamePolicy { 31 | private final JavaCodeStyleManager codeStyleManager; 32 | 33 | @Inject 34 | public ParameterNamePolicy(@Nonnull JavaCodeStyleManager codeStyleManager) { 35 | this.codeStyleManager = codeStyleManager; 36 | } 37 | 38 | @Nonnull 39 | @Override 40 | public String convert(@Nonnull String name, @Nonnull TypeName type) { 41 | final String propertyName = DefaultNamePolicy.format(name, CaseFormat.LOWER_CAMEL); 42 | final String parameterName = codeStyleManager.propertyNameToVariableName(propertyName, VariableKind.PARAMETER); 43 | if (Strings.isNullOrEmpty(parameterName)) { 44 | throw new IllegalArgumentException("Cannot convert '" + name + "' to a parameter name"); 45 | } 46 | return parameterName; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/naming/ClassNamePolicy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /** 18 | * 19 | */ 20 | package io.t28.json2java.idea.naming; 21 | 22 | import com.google.inject.Inject; 23 | import com.intellij.psi.PsiNameHelper; 24 | import com.squareup.javapoet.TypeName; 25 | import io.t28.json2java.core.naming.DefaultNamePolicy; 26 | import io.t28.json2java.core.naming.NamePolicy; 27 | 28 | import javax.annotation.Nonnull; 29 | 30 | public class ClassNamePolicy implements NamePolicy { 31 | private final PsiNameHelper nameHelper; 32 | private final String prefix; 33 | private final String suffix; 34 | 35 | @Inject 36 | public ClassNamePolicy(@Nonnull PsiNameHelper nameHelper, @Nonnull String prefix, @Nonnull String suffix) { 37 | this.nameHelper = nameHelper; 38 | this.prefix = prefix; 39 | this.suffix = suffix; 40 | } 41 | 42 | @Nonnull 43 | @Override 44 | public String convert(@Nonnull String name, @Nonnull TypeName type) { 45 | final StringBuilder builder = new StringBuilder(); 46 | builder.append(prefix) 47 | .append(DefaultNamePolicy.CLASS.convert(name, type)) 48 | .append(suffix); 49 | final String className = builder.toString(); 50 | if (!nameHelper.isQualifiedName(className)) { 51 | throw new IllegalArgumentException("Cannot convert '" + name + "' to class name"); 52 | } 53 | return className; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/validator/ClassSuffixValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.validator; 18 | 19 | import com.google.common.base.Strings; 20 | import com.google.inject.Inject; 21 | import com.intellij.openapi.ui.InputValidatorEx; 22 | import com.intellij.psi.PsiNameHelper; 23 | import io.t28.json2java.idea.Json2JavaBundle; 24 | import org.jetbrains.annotations.NotNull; 25 | import org.jetbrains.annotations.Nullable; 26 | 27 | public class ClassSuffixValidator implements InputValidatorEx { 28 | private final Json2JavaBundle bundle; 29 | private final PsiNameHelper nameHelper; 30 | 31 | @Inject 32 | public ClassSuffixValidator(@NotNull Json2JavaBundle bundle, @NotNull PsiNameHelper nameHelper) { 33 | this.bundle = bundle; 34 | this.nameHelper = nameHelper; 35 | } 36 | 37 | @Nullable 38 | @Override 39 | public String getErrorText(@Nullable String suffix) { 40 | if (canClose(suffix)) { 41 | return null; 42 | } 43 | return bundle.message("error.message.validator.class.suffix", suffix); 44 | } 45 | 46 | @Override 47 | public boolean checkInput(@Nullable String suffix) { 48 | return true; 49 | } 50 | 51 | @Override 52 | public boolean canClose(@Nullable String suffix) { 53 | if (Strings.isNullOrEmpty(suffix)) { 54 | return true; 55 | } 56 | 57 | return nameHelper.isIdentifier(suffix); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /core/src/test/java/io/t28/json2java/core/Assertions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core; 18 | 19 | import com.squareup.javapoet.FieldSpec; 20 | import com.squareup.javapoet.FieldSpecAssert; 21 | import com.squareup.javapoet.MethodSpec; 22 | import com.squareup.javapoet.MethodSpecAssert; 23 | import com.squareup.javapoet.TypeSpec; 24 | import com.squareup.javapoet.TypeSpecAssert; 25 | import io.t28.json2java.core.json.JsonValue; 26 | import io.t28.json2java.core.json.JsonValueAssert; 27 | 28 | import javax.annotation.CheckReturnValue; 29 | import javax.annotation.Nonnull; 30 | import javax.annotation.Nullable; 31 | 32 | public class Assertions extends org.assertj.core.api.Assertions { 33 | private Assertions() { 34 | } 35 | 36 | @Nonnull 37 | @CheckReturnValue 38 | public static JsonValueAssert assertThat(@Nullable JsonValue actual) { 39 | return new JsonValueAssert(actual); 40 | } 41 | 42 | @Nonnull 43 | @CheckReturnValue 44 | public static FieldSpecAssert assertThat(@Nullable FieldSpec actual) { 45 | return new FieldSpecAssert(actual); 46 | } 47 | 48 | @Nonnull 49 | @CheckReturnValue 50 | public static MethodSpecAssert assertThat(@Nullable MethodSpec actual) { 51 | return new MethodSpecAssert(actual); 52 | } 53 | 54 | @Nonnull 55 | @CheckReturnValue 56 | public static TypeSpecAssert assertThat(@Nullable TypeSpec actual) { 57 | return new TypeSpecAssert(actual); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /core/src/main/java/io/t28/json2java/core/json/JsonObject.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.json; 18 | 19 | import com.google.common.collect.ImmutableMap; 20 | import com.squareup.javapoet.ParameterizedTypeName; 21 | import com.squareup.javapoet.TypeName; 22 | 23 | import javax.annotation.CheckReturnValue; 24 | import javax.annotation.Nonnull; 25 | import java.util.HashMap; 26 | import java.util.LinkedHashMap; 27 | import java.util.Map; 28 | import java.util.stream.Stream; 29 | 30 | public class JsonObject extends JsonValue { 31 | private final Map value; 32 | 33 | public JsonObject(@Nonnull Map value) { 34 | this.value = new LinkedHashMap<>(value); 35 | } 36 | 37 | @Nonnull 38 | @Override 39 | public TypeName getType() { 40 | return ParameterizedTypeName.get(Map.class, String.class, Object.class); 41 | } 42 | 43 | @Nonnull 44 | @Override 45 | public Map getValue() { 46 | return ImmutableMap.copyOf(value); 47 | } 48 | 49 | @Nonnull 50 | @CheckReturnValue 51 | public Stream> stream() { 52 | return value.entrySet() 53 | .stream() 54 | .map(entry -> { 55 | final String name = entry.getKey(); 56 | final Object value = entry.getValue(); 57 | return new HashMap.SimpleImmutableEntry<>(name, wrap(value)); 58 | }); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /.idea/modules/core/core_main.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /core/src/test/java/io/t28/json2java/core/json/JsonArrayTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.json; 18 | 19 | import com.squareup.javapoet.ParameterizedTypeName; 20 | import com.squareup.javapoet.TypeName; 21 | import org.junit.Before; 22 | import org.junit.Test; 23 | 24 | import java.util.Arrays; 25 | import java.util.List; 26 | import java.util.stream.Stream; 27 | 28 | import static io.t28.json2java.core.Assertions.assertThat; 29 | 30 | public class JsonArrayTest { 31 | private JsonArray underTest; 32 | 33 | @Before 34 | public void setUp() throws Exception { 35 | underTest = new JsonArray(Arrays.asList("foo", "bar", "baz", "qux")); 36 | } 37 | 38 | @Test 39 | public void getType() throws Exception { 40 | // exercise 41 | final TypeName actual = underTest.getType(); 42 | 43 | // verify 44 | assertThat(actual) 45 | .isEqualTo(ParameterizedTypeName.get(List.class, Object.class)); 46 | } 47 | 48 | @Test 49 | public void getValues() throws Exception { 50 | // exercise 51 | final List actual = underTest.getValue(); 52 | 53 | // verify 54 | assertThat(actual) 55 | .hasSize(4) 56 | .containsOnlyOnce("foo", "bar", "baz", "qux"); 57 | } 58 | 59 | @Test 60 | public void stream() throws Exception { 61 | // exercise 62 | final Stream actual = underTest.stream(); 63 | 64 | // verify 65 | assertThat(actual) 66 | .hasSize(4) 67 | .doesNotContainNull(); 68 | } 69 | } -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/inject/GuiceManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.inject; 18 | 19 | import com.google.inject.Guice; 20 | import com.google.inject.Injector; 21 | import com.intellij.openapi.components.AbstractProjectComponent; 22 | import com.intellij.openapi.project.Project; 23 | 24 | import javax.annotation.Nonnull; 25 | import javax.annotation.Nullable; 26 | 27 | public class GuiceManager extends AbstractProjectComponent { 28 | private static final String NAME = GuiceManager.class.getSimpleName(); 29 | 30 | @Nullable 31 | private Injector injector; 32 | 33 | public GuiceManager(@Nonnull Project project) { 34 | super(project); 35 | } 36 | 37 | @Nonnull 38 | public static GuiceManager getInstance(@Nonnull Project project) { 39 | return project.getComponent(GuiceManager.class); 40 | } 41 | 42 | @Override 43 | public void projectOpened() { 44 | injector = Guice.createInjector(new ProjectModule(getProject())); 45 | } 46 | 47 | @Override 48 | public void projectClosed() { 49 | injector = null; 50 | } 51 | 52 | @Nonnull 53 | @Override 54 | public String getComponentName() { 55 | return NAME; 56 | } 57 | 58 | @Nonnull 59 | public Injector getInjector() { 60 | if (injector == null) { 61 | throw new IllegalStateException("Injector has not been initialized yet, ot has been already disposed"); 62 | } 63 | return injector; 64 | } 65 | 66 | @Nonnull 67 | private Project getProject() { 68 | return myProject; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/setting/Json2JavaSettings.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.setting; 18 | 19 | import com.intellij.openapi.components.ServiceManager; 20 | import com.intellij.openapi.project.Project; 21 | import io.t28.json2java.core.Style; 22 | 23 | import javax.annotation.CheckReturnValue; 24 | import javax.annotation.Nonnull; 25 | 26 | public interface Json2JavaSettings { 27 | @Nonnull 28 | @CheckReturnValue 29 | static Json2JavaSettings getInstance() { 30 | return new TemporaryJson2JavaSettings(); 31 | } 32 | 33 | @Nonnull 34 | @CheckReturnValue 35 | static Json2JavaSettings getInstance(@Nonnull Project project) { 36 | return ServiceManager.getService(project, Json2JavaSettings.class); 37 | } 38 | 39 | @Nonnull 40 | @CheckReturnValue 41 | Style getStyle(); 42 | 43 | @Nonnull 44 | Json2JavaSettings setStyle(@Nonnull Style style); 45 | 46 | @Nonnull 47 | @CheckReturnValue 48 | String getClassNamePrefix(); 49 | 50 | @Nonnull 51 | Json2JavaSettings setClassNamePrefix(@Nonnull String classNamePrefix); 52 | 53 | @Nonnull 54 | @CheckReturnValue 55 | String getClassNameSuffix(); 56 | 57 | @Nonnull 58 | Json2JavaSettings setClassNameSuffix(@Nonnull String classNameSuffix); 59 | 60 | @CheckReturnValue 61 | boolean isGeneratedAnnotationEnabled(); 62 | 63 | @Nonnull 64 | Json2JavaSettings setGeneratedAnnotationEnabled(boolean enabled); 65 | 66 | @CheckReturnValue 67 | boolean isSuppressWarningsAnnotationEnabled(); 68 | 69 | @Nonnull 70 | Json2JavaSettings setSuppressWarningsAnnotationEnabled(boolean enabled); 71 | } 72 | -------------------------------------------------------------------------------- /core/src/test/java/io/t28/json2java/core/json/JsonObjectTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.json; 18 | 19 | import com.google.common.collect.ImmutableMap; 20 | import com.squareup.javapoet.ParameterizedTypeName; 21 | import com.squareup.javapoet.TypeName; 22 | import org.junit.Before; 23 | import org.junit.Test; 24 | 25 | import java.util.Map; 26 | import java.util.stream.Stream; 27 | 28 | import static io.t28.json2java.core.Assertions.assertThat; 29 | 30 | public class JsonObjectTest { 31 | private JsonObject underTest; 32 | 33 | @Before 34 | public void setUp() throws Exception { 35 | underTest = new JsonObject(ImmutableMap.of("foo", 42, "bar", 32)); 36 | } 37 | 38 | @Test 39 | public void getType() throws Exception { 40 | //exercise 41 | final TypeName actual = underTest.getType(); 42 | 43 | // verify 44 | assertThat(actual) 45 | .isEqualTo(ParameterizedTypeName.get(Map.class, String.class, Object.class)); 46 | } 47 | 48 | @Test 49 | public void getValue() throws Exception { 50 | // exercise 51 | final Map actual = underTest.getValue(); 52 | 53 | // verify 54 | assertThat(actual) 55 | .hasSize(2) 56 | .containsEntry("foo", 42) 57 | .containsEntry("bar", 32); 58 | } 59 | 60 | @Test 61 | public void stream() throws Exception { 62 | // exercise 63 | final Stream> actual = underTest.stream(); 64 | 65 | // verify 66 | assertThat(actual) 67 | .hasSize(2) 68 | .doesNotContainNull(); 69 | } 70 | } -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/validator/ClassPrefixValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.validator; 18 | 19 | import com.google.common.base.Strings; 20 | import com.google.inject.Inject; 21 | import com.intellij.openapi.ui.InputValidatorEx; 22 | import com.intellij.openapi.util.text.StringUtil; 23 | import com.intellij.psi.PsiNameHelper; 24 | import io.t28.json2java.idea.Json2JavaBundle; 25 | import org.jetbrains.annotations.NotNull; 26 | import org.jetbrains.annotations.Nullable; 27 | 28 | public class ClassPrefixValidator implements InputValidatorEx { 29 | private final Json2JavaBundle bundle; 30 | private final PsiNameHelper nameHelper; 31 | 32 | @Inject 33 | public ClassPrefixValidator(@NotNull Json2JavaBundle bundle, @NotNull PsiNameHelper nameHelper) { 34 | this.bundle = bundle; 35 | this.nameHelper = nameHelper; 36 | } 37 | 38 | @Nullable 39 | @Override 40 | public String getErrorText(@Nullable String prefix) { 41 | if (canClose(prefix)) { 42 | return null; 43 | } 44 | return bundle.message("error.message.validator.class.prefix", prefix); 45 | } 46 | 47 | @Override 48 | public boolean checkInput(@Nullable String prefix) { 49 | return true; 50 | } 51 | 52 | @Override 53 | public boolean canClose(@Nullable String prefix) { 54 | if (Strings.isNullOrEmpty(prefix)) { 55 | return true; 56 | } 57 | 58 | if (!nameHelper.isIdentifier(prefix)) { 59 | return false; 60 | } 61 | 62 | final char first = prefix.charAt(0); 63 | return StringUtil.isJavaIdentifierStart(first); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /idea/src/test/java/org/jdom/ElementAssert.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.jdom; 18 | 19 | import org.assertj.core.api.AbstractAssert; 20 | import org.assertj.core.api.Assertions; 21 | 22 | import javax.annotation.Nonnull; 23 | import javax.annotation.Nullable; 24 | 25 | public class ElementAssert extends AbstractAssert { 26 | public ElementAssert(@Nullable Element actual) { 27 | super(actual, ElementAssert.class); 28 | } 29 | 30 | @Nonnull 31 | public ElementAssert hasName(@Nonnull String expected) { 32 | isNotNull(); 33 | 34 | final String actual = this.actual.getName(); 35 | Assertions.assertThat(actual) 36 | .overridingErrorMessage("Expected root element to be named <%s> but was <%s>", expected, actual) 37 | .isEqualTo(expected); 38 | 39 | return this; 40 | } 41 | 42 | @Nonnull 43 | public ElementAssert hasAttribute(@Nonnull String expectedName, @Nonnull String expectedValue) { 44 | isNotNull(); 45 | 46 | final Attribute actual = this.actual.getAttribute(expectedName); 47 | Assertions.assertThat(actual) 48 | .overridingErrorMessage("Expected attribute named <%s> does not exist", expectedName) 49 | .isNotNull(); 50 | 51 | final String actualName = actual.getName(); 52 | Assertions.assertThat(actualName) 53 | .overridingErrorMessage("Expected attribute name to be <%s> but was <%s>", expectedName, actualName) 54 | .isEqualTo(expectedName); 55 | 56 | final String actualValue = actual.getValue(); 57 | Assertions.assertThat(actualValue) 58 | .overridingErrorMessage("Expected attribute value to be <%s> but was <%s>", expectedValue, actualValue) 59 | .isEqualTo(expectedValue); 60 | 61 | return this; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/naming/MethodNamePolicy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.naming; 18 | 19 | import com.google.common.base.CaseFormat; 20 | import com.google.common.base.Preconditions; 21 | import com.google.common.base.Strings; 22 | import com.google.inject.Inject; 23 | import com.intellij.codeInsight.generation.GenerateMembersUtil; 24 | import com.intellij.openapi.project.Project; 25 | import com.intellij.psi.PsiType; 26 | import com.intellij.util.IncorrectOperationException; 27 | import com.squareup.javapoet.TypeName; 28 | import io.t28.json2java.core.naming.DefaultNamePolicy; 29 | import io.t28.json2java.core.naming.NamePolicy; 30 | import io.t28.json2java.idea.util.PsiTypeConverter; 31 | 32 | import javax.annotation.Nonnull; 33 | 34 | public class MethodNamePolicy implements NamePolicy { 35 | private final Project project; 36 | private final PsiTypeConverter typeConverter; 37 | 38 | @Inject 39 | public MethodNamePolicy(@Nonnull Project project, @Nonnull PsiTypeConverter typeConverter) { 40 | this.project = Preconditions.checkNotNull(project); 41 | this.typeConverter = Preconditions.checkNotNull(typeConverter); 42 | } 43 | 44 | @Nonnull 45 | @Override 46 | public String convert(@Nonnull String name, @Nonnull TypeName type) { 47 | final String variableName = DefaultNamePolicy.format(name, CaseFormat.UPPER_CAMEL); 48 | if (Strings.isNullOrEmpty(variableName)) { 49 | throw new IllegalArgumentException("Cannot convert '" + name + "' to a method name"); 50 | } 51 | 52 | try { 53 | final PsiType psiType = typeConverter.apply(type); 54 | return GenerateMembersUtil.suggestGetterName(variableName, psiType, project); 55 | } catch (IncorrectOperationException e) { 56 | throw new IllegalArgumentException("Cannot convert '" + name + "' to a method name", e); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /idea/src/test/java/io/t28/json2java/idea/naming/ParameterNamePolicyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.naming; 18 | 19 | import com.intellij.openapi.project.Project; 20 | import com.intellij.psi.codeStyle.JavaCodeStyleManager; 21 | import com.squareup.javapoet.TypeName; 22 | import io.t28.json2java.idea.IdeaProjectTest; 23 | import org.junit.Before; 24 | import org.junit.Test; 25 | 26 | import static org.assertj.core.api.Assertions.assertThat; 27 | import static org.assertj.core.api.Assertions.assertThatThrownBy; 28 | 29 | public class ParameterNamePolicyTest extends IdeaProjectTest { 30 | private ParameterNamePolicy underTest; 31 | 32 | @Before 33 | @Override 34 | public void setUp() throws Exception { 35 | super.setUp(); 36 | final Project project = getProject(); 37 | final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project); 38 | underTest = new ParameterNamePolicy(codeStyleManager); 39 | } 40 | 41 | @Test 42 | public void convertShouldReturnLowerCamelText() throws Exception { 43 | // exercise 44 | final String actual = underTest.convert("Foo", TypeName.OBJECT); 45 | 46 | // verify 47 | assertThat(actual) 48 | .isEqualTo("foo"); 49 | } 50 | 51 | @Test 52 | public void convertShouldAppendPrefixWhenReserved() throws Exception { 53 | // exercise 54 | final String actual = underTest.convert("Class", TypeName.OBJECT); 55 | 56 | // verify 57 | assertThat(actual) 58 | .isEqualTo("aClass"); 59 | } 60 | 61 | @Test 62 | @SuppressWarnings("ResultOfMethodCallIgnored") 63 | public void convertShouldThrowExceptionWhenInvalidText() throws Exception { 64 | // verify 65 | assertThatThrownBy(() -> { 66 | // exercise 67 | underTest.convert("+", TypeName.OBJECT); 68 | }).isInstanceOf(IllegalArgumentException.class); 69 | } 70 | } -------------------------------------------------------------------------------- /idea/src/main/resources/messages/Json2Java4IdeaBundle.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2017 Tatsuya Maki 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | dialog.title=Create New Class from JSON 17 | dialog.label.name=Name: 18 | dialog.label.json=JSON: 19 | dialog.action.format=Format 20 | dialog.action.settings=Settings 21 | progress.title=Generating Class 22 | progress.text=Generating a class {0} 23 | error.title.cannot.create.class=Cannot Create Class 24 | error.title.directory.invalid=Invalid Directory 25 | error.message.unknown=An unknown error has occurred 26 | error.message.directory.invalid=Creating a class in the directory is not allowed. 27 | error.message.cannot.create=Unable to create class {0}. 28 | error.message.class.exists=Cannot create a class {0}. File already exists., 29 | error.message.validator.name.invalid=This is not a valid Java class name. 30 | error.message.validator.json.empty=JSON must not be empty. 31 | error.message.validator.json.parse=JSON cannot be parsed. 32 | error.message.validator.json.invalid=This is not a valid JSON. 33 | error.message.validator.json.primitive=JSON must be an Array or Object 34 | error.message.validator.class.prefix=Not a Java identifier part in the class prefix {0} 35 | error.message.validator.class.suffix=Not a Java identifier part in the class suffix {0} 36 | settings.name=Json2Java4Idea 37 | settings.name.style.default=default 38 | settings.name.style.gson=gson 39 | settings.name.style.jackson=jackson 40 | settings.name.style.moshi=moshi 41 | settings.name.class.prefix=class name prefix 42 | settings.name.class.suffix=class name suffix 43 | settings.label.style=Style 44 | settings.label.style.default=Default 45 | settings.label.style.gson=Gson 46 | settings.label.style.jackson=Jackson 47 | settings.label.style.moshi=Moshi 48 | settings.label.class=Class 49 | settings.label.class.prefix=Name prefix: 50 | settings.label.class.suffix=Name suffix: 51 | settings.label.annotation=Annotation 52 | settings.label.annotation.generated=Insert @Generated annotation 53 | settings.label.annotation.suppresswarnings=Insert @SuppressWarnings annotation 54 | -------------------------------------------------------------------------------- /idea/src/test/java/io/t28/json2java/idea/IdeaProjectTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea; 18 | 19 | import com.intellij.openapi.application.Application; 20 | import com.intellij.openapi.application.ApplicationManager; 21 | import com.intellij.openapi.project.Project; 22 | import com.intellij.testFramework.fixtures.IdeaProjectTestFixture; 23 | import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory; 24 | import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture; 25 | import com.intellij.testFramework.fixtures.JavaTestFixtureFactory; 26 | import com.intellij.testFramework.fixtures.TestFixtureBuilder; 27 | import org.assertj.core.util.CheckReturnValue; 28 | import org.assertj.swing.dependency.jsr305.Nonnull; 29 | import org.junit.After; 30 | import org.junit.Before; 31 | 32 | public class IdeaProjectTest { 33 | private JavaCodeInsightTestFixture fixture; 34 | 35 | @Before 36 | public void setUp() throws Exception { 37 | final IdeaTestFixtureFactory ideaFixtureFactory = IdeaTestFixtureFactory.getFixtureFactory(); 38 | final JavaTestFixtureFactory javaFixtureFactory = JavaTestFixtureFactory.getFixtureFactory(); 39 | final TestFixtureBuilder fixtureBuilder = ideaFixtureFactory.createLightFixtureBuilder(); 40 | fixture = javaFixtureFactory.createCodeInsightFixture(fixtureBuilder.getFixture()); 41 | fixture.setUp(); 42 | } 43 | 44 | @After 45 | public void tearDown() throws Exception { 46 | fixture.tearDown(); 47 | } 48 | 49 | @Nonnull 50 | @CheckReturnValue 51 | protected Application getApplication() { 52 | if (fixture == null) { 53 | throw new AssertionError("getApplication() must call after setUp()"); 54 | } 55 | return ApplicationManager.getApplication(); 56 | } 57 | 58 | @Nonnull 59 | @CheckReturnValue 60 | protected Project getProject() { 61 | if (fixture == null) { 62 | throw new AssertionError("getProject() must call after setUp()"); 63 | } 64 | return fixture.getProject(); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /.idea/modules/core/core_test.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /idea/src/test/java/io/t28/json2java/idea/util/ExtensionsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.util; 18 | 19 | import com.intellij.ide.highlighter.JavaFileType; 20 | import org.junit.Test; 21 | 22 | import static org.assertj.core.api.Assertions.assertThat; 23 | 24 | public class ExtensionsTest { 25 | @Test 26 | public void removeShouldRemoveExtension() throws Exception { 27 | // exercise 28 | final String actual = Extensions.remove("foo.java", JavaFileType.INSTANCE); 29 | 30 | // verify 31 | assertThat(actual) 32 | .isEqualTo("foo"); 33 | } 34 | 35 | @Test 36 | public void removeShouldNotRemoveExtensionWhenNotMatch() throws Exception { 37 | // exercise 38 | final String actual = Extensions.remove("foo.java.tmp", JavaFileType.INSTANCE); 39 | 40 | // verify 41 | assertThat(actual) 42 | .isEqualTo("foo.java.tmp"); 43 | } 44 | 45 | @Test 46 | public void removeShouldReturnFileNameWhenExtensionDoesNotExist() throws Exception { 47 | // exercise 48 | final String actual = Extensions.remove("foo", JavaFileType.INSTANCE); 49 | 50 | // verify 51 | assertThat(actual) 52 | .isEqualTo("foo"); 53 | } 54 | 55 | @Test 56 | public void appendShouldAppendExtension() throws Exception { 57 | // exercise 58 | final String actual = Extensions.append("foo", JavaFileType.INSTANCE); 59 | 60 | // verify 61 | assertThat(actual) 62 | .isEqualTo("foo.java"); 63 | } 64 | 65 | @Test 66 | public void appendShouldNotAppendExtensionWhenExtensionExists() throws Exception { 67 | // exercise 68 | final String actual = Extensions.append("foo.java", JavaFileType.INSTANCE); 69 | 70 | // verify 71 | assertThat(actual) 72 | .isEqualTo("foo.java"); 73 | } 74 | 75 | @Test 76 | public void appendShouldAppendExtensionWhenAnotherExtensionExists() throws Exception { 77 | // exercise 78 | final String actual = Extensions.append("foo.tmp", JavaFileType.INSTANCE); 79 | 80 | // verify 81 | assertThat(actual) 82 | .isEqualTo("foo.tmp.java"); 83 | } 84 | } -------------------------------------------------------------------------------- /idea/src/test/java/io/t28/json2java/idea/naming/FieldNamePolicyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.naming; 18 | 19 | import com.intellij.openapi.project.Project; 20 | import com.intellij.psi.codeStyle.JavaCodeStyleManager; 21 | import com.squareup.javapoet.TypeName; 22 | import io.t28.json2java.idea.IdeaProjectTest; 23 | import org.junit.Before; 24 | import org.junit.Test; 25 | 26 | import static org.assertj.core.api.Assertions.assertThat; 27 | import static org.assertj.core.api.Assertions.assertThatThrownBy; 28 | 29 | public class FieldNamePolicyTest extends IdeaProjectTest { 30 | private FieldNamePolicy underTest; 31 | 32 | @Before 33 | @Override 34 | public void setUp() throws Exception { 35 | super.setUp(); 36 | final Project project = getProject(); 37 | final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project); 38 | underTest = new FieldNamePolicy(codeStyleManager); 39 | } 40 | 41 | @Test 42 | public void convertShouldReturnLowerCamelText() throws Exception { 43 | // exercise 44 | final String actual = underTest.convert("Foo", TypeName.OBJECT); 45 | 46 | // verify 47 | assertThat(actual) 48 | .isEqualTo("foo"); 49 | } 50 | 51 | @Test 52 | public void convertShouldReturnTextWithPrefixWhenReserved() throws Exception { 53 | // exercise 54 | final String actual = underTest.convert("Private", TypeName.OBJECT); 55 | 56 | // verify 57 | assertThat(actual) 58 | .isEqualTo("aPrivate"); 59 | } 60 | 61 | @Test 62 | public void convertShouldRemoveInvalidCharacter() throws Exception { 63 | // exercise 64 | final String actual = underTest.convert("1nva|id", TypeName.OBJECT); 65 | 66 | // verify 67 | assertThat(actual) 68 | .isEqualTo("a1nvaid"); 69 | } 70 | 71 | @Test 72 | @SuppressWarnings("ResultOfMethodCallIgnored") 73 | public void convertShouldThrowExceptionWhenInvalidName() throws Exception { 74 | assertThatThrownBy(() -> { 75 | // exercise 76 | underTest.convert("+-*/", TypeName.OBJECT); 77 | }).isInstanceOf(IllegalArgumentException.class); 78 | } 79 | } -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/validator/JsonValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.validator; 18 | 19 | import com.google.common.base.Strings; 20 | import com.google.gson.JsonElement; 21 | import com.google.gson.JsonParseException; 22 | import com.google.gson.JsonParser; 23 | import com.google.gson.JsonSyntaxException; 24 | import com.google.inject.Inject; 25 | import com.intellij.openapi.ui.InputValidatorEx; 26 | import io.t28.json2java.idea.Json2JavaBundle; 27 | 28 | import javax.annotation.Nonnull; 29 | import javax.annotation.Nullable; 30 | 31 | public class JsonValidator implements InputValidatorEx { 32 | private final Json2JavaBundle bundle; 33 | private final JsonParser parser; 34 | 35 | @Inject 36 | public JsonValidator(@Nonnull Json2JavaBundle bundle, @Nonnull JsonParser parser) { 37 | this.bundle = bundle; 38 | this.parser = parser; 39 | } 40 | 41 | @Nullable 42 | @Override 43 | public String getErrorText(@Nullable String json) { 44 | if (Strings.isNullOrEmpty(json)) { 45 | return bundle.message("error.message.validator.json.empty"); 46 | } 47 | 48 | try { 49 | final JsonElement root = parser.parse(json); 50 | if (root.isJsonNull() || root.isJsonPrimitive()) { 51 | return bundle.message("error.message.validator.json.primitive"); 52 | } 53 | } catch (JsonSyntaxException e) { 54 | return bundle.message("error.message.validator.json.invalid"); 55 | } catch (JsonParseException e) { 56 | return bundle.message("error.message.validator.json.parse"); 57 | } 58 | return null; 59 | } 60 | 61 | @Override 62 | public boolean checkInput(@Nullable String json) { 63 | return true; 64 | } 65 | 66 | @Override 67 | @SuppressWarnings("SimplifiableIfStatement") 68 | public boolean canClose(@Nullable String json) { 69 | if (Strings.isNullOrEmpty(json)) { 70 | return false; 71 | } 72 | 73 | try { 74 | final JsonElement root = parser.parse(json); 75 | return !root.isJsonNull() && !root.isJsonPrimitive(); 76 | } catch (JsonParseException e) { 77 | return false; 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /idea/src/test/java/io/t28/json2java/idea/util/PsiTypeConverterTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.util; 18 | 19 | import com.intellij.openapi.project.Project; 20 | import com.intellij.psi.PsiManager; 21 | import com.intellij.psi.PsiType; 22 | import com.squareup.javapoet.TypeName; 23 | import io.t28.json2java.idea.IdeaProjectTest; 24 | import org.junit.Before; 25 | import org.junit.Test; 26 | import org.junit.runner.RunWith; 27 | import org.junit.runners.Parameterized; 28 | import org.junit.runners.Parameterized.Parameters; 29 | 30 | import javax.annotation.Nonnull; 31 | import java.util.Arrays; 32 | import java.util.Collection; 33 | 34 | import static org.assertj.core.api.Assertions.assertThat; 35 | 36 | @RunWith(Parameterized.class) 37 | public class PsiTypeConverterTest extends IdeaProjectTest { 38 | @Parameters(name = "should return {1} when {0}") 39 | public static Collection fixtures() { 40 | return Arrays.asList(new Object[][]{ 41 | {TypeName.BOOLEAN, PsiType.BOOLEAN}, 42 | {TypeName.BYTE, PsiType.BYTE}, 43 | {TypeName.CHAR, PsiType.CHAR}, 44 | {TypeName.DOUBLE, PsiType.DOUBLE}, 45 | {TypeName.FLOAT, PsiType.FLOAT}, 46 | {TypeName.INT, PsiType.INT}, 47 | {TypeName.LONG, PsiType.LONG}, 48 | {TypeName.SHORT, PsiType.SHORT}, 49 | {TypeName.VOID, PsiType.VOID}, 50 | }); 51 | } 52 | 53 | private final TypeName typeName; 54 | 55 | private final PsiType expected; 56 | 57 | private PsiTypeConverter underTest; 58 | 59 | public PsiTypeConverterTest(@Nonnull TypeName typeName, @Nonnull PsiType expected) { 60 | this.typeName = typeName; 61 | this.expected = expected; 62 | } 63 | 64 | @Before 65 | @Override 66 | public void setUp() throws Exception { 67 | super.setUp(); 68 | final Project project = getProject(); 69 | final PsiManager psiManager = PsiManager.getInstance(project); 70 | underTest = new PsiTypeConverter(psiManager); 71 | } 72 | 73 | @Test 74 | public void apply() throws Exception { 75 | // exercise 76 | final PsiType actual = underTest.apply(typeName); 77 | 78 | // verify 79 | assertThat(actual) 80 | .isEqualTo(expected); 81 | } 82 | } -------------------------------------------------------------------------------- /idea/src/main/resources/META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | io.t28.json2java 19 | Json2Java4Idea 20 | 0.2.2 21 | T28 22 | 23 | Json2Java4Idea is a code generation plugin which converts JSON to Java in IntelliJ IDEA or Android Studio.

25 | ]]> 26 |
27 | 28 | 0.2.2:

30 |
    31 |
  • Fixed a preferences dialog freezing bug #142
  • 32 |
33 |

0.2.1:

34 |
    35 |
  • Fixed a crash for AndroidStudio 2.3.x #139
  • 36 |
37 |

0.2.0:

38 |
    39 |
  • Added annotation style settings #129
  • 40 |
  • Fixed transition from dialog
  • 41 |
42 |

0.1.0:

43 |
    44 |
  • Initial release.
  • 45 |
46 | ]]> 47 |
48 | 49 | 50 | 51 | com.intellij.modules.lang 52 | 53 | 54 | 57 | 60 | 61 | 62 | 63 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | io.t28.json2java.idea.inject.GuiceManager 72 | 73 | 74 |
-------------------------------------------------------------------------------- /idea/src/test/java/io/t28/json2java/idea/naming/ClassNamePolicyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.naming; 18 | 19 | import com.intellij.openapi.project.Project; 20 | import com.intellij.psi.PsiNameHelper; 21 | import com.squareup.javapoet.TypeName; 22 | import io.t28.json2java.idea.IdeaProjectTest; 23 | import org.junit.Before; 24 | import org.junit.Test; 25 | 26 | import static org.assertj.core.api.Assertions.assertThat; 27 | import static org.assertj.core.api.Assertions.assertThatThrownBy; 28 | 29 | public class ClassNamePolicyTest extends IdeaProjectTest { 30 | private PsiNameHelper nameHelper; 31 | 32 | @Before 33 | @Override 34 | public void setUp() throws Exception { 35 | super.setUp(); 36 | final Project project = getProject(); 37 | nameHelper = PsiNameHelper.getInstance(project); 38 | } 39 | 40 | @Test 41 | public void convertShouldReturnUpperCamelText() throws Exception { 42 | // exercise 43 | final ClassNamePolicy underTest = new ClassNamePolicy(nameHelper, "", ""); 44 | final String actual = underTest.convert("foo_bar", TypeName.OBJECT); 45 | 46 | // verify 47 | assertThat(actual) 48 | .isEqualTo("FooBar"); 49 | } 50 | 51 | @Test 52 | public void convertShouldReturnTextWithPrefix() throws Exception { 53 | // exercise 54 | final ClassNamePolicy underTest = new ClassNamePolicy(nameHelper, "Baz", ""); 55 | final String actual = underTest.convert("foo_bar", TypeName.OBJECT); 56 | 57 | // verify 58 | assertThat(actual) 59 | .isEqualTo("BazFooBar"); 60 | } 61 | 62 | @Test 63 | public void convertShouldReturnTextWithSuffix() throws Exception { 64 | // exercise 65 | final ClassNamePolicy underTest = new ClassNamePolicy(nameHelper, "", "Baz"); 66 | final String actual = underTest.convert("foo_bar", TypeName.OBJECT); 67 | 68 | // verify 69 | assertThat(actual) 70 | .isEqualTo("FooBarBaz"); 71 | } 72 | 73 | @Test 74 | @SuppressWarnings("ResultOfMethodCallIgnored") 75 | public void convertShouldThrowExceptionWhenUnQualifiedName() throws Exception { 76 | // verify 77 | assertThatThrownBy(() -> { 78 | // exercise 79 | final ClassNamePolicy underTest = new ClassNamePolicy(nameHelper, "", ""); 80 | underTest.convert("+1", TypeName.OBJECT); 81 | }).isInstanceOf(IllegalArgumentException.class); 82 | } 83 | } -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/setting/TemporaryJson2JavaSettings.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.setting; 18 | 19 | import io.t28.json2java.core.Style; 20 | 21 | import javax.annotation.Nonnull; 22 | 23 | public class TemporaryJson2JavaSettings implements Json2JavaSettings { 24 | @Nonnull 25 | private Style style; 26 | @Nonnull 27 | private String classNamePrefix; 28 | @Nonnull 29 | private String classNameSuffix; 30 | private boolean isGeneratedAnnotationEnabled; 31 | private boolean isSuppressWarningsAnnotationEnabled; 32 | 33 | TemporaryJson2JavaSettings() { 34 | style = Style.NONE; 35 | classNamePrefix = ""; 36 | classNameSuffix = ""; 37 | } 38 | 39 | @Nonnull 40 | @Override 41 | public Style getStyle() { 42 | return style; 43 | } 44 | 45 | @Nonnull 46 | @Override 47 | public TemporaryJson2JavaSettings setStyle(@Nonnull Style style) { 48 | this.style = style; 49 | return this; 50 | } 51 | 52 | @Nonnull 53 | @Override 54 | public String getClassNamePrefix() { 55 | return classNamePrefix; 56 | } 57 | 58 | @Nonnull 59 | @Override 60 | public TemporaryJson2JavaSettings setClassNamePrefix(@Nonnull String classNamePrefix) { 61 | this.classNamePrefix = classNamePrefix; 62 | return this; 63 | } 64 | 65 | @Nonnull 66 | @Override 67 | public String getClassNameSuffix() { 68 | return classNameSuffix; 69 | } 70 | 71 | @Nonnull 72 | @Override 73 | public TemporaryJson2JavaSettings setClassNameSuffix(@Nonnull String classNameSuffix) { 74 | this.classNameSuffix = classNameSuffix; 75 | return this; 76 | } 77 | 78 | @Override 79 | public boolean isGeneratedAnnotationEnabled() { 80 | return isGeneratedAnnotationEnabled; 81 | } 82 | 83 | @Nonnull 84 | @Override 85 | public Json2JavaSettings setGeneratedAnnotationEnabled(boolean enabled) { 86 | isGeneratedAnnotationEnabled = enabled; 87 | return this; 88 | } 89 | 90 | @Override 91 | public boolean isSuppressWarningsAnnotationEnabled() { 92 | return isSuppressWarningsAnnotationEnabled; 93 | } 94 | 95 | @Nonnull 96 | @Override 97 | public Json2JavaSettings setSuppressWarningsAnnotationEnabled(boolean enabled) { 98 | isSuppressWarningsAnnotationEnabled = enabled; 99 | return this; 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/view/NewClassDialog.form: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 |
57 | -------------------------------------------------------------------------------- /idea/src/test/java/io/t28/json2java/idea/naming/MethodNamePolicyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.naming; 18 | 19 | import com.intellij.openapi.application.Application; 20 | import com.intellij.openapi.project.Project; 21 | import com.intellij.psi.PsiManager; 22 | import com.squareup.javapoet.TypeName; 23 | import io.t28.json2java.idea.IdeaProjectTest; 24 | import io.t28.json2java.idea.util.PsiTypeConverter; 25 | import org.junit.Before; 26 | import org.junit.Test; 27 | 28 | import static org.assertj.core.api.Assertions.assertThat; 29 | import static org.assertj.core.api.Assertions.assertThatThrownBy; 30 | 31 | public class MethodNamePolicyTest extends IdeaProjectTest { 32 | private Application application; 33 | 34 | private MethodNamePolicy underTest; 35 | 36 | @Before 37 | @Override 38 | public void setUp() throws Exception { 39 | super.setUp(); 40 | application = getApplication(); 41 | 42 | final Project project = getProject(); 43 | final PsiManager psiManager = PsiManager.getInstance(project); 44 | final PsiTypeConverter converter = new PsiTypeConverter(psiManager); 45 | underTest = new MethodNamePolicy(project, converter); 46 | } 47 | 48 | @Test 49 | public void convertShouldReturnLowerCamelTextWithPrefix() throws Exception { 50 | application.invokeAndWait(() -> { 51 | // exercise 52 | final String actual = underTest.convert("Foo_Bar", TypeName.OBJECT); 53 | 54 | // verify 55 | assertThat(actual) 56 | .isEqualTo("getFooBar"); 57 | }); 58 | } 59 | 60 | @Test 61 | public void convertShouldReturnWithPrefixWhenBoolean() throws Exception { 62 | application.invokeAndWait(() -> { 63 | // exercise 64 | final String actual = underTest.convert("Foo_Bar", TypeName.BOOLEAN); 65 | 66 | // verify 67 | assertThat(actual) 68 | .isEqualTo("isFooBar"); 69 | }); 70 | } 71 | 72 | @Test 73 | public void convertShouldRemoveInvalidCharacters() throws Exception { 74 | application.invokeAndWait(() -> { 75 | // exercise 76 | final String actual = underTest.convert("/*Foo_Bar*/", TypeName.OBJECT); 77 | 78 | // verify 79 | assertThat(actual) 80 | .isEqualTo("getFooBar"); 81 | }); 82 | } 83 | 84 | @Test 85 | @SuppressWarnings("ResultOfMethodCallIgnored") 86 | public void convertShouldThrowExceptionWhenInvalidName() throws Exception { 87 | application.invokeAndWait(() -> { 88 | // verify 89 | assertThatThrownBy(() -> { 90 | // exercise 91 | underTest.convert("+-*/", TypeName.OBJECT); 92 | }).isInstanceOf(IllegalArgumentException.class); 93 | }); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /core/src/main/java/io/t28/json2java/core/builder/ModelClassBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.builder; 18 | 19 | import com.google.common.collect.ImmutableList; 20 | import com.squareup.javapoet.FieldSpec; 21 | import com.squareup.javapoet.MethodSpec; 22 | import com.squareup.javapoet.ParameterSpec; 23 | import com.squareup.javapoet.TypeName; 24 | import io.t28.json2java.core.naming.NamePolicy; 25 | 26 | import javax.annotation.Nonnull; 27 | import javax.lang.model.element.Modifier; 28 | import java.util.List; 29 | import java.util.stream.Collectors; 30 | 31 | public class ModelClassBuilder extends ClassBuilder { 32 | public ModelClassBuilder(@Nonnull NamePolicy fieldNameStrategy, 33 | @Nonnull NamePolicy methodNameStrategy, 34 | @Nonnull NamePolicy parameterNameStrategy) { 35 | super(fieldNameStrategy, methodNameStrategy, parameterNameStrategy); 36 | } 37 | 38 | @Nonnull 39 | @Override 40 | protected List buildFields() { 41 | return getProperties() 42 | .entrySet() 43 | .stream() 44 | .map(property -> { 45 | final String name = property.getKey(); 46 | final TypeName type = property.getValue(); 47 | final String fieldName = fieldNamePolicy.convert(name, type); 48 | return FieldSpec.builder(type, fieldName) 49 | .addModifiers(Modifier.PRIVATE, Modifier.FINAL) 50 | .build(); 51 | }) 52 | .collect(Collectors.toList()); 53 | } 54 | 55 | @Nonnull 56 | @Override 57 | protected List buildMethods() { 58 | final MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder() 59 | .addModifiers(Modifier.PUBLIC); 60 | 61 | final ImmutableList.Builder builder = ImmutableList.builder(); 62 | getProperties().entrySet().forEach(property -> { 63 | final String name = property.getKey(); 64 | final TypeName type = property.getValue(); 65 | final String fieldName = fieldNamePolicy.convert(name, type); 66 | final String methodName = methodNamePolicy.convert(name, type); 67 | builder.add(MethodSpec.methodBuilder(methodName) 68 | .addModifiers(Modifier.PUBLIC) 69 | .returns(type) 70 | .addStatement("return $L", fieldName) 71 | .build()); 72 | 73 | final String propertyName = parameterNamePolicy.convert(name, type); 74 | constructorBuilder.addParameter(ParameterSpec.builder(type, propertyName) 75 | .build()) 76 | .addStatement("this.$L = $L", fieldName, propertyName); 77 | }); 78 | builder.add(constructorBuilder.build()); 79 | return builder.build(); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/inject/JavaConverterFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.inject; 18 | 19 | import com.google.inject.Inject; 20 | import com.google.inject.Provider; 21 | import com.google.inject.name.Named; 22 | import com.intellij.psi.PsiNameHelper; 23 | import io.t28.json2java.core.Configuration; 24 | import io.t28.json2java.core.JavaConverter; 25 | import io.t28.json2java.core.annotation.GeneratedAnnotationPolicy; 26 | import io.t28.json2java.core.annotation.SuppressWarningsAnnotationPolicy; 27 | import io.t28.json2java.core.naming.NamePolicy; 28 | import io.t28.json2java.idea.naming.ClassNamePolicy; 29 | import io.t28.json2java.idea.setting.Json2JavaSettings; 30 | 31 | import javax.annotation.Nonnull; 32 | 33 | public class JavaConverterFactory { 34 | private final Provider nameHelperProvider; 35 | private final Provider fieldNamePolicyProvider; 36 | private final Provider methodNamePolicyProvider; 37 | private final Provider parameterNamePolicyProvider; 38 | 39 | @Inject 40 | public JavaConverterFactory(@Nonnull Provider nameHelperProvider, 41 | @Nonnull @Named("FieldName") Provider fieldNamePolicyProvider, 42 | @Nonnull @Named("MethodName") Provider methodNamePolicyProvider, 43 | @Nonnull @Named("ParameterName") Provider parameterNamePolicyProvider) { 44 | this.nameHelperProvider = nameHelperProvider; 45 | this.fieldNamePolicyProvider = fieldNamePolicyProvider; 46 | this.methodNamePolicyProvider = methodNamePolicyProvider; 47 | this.parameterNamePolicyProvider = parameterNamePolicyProvider; 48 | } 49 | 50 | @Nonnull 51 | public JavaConverter create(@Nonnull Json2JavaSettings settings) { 52 | final Configuration.Builder builder = Configuration.builder() 53 | .style(settings.getStyle()) 54 | .classNamePolicy(new ClassNamePolicy( 55 | nameHelperProvider.get(), 56 | settings.getClassNamePrefix(), 57 | settings.getClassNameSuffix()) 58 | ) 59 | .fieldNamePolicy(fieldNamePolicyProvider.get()) 60 | .methodNamePolicy(methodNamePolicyProvider.get()) 61 | .parameterNamePolicy(parameterNamePolicyProvider.get()); 62 | 63 | // It is necessary to refactor the following code because condition branch makes code dirty 64 | if (settings.isGeneratedAnnotationEnabled()) { 65 | builder.annotationPolicy(new GeneratedAnnotationPolicy(JavaConverter.class)); 66 | } 67 | if (settings.isSuppressWarningsAnnotationEnabled()) { 68 | builder.annotationPolicy(new SuppressWarningsAnnotationPolicy("all")); 69 | } 70 | return new JavaConverter(builder.build()); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /core/src/main/java/io/t28/json2java/core/builder/MoshiClassBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core.builder; 18 | 19 | import com.google.common.collect.ImmutableList; 20 | import com.squareup.javapoet.AnnotationSpec; 21 | import com.squareup.javapoet.FieldSpec; 22 | import com.squareup.javapoet.MethodSpec; 23 | import com.squareup.javapoet.ParameterSpec; 24 | import com.squareup.javapoet.TypeName; 25 | import com.squareup.moshi.Json; 26 | import io.t28.json2java.core.naming.NamePolicy; 27 | 28 | import javax.annotation.Nonnull; 29 | import javax.lang.model.element.Modifier; 30 | import java.util.List; 31 | import java.util.stream.Collectors; 32 | 33 | public class MoshiClassBuilder extends ClassBuilder { 34 | public MoshiClassBuilder(@Nonnull NamePolicy fieldNamePolicy, 35 | @Nonnull NamePolicy methodNamePolicy, 36 | @Nonnull NamePolicy parameterNamePolicy) { 37 | super(fieldNamePolicy, methodNamePolicy, parameterNamePolicy); 38 | } 39 | 40 | @Nonnull 41 | @Override 42 | protected List buildFields() { 43 | return getProperties().entrySet().stream().map(property -> { 44 | final String name = property.getKey(); 45 | final TypeName type = property.getValue(); 46 | final String fieldName = fieldNamePolicy.convert(name, type); 47 | return FieldSpec.builder(type, fieldName) 48 | .addModifiers(Modifier.PRIVATE, Modifier.FINAL) 49 | .addAnnotation(AnnotationSpec.builder(Json.class) 50 | .addMember("name", "$S", name) 51 | .build()) 52 | .build(); 53 | }).collect(Collectors.toList()); 54 | } 55 | 56 | @Nonnull 57 | @Override 58 | protected List buildMethods() { 59 | final MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder() 60 | .addModifiers(Modifier.PUBLIC); 61 | 62 | final ImmutableList.Builder builder = ImmutableList.builder(); 63 | getProperties().entrySet().forEach(property -> { 64 | final String name = property.getKey(); 65 | final TypeName type = property.getValue(); 66 | final String fieldName = fieldNamePolicy.convert(name, type); 67 | final String methodName = methodNamePolicy.convert(name, type); 68 | builder.add(MethodSpec.methodBuilder(methodName) 69 | .addModifiers(Modifier.PUBLIC) 70 | .returns(type) 71 | .addStatement("return $L", fieldName) 72 | .build()); 73 | 74 | final String propertyName = parameterNamePolicy.convert(name, type); 75 | constructorBuilder.addParameter(ParameterSpec.builder(type, propertyName) 76 | .build()) 77 | .addStatement("this.$L = $L", fieldName, propertyName); 78 | }); 79 | builder.add(constructorBuilder.build()); 80 | return builder.build(); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /idea/src/main/java/io/t28/json2java/idea/util/PsiTypeConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.idea.util; 18 | 19 | import com.google.inject.Inject; 20 | import com.intellij.psi.PsiManager; 21 | import com.intellij.psi.PsiType; 22 | import com.intellij.psi.search.GlobalSearchScope; 23 | import com.squareup.javapoet.TypeName; 24 | 25 | import javax.annotation.CheckReturnValue; 26 | import javax.annotation.Nonnull; 27 | import java.util.function.Function; 28 | import java.util.stream.Stream; 29 | 30 | public class PsiTypeConverter implements Function { 31 | private final PsiManager psiManager; 32 | 33 | @Inject 34 | public PsiTypeConverter(@Nonnull PsiManager psiManager) { 35 | this.psiManager = psiManager; 36 | } 37 | 38 | @Nonnull 39 | @CheckReturnValue 40 | @Override 41 | public PsiType apply(@Nonnull TypeName typeName) { 42 | return Stream.of(PrimitiveType.values()) 43 | .filter(primitiveType -> primitiveType.isSameAs(typeName)) 44 | .map(PrimitiveType::psiType) 45 | .findFirst() 46 | .orElseGet(() -> PsiType.getJavaLangObject(psiManager, GlobalSearchScope.EMPTY_SCOPE)); 47 | } 48 | 49 | @Nonnull 50 | @CheckReturnValue 51 | static TypeName box(@Nonnull TypeName typeName) { 52 | try { 53 | return typeName.box(); 54 | } catch (Exception e) { 55 | return typeName; 56 | } 57 | } 58 | 59 | @Nonnull 60 | @CheckReturnValue 61 | static TypeName unbox(@Nonnull TypeName typeName) { 62 | try { 63 | return typeName.unbox(); 64 | } catch (Exception e) { 65 | return typeName; 66 | } 67 | } 68 | 69 | @SuppressWarnings("unused") 70 | enum PrimitiveType { 71 | BOOLEAN(PsiType.BOOLEAN, TypeName.BOOLEAN), 72 | BYTE(PsiType.BYTE, TypeName.BYTE), 73 | CHAR(PsiType.CHAR, TypeName.CHAR), 74 | DOUBLE(PsiType.DOUBLE, TypeName.DOUBLE), 75 | FLOAT(PsiType.FLOAT, TypeName.FLOAT), 76 | INT(PsiType.INT, TypeName.INT), 77 | LONG(PsiType.LONG, TypeName.LONG), 78 | SHORT(PsiType.SHORT, TypeName.SHORT), 79 | VOID(PsiType.VOID, TypeName.VOID); 80 | 81 | private final PsiType psiType; 82 | private final TypeName typeName; 83 | 84 | PrimitiveType(@Nonnull PsiType psiType, @Nonnull TypeName typeName) { 85 | this.psiType = psiType; 86 | this.typeName = typeName; 87 | } 88 | 89 | @Nonnull 90 | @CheckReturnValue 91 | PsiType psiType() { 92 | return psiType; 93 | } 94 | 95 | @CheckReturnValue 96 | boolean isSameAs(@Nonnull TypeName typeName) { 97 | final TypeName boxTypeName = box(typeName); 98 | final TypeName unboxTypeName = unbox(typeName); 99 | return this.typeName.equals(boxTypeName) || this.typeName.equals(unboxTypeName); 100 | } 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Json2Java4Idea 2 | ===== 3 | [![CircleCI](https://circleci.com/gh/t28hub/json2java4idea/tree/master.svg?style=shield&circle-token=30a68b03ab00d912be2f9f93b619ca8f4e36f061)](https://circleci.com/gh/t28hub/json2java4idea/tree/master) 4 | [![Apache 2.0](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/t28hub/json2java4idea/blob/master/LICENSE) 5 | [![Codacy Grade](https://api.codacy.com/project/badge/Grade/9fe5c57cf41a4cefb571dccdb68de994)](https://www.codacy.com/app/t28/json2java4idea?utm_source=github.com&utm_medium=referral&utm_content=t28hub/json2java4idea&utm_campaign=Badge_Grade) 6 | [![Codacy Coverage](https://api.codacy.com/project/badge/Coverage/9fe5c57cf41a4cefb571dccdb68de994)](https://www.codacy.com/app/t28/json2java4idea?utm_source=github.com&utm_medium=referral&utm_content=t28hub/json2java4idea&utm_campaign=Badge_Coverage) 7 | 8 | **Json2Java4Idea** is a plugin which generates Java class from JSON. 9 | 10 | ![Demo](demo.gif) 11 | 12 | ## Table of Contents 13 | * [Background](#background) 14 | * [Installation](#installation) 15 | * [Settings](#settings) 16 | * [Troubleshooting](#troubleshooting) 17 | * [Related Links](#related_links) 18 | * [License](#license) 19 | 20 | 21 | ## Background 22 | There are several JSON Serializing and deserializing library something like Jackson or Gson.
23 | However implementing a class which is converted from JSON is a bored stuff.
24 | Json2Java4Idea could be a solution for the stuff.
25 | The plugin generates immutable Java class from JSON and supports following libraries.
26 | * [Jackson](https://github.com/FasterXML/jackson) 27 | * [GSON](https://github.com/google/gson) 28 | * [Moshi](https://github.com/square/moshi) 29 | 30 | 31 | ## Installation 32 | 1. Open the **Settings/Preferences** dialog. 33 | 1. In the left pane, select **Plugins**. 34 | 1. In the right pane, click the **Browse repositories...** button. 35 | 1. In the dialog which opens, enter **Json2Java4Idea** into the search box. 36 | 1. In the right pane, click the **Install** button. 37 | 1. After installation, restart your IDE. 38 | 39 | 40 | ## Settings 41 | 1. Open the **Settings/Preferences** dialog. 42 | 1. In the left pane, select **Other Settings**. 43 | 1. In the right pane, click the **Json2Java4Idea** link. 44 | 1. After configuration changing finished, click the **Apply** button. 45 | 46 | Currently there are 3 types of settings you can configure. 47 | 1. Class style 48 | 1. Prefix of class name 49 | 1. Suffix of class name 50 | 1. Whether insert `@Generated` annotation 51 | 1. Whether insert `@SuppressWarnings` annotation 52 | 53 | 54 | ## Troubleshooting 55 | Please [raise an issue](https://github.com/t28hub/json2java4idea/issues/new) on this repository. 56 | 57 | 58 | ## Related Links 59 | * [Android Arsenal](https://android-arsenal.com/details/1/5450) 60 | * [Android Weekly#123](http://www.androidweekly.cn/android-dev-weekly-issue-123/) 61 | * [MobDevGroup](http://mobdevgroup.com/tools/android#json2java4idea) 62 | 63 | 64 | ## License 65 | ``` 66 | Copyright (c) 2017 Tatsuya Maki 67 | 68 | Licensed under the Apache License, Version 2.0 (the "License"); 69 | you may not use this file except in compliance with the License. 70 | You may obtain a copy of the License at 71 | 72 | http://www.apache.org/licenses/LICENSE-2.0 73 | 74 | Unless required by applicable law or agreed to in writing, software 75 | distributed under the License is distributed on an "AS IS" BASIS, 76 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 77 | See the License for the specific language governing permissions and 78 | limitations under the License. 79 | ``` 80 | -------------------------------------------------------------------------------- /core/src/main/java/io/t28/json2java/core/Style.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Tatsuya Maki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package io.t28.json2java.core; 18 | 19 | import io.t28.json2java.core.builder.ClassBuilder; 20 | import io.t28.json2java.core.builder.GsonClassBuilder; 21 | import io.t28.json2java.core.builder.JacksonClassBuilder; 22 | import io.t28.json2java.core.builder.ModelClassBuilder; 23 | import io.t28.json2java.core.builder.MoshiClassBuilder; 24 | import io.t28.json2java.core.naming.NamePolicy; 25 | 26 | import javax.annotation.CheckReturnValue; 27 | import javax.annotation.Nonnull; 28 | import java.util.Optional; 29 | import java.util.stream.Stream; 30 | 31 | public enum Style { 32 | NONE { 33 | @Nonnull 34 | @Override 35 | ClassBuilder toBuilder(@Nonnull NamePolicy fieldNameStrategy, 36 | @Nonnull NamePolicy methodNameStrategy, 37 | @Nonnull NamePolicy parameterNameStrategy) { 38 | return new ModelClassBuilder(fieldNameStrategy, methodNameStrategy, parameterNameStrategy); 39 | } 40 | }, 41 | GSON { 42 | @Nonnull 43 | @Override 44 | ClassBuilder toBuilder(@Nonnull NamePolicy fieldNameStrategy, 45 | @Nonnull NamePolicy methodNameStrategy, 46 | @Nonnull NamePolicy parameterNameStrategy) { 47 | return new GsonClassBuilder(fieldNameStrategy, methodNameStrategy, parameterNameStrategy); 48 | } 49 | }, 50 | JACKSON { 51 | @Nonnull 52 | @Override 53 | ClassBuilder toBuilder(@Nonnull NamePolicy fieldNameStrategy, 54 | @Nonnull NamePolicy methodNameStrategy, 55 | @Nonnull NamePolicy parameterNameStrategy) { 56 | return new JacksonClassBuilder(fieldNameStrategy, methodNameStrategy, parameterNameStrategy); 57 | } 58 | }, 59 | MOSHI { 60 | @Nonnull 61 | @Override 62 | ClassBuilder toBuilder(@Nonnull NamePolicy fieldNameStrategy, 63 | @Nonnull NamePolicy methodNameStrategy, 64 | @Nonnull NamePolicy parameterNameStrategy) { 65 | return new MoshiClassBuilder(fieldNameStrategy, methodNameStrategy, parameterNameStrategy); 66 | } 67 | }; 68 | 69 | @Nonnull 70 | @CheckReturnValue 71 | abstract ClassBuilder toBuilder(@Nonnull NamePolicy fieldNameStrategy, 72 | @Nonnull NamePolicy methodNameStrategy, 73 | @Nonnull NamePolicy parameterNameStrategy); 74 | 75 | @Nonnull 76 | @CheckReturnValue 77 | public static Optional