Search in sources :

Example 6 with GenerationException

use of org.jsonschema2pojo.exception.GenerationException in project jsonschema2pojo by joelittlejohn.

the class TypeUtil method resolveType.

public static JClass resolveType(JClassContainer _package, String typeDefinition) {
    try {
        FieldDeclaration fieldDeclaration = (FieldDeclaration) JavaParser.parseBodyDeclaration(typeDefinition + " foo;");
        ClassOrInterfaceType c = (ClassOrInterfaceType) ((ReferenceType) fieldDeclaration.getType()).getType();
        return buildClass(_package, c, 0);
    } catch (ParseException e) {
        throw new GenerationException("Couldn't parse type: " + typeDefinition, e);
    }
}
Also used : GenerationException(org.jsonschema2pojo.exception.GenerationException) ClassOrInterfaceType(japa.parser.ast.type.ClassOrInterfaceType) ParseException(japa.parser.ParseException) FieldDeclaration(japa.parser.ast.body.FieldDeclaration)

Example 7 with GenerationException

use of org.jsonschema2pojo.exception.GenerationException in project jsonschema2pojo by joelittlejohn.

the class SerializableHelper method addSerializableSupport.

public static void addSerializableSupport(JDefinedClass jclass) {
    jclass._implements(Serializable.class);
    try {
        final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        final DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
        processDefinedClassForSerializableSupport(jclass, dataOutputStream);
        dataOutputStream.flush();
        final MessageDigest digest = MessageDigest.getInstance("SHA");
        final byte[] digestBytes = digest.digest(byteArrayOutputStream.toByteArray());
        long serialVersionUID = 0L;
        for (int i = Math.min(digestBytes.length, 8) - 1; i >= 0; i--) {
            serialVersionUID = serialVersionUID << 8 | digestBytes[i] & 0xff;
        }
        JFieldVar serialUIDField = jclass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, long.class, "serialVersionUID");
        serialUIDField.init(JExpr.lit(serialVersionUID));
    } catch (IOException exception) {
        throw new GenerationException("IOException while generating serialversionUID field while adding serializable support to class: " + jclass.fullName(), exception);
    } catch (NoSuchAlgorithmException exception) {
        throw new GenerationException("SHA algorithm not found when trying to generate serialversionUID field while adding serializable support to class: " + jclass.fullName(), exception);
    }
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) DataOutputStream(java.io.DataOutputStream) GenerationException(org.jsonschema2pojo.exception.GenerationException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

GenerationException (org.jsonschema2pojo.exception.GenerationException)7 CodeWriter (com.sun.codemodel.CodeWriter)2 JCodeModel (com.sun.codemodel.JCodeModel)2 URL (java.net.URL)2 RuleFactory (org.jsonschema2pojo.rules.RuleFactory)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)1 SchemaAware (com.fasterxml.jackson.databind.jsonschema.SchemaAware)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 DefaultSerializerProvider (com.fasterxml.jackson.databind.ser.DefaultSerializerProvider)1 JClass (com.sun.codemodel.JClass)1 JClassAlreadyExistsException (com.sun.codemodel.JClassAlreadyExistsException)1 JDefinedClass (com.sun.codemodel.JDefinedClass)1 JFieldVar (com.sun.codemodel.JFieldVar)1 ParseException (japa.parser.ParseException)1 FieldDeclaration (japa.parser.ast.body.FieldDeclaration)1 ClassOrInterfaceType (japa.parser.ast.type.ClassOrInterfaceType)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 IOException (java.io.IOException)1