org.jvnet.ws.wadl2java.common
Class BaseResourceClassGenerator

java.lang.Object
  extended by org.jvnet.ws.wadl2java.common.BaseResourceClassGenerator
All Implemented Interfaces:
ResourceClassGenerator
Direct Known Subclasses:
JAXRS20ResourceClassGenerator, Jersey1xResourceClassGenerator

public abstract class BaseResourceClassGenerator
extends Object
implements ResourceClassGenerator

Generator class for nested static classes used to represent web resources.

Author:
mh124079

Nested Class Summary
protected static class BaseResourceClassGenerator.MethodType
           
 
Field Summary
protected  com.sun.codemodel.JCodeModel codeModel
           
protected static String CREATE_CLIENT_INSTANCE
          The method that can be called to create the client instance so that generators can override factory methods
protected static String CREATE_CLIENT_METHOD
          The method that can be called to create a fully configured client
protected static String CUSTOMIZE_CLIENT_METHOD
          The method that can be called so that generators have a simple template to customise the client
protected  com.sun.codemodel.JPackage pkg
           
 
Constructor Summary
BaseResourceClassGenerator(MessageListener messageListener, Resolver resolver, com.sun.codemodel.JCodeModel codeModel, com.sun.codemodel.JPackage pkg, String generatedPackages, JavaDocUtil javaDoc, com.sun.codemodel.JDefinedClass clazz)
          Creates a new instance of BaseResourceClassGenerator.
BaseResourceClassGenerator(MessageListener messageListener, Resolver resolver, com.sun.codemodel.JCodeModel codeModel, com.sun.codemodel.JPackage pkg, String generatedPackages, JavaDocUtil javaDoc, ResourceNode resource)
          Creates a new instance of BaseResourceClassGenerator.
 
Method Summary
protected abstract  String buildMethod()
           
protected abstract  String clientFactoryMethod()
           
protected abstract  com.sun.codemodel.JClass clientFactoryType()
           
protected abstract  com.sun.codemodel.JClass clientResponseClientType()
           
protected abstract  com.sun.codemodel.JClass clientType()
           
protected abstract  com.sun.codemodel.JExpression[] createProcessInvocation(MethodNode method, com.sun.codemodel.JBlock $methodBody, com.sun.codemodel.JVar $resourceBuilder, String methodString, RepresentationNode inputRep, com.sun.codemodel.JType returnType, com.sun.codemodel.JExpression $returnTypeExpr, com.sun.codemodel.JExpression $entityExpr)
           
protected abstract  com.sun.codemodel.JVar createRequestBuilderAndAccept(com.sun.codemodel.JBlock $methodBody, com.sun.codemodel.JVar $resource, RepresentationNode outputRep)
           
 void generateBeanProperty(com.sun.codemodel.JDefinedClass $impl, List<Param> matrixParameters, Param p, boolean isAbstract)
          Generate a bean setter and getter for a parameter.
protected  void generateBody(MethodNode method, boolean isJAXB, Map<com.sun.codemodel.JType,com.sun.codemodel.JDefinedClass> exceptionMap, RepresentationNode outputRep, com.sun.codemodel.JVar $genericMethodParameter, boolean wrapInputTypeInJAXBElement, com.sun.codemodel.JType inputType, com.sun.codemodel.JType returnType, com.sun.codemodel.JVar $resourceBuilder, RepresentationNode inputRep, com.sun.codemodel.JBlock $methodBody)
          Generate a method body that uses a JAXBDispatcher, used when the payloads are XML.
 com.sun.codemodel.JDefinedClass generateClass(com.sun.codemodel.JDefinedClass parentClass, com.sun.codemodel.JVar $global_base_uri)
          Generate a static member class that represents a WADL resource.
protected abstract  void generateClientFactoryMethod(com.sun.codemodel.JDefinedClass parentClass)
          This method should create a static private method called CREATE_CLIENT_METHOD that generate the right factory code for this particular implementation
