Search in sources :

Example 1 with JAnnotationUse

use of com.sun.codemodel.JAnnotationUse in project scout.rt by eclipse.

the class HandlerArtifactProcessor method createAndPersistAuthHandler.

/**
 * Generates the AuthHandler.
 */
public String createAndPersistAuthHandler(final JClass portTypeEntryPoint, final EntryPointDefinition entryPointDefinition, final ProcessingEnvironment processingEnv) throws IOException, JClassAlreadyExistsException {
    final JCodeModel model = new JCodeModel();
    final String fullName = entryPointDefinition.getEntryPointQualifiedName() + "_" + AUTH_HANDLER_NAME;
    final JDefinedClass authHandler = model._class(fullName)._extends(model.ref(AuthenticationHandler.class));
    // Add 'Generated' annotation
    final JAnnotationUse generatedAnnotation = authHandler.annotate(Generated.class);
    generatedAnnotation.param("value", JaxWsAnnotationProcessor.class.getName());
    generatedAnnotation.param("date", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSSZ").format(new Date()));
    generatedAnnotation.param("comments", format("Authentication Handler for [method=%s, verifier=%s]", AptUtil.toSimpleName(entryPointDefinition.getAuthMethod()), AptUtil.toSimpleName(entryPointDefinition.getAuthVerifier())));
    // Add default constructor with super call to provide authentication annotation.
    final JMethod defaultConstructor = authHandler.constructor(JMod.PUBLIC);
    final JClass entryPointDefinitionClass = model.ref(entryPointDefinition.getQualifiedName());
    defaultConstructor.body().invoke("super").arg(JExpr.dotclass(entryPointDefinitionClass).invoke("getAnnotation").arg(JExpr.dotclass(model.ref(WebServiceEntryPoint.class))).invoke("authentication"));
    AptUtil.addJavaDoc(authHandler, format("This class is auto-generated by APT triggered by Maven build and is based on the authentication configuration declared in {@link %s}.", entryPointDefinition.getSimpleName()));
    AptUtil.buildAndPersist(model, processingEnv.getFiler());
    return authHandler.fullName();
}
Also used : JaxWsAnnotationProcessor(org.eclipse.scout.jaxws.apt.JaxWsAnnotationProcessor) JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass) JAnnotationUse(com.sun.codemodel.JAnnotationUse) AuthenticationHandler(org.eclipse.scout.rt.server.jaxws.provider.auth.handler.AuthenticationHandler) JMethod(com.sun.codemodel.JMethod) JCodeModel(com.sun.codemodel.JCodeModel) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 2 with JAnnotationUse

use of com.sun.codemodel.JAnnotationUse in project jsonschema2pojo by joelittlejohn.

the class BuilderRule method generateNoArgsBaseBuilderConstructor.

private void generateNoArgsBaseBuilderConstructor(JDefinedClass instanceClass, JDefinedClass builderClass, JDefinedClass concreteBuilderClass) {
    JMethod noArgsConstructor = builderClass.constructor(JMod.PUBLIC);
    JAnnotationUse warningSuppression = noArgsConstructor.annotate(SuppressWarnings.class);
    warningSuppression.param("value", "unchecked");
    JBlock constructorBlock = noArgsConstructor.body();
    JFieldVar instanceField = reflectionHelper.searchClassAndSuperClassesForField("instance", builderClass);
    // Determine if we need to invoke the super() method for our parent builder
    JClass parentClass = builderClass._extends();
    if (!(parentClass.isPrimitive() || reflectionHelper.isFinal(parentClass) || Objects.equals(parentClass.fullName(), "java.lang.Object"))) {
        constructorBlock.invoke("super");
    }
    // Only initialize the instance if the object being constructed is actually this class
    // if it's a subtype then ignore the instance initialization since the subclass will initialize it
    constructorBlock.directStatement("// Skip initialization when called from subclass");
    JInvocation comparison = JExpr._this().invoke("getClass").invoke("equals").arg(JExpr.dotclass(concreteBuilderClass));
    JConditional ifNotSubclass = constructorBlock._if(comparison);
    ifNotSubclass._then().assign(JExpr._this().ref(instanceField), JExpr.cast(instanceField.type(), JExpr._new(instanceClass)));
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) JClass(com.sun.codemodel.JClass) JAnnotationUse(com.sun.codemodel.JAnnotationUse) JBlock(com.sun.codemodel.JBlock) JInvocation(com.sun.codemodel.JInvocation) JConditional(com.sun.codemodel.JConditional) JMethod(com.sun.codemodel.JMethod)

