protected String toStringExpression(JType type, String expr) { if (type.isPrimitive() != null) { return "\"\"+" + expr; } if (STRING_TYPE == type) { return expr; } if (type.isClass() != null && isOverlayArrayType(type.isClass())) { return "(new " + JSON_ARRAY_CLASS + "(" + expr + ")).toString()"; } if (type.isClass() != null && OVERLAY_VALUE_TYPE.isAssignableFrom(type.isClass())) { return "(new " + JSON_OBJECT_CLASS + "(" + expr + ")).toString()"; } return String.format("(%s != null ? %s.toString() : null)", expr, expr); }
protected String toFormStringExpression(JParameter argument, Style classStyle) throws UnableToCompleteException { JType type = argument.getType(); String expr = argument.getName(); if (type.isPrimitive() != null) { return "\"\"+" + expr; } if (STRING_TYPE == type) { return expr; } if (type.isClass() != null && isOverlayArrayType(type.isClass())) { return "(new " + JSON_ARRAY_CLASS + "(" + expr + ")).toString()"; } if (type.isClass() != null && OVERLAY_VALUE_TYPE.isAssignableFrom(type.isClass())) { return "(new " + JSON_OBJECT_CLASS + "(" + expr + ")).toString()"; } if (type.getQualifiedBinaryName().startsWith("java.lang.")) { return String.format("(%s != null ? %s.toString() : null)", expr, expr); } Json jsonAnnotation = argument.getAnnotation(Json.class); final Style style = jsonAnnotation != null ? jsonAnnotation.style() : classStyle; return locator.encodeExpression(type, expr, style) + ".toString()"; }
private void buildMethodInterceptor( JDefinedClass definedClass, ConstructorInjectionPoint proxyConstructorInjectionPoint, JMethod constructor, JBlock constructorBody, Map<ASTMethod, Map<InjectionNode, JFieldVar>> interceptorFields, Map.Entry<ASTMethod, Set<InjectionNode>> methodInterceptorEntry) throws ClassNotFoundException { ASTMethod method = methodInterceptorEntry.getKey(); if (method.getAccessModifier().equals(ASTAccessModifier.PRIVATE)) { throw new TransfuseAnalysisException("Unable to provide AOP on private methods"); } if (!interceptorFields.containsKey(methodInterceptorEntry.getKey())) { interceptorFields.put( methodInterceptorEntry.getKey(), new HashMap<InjectionNode, JFieldVar>()); } Map<InjectionNode, JFieldVar> injectionNodeInstanceNameMap = interceptorFields.get(methodInterceptorEntry.getKey()); // setup interceptor fields for (InjectionNode interceptorInjectionNode : methodInterceptorEntry.getValue()) { String interceptorInstanceName = namer.generateName(interceptorInjectionNode); JFieldVar interceptorField = definedClass.field( JMod.PRIVATE, codeModel.ref(interceptorInjectionNode.getClassName()), interceptorInstanceName); injectionNodeInstanceNameMap.put(interceptorInjectionNode, interceptorField); JVar interceptorParam = constructor.param( codeModel.ref(interceptorInjectionNode.getClassName()), namer.generateName(interceptorInjectionNode)); constructorBody.assign(interceptorField, interceptorParam); proxyConstructorInjectionPoint.addInjectionNode(interceptorInjectionNode); } JType returnType = codeModel.parseType(method.getReturnType().getName()); JMethod methodDeclaration = definedClass.method( method.getAccessModifier().getCodeModelJMod(), returnType, method.getName()); JBlock body = methodDeclaration.body(); // define method parameter Map<ASTParameter, JVar> parameterMap = new HashMap<ASTParameter, JVar>(); for (ASTParameter parameter : method.getParameters()) { parameterMap.put( parameter, methodDeclaration.param( JMod.FINAL, codeModel.ref(parameter.getASTType().getName()), namer.generateName(parameter.getASTType()))); } // aop interceptor Map<InjectionNode, JFieldVar> interceptorNameMap = interceptorFields.get(methodInterceptorEntry.getKey()); JArray paramArray = JExpr.newArray(codeModel.ref(Object.class)); for (ASTParameter astParameter : method.getParameters()) { paramArray.add(parameterMap.get(astParameter)); } JInvocation interceptorInvocation = buildInterceptorChain( definedClass, method, parameterMap, methodInterceptorEntry.getValue(), interceptorNameMap) .invoke("invoke"); interceptorInvocation.arg(paramArray); if (method.getReturnType().equals(ASTVoidType.VOID)) { body.add(interceptorInvocation); } else { body._return(JExpr.cast(returnType.boxify(), interceptorInvocation)); } }