Search in sources :

Example 61 with AbstractJClass

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

the class AbstractPresentationModelObjectClassGen method defineTryToCreateFunction.

/*
	@Override
	public Function tryToCreateFunction(MethodDescriptor methodDescriptor) {
		if(methodDescriptor.equals(createMethodDescriptor(ON_CLICK))) {
			return new Function() {
				
				@Override
				public Object call(Object... args) {
					presentationModel.onClick();
					return null;
				}
			};
		}
		
		if(methodDescriptor.equals(createMethodDescriptor(ON_CLICK_WITH_EVENT, AbstractViewEvent.class))){
			return new Function() {
				
				@Override
				public Object call(Object... args) {
					boolean result = presentationModel.onLongClickWithEvent((AbstractViewEvent)args[0]);
					return (Boolean)result;
				}
			};
		}
		
		return null;
	}
	 */
public void defineTryToCreateFunction() {
    JMethod method = declarePublicMethodOverride("tryToCreateFunction", Function.class);
    JVar methodDescriptorParam = method.param(MethodDescriptor.class, "methodDescriptor");
    JBlock body = method.body();
    for (EventMethodInfo eventMethodInfo : presentationModelInfo.eventMethods()) {
        JInvocation getMethod = JExpr.invoke("createMethodDescriptor").arg(eventMethodInfo.name());
        if (eventMethodInfo.hasEventArg()) {
            getMethod.arg(codeModel.ref(eventMethodInfo.eventArgType()).dotclass());
        }
        JConditional conditional = body._if(methodDescriptorParam.invoke("equals").arg(getMethod));
        JBlock conditionalBody = conditional._then();
        //create Function.
        JDefinedClass anonymousFunction = codeModel.anonymousClass(Function.class);
        JMethod call = declarePublicMethodOverride(anonymousFunction, "call", Object.class);
        JVar argsVar = call.varParam(Object.class, "args");
        JBlock callBody = call.body();
        //call event method.
        JInvocation onEvent = presentationModelFieldWithoutThis.invoke(eventMethodInfo.name());
        if (eventMethodInfo.hasEventArg()) {
            AbstractJClass eventArgClass = codeModel.ref(eventMethodInfo.eventArgType());
            onEvent.arg(JExpr.cast(eventArgClass, argsVar.component(JExpr.lit(0))));
        }
        //call return.
        if (eventMethodInfo.hasReturn()) {
            JVar returnVar = callBody.decl(codeModel.ref(eventMethodInfo.nonPrimitiveReturnType()), "result", onEvent);
            callBody._return(returnVar);
        } else {
            callBody.add(onEvent);
            callBody._return(JExpr._null());
        }
        //return Function.
        conditionalBody._return(JExpr._new(anonymousFunction));
    }
    body._return(JExpr._null());
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) JConditional(com.helger.jcodemodel.JConditional) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 62 with AbstractJClass

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

the class APTCodeModelHelper method addParamToMethod.

private void addParamToMethod(JMethod method, VariableElement parameter, int mod, Map<String, TypeMirror> actualTypes, boolean varParam) {
    String parameterName = parameter.getSimpleName().toString();
    AbstractJClass parameterClass = typeMirrorToJClass(parameter.asType(), actualTypes);
    JVar param = varParam ? method.varParam(mod, parameterClass.elementType(), parameterName) : method.param(mod, parameterClass, parameterName);
    copyNonAAAnnotations(param, parameter.getAnnotationMirrors());
}
Also used : AbstractJClass(com.helger.jcodemodel.AbstractJClass) JVar(com.helger.jcodemodel.JVar)

Example 63 with AbstractJClass

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

the class APTCodeModelHelper method copyAnnotation.

public void copyAnnotation(IJAnnotatable annotatable, AnnotationMirror annotationMirror) {
    Map<? extends ExecutableElement, ? extends AnnotationValue> parameters = annotationMirror.getElementValues();
    if (!hasAnnotation(annotatable, annotationMirror)) {
        AbstractJClass annotation = typeMirrorToJClass(annotationMirror.getAnnotationType());
        JAnnotationUse annotate = annotatable.annotate(annotation);
        for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> param : parameters.entrySet()) {
            param.getValue().accept(new AnnotationParamExtractor(annotate, this), param.getKey().getSimpleName().toString());
        }
    }
}
Also used : JAnnotationUse(com.helger.jcodemodel.JAnnotationUse) AbstractJClass(com.helger.jcodemodel.AbstractJClass) AnnotationParamExtractor(org.androidannotations.internal.helper.AnnotationParamExtractor) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 64 with AbstractJClass

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

the class APTCodeModelHelper method overrideAnnotatedMethod.

