Search in sources :

Example 6 with AbstractJType

use of com.helger.jcodemodel.AbstractJType in project androidannotations by androidannotations.

the class RestAnnotationHelper method declareHttpEntity.

public IJExpression declareHttpEntity(JBlock body, JVar entitySentToServer, JVar httpHeaders) {
    AbstractJType entityType = getEnvironment().getJClass(Object.class);
    if (entitySentToServer != null) {
        entityType = entitySentToServer.type();
        if (entityType.isPrimitive()) {
            // Don't narrow primitive types...
            entityType = entityType.boxify();
        }
    }
    AbstractJClass httpEntity = getEnvironment().getJClass(HTTP_ENTITY);
    AbstractJClass narrowedHttpEntity = httpEntity.narrow(entityType);
    JInvocation newHttpEntityVarCall = JExpr._new(narrowedHttpEntity);
    if (entitySentToServer != null) {
        newHttpEntityVarCall.arg(entitySentToServer);
    }
    if (httpHeaders != null) {
        newHttpEntityVarCall.arg(httpHeaders);
    } else if (entitySentToServer == null) {
        return JExpr._null();
    }
    return body.decl(narrowedHttpEntity, "requestEntity", newHttpEntityVarCall);
}
Also used : AbstractJType(com.helger.jcodemodel.AbstractJType) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation)

Example 7 with AbstractJType

use of com.helger.jcodemodel.AbstractJType in project RoboBinding by RoboBinding.

the class ItemPresentationModelObjectClassGen method defineConstructor.

/**
	 *	public StringItemPresentationModel_IPM(StringItemPresentationModel itemPresentationModel) {
	 *		super(itemPresentationModel);
	 *		this.itemPresentationModel = itemPresentationModel;
	 *	}
	 * 
	 */
@Override
public void defineConstructor() {
    /*
		public class StringItemPresentationModel_IPM extends AbstractItemPresentationModelObject {
		 */
    definedClass._extends(AbstractItemPresentationModelObject.class);
    JMethod constructor = definedClass.constructor(JMod.PUBLIC);
    AbstractJType itemPresentationModelClass = codeModel.ref(presentationModelInfo.getPresentationModelTypeName());
    JVar itemPresentationModelParam = constructor.param(itemPresentationModelClass, "itemPresentationModel");
    JBlock block = constructor.body();
    JInvocation superInvocation = JExpr.invoke("super").arg(itemPresentationModelParam);
    block.add(superInvocation);
    block.assign(presentationModelField, itemPresentationModelParam);
}
Also used : AbstractJType(com.helger.jcodemodel.AbstractJType) JBlock(com.helger.jcodemodel.JBlock) JInvocation(com.helger.jcodemodel.JInvocation) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 8 with AbstractJType

use of com.helger.jcodemodel.AbstractJType in project adt4j by sviperll.

the class FinalValueClassModel method buildCaseClass.