protected  void generateConditionalForFaultNode(MethodNode method, com.sun.codemodel.JBlock $methodBody, com.sun.codemodel.JVar $response, com.sun.codemodel.JType returnType, com.sun.codemodel.JExpression $returnTypeExpr)
          Generates the switch block based on status code that will throw exceptions for a specified failure
protected  com.sun.codemodel.JDefinedClass generateExceptionClass(FaultNode f)
          Create an exception class that wraps an element used for indicating a fault condition.
protected abstract  com.sun.codemodel.JDefinedClass generateExceptionClassInternal(String exName, FaultNode f)
          Try to create a new exception class that is relevant for the platform
protected  void generateIfOnStatus(com.sun.codemodel.JBlock block, com.sun.codemodel.JVar $response, com.sun.codemodel.JType returnType, com.sun.codemodel.JExpression $returnTypeExpr)
          Create a if statement in a block that will throw an exception if the status is >= 400 is the returnType and return expression are not if the type clientReponseType
protected  void generateMethodDecl(Map<com.sun.codemodel.JType,com.sun.codemodel.JDefinedClass> exceptionMap, MethodNode method, boolean includeOptionalParams, RepresentationNode inputRep, RepresentationNode outputRep, BaseResourceClassGenerator.MethodType methodType, boolean isAbstract)
          Generate a Java method for a specified combination of WADL method, input representation and output representation elements.
 void generateMethodDecls(MethodNode method, boolean isAbstract)
          Generate a set of method declarations for a WADL method element.
protected  void generateMethodVariants(Map<com.sun.codemodel.JType,com.sun.codemodel.JDefinedClass> exceptionMap, MethodNode method, boolean includeOptionalParams, RepresentationNode inputRep, RepresentationNode outputRep, boolean isAbstract)
          Generate one or two Java methods for a specified combination of WADL method, input representation and output representation elements.
protected abstract  void generateThrowWebApplicationExceptionFromResponse(com.sun.codemodel.JBlock caseBody, com.sun.codemodel.JVar $response)
          Invoked when we need to throw a generic failure exception because we don't have an element mapped.
protected abstract  com.sun.codemodel.JClass genericTypeType()
           
protected  com.sun.codemodel.JDefinedClass getGeneratedClass()
          Get the class for which methods will be generated.
protected  String getMethodName(MethodNode method, RepresentationNode inputRep, RepresentationNode outputRep, com.sun.codemodel.JType returnType)
          Generate a name for the method.
protected  com.sun.codemodel.JType getTypeFromElement(QName element)
          Get the Java type generated for the specified XML element name.
protected  com.sun.codemodel.JType getTypeFromURI(String path)
          Get the Java type generated for the specified JSON URI
protected  void overrideMessageOnException(com.sun.codemodel.JDefinedClass $exception)
          Override the getMessage class on an exception to make sure the status code is displayed
protected abstract  com.sun.codemodel.JClass resourceBuilderType()
           
protected abstract  String resourceFromClientMethod()
           
protected abstract  com.sun.codemodel.JClass resourceType()
           
protected abstract  String responseGetEntityMethod()
           
protected abstract  com.sun.codemodel.JClass uriTemplateType()
           
protected  com.sun.codemodel.JClass webApplicationExceptionType()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CREATE_CLIENT_METHOD

protected static final String CREATE_CLIENT_METHOD
The method that can be called to create a fully configured client

See Also:
Constant Field Values

CUSTOMIZE_CLIENT_METHOD

protected static final String CUSTOMIZE_CLIENT_METHOD
The method that can be called so that generators have a simple template to customise the client

See Also:
Constant Field Values

CREATE_CLIENT_INSTANCE

protected static final String CREATE_CLIENT_INSTANCE
The method that can be called to create the client instance so that generators can override factory methods

See Also:
Constant Field Values

pkg

protected com.sun.codemodel.JPackage pkg

codeModel

