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;
}
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" });
}
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);
}
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);
}
}
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;
}
Aggregations