Java tutorial
package br.com.d4n.ui4entity.mvc; /* * #%L * ui4entity * %% * Copyright (C) 2012 - 2015 DevFor Ninjas * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * #L% */ import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.core.MethodParameter; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.method.support.HandlerMethodReturnValueHandler; import org.springframework.web.method.support.ModelAndViewContainer; import br.com.d4n.ui4entity.ValueOptions; import br.com.d4n.ui4entity.core.ValueOptionsProviderResolver; public class ValueOptionReturnValueHandler implements HandlerMethodReturnValueHandler { private final HandlerMethodReturnValueHandler delegate; public ValueOptionReturnValueHandler(HandlerMethodReturnValueHandler delegate) { this.delegate = delegate; } @Override public boolean supportsReturnType(MethodParameter returnType) { return delegate.supportsReturnType(returnType); } @SuppressWarnings("rawtypes") @Override public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { if (returnType.getMethodAnnotation(ValueOptions.class) != null) { Map<String, Object> map = new HashMap<String, Object>(); if (returnValue instanceof List) { map.put("type", "list"); } else if (returnValue instanceof Map) { returnValue = ((Map) returnValue).entrySet(); map.put("type", "map"); } else throw new ClassCastException("@ValueOptions method must be return Map or List"); map.put(ValueOptionsProviderResolver.OPTIONS_ATTR_NAME, returnValue); returnValue = map; returnType = new UI4EntityMethodParameter(returnType, returnValue.getClass()); } delegate.handleReturnValue(returnValue, returnType, mavContainer, webRequest); } }