public JMethod overrideAnnotatedMethod(ExecutableElement executableElement, GeneratedClassHolder holder) {
    TypeMirror annotatedClass = holder.getAnnotatedElement().asType();
    DeclaredType baseClass = (DeclaredType) executableElement.getEnclosingElement().asType();
    Types typeUtils = environment.getProcessingEnvironment().getTypeUtils();
    Map<String, TypeMirror> actualTypes = getActualTypes(typeUtils, baseClass, annotatedClass);
    Map<String, List<AbstractJClass>> methodTypes = new LinkedHashMap<>();
    for (TypeParameterElement typeParameter : executableElement.getTypeParameters()) {
        List<? extends TypeMirror> bounds = typeParameter.getBounds();
        List<AbstractJClass> addedBounds = typeBoundsToJClass(bounds, actualTypes);
        methodTypes.put(typeParameter.toString(), addedBounds);
    }
    actualTypes.keySet().removeAll(methodTypes.keySet());
    JMethod existingMethod = findAlreadyGeneratedMethod(executableElement, holder);
    if (existingMethod != null) {
        return existingMethod;
    }
    String methodName = executableElement.getSimpleName().toString();
    AbstractJClass returnType = typeMirrorToJClass(executableElement.getReturnType(), actualTypes);
    int modifier = elementVisibilityModifierToJMod(executableElement);
    JMethod method = holder.getGeneratedClass().method(modifier, returnType, methodName);
    copyNonAAAnnotations(method, executableElement.getAnnotationMirrors());
    if (!hasAnnotation(method, Override.class)) {
        method.annotate(Override.class);
    }
    for (Map.Entry<String, List<AbstractJClass>> typeDeclaration : methodTypes.entrySet()) {
        List<AbstractJClass> bounds = typeDeclaration.getValue();
        addTypeBounds(method, bounds, typeDeclaration.getKey());
    }
    int i = 0;
    for (VariableElement parameter : executableElement.getParameters()) {
        boolean varParam = i == executableElement.getParameters().size() - 1 && executableElement.isVarArgs();
        addParamToMethod(method, parameter, JMod.FINAL, actualTypes, varParam);
        i++;
    }
    for (TypeMirror superThrownType : executableElement.getThrownTypes()) {
        AbstractJClass thrownType = typeMirrorToJClass(superThrownType, actualTypes);
        method._throws(thrownType);
    }
    callSuperMethod(method, holder, method.body());
    return method;
}
Also used : Types(javax.lang.model.util.Types) AbstractJClass(com.helger.jcodemodel.AbstractJClass) VariableElement(javax.lang.model.element.VariableElement) LinkedHashMap(java.util.LinkedHashMap) TypeParameterElement(javax.lang.model.element.TypeParameterElement) TypeMirror(javax.lang.model.type.TypeMirror) List(java.util.List) ArrayList(java.util.ArrayList) JMethod(com.helger.jcodemodel.JMethod) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DeclaredType(javax.lang.model.type.DeclaredType)

Example 65 with AbstractJClass

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

the class APTCodeModelHelper method narrowGeneratedClass.

public AbstractJClass narrowGeneratedClass(AbstractJClass generatedClass, TypeMirror fromTypeArguments) {
    DeclaredType type = (DeclaredType) fromTypeArguments;
    for (TypeMirror param : type.getTypeArguments()) {
        AbstractJClass paramClass = typeMirrorToJClass(param);
        generatedClass = generatedClass.narrow(paramClass);
    }
    return generatedClass;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) AbstractJClass(com.helger.jcodemodel.AbstractJClass) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

AbstractJClass (com.helger.jcodemodel.AbstractJClass)125 JVar (com.helger.jcodemodel.JVar)48 JMethod (com.helger.jcodemodel.JMethod)36 JBlock (com.helger.jcodemodel.JBlock)35 JInvocation (com.helger.jcodemodel.JInvocation)31 TypeMirror (javax.lang.model.type.TypeMirror)24 IJExpression (com.helger.jcodemodel.IJExpression)23 JDefinedClass (com.helger.jcodemodel.JDefinedClass)19 VariableElement (javax.lang.model.element.VariableElement)14 JFieldVar (com.helger.jcodemodel.JFieldVar)13 GenerationProcess (com.github.sviperll.adt4j.model.util.GenerationProcess)9 ExecutableElement (javax.lang.model.element.ExecutableElement)9 JAnnotationUse (com.helger.jcodemodel.JAnnotationUse)8 JConditional (com.helger.jcodemodel.JConditional)8 JTypeVar (com.helger.jcodemodel.JTypeVar)8 DeclaredType (javax.lang.model.type.DeclaredType)8 VisitorDefinition (com.github.sviperll.adt4j.model.config.VisitorDefinition)6 JFieldRef (com.helger.jcodemodel.JFieldRef)6 ArrayList (java.util.ArrayList)6 BundleHelper (org.androidannotations.helper.BundleHelper)6