protected com.sun.codemodel.JCodeModel codeModel
Constructor Detail

BaseResourceClassGenerator

public BaseResourceClassGenerator(MessageListener messageListener,
                                  Resolver resolver,
                                  com.sun.codemodel.JCodeModel codeModel,
                                  com.sun.codemodel.JPackage pkg,
                                  String generatedPackages,
                                  JavaDocUtil javaDoc,
                                  ResourceNode resource)
Creates a new instance of BaseResourceClassGenerator.

Parameters:
javaDoc - a JavaDocUtil instance for use when generating documentation.
resolver - the schema2java model to use for element to class mapping lookups.
codeModel - code model instance to use when generating code.
pkg - package for new classes.
resource - the resource element for which to generate a class.

BaseResourceClassGenerator

public BaseResourceClassGenerator(MessageListener messageListener,
                                  Resolver resolver,
                                  com.sun.codemodel.JCodeModel codeModel,
                                  com.sun.codemodel.JPackage pkg,
                                  String generatedPackages,
                                  JavaDocUtil javaDoc,
                                  com.sun.codemodel.JDefinedClass clazz)
Creates a new instance of BaseResourceClassGenerator.

Parameters:
javaDoc - a JavaDocUtil instance for use when generating documentation.
resolver - the schema2java model to use for element to class mapping lookups.
codeModel - code model instance to use when generating code.
pkg - package for new classes.
clazz - the existing class.
Method Detail

generateClientFactoryMethod

protected abstract void generateClientFactoryMethod(com.sun.codemodel.JDefinedClass parentClass)
This method should create a static private method called CREATE_CLIENT_METHOD that generate the right factory code for this particular implementation

Parameters:
parentClass - The root class to add the method to

getGeneratedClass

protected com.sun.codemodel.JDefinedClass getGeneratedClass()
Get the class for which methods will be generated.

Returns:
the class or null if no class has yet been generated.

clientType

protected abstract com.sun.codemodel.JClass clientType()

clientFactoryType

protected abstract com.sun.codemodel.JClass clientFactoryType()

clientFactoryMethod

protected abstract String clientFactoryMethod()

clientResponseClientType

protected abstract com.sun.codemodel.JClass clientResponseClientType()

genericTypeType

protected abstract com.sun.codemodel.JClass genericTypeType()

resourceType

protected abstract com.sun.codemodel.JClass resourceType()

uriTemplateType

protected abstract com.sun.codemodel.JClass uriTemplateType()

resourceFromClientMethod

protected abstract String resourceFromClientMethod()

resourceBuilderType

protected abstract com.sun.codemodel.JClass resourceBuilderType()

buildMethod

protected abstract String buildMethod()

responseGetEntityMethod

protected abstract String responseGetEntityMethod()

webApplicationExceptionType

protected com.sun.codemodel.JClass webApplicationExceptionType()
Returns:
Eventually this will return just WebApplicationException; but until JAX_RS_SPEC-312 is resolved we need to aid the user by overriding some of the methods and providing an alternative

overrideMessageOnException

protected void overrideMessageOnException(com.sun.codemodel.JDefinedClass $exception)
Override the getMessage class on an exception to make sure the status code is displayed

Parameters:
$exception -

createRequestBuilderAndAccept

protected abstract com.sun.codemodel.JVar createRequestBuilderAndAccept(com.sun.codemodel.JBlock $methodBody,
                                                                        com.sun.codemodel.JVar $resource,
                                                                        RepresentationNode outputRep)

createProcessInvocation

protected abstract com.sun.codemodel.JExpression[] createProcessInvocation(MethodNode method,
                                                                           com.sun.codemodel.JBlock $methodBody,
                                                                           com.sun.codemodel.JVar $resourceBuilder,
                                                                           String methodString,
                                                                           RepresentationNode inputRep,
                                                                           com.sun.codemodel.JType returnType,
                                                                           com.sun.codemodel.JExpression $returnTypeExpr,
                                                                           com.sun.codemodel.JExpression $entityExpr)