private JDefinedClass buildCaseClass(String interfaceMethodName, Serialization serialization) throws JClassAlreadyExistsException {
    JDefinedClass caseClass = environment.buildValueClassInnerClass(JMod.PRIVATE | JMod.STATIC, Source.capitalize(interfaceMethodName) + "Case" + environment.acceptingInterfaceName(), EClassType.CLASS);
    for (JTypeVar visitorTypeParameter : environment.getValueTypeParameters()) {
        JTypeVar typeParameter = caseClass.generify(visitorTypeParameter.name());
        typeParameter.boundLike(visitorTypeParameter);
    }
    AbstractJClass usedAcceptingInterfaceType = environment.acceptingInterfaceType(caseClass.typeParams());
    AbstractJClass usedValueClassType = environment.wrappedValueClassType(caseClass.typeParams());
    VisitorDefinition.VisitorUsage usedVisitor = environment.visitor(usedValueClassType, usedValueClassType, types._RuntimeException);
    MethodUsage interfaceMethod = usedVisitor.findMethod(interfaceMethodName);
    if (interfaceMethod == null)
        throw new IllegalStateException("Method with given name not found: " + interfaceMethodName);
    JTypeVar[] methodArguments = new JTypeVar[interfaceMethod.typeParams().length];
    for (int i = 0; i < methodArguments.length; i++) {
        JTypeVar visitorMethodTypeParameter = interfaceMethod.typeParams()[i];
        JTypeVar typeParameter = caseClass.generify(visitorMethodTypeParameter.name());
        typeParameter.boundLike(visitorMethodTypeParameter);
        methodArguments[i] = typeParameter;
    }
    MethodUsage usedInterfaceMethod = interfaceMethod.narrow(methodArguments);
    caseClass._implements(usedAcceptingInterfaceType);
    if (serialization.isSerializable()) {
        caseClass._implements(types._Serializable);
        caseClass.field(JMod.PRIVATE | JMod.FINAL | JMod.STATIC, types._long, "serialVersionUID", JExpr.lit(serialization.serialVersionUIDForGeneratedCode()));
    }
    JMethod constructor = caseClass.constructor(JMod.NONE);
    for (VariableDeclaration param : usedInterfaceMethod.params()) {
        AbstractJType paramType = param.type().declarable();
        JFieldVar field = caseClass.field(JMod.PRIVATE | JMod.FINAL, paramType, param.name());
        JVar argument = constructor.param(paramType, param.name());
        constructor.body().assign(JExpr._this().ref(field), argument);
    }
    VariableDeclaration param = usedInterfaceMethod.varParam();
    if (param != null) {
        AbstractJType paramType = param.type().elementType().declarable();
        JFieldVar field = caseClass.field(JMod.PRIVATE | JMod.FINAL, paramType.array(), param.name());
        JVar argument = constructor.varParam(paramType, param.name());
        constructor.body().assign(JExpr._this().ref(field), argument);
    }
    JMethod acceptMethod = declareAcceptMethod(caseClass, usedValueClassType);
    JInvocation invocation = JExpr.invoke(acceptMethod.params().get(0), usedInterfaceMethod.name());
    for (AbstractJClass argument : methodArguments) {
        invocation.narrow(argument);
    }
    for (VariableDeclaration param1 : usedInterfaceMethod.params()) {
        invocation.arg(JExpr._this().ref(param1.name()));
    }
    VariableDeclaration param1 = usedInterfaceMethod.varParam();
    if (param1 != null) {
        invocation.arg(JExpr._this().ref(param1.name()));
    }
    acceptMethod.body()._return(invocation);
    return caseClass;
}
Also used : VisitorDefinition(com.github.sviperll.adt4j.model.config.VisitorDefinition) JDefinedClass(com.helger.jcodemodel.JDefinedClass) AbstractJType(com.helger.jcodemodel.AbstractJType) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) JTypeVar(com.helger.jcodemodel.JTypeVar) JFieldVar(com.helger.jcodemodel.JFieldVar) VariableDeclaration(com.github.sviperll.adt4j.model.config.VariableDeclaration) MethodUsage(com.github.sviperll.adt4j.model.config.VisitorDefinition.MethodUsage) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Aggregations

AbstractJType (com.helger.jcodemodel.AbstractJType)8 AbstractJClass (com.helger.jcodemodel.AbstractJClass)5 JInvocation (com.helger.jcodemodel.JInvocation)5 JMethod (com.helger.jcodemodel.JMethod)4 JVar (com.helger.jcodemodel.JVar)4 JBlock (com.helger.jcodemodel.JBlock)2 JTypeVar (com.helger.jcodemodel.JTypeVar)2 VariableDeclaration (com.github.sviperll.adt4j.model.config.VariableDeclaration)1 VisitorDefinition (com.github.sviperll.adt4j.model.config.VisitorDefinition)1 MethodUsage (com.github.sviperll.adt4j.model.config.VisitorDefinition.MethodUsage)1 GenerationProcess (com.github.sviperll.adt4j.model.util.GenerationProcess)1 IJExpression (com.helger.jcodemodel.IJExpression)1 JArray (com.helger.jcodemodel.JArray)1 JDefinedClass (com.helger.jcodemodel.JDefinedClass)1 JFieldVar (com.helger.jcodemodel.JFieldVar)1 JForEach (com.helger.jcodemodel.JForEach)1 TreeMap (java.util.TreeMap)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 VariableElement (javax.lang.model.element.VariableElement)1