Example 3 with JAnnotationUse

use of com.sun.codemodel.JAnnotationUse in project jsonschema2pojo by joelittlejohn.

the class MinimumMaximumRule method apply.

@Override
public JFieldVar apply(String nodeName, JsonNode node, JsonNode parent, JFieldVar field, Schema currentSchema) {
    if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations() && isApplicableType(field)) {
        if (node.has("minimum")) {
            final Class<? extends Annotation> decimalMinClass = ruleFactory.getGenerationConfig().isUseJakartaValidation() ? DecimalMin.class : javax.validation.constraints.DecimalMin.class;
            JAnnotationUse annotation = field.annotate(decimalMinClass);
            annotation.param("value", node.get("minimum").asText());
        }
        if (node.has("maximum")) {
            final Class<? extends Annotation> decimalMaxClass = ruleFactory.getGenerationConfig().isUseJakartaValidation() ? DecimalMax.class : javax.validation.constraints.DecimalMax.class;
            JAnnotationUse annotation = field.annotate(decimalMaxClass);
            annotation.param("value", node.get("maximum").asText());
        }
    }
    return field;
}
Also used : JAnnotationUse(com.sun.codemodel.JAnnotationUse)

Example 4 with JAnnotationUse

use of com.sun.codemodel.JAnnotationUse in project jsonschema2pojo by joelittlejohn.

the class DigitsRule method apply.

@Override
public JFieldVar apply(String nodeName, JsonNode node, JsonNode parent, JFieldVar field, Schema currentSchema) {
    if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations() && node.has("integerDigits") && node.has("fractionalDigits") && isApplicableType(field)) {
        final Class<? extends Annotation> digitsClass = ruleFactory.getGenerationConfig().isUseJakartaValidation() ? Digits.class : javax.validation.constraints.Digits.class;
        JAnnotationUse annotation = field.annotate(digitsClass);
        annotation.param("integer", node.get("integerDigits").asInt());
        annotation.param("fraction", node.get("fractionalDigits").asInt());
    }
    return field;
}
Also used : JAnnotationUse(com.sun.codemodel.JAnnotationUse)

Example 5 with JAnnotationUse

use of com.sun.codemodel.JAnnotationUse in project jsonschema2pojo by joelittlejohn.

the class MinLengthMaxLengthRule method apply.

@Override
public JFieldVar apply(String nodeName, JsonNode node, JsonNode parent, JFieldVar field, Schema currentSchema) {
    if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations() && (node.has("minLength") || node.has("maxLength")) && isApplicableType(field)) {
        final Class<? extends Annotation> sizeClass = ruleFactory.getGenerationConfig().isUseJakartaValidation() ? Size.class : javax.validation.constraints.Size.class;
        JAnnotationUse annotation = field.annotate(sizeClass);
        if (node.has("minLength")) {
            annotation.param("min", node.get("minLength").asInt());
        }
        if (node.has("maxLength")) {
            annotation.param("max", node.get("maxLength").asInt());
        }
    }
    return field;
}
Also used : JAnnotationUse(com.sun.codemodel.JAnnotationUse)

Aggregations

JAnnotationUse (com.sun.codemodel.JAnnotationUse)16 JClass (com.sun.codemodel.JClass)4 JCodeModel (com.sun.codemodel.JCodeModel)3 JDefinedClass (com.sun.codemodel.JDefinedClass)3 JMethod (com.sun.codemodel.JMethod)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 JaxWsAnnotationProcessor (org.eclipse.scout.jaxws.apt.JaxWsAnnotationProcessor)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 JAnnotationArrayMember (com.sun.codemodel.JAnnotationArrayMember)1 JBlock (com.sun.codemodel.JBlock)1 JConditional (com.sun.codemodel.JConditional)1 JFieldVar (com.sun.codemodel.JFieldVar)1 JInvocation (com.sun.codemodel.JInvocation)1 WebService (javax.jws.WebService)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 AnnotationValue (javax.lang.model.element.AnnotationValue)1 WebServiceClient (javax.xml.ws.WebServiceClient)1 LogicalMessageContext (javax.xml.ws.handler.LogicalMessageContext)1