Skip to content

Commit

Permalink
Merge pull request #1483 from joelittlejohn/better-logging
Browse files Browse the repository at this point in the history
Add useful debug logging when reading schemas and creating classes
  • Loading branch information
joelittlejohn authored Feb 15, 2023
2 parents a3f47de + a55ee31 commit 21eebc3
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static void generate(GenerationConfig config, RuleLogger logger) throws I
ruleFactory.setAnnotator(annotator);
ruleFactory.setGenerationConfig(config);
ruleFactory.setLogger(logger);
ruleFactory.setSchemaStore(new SchemaStore(createContentResolver(config)));
ruleFactory.setSchemaStore(new SchemaStore(createContentResolver(config), logger));

SchemaMapper mapper = new SchemaMapper(ruleFactory, createSchemaGenerator(config));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright © 2010-2020 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;

public class NoopRuleLogger extends AbstractRuleLogger {

@Override
protected void doDebug(String msg) {
}

@Override
protected void doError(String msg, Throwable e) {
}

@Override
protected void doInfo(String msg) {
}

@Override
protected void doTrace(String msg) {
}

@Override
protected void doWarn(String msg, Throwable e) {
}

@Override
public boolean isDebugEnabled() {
return false;
}

@Override
public boolean isErrorEnabled() {
return false;
}

@Override
public boolean isInfoEnabled() {
return false;
}

@Override
public boolean isTraceEnabled() {
return false;
}

@Override
public boolean isWarnEnabled() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ public class SchemaStore {

protected final FragmentResolver fragmentResolver = new FragmentResolver();
protected final ContentResolver contentResolver;
protected final RuleLogger logger;

public SchemaStore() {
this.contentResolver = new ContentResolver();
this.logger = new NoopRuleLogger();
}

public SchemaStore(ContentResolver contentResolver) {
public SchemaStore(ContentResolver contentResolver, RuleLogger logger) {
this.contentResolver = contentResolver;
this.logger = logger;
}

/**
Expand All @@ -59,6 +62,7 @@ public synchronized Schema create(URI id, String refFragmentPathDelimiters) {

URI baseId = removeFragment(id).normalize();
if (!schemas.containsKey(baseId)) {
logger.debug("Reading schema: " + baseId);
final JsonNode baseContent = contentResolver.resolve(baseId);
schemas.put(baseId, new Schema(baseId, baseContent, null));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ protected EnumDefinition buildEnumDefinition(String nodeName, JsonNode node, JTy

if (!javaEnumNames.isMissingNode())
{
logger.error("javaEnumNames is deprecated; please migrate to javaEnums.");
logger.warn("javaEnumNames is deprecated; please migrate to javaEnums.");
}

EnumDefinition enumDefinition;
Expand Down Expand Up @@ -353,11 +353,15 @@ protected JDefinedClass createEnum(JsonNode node, String nodeName, JClassContain
Class<?> existingClass = Thread.currentThread().getContextClassLoader().loadClass(fqn);
throw new ClassAlreadyExistsException(container.owner().ref(existingClass));
} catch (ClassNotFoundException e) {
return container.owner()._class(fqn, ClassType.ENUM);
JDefinedClass enumClass = container.owner()._class(fqn, ClassType.ENUM);
ruleFactory.getLogger().debug("Adding " + enumClass.fullName());
return enumClass;
}
} else {
try {
return container._class(JMod.PUBLIC, getEnumName(nodeName, node, container), ClassType.ENUM);
JDefinedClass enumClass = container._class(JMod.PUBLIC, getEnumName(nodeName, node, container), ClassType.ENUM);
ruleFactory.getLogger().debug("Adding " + enumClass.fullName());
return enumClass;
} catch (JClassAlreadyExistsException e) {
throw new GenerationException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@
import org.jsonschema2pojo.Schema;
import org.jsonschema2pojo.exception.ClassAlreadyExistsException;
import org.jsonschema2pojo.exception.GenerationException;
import org.jsonschema2pojo.util.AnnotationHelper;
import org.jsonschema2pojo.util.ParcelableHelper;
import org.jsonschema2pojo.util.ReflectionHelper;
import org.jsonschema2pojo.util.SerializableHelper;
import org.jsonschema2pojo.util.AnnotationHelper;

import com.fasterxml.jackson.databind.JsonNode;
import com.sun.codemodel.ClassType;
import com.sun.codemodel.JAnnotationUse;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JClassAlreadyExistsException;
Expand Down Expand Up @@ -242,12 +241,15 @@ private JDefinedClass createClass(String nodeName, JsonNode node, JPackage _pack
} else {
newType = _package.owner()._class(fqn);
}
ruleFactory.getLogger().debug("Adding " + newType.fullName());
} else {
final String className = ruleFactory.getNameHelper().getUniqueClassName(nodeName, node, _package);
if (usePolymorphicDeserialization) {
newType = _package._class(JMod.PUBLIC, ruleFactory.getNameHelper().getUniqueClassName(nodeName, node, _package), ClassType.CLASS);
newType = _package._class(JMod.PUBLIC, className, ClassType.CLASS);
} else {
newType = _package._class(ruleFactory.getNameHelper().getUniqueClassName(nodeName, node, _package));
newType = _package._class(className);
}
ruleFactory.getLogger().debug("Adding " + newType.fullName());
}
} catch (JClassAlreadyExistsException e) {
throw new ClassAlreadyExistsException(e.getExistingClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.jsonschema2pojo.DefaultGenerationConfig;
import org.jsonschema2pojo.GenerationConfig;
import org.jsonschema2pojo.Jackson2Annotator;
import org.jsonschema2pojo.NoopRuleLogger;
import org.jsonschema2pojo.RuleLogger;
import org.jsonschema2pojo.SchemaStore;
import org.jsonschema2pojo.util.NameHelper;
Expand Down Expand Up @@ -66,6 +67,7 @@ public RuleFactory(GenerationConfig generationConfig, Annotator annotator, Schem
this.schemaStore = schemaStore;
this.nameHelper = new NameHelper(generationConfig);
this.reflectionHelper = new ReflectionHelper(this);
this.logger = new NoopRuleLogger();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class GenerateJsonSchemaAndroidTask extends SourceTask {
if (!configuration.targetVersion) {
def compileJavaTask = project.getTasksByName("compileJava", false).first()
configuration.targetVersion = compileJavaTask.getProperties().get("sourceCompatibility")
logger.warn 'Using Gradle targetCompatibility as targetVersion for jsonschema2pojo: ' + configuration.targetVersion
logger.info 'Using Gradle sourceCompatibility as targetVersion for jsonschema2pojo: ' + configuration.targetVersion
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class GenerateJsonSchemaJavaTask extends DefaultTask {
if (!configuration.targetVersion) {
def compileJavaTask = project.getTasksByName("compileJava", false).first()
configuration.targetVersion = compileJavaTask.getProperties().get("sourceCompatibility")
logger.warn 'Using Gradle targetCompatibility as targetVersion for jsonschema2pojo: ' + configuration.targetVersion
logger.info 'Using Gradle sourceCompatibility as targetVersion for jsonschema2pojo: ' + configuration.targetVersion
}
}
}
35 changes: 35 additions & 0 deletions jsonschema2pojo-intellij-formatter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<code_scheme name="Default" version="173">
<option name="ENABLE_SECOND_REFORMAT" value="true" />
<GroovyCodeStyleSettings>
<option name="CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND" value="99" />
<option name="NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND" value="1" />
<option name="IMPORT_LAYOUT_TABLE">
<value>
<package name="" withSubpackages="true" static="true" />
<emptyLine />
<package name="org.jsonschema2pojo" withSubpackages="true" static="false" />
<emptyLine />
<package name="" withSubpackages="true" static="false" />
<emptyLine />
</value>
</option>
</GroovyCodeStyleSettings>
<JavaCodeStyleSettings>
<option name="CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND" value="99" />
<option name="NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND" value="1" />
<option name="IMPORT_LAYOUT_TABLE">
<value>
<package name="" withSubpackages="true" static="true" />
<emptyLine />
<package name="java" withSubpackages="true" static="false" />
<emptyLine />
<package name="org" withSubpackages="true" static="false" />
<emptyLine />
<package name="com" withSubpackages="true" static="false" />
<emptyLine />
<package name="" withSubpackages="true" static="false" />
<emptyLine />
</value>
</option>
</JavaCodeStyleSettings>
</code_scheme>

0 comments on commit 21eebc3

Please sign in to comment.