Returns:
A list of expressions, the first being a getEntity like call and the second if present the straight (Client)Response object

generateClass

public com.sun.codemodel.JDefinedClass generateClass(com.sun.codemodel.JDefinedClass parentClass,
                                                     com.sun.codemodel.JVar $global_base_uri)
                                              throws com.sun.codemodel.JClassAlreadyExistsException
Generate a static member class that represents a WADL resource.

Specified by:
generateClass in interface ResourceClassGenerator
Parameters:
parentClass - the parent class for the generated class.
$global_base_uri - a reference to the field that contains the base URI.
Returns:
the generated class.
Throws:
com.sun.codemodel.JClassAlreadyExistsException - if a class with the same name already exists.

generateExceptionClass

protected com.sun.codemodel.JDefinedClass generateExceptionClass(FaultNode f)
Create an exception class that wraps an element used for indicating a fault condition.

Parameters:
f - the WADL fault element for which to generate the exception class.
Returns:
the generated exception class.

generateExceptionClassInternal

protected abstract com.sun.codemodel.JDefinedClass generateExceptionClassInternal(String exName,
                                                                                  FaultNode f)
                                                                           throws com.sun.codemodel.JClassAlreadyExistsException
Try to create a new exception class that is relevant for the platform

Throws:
com.sun.codemodel.JClassAlreadyExistsException - should it already exists

generateThrowWebApplicationExceptionFromResponse

protected abstract void generateThrowWebApplicationExceptionFromResponse(com.sun.codemodel.JBlock caseBody,
                                                                         com.sun.codemodel.JVar $response)
Invoked when we need to throw a generic failure exception because we don't have an element mapped.


generateConditionalForFaultNode

protected void generateConditionalForFaultNode(MethodNode method,
                                               com.sun.codemodel.JBlock $methodBody,
                                               com.sun.codemodel.JVar $response,
                                               com.sun.codemodel.JType returnType,
                                               com.sun.codemodel.JExpression $returnTypeExpr)
Generates the switch block based on status code that will throw exceptions for a specified failure


generateIfOnStatus

protected void generateIfOnStatus(com.sun.codemodel.JBlock block,
                                  com.sun.codemodel.JVar $response,
                                  com.sun.codemodel.JType returnType,
                                  com.sun.codemodel.JExpression $returnTypeExpr)
Create a if statement in a block that will throw an exception if the status is >= 400 is the returnType and return expression are not if the type clientReponseType

Parameters:
block -
$response -

generateMethodDecls

public void generateMethodDecls(MethodNode method,
                                boolean isAbstract)
Generate a set of method declarations for a WADL method element.

Generates two Java methods per returned representation type for each request type, one with all optional parameters and one without. I.e. if the WADL method specifies two possible request representation formats and three supported response representation formats, this method will generate twelve Java methods, one for each combination.

Specified by:
generateMethodDecls in interface ResourceClassGenerator
Parameters:
isAbstract - controls whether the generated methods will have a body false or not true.
method - the WADL method element to process.

getTypeFromElement

protected com.sun.codemodel.JType getTypeFromElement(QName element)
Get the Java type generated for the specified XML element name.

Note that the specified element must be declared as a top-level element in a schema imported by the WADL file otherwise no such Java type will have been generated and this method will return Object.

Parameters:
element - the name of the XML element.
Returns:
the Java type that was generated for the specified element or null if no matching generated type was found.

getTypeFromURI

protected com.sun.codemodel.JType getTypeFromURI(String path)
Get the Java type generated for the specified JSON URI

Returns:
the Java type that was generated for the specified uri or null if no matching generated type was found.

generateMethodVariants

protected void generateMethodVariants(Map<com.sun.codemodel.JType,com.sun.codemodel.JDefinedClass> exceptionMap,
                                      MethodNode method,
                                      boolean includeOptionalParams,
                                      RepresentationNode inputRep,
                                      RepresentationNode outputRep,
                                      boolean isAbstract)
