1 /* 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.xml.internal.bind.v2.model.annotation; 27 28 import java.lang.annotation.Annotation; 29 30 import com.sun.xml.internal.bind.v2.model.core.ErrorHandler; 31 import com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationException; 32 33 /** 34 * {@link AnnotationReader} that reads annotation from classes, 35 * not from external binding files. 36 * 37 * This is meant to be used as a convenient partial implementation. 38 * 39 * @author Kohsuke Kawaguchi (kk@kohsuke.org) 40 */ 41 public abstract class AbstractInlineAnnotationReaderImpl<T,C,F,M> 42 implements AnnotationReader<T,C,F,M> { 43 44 private ErrorHandler errorHandler; 45 46 public void setErrorHandler(ErrorHandler errorHandler) { 47 if(errorHandler==null) 48 throw new IllegalArgumentException(); 49 this.errorHandler = errorHandler; 50 } 51 52 /** 53 * Always return a non-null valid {@link ErrorHandler} 54 */ 55 public final ErrorHandler getErrorHandler() { 56 assert errorHandler!=null : "error handler must be set before use"; 57 return errorHandler; 58 } 59 60 public final <A extends Annotation> A getMethodAnnotation(Class<A> annotation, M getter, M setter, Locatable srcPos) { 61 A a1 = getter==null?null:getMethodAnnotation(annotation,getter,srcPos); 62 A a2 = setter==null?null:getMethodAnnotation(annotation,setter,srcPos); 63 64 if(a1==null) { 65 if(a2==null) 66 return null; 67 else 68 return a2; 69 } else { 70 if(a2==null) 71 return a1; 72 else { 73 // both are present 74 getErrorHandler().error(new IllegalAnnotationException( 75 Messages.DUPLICATE_ANNOTATIONS.format( 76 annotation.getName(), fullName(getter),fullName(setter)), 77 a1, a2 )); 78 // recover by ignoring one of them 79 return a1; 80 } 81 } 82 } 83 84 public boolean hasMethodAnnotation(Class<? extends Annotation> annotation, String propertyName, M getter, M setter, Locatable srcPos) { 85 boolean x = ( getter != null && hasMethodAnnotation(annotation, getter) ); 86 boolean y = ( setter != null && hasMethodAnnotation(annotation, setter) ); 87 88 if(x && y) { 89 // both are present. have getMethodAnnotation report an error 90 getMethodAnnotation(annotation,getter,setter,srcPos); 91 } 92 93 return x||y; 94 } 95 96 /** 97 * Gets the fully-qualified name of the method. 98 * 99 * Used for error messages. 100 */ 101 protected abstract String fullName(M m); 102 }