Java tutorial
package tv.arte.resteventapi.core.presentation.decoration; /* * #%L * RestEventAPI * %% * Copyright (C) 2014 ARTE G.E.I.E * %% * This program is free software: you can redistribute it and/or modify * it under the terms of The MIT License (MIT) as published by the Open Source * Initiative. * * 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 * MIT License (MIT) for more details. * * You should have received a copy of The MIT License (MIT) * along with this program. If not, see <http://opensource.org/licenses/MIT> * #L% */ import static org.springframework.core.annotation.AnnotationUtils.findAnnotation; import static org.springframework.core.annotation.AnnotationUtils.getValue; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import org.springframework.hateoas.core.AnnotationMappingDiscoverer; import org.springframework.hateoas.core.MappingDiscoverer; import org.springframework.util.Assert; /* * Code particialy copied from string-hateoas project */ /** * Helper to discover an annotation a class * * @author Oliver Gierke * @author Kamill Sokol * @author Simeon Petev */ public class RestEventApiAnnotationMappingDiscoverer implements MappingDiscoverer { private final Class<? extends Annotation> annotationType; private final String mappingAttributeName; /** * Creates an {@link AnnotationMappingDiscoverer} for the given annotation type. Will lookup the {@code value} * attribute by default. * * @param annotation must not be {@literal null}. */ public RestEventApiAnnotationMappingDiscoverer(Class<? extends Annotation> annotation) { this(annotation, null); } /** * Creates an {@link AnnotationMappingDiscoverer} for the given annotation type and attribute name. * * @param annotation must not be {@literal null}. * @param mappingAttributeName if {@literal null}, it defaults to {@code value}. */ public RestEventApiAnnotationMappingDiscoverer(Class<? extends Annotation> annotation, String mappingAttributeName) { Assert.notNull(annotation); this.annotationType = annotation; this.mappingAttributeName = mappingAttributeName; } public String getMapping(Class<?> type) { Assert.notNull(type, "Type must not be null!"); String[] mapping = getMappingFrom(findAnnotation(type, annotationType)); return mapping.length == 0 ? null : mapping[0]; } public String getMapping(Method method) { Assert.notNull(method, "Method must not be null!"); return getMapping(method.getDeclaringClass(), method); } public String getMapping(Class<?> type, Method method) { Assert.notNull(type, "Type must not be null!"); Assert.notNull(method, "Method must not be null!"); String[] mapping = getMappingFrom(findAnnotation(method, annotationType)); String typeMapping = getMapping(type); if (mapping == null || mapping.length == 0) { return typeMapping; } return typeMapping == null || "/".equals(typeMapping) ? mapping[0] : typeMapping + mapping[0]; } private String[] getMappingFrom(Annotation annotation) { Object value = mappingAttributeName == null ? getValue(annotation) : getValue(annotation, mappingAttributeName); if (value instanceof String) { return new String[] { (String) value }; } else if (value instanceof String[]) { return (String[]) value; } else if (value == null) { return new String[0]; } throw new IllegalStateException( String.format("Unsupported type for the mapping attribute! Support String and String[] but got %s!", value.getClass())); } }