use of com.sun.codemodel.JCodeModel in project scout.rt by eclipse.
the class HandlerArtifactProcessor method createAndPersistHandlerDelegate.
/**
* Generates the entry point for a handler.
*/
public String createAndPersistHandlerDelegate(final JClass portTypeEntryPoint, final EntryPointDefinition entryPointDefinition, final HandlerDefinition handler, final int idx, final ProcessingEnvironment env) throws IOException, JClassAlreadyExistsException {
final JCodeModel model = new JCodeModel();
final String fullName = entryPointDefinition.getEntryPointQualifiedName() + "_" + handler.getHandlerSimpleName() + HANDLER_SUFFIX;
final JDefinedClass handlerDelegate = model._class(fullName);
// Add 'Generated' annotation
final JAnnotationUse generatedAnnotation = handlerDelegate.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", "Handler delegate for " + handler.getHandlerQualifiedName());
AptUtil.addJavaDoc(handlerDelegate, format("This class is auto-generated by APT triggered by Maven build and is based on the handler configuration declared in {@link %s}.", entryPointDefinition.getSimpleName()));
switch(handler.getHandlerType()) {
case SOAP:
handlerDelegate._extends(model.ref(SOAPHandlerDelegate.class));
break;
case LOGICAL:
handlerDelegate._extends(model.ref(HandlerDelegate.class).narrow(LogicalMessageContext.class));
handlerDelegate._implements(model.ref(LogicalHandler.class).narrow(LogicalMessageContext.class));
break;
default:
handlerDelegate._extends(model.ref(HandlerDelegate.class).narrow(MessageContext.class));
break;
}
// Add default constructor with super call to provide handler annotation.
final JClass entryPointDefinitionClass = model.ref(entryPointDefinition.getQualifiedName());
final JMethod defaultConstructor = handlerDelegate.constructor(JMod.PUBLIC);
defaultConstructor.body().invoke("super").arg(JExpr.dotclass(entryPointDefinitionClass).invoke("getAnnotation").arg(JExpr.dotclass(model.ref(WebServiceEntryPoint.class))).invoke("handlerChain").component(JExpr.lit(idx)));
AptUtil.buildAndPersist(model, env.getFiler());
return handlerDelegate.fullName();
}
use of com.sun.codemodel.JCodeModel in project drill by apache.
the class HiveFuncHolder method generateEval.
private HoldingContainer generateEval(ClassGenerator<?> g, HoldingContainer[] inputVariables, JVar[] workspaceJVars) {
HoldingContainer out = g.declare(returnType);
JCodeModel m = g.getModel();
JBlock sub = new JBlock(true, true);
// initialize DeferredObject's. For an optional type, assign the value holder only if it is not null
for (int i = 0; i < argTypes.length; i++) {
if (inputVariables[i].isOptional()) {
sub.assign(workspaceJVars[3].component(JExpr.lit(i)), workspaceJVars[2].component(JExpr.lit(i)));
JBlock conditionalBlock = new JBlock(false, false);
JConditional jc = conditionalBlock._if(inputVariables[i].getIsSet().ne(JExpr.lit(0)));
jc._then().assign(JExpr.ref(workspaceJVars[3].component(JExpr.lit(i)), "valueHolder"), inputVariables[i].getHolder());
jc._else().assign(JExpr.ref(workspaceJVars[3].component(JExpr.lit(i)), "valueHolder"), JExpr._null());
sub.add(conditionalBlock);
} else {
sub.assign(workspaceJVars[3].component(JExpr.lit(i)), workspaceJVars[2].component(JExpr.lit(i)));
sub.assign(JExpr.ref(workspaceJVars[3].component(JExpr.lit(i)), "valueHolder"), inputVariables[i].getHolder());
}
}
// declare generic object for storing return value from GenericUDF.evaluate
JVar retVal = sub.decl(m._ref(Object.class), "ret");
// create try..catch block to call the GenericUDF instance with given input
JTryBlock udfEvalTry = sub._try();
udfEvalTry.body().assign(retVal, workspaceJVars[1].invoke("evaluate").arg(workspaceJVars[3]));
JCatchBlock udfEvalCatch = udfEvalTry._catch(m.directClass(Exception.class.getCanonicalName()));
JVar exVar = udfEvalCatch.param("ex");
udfEvalCatch.body()._throw(JExpr._new(m.directClass(RuntimeException.class.getCanonicalName())).arg(JExpr.lit(String.format("GenericUDF.evaluate method failed"))).arg(exVar));
// get the ValueHolder from retVal and return ObjectInspector
sub.add(ObjectInspectorHelper.getDrillObject(m, returnOI, workspaceJVars[0], workspaceJVars[4], retVal));
sub.assign(out.getHolder(), workspaceJVars[4]);
// now add it to the doEval block in Generated class
JBlock setup = g.getBlock(ClassGenerator.BlockType.EVAL);
setup.directStatement(String.format("/** start %s for function %s **/ ", ClassGenerator.BlockType.EVAL.name(), genericUdfClazz.getName() + (!isGenericUDF ? "(" + udfName + ")" : "")));
setup.add(sub);
setup.directStatement(String.format("/** end %s for function %s **/ ", ClassGenerator.BlockType.EVAL.name(), genericUdfClazz.getName() + (!isGenericUDF ? "(" + udfName + ")" : "")));
return out;
}
Aggregations