Search in sources :

Example 1 with AbstractJType

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

the class APTCodeModelHelper method implementMethod.

public JMethod implementMethod(GeneratedClassHolder holder, List<ExecutableElement> methods, String methodName, String returnType, boolean finalParams, String... parameterTypes) {
    // First get the ExecutableElement method object from the util function.
    ExecutableElement method = getMethod(methods, methodName, returnType, parameterTypes);
    JMethod jmethod = null;
    if (method != null) {
        // Get the return type or VOID if none.
        AbstractJType jcReturnType = returnType.equals(TypeKind.VOID.toString()) ? environment.getCodeModel().VOID : environment.getJClass(returnType);
        // Create the implementation and annotate it with the Override
        // annotation.
        jmethod = holder.getGeneratedClass().method(JMod.PUBLIC, jcReturnType, method.getSimpleName().toString());
        jmethod.annotate(Override.class);
        // Create the parameters.
        int paramMods = finalParams ? JMod.FINAL : JMod.NONE;
        for (int i = 0; i < method.getParameters().size(); i++) {
            VariableElement param = method.getParameters().get(i);
            jmethod.param(paramMods, environment.getJClass(parameterTypes[i]), param.getSimpleName().toString());
        }
    }
    return jmethod;
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) AbstractJType(com.helger.jcodemodel.AbstractJType) VariableElement(javax.lang.model.element.VariableElement) JMethod(com.helger.jcodemodel.JMethod)

Example 2 with AbstractJType

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

the class EActivityHolder method setSetContentView.

private void setSetContentView() {
    getOnCreate();
    AbstractJClass layoutParamsClass = getClasses().VIEW_GROUP_LAYOUT_PARAMS;
    setContentViewLayout = setContentViewMethod(new AbstractJType[] { getCodeModel().INT }, new String[] { "layoutResID" });
    setContentViewMethod(new AbstractJType[] { getClasses().VIEW, layoutParamsClass }, new String[] { "view", "params" });
    setContentViewMethod(new AbstractJType[] { getClasses().VIEW }, new String[] { "view" });
}
Also used : AbstractJType(com.helger.jcodemodel.AbstractJType) AbstractJClass(com.helger.jcodemodel.AbstractJClass)

Example 3 with AbstractJType

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

the class VisitorDefinition method createMethodMap.

private static GenerationResult<Map<String, JMethod>> createMethodMap(JDefinedClass jVisitorModel, SpecialTypeVariables specialTypeVariables) {
    GenerationProcess generation = new GenerationProcess();
    Map<String, JMethod> methods = new TreeMap<>();
    for (JMethod method : jVisitorModel.methods()) {
        AbstractJType methodType = method.type();
        if (methodType == null)
            generation.reportError(MessageFormat.format("Visitor method result type is missing: {0}", method.name()));
        else if (methodType.isError()) {
            generation.reportError(MessageFormat.format("Visitor method result type is erroneous: {0}", method.name()));
        } else if (!specialTypeVariables.isResult(method.type())) {
            generation.reportError(MessageFormat.format("Visitor method is only allowed to return type declared as a result type of visitor: {0}: expecting {1}, found: {2}", method.name(), specialTypeVariables.resultTypeParameter().name(), methodType.fullName()));
        }
        for (JTypeVar typeVariable : method.typeParamList()) {
            for (AbstractJClass bound : typeVariable.bounds()) {
                if (bound.containsTypeVar(specialTypeVariables.resultTypeParameter())) {
                    generation.reportError(MessageFormat.format("Visitor method type-parameters shouldn''t depend on result type: {0}: {1} type-variable", method.name(), typeVariable.name()));
                }
            }
        }
        for (JVar parameter : method.listParams()) {
            if (parameter.type().containsTypeVar(specialTypeVariables.resultTypeParameter())) {
                generation.reportError(MessageFormat.format("Visitor method shouldn''t have result type as a parameter: {0}: result type-parameter: {1}", method.name(), specialTypeVariables.resultTypeParameter().name()));
            }
        }
        Collection<AbstractJClass> exceptions = method.getThrows();
        if (exceptions.size() > 1)
            generation.reportError(MessageFormat.format("Visitor method is allowed to throw no exceptions or throw single exception, declared as type-variable: {0}", method.name()));
        else if (exceptions.size() == 1) {
            AbstractJClass exception = exceptions.iterator().next();
            if (exception.isError())
                generation.reportError(MessageFormat.format("Visitor method exception type is erroneous: {0}", method.name()));
            else if (!specialTypeVariables.isException(exception))
                generation.reportError(MessageFormat.format("Visitor method throws exception, not declared as type-variable: {0}: {1}", method.name(), exception.fullName()));
        }
        JMethod exitingValue = methods.put(method.name(), method);
        if (exitingValue != null) {
            generation.reportError(MessageFormat.format("Method overloading is not supported for visitor interfaces: two methods with the same name: {0}", method.name()));
        }
        for (JVar param : method.params()) {
            generation.processGenerationResult(Source.getNullability(param));
        }
    }
    return generation.createGenerationResult(methods);
}
Also used : JTypeVar(com.helger.jcodemodel.JTypeVar) AbstractJType(com.helger.jcodemodel.AbstractJType) AbstractJClass(com.helger.jcodemodel.AbstractJClass) GenerationProcess(com.github.sviperll.adt4j.model.util.GenerationProcess) TreeMap(java.util.TreeMap) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 4 with AbstractJType

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