Generate one or two Java methods for a specified combination of WADL method, input representation and output representation elements. Always generates one method that works with DataSources and generates an additional method that uses JAXB_MAPPING when XML representations are used and the document element is specified.

Parameters:
isAbstract - controls whether the generated methods will have a body false or not true.
exceptionMap - maps generated types to the corresponding exception class. Used to generate the throws clause for the method and the code to map output types to exceptions when the output type is designated as a fault.
method - the WADL method element for the Java method being generated.
includeOptionalParams - whether to include optional parameters in the method signature or not.
inputRep - the WADL representation element for the request format.
outputRep - the WADL representation element for the response format.

getMethodName

protected String getMethodName(MethodNode method,
                               RepresentationNode inputRep,
                               RepresentationNode outputRep,
                               com.sun.codemodel.JType returnType)
Generate a name for the method.

Parameters:
method - the WADL method element for the Java method being generated.
inputRep - the WADL representation element for the request format.
outputRep - the WADL representation element for the response format.
returnType - a reference to the Java return type.
Returns:
a suitable method name.

generateMethodDecl

protected void generateMethodDecl(Map<com.sun.codemodel.JType,com.sun.codemodel.JDefinedClass> exceptionMap,
                                  MethodNode method,
                                  boolean includeOptionalParams,
                                  RepresentationNode inputRep,
                                  RepresentationNode outputRep,
                                  BaseResourceClassGenerator.MethodType methodType,
                                  boolean isAbstract)
Generate a Java method for a specified combination of WADL method, input representation and output representation elements.

Parameters:
methodType - BaseResourceClassGenerator.MethodType
isAbstract - controls whether the generated methods will have a body false or not true.
exceptionMap - maps generated types to the corresponding exception class. Used to generate the throws clause for the method and the code to map output types to exceptions when the output type is designated as a fault.
method - the WADL method element for the Java method being generated.
includeOptionalParams - whether to include optional parameters in the method signature or not.
inputRep - the WADL representation element for the request format.
outputRep - the WADL representation element for the response format.

generateBody

protected void generateBody(MethodNode method,
                            boolean isJAXB,
                            Map<com.sun.codemodel.JType,com.sun.codemodel.JDefinedClass> exceptionMap,
                            RepresentationNode outputRep,
                            com.sun.codemodel.JVar $genericMethodParameter,
                            boolean wrapInputTypeInJAXBElement,
                            com.sun.codemodel.JType inputType,
                            com.sun.codemodel.JType returnType,
                            com.sun.codemodel.JVar $resourceBuilder,
                            RepresentationNode inputRep,
                            com.sun.codemodel.JBlock $methodBody)
Generate a method body that uses a JAXBDispatcher, used when the payloads are XML.

Parameters:
method - the method to generate a body for.
isJAXB, - whether we are generating a generic of JAXB_MAPPING version.
exceptionMap - the generated exceptions that the method can raise.
outputRep - the output representation.
$genericMethodParameter - TODO.
wrapInputTypeInJAXBElement - If the JAX-B element is not @XmlRootElement we have to do more.
returnType - the type of the method return.
$resourceBuilder - TODO.
inputRep - the input representation.
$methodBody - a reference to the method body in which to generate code.

generateBeanProperty

public void generateBeanProperty(com.sun.codemodel.JDefinedClass $impl,
                                 List<Param> matrixParameters,
                                 Param p,
                                 boolean isAbstract)
Generate a bean setter and getter for a parameter.

Specified by:
generateBeanProperty in interface ResourceClassGenerator
Parameters:
$impl - The class or interface to add the bean setter and getter to.
p - the WADL parameter for which to create the setter and getter.
isAbstract - controls whether a method body is created false or not true. Set to true for interface methods, false for class methods.


Copyright © 2014. All Rights Reserved.