1 /* 2 * Copyright (c) 2010 Carman Consulting, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.metastopheles.annotation; 18 19 import org.metastopheles.MetaDataDecorator; 20 import org.metastopheles.MetaDataObject; 21 22 import java.lang.annotation.Annotation; 23 24 /** 25 * An annotation-based metadata decorator. Subclasses merely need to override the #{decorate} method. 26 * @since 1.0 27 * @author James Carman 28 */ 29 public abstract class AnnotationBasedMetaDataDecorator<T extends MetaDataObject, A extends Annotation> implements MetaDataDecorator<T> 30 { 31 //********************************************************************************************************************** 32 // Fields 33 //********************************************************************************************************************** 34 35 private final Class<A> annotationType; 36 37 //********************************************************************************************************************** 38 // Constructors 39 //********************************************************************************************************************** 40 41 public AnnotationBasedMetaDataDecorator(Class<A> annotationType) 42 { 43 this.annotationType = annotationType; 44 } 45 46 //********************************************************************************************************************** 47 // Abstract Methods 48 //********************************************************************************************************************** 49 50 protected abstract void decorate(T metaData, Annotation annotation); 51 52 //********************************************************************************************************************** 53 // MetaDataDecorator Implementation 54 //********************************************************************************************************************** 55 56 public void decorate(T metaData) 57 { 58 A annotation = metaData.getAnnotation(annotationType); 59 if (annotation != null) 60 { 61 decorate(metaData, annotation); 62 } 63 } 64 }