the class APTCodeModelHelper method callSuperMethod.

public void callSuperMethod(JMethod superMethod, GeneratedClassHolder holder, JBlock callBlock) {
    JInvocation superCall = getSuperCall(holder, superMethod);
    AbstractJType returnType = superMethod.type();
    if (returnType.fullName().equals("void")) {
        callBlock.add(superCall);
    } else {
        callBlock._return(superCall);
    }
}
Also used : AbstractJType(com.helger.jcodemodel.AbstractJType) JInvocation(com.helger.jcodemodel.JInvocation)

Example 5 with AbstractJType

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

the class RestMethodHandler method setCookies.

private IJExpression setCookies(ExecutableElement executableElement, RestHolder restHolder, JBlock methodBody, JInvocation exchangeCall) {
    String[] settingCookies = restAnnotationHelper.settingCookies(executableElement);
    if (settingCookies != null) {
        boolean methodReturnVoid = executableElement.getReturnType().getKind() == TypeKind.VOID;
        AbstractJClass exchangeResponseClass = restAnnotationHelper.retrieveResponseClass(executableElement.getReturnType(), restHolder);
        AbstractJType narrowType = exchangeResponseClass == null || methodReturnVoid ? getCodeModel().VOID : exchangeResponseClass;
        AbstractJClass responseEntityClass = getJClass(RESPONSE_ENTITY).narrow(narrowType);
        JVar responseEntity = methodBody.decl(responseEntityClass, "response", exchangeCall);
        // set cookies
        AbstractJClass stringListClass = getClasses().LIST.narrow(getClasses().STRING);
        AbstractJClass stringArrayClass = getClasses().STRING.array();
        JArray cookiesArray = JExpr.newArray(getClasses().STRING);
        for (String cookie : settingCookies) {
            cookiesArray.add(JExpr.lit(cookie));
        }
        JVar requestedCookiesVar = methodBody.decl(stringArrayClass, "requestedCookies", cookiesArray);
        JInvocation setCookiesList = JExpr.invoke(responseEntity, "getHeaders").invoke("get").arg("Set-Cookie");
        JVar allCookiesList = methodBody.decl(stringListClass, "allCookies", setCookiesList);
        // for loop over list... add if in string array
        JForEach forEach = //
        methodBody._if(allCookiesList.ne(JExpr._null()))._then().forEach(getClasses().STRING, "rawCookie", allCookiesList);
        JVar rawCookieVar = forEach.var();
        JBlock forLoopBody = forEach.body();
        JForEach innerForEach = forLoopBody.forEach(getClasses().STRING, "thisCookieName", requestedCookiesVar);
        JBlock innerBody = innerForEach.body();
        JBlock thenBlock = innerBody._if(JExpr.invoke(rawCookieVar, "startsWith").arg(innerForEach.var().plus("=")))._then();
        // where does the cookie VALUE end?
        JInvocation valueEnd = rawCookieVar.invoke("indexOf").arg(JExpr.lit(';'));
        JVar valueEndVar = thenBlock.decl(getCodeModel().INT, "valueEnd", valueEnd);
        JBlock fixValueEndBlock = thenBlock._if(valueEndVar.eq(JExpr.lit(-1)))._then();
        fixValueEndBlock.assign(valueEndVar, rawCookieVar.invoke("length"));
        IJExpression indexOfValue = rawCookieVar.invoke("indexOf").arg("=").plus(JExpr.lit(1));
        JInvocation cookieValue = rawCookieVar.invoke("substring").arg(indexOfValue).arg(valueEndVar);
        thenBlock.invoke(restHolder.getAvailableCookiesField(), "put").arg(innerForEach.var()).arg(cookieValue);
        thenBlock._break();
        return responseEntity;
    }
    return exchangeCall;
}
Also used : AbstractJType(com.helger.jcodemodel.AbstractJType) JBlock(com.helger.jcodemodel.JBlock) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) JForEach(com.helger.jcodemodel.JForEach) JVar(com.helger.jcodemodel.JVar) JArray(com.helger.jcodemodel.JArray)

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