Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set prefixes/suffixes for generated classes #258

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public class Jsonschema2PojoTask extends Task implements GenerationConfig {

private boolean initializeCollections = true;

private String classNamePrefix = "";

private String classNameSuffix = "";

/**
* Execute this task (it's expected that all relevant setters will have been
* called by Ant to provide task configuration <em>before</em> this method
Expand Down Expand Up @@ -571,4 +575,14 @@ public boolean isInitializeCollections() {
return initializeCollections;
}

@Override
public String getClassNamePrefix() {
return classNamePrefix;
}

@Override
public String getClassNameSuffix() {
return classNameSuffix;
}

}
10 changes: 10 additions & 0 deletions jsonschema2pojo-ant/src/site/Jsonschema2PojoTask.html
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ <h3>Parameters</h3>
<td valign="top">Whether to initialize Set and List fields as empty collections, or leave them as <code>null</code>.</td>
<td align="center" valign="top">Yes (default <code>true</code>)</td>
</tr>
<tr>
<td valign="top">classNamePrefix</td>
<td valign="top">Whether to add a prefix to generated classes</td>
<td align="center" valign="top">No (default <code>""</code>)</td>
</tr>
<tr>
<td valign="top">classNameSuffix</td>
<td valign="top">Whether to add a Suffix to generated classes</td>
<td align="center" valign="top">No (default <code>""</code>)</td>
</tr>
</table>

<h3>Examples</h3>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ public class Arguments implements GenerationConfig {

@Parameter(names = { "-N", "--null-collections" }, description = "Initialize Set and List fields to null instead of an empty collection.")
private boolean nullCollections = false;

@Parameter(names = { "-y", "--class-prefix" }, description = "Initialize Set and List fields to null instead of an empty collection.")
private String classNamePrefix = "";

@Parameter(names = { "-x", "--class-suffix" }, description = "Initialize Set and List fields to null instead of an empty collection.")
private String classNameSuffix = "";

private static final int EXIT_OKAY = 0;
private static final int EXIT_ERROR = 1;
Expand Down Expand Up @@ -253,4 +259,14 @@ public boolean isInitializeCollections() {
return !nullCollections;
}

@Override
public String getClassNamePrefix() {
return classNamePrefix;
}

@Override
public String getClassNameSuffix() {
return classNameSuffix;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,14 @@ public FileFilter getFileFilter() {
public boolean isInitializeCollections() {
return true;
}

@Override
public String getClassNamePrefix() {
return "";
}

@Override
public String getClassNameSuffix() {
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,18 @@ public interface GenerationConfig {
* @return Whether to initialize collections with empty instance or null.
*/
boolean isInitializeCollections();

/**
* Gets the 'getClassNamePrefix' configuration option.
*
* @return Whether to initialize collections with empty instance or null.
*/
String getClassNamePrefix();

/**
* Gets the 'getClassNameSuffix' configuration option.
*
* @return Whether to initialize collections with empty instance or null.
*/
String getClassNameSuffix();
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,12 @@ private JDefinedClass createClass(String nodeName, JsonNode node, JPackage _pack
if (isPrimitive(fqn, _package.owner())) {
throw new ClassAlreadyExistsException(primitiveType(fqn, _package.owner()));
}


int index = fqn.lastIndexOf(".") + 1;
if(index >= 0 && index<fqn.length()) {
fqn = fqn.substring(0, index) + ruleFactory.getGenerationConfig().getClassNamePrefix() + fqn.substring(index) +ruleFactory.getGenerationConfig().getClassNameSuffix();
}

try {
JClass existingClass = _package.owner().ref(Thread.currentThread().getContextClassLoader().loadClass(fqn));

Expand Down
54 changes: 54 additions & 0 deletions jsonschema2pojo-core/src/test/resources/schema/array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"description": "list of products",
"type": "object",
"javaType": "Product",
"properties":
{
"id":
{
"title": "id",
"type": "string",
"additionalProperties": false
},

"titleSortName":
{
"type": "string"
},

"productType":
{
"title": "Type of products",
"type": "array",
"items":
{
"$ref": "#/definitions/product-type"
}
}
},

"definitions":
{
"product-type":
{
"title": "product type",
"type": "object",
"javaType": "ProductTypes",
"properties":
{
"id":
{
"title": "Id",
"type": "string",
"additionalProperties": false
},

"type":
{
"title": "Type",
"type": "string"
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class JsonSchemaExtension implements GenerationConfig {
boolean useCommonsLang3
FileFilter fileFilter
boolean initializeCollections
String classNamePrefix
String classNameSuffix

public JsonSchemaExtension() {
// See DefaultGenerationConfig
Expand All @@ -74,6 +76,8 @@ public class JsonSchemaExtension implements GenerationConfig {
useCommonsLang3 = false
fileFilter = new AllFileFilter()
initializeCollections = true
classNamePrefix = ''
classNameSuffix = ''
}

@Override
Expand Down Expand Up @@ -102,6 +106,22 @@ public class JsonSchemaExtension implements GenerationConfig {
public void setSourceType(String s) {
sourceType = SourceType.valueOf(s.toUpperCase())
}

public void setClassNamePrefix(String s) {
classNamePrefix = s
}

public String getClassNamePrefix() {
classNamePrefix
}

public void setClassNameSuffix(String s) {
classNameSuffix = s
}

public String getClassNameSuffix() {
classNameSuffix
}

@Override
public String toString() {
Expand All @@ -125,6 +145,8 @@ public class JsonSchemaExtension implements GenerationConfig {
|useJodaDates = ${useJodaDates}
|useCommonsLang3 = ${useCommonsLang3}
|initializeCollections = ${initializeCollections}
|classNamePrefix = ${classNamePrefix}
|classNameSuffix = ${classNameSuffix}
""".stripMargin()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Copyright © 2010-2014 Nokia
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jsonschema2pojo.integration.config;

import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import java.lang.reflect.Method;

import org.apache.maven.plugin.MojoExecutionException;
import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.JsonNode;

import org.jsonschema2pojo.Annotator;

import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JMethod;

public class PrefixSuffixIT {

@Test
public void defaultClassPrefix() throws ClassNotFoundException {

ClassLoader resultsClassLoader = generateAndCompile("/schema/properties/primitiveProperties.json", "com.example");
resultsClassLoader.loadClass("com.example.PrimitiveProperties");
}

@Test
public void customClassPrefix() throws ClassNotFoundException{

ClassLoader resultsClassLoader = generateAndCompile("/schema/properties/primitiveProperties.json", "com.example", config("classNamePrefix","Abstract"));
resultsClassLoader.loadClass("com.example.AbstractPrimitiveProperties");
}

@Test
public void defaultClassSufix() throws ClassNotFoundException{

ClassLoader resultsClassLoader = generateAndCompile("/schema/properties/primitiveProperties.json", "com.example");
resultsClassLoader.loadClass("com.example.PrimitiveProperties");
}

@Test
public void customClassSuffix() throws ClassNotFoundException{

ClassLoader resultsClassLoader = generateAndCompile("/schema/properties/primitiveProperties.json", "com.example", config("classNameSuffix","Dao"));
resultsClassLoader.loadClass("com.example.PrimitivePropertiesDao");
}

@Test(expected = ClassNotFoundException.class)
public void NotExitstingClassPrefix() throws ClassNotFoundException{

ClassLoader resultsClassLoader = generateAndCompile("/schema/properties/primitiveProperties.json", "com.example", config("classNamePrefix","Abstract"));
resultsClassLoader.loadClass("com.example.NotExistingPrimitiveProperties");
}

@Test(expected = ClassNotFoundException.class)
public void NotExitstingClassSufix() throws ClassNotFoundException{

ClassLoader resultsClassLoader = generateAndCompile("/schema/properties/primitiveProperties.json", "com.example", config("classNameSuffix","Dao"));
resultsClassLoader.loadClass("com.example.NotExistingPrimitiveProperties");
}

@Test(expected = ClassNotFoundException.class)
public void SuffixWithDefaultPackageName() throws ClassNotFoundException{
ClassLoader resultsClassLoader = generateAndCompile("/schema/properties/primitiveProperties.json", "", config("classNameSuffix","Dao"));
resultsClassLoader.loadClass("com.example.NotExistingPrimitiveProperties");
}

@Test(expected = ClassNotFoundException.class)
public void PrefixWithDefaultPackageName() throws ClassNotFoundException{
ClassLoader resultsClassLoader = generateAndCompile("/schema/properties/primitiveProperties.json", "", config("classNamePrefix","Abstract"));
resultsClassLoader.loadClass("com.example.NotExistingPrimitiveProperties");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,22 @@ public class Jsonschema2PojoMojo extends AbstractMojo implements GenerationConfi
private MavenProject project;

private FileFilter fileFilter = new AllFileFilter();

/**
* Whether to add a prefix to generated classes.
*
* @parameter expression="${jsonschema2pojo.classNamePrefix}"
* @since 0.4.6
*/
private String classNamePrefix = "";

/**
* Whether to add a prefix to generated classes.
*
* @parameter expression="${jsonschema2pojo.classNameSuffix}"
* @since 0.4.6
*/
private String classNameSuffix = "";

/**
* Executes the plugin, to read the given source and behavioural properties
Expand Down Expand Up @@ -571,4 +587,14 @@ FileFilter createFileFilter() throws MojoExecutionException {
throw new MojoExecutionException("could not create file filter", e);
}
}

@Override
public String getClassNamePrefix() {
return classNamePrefix;
}

@Override
public String getClassNameSuffix() {
return classNameSuffix;
}
}