1 /* 2 * Copyright (C) 2007-2010 Jlio Vilmar Gesser. 3 * Copyright (C) 2011, 2013-2015 The JavaParser Team. 4 * 5 * This file is part of JavaParser. 6 * 7 * JavaParser can be used either under the terms of 8 * a) the GNU Lesser General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * b) the terms of the Apache License 12 * 13 * You should have received a copy of both licenses in LICENCE.LGPL and 14 * LICENCE.APACHE. Please refer to those files for details. 15 * 16 * JavaParser is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU Lesser General Public License for more details. 20 */ 21 22 package com.github.javaparser.ast.nodeTypes; 23 24 import java.lang.annotation.Annotation; 25 import java.util.List; 26 27 import com.github.javaparser.ast.Node; 28 import com.github.javaparser.ast.expr.AnnotationExpr; 29 import com.github.javaparser.ast.expr.MarkerAnnotationExpr; 30 import com.github.javaparser.ast.expr.NameExpr; 31 import com.github.javaparser.ast.expr.NormalAnnotationExpr; 32 import com.github.javaparser.ast.expr.SingleMemberAnnotationExpr; 33 34 import static com.github.javaparser.ast.expr.NameExpr.*; 35 36 /** 37 * An element which can be the target of annotations. 38 * 39 * @author Federico Tomassetti 40 * @since July 2014 41 */ 42 public interface NodeWithAnnotations<T> { 43 List<AnnotationExpr> getAnnotations(); 44 45 T setAnnotations(List<AnnotationExpr> annotations); 46 47 /** 48 * Annotates this 49 * 50 * @param name the name of the annotation 51 * @return the {@link NormalAnnotationExpr} added 52 */ 53 public default NormalAnnotationExpr addAnnotation(String name) { 54 NormalAnnotationExpr normalAnnotationExpr = new NormalAnnotationExpr( 55 name(name), null); 56 getAnnotations().add(normalAnnotationExpr); 57 normalAnnotationExpr.setParentNode((Node) this); 58 return normalAnnotationExpr; 59 } 60 61 /** 62 * Annotates this and automatically add the import 63 * 64 * @param clazz the class of the annotation 65 * @return the {@link NormalAnnotationExpr} added 66 */ 67 public default NormalAnnotationExpr addAnnotation(Class<? extends Annotation> clazz) { 68 ((Node) this).tryAddImportToParentCompilationUnit(clazz); 69 return addAnnotation(clazz.getSimpleName()); 70 } 71 72 /** 73 * Annotates this with a marker annotation 74 * 75 * @param name the name of the annotation 76 * @return this 77 */ 78 @SuppressWarnings("unchecked") 79 public default T addMarkerAnnotation(String name) { 80 MarkerAnnotationExpr markerAnnotationExpr = new MarkerAnnotationExpr( 81 name(name)); 82 getAnnotations().add(markerAnnotationExpr); 83 markerAnnotationExpr.setParentNode((Node) this); 84 return (T) this; 85 } 86 87 /** 88 * Annotates this with a marker annotation and automatically add the import 89 * 90 * @param clazz the class of the annotation 91 * @return this 92 */ 93 public default T addMarkerAnnotation(Class<? extends Annotation> clazz) { 94 ((Node) this).tryAddImportToParentCompilationUnit(clazz); 95 return addMarkerAnnotation(clazz.getSimpleName()); 96 } 97 98 /** 99 * Annotates this with a single member annotation 100 * 101 * @param name the name of the annotation 102 * @param value the value, don't forget to add \"\" for a string value 103 * @return this 104 */ 105 @SuppressWarnings("unchecked") 106 public default T addSingleMemberAnnotation(String name, String value) { 107 SingleMemberAnnotationExpr singleMemberAnnotationExpr = new SingleMemberAnnotationExpr( 108 name(name), name(value)); 109 getAnnotations().add(singleMemberAnnotationExpr); 110 singleMemberAnnotationExpr.setParentNode((Node) this); 111 return (T) this; 112 } 113 114 /** 115 * Annotates this with a single member annotation and automatically add the import 116 * 117 * @param clazz the class of the annotation 118 * @param value the value, don't forget to add \"\" for a string value 119 * @return this 120 */ 121 public default T addSingleMemberAnnotation(Class<? extends Annotation> clazz, 122 String value) { 123 ((Node) this).tryAddImportToParentCompilationUnit(clazz); 124 return addSingleMemberAnnotation(clazz.getSimpleName(), value); 125 } 126 127 /** 128 * Check whether an annotation with this name is present on this element 129 * 130 * @param annotationName the name of the annotation 131 * @return true if found, false if not 132 */ 133 public default boolean isAnnotationPresent(String annotationName) { 134 return getAnnotations().stream().anyMatch(a -> a.getName().getName().equals(annotationName)); 135 } 136 137 /** 138 * Check whether an annotation with this class is present on this element 139 * 140 * @param annotationClass the class of the annotation 141 * @return true if found, false if not 142 */ 143 public default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) { 144 return isAnnotationPresent(annotationClass.getSimpleName()); 145 } 146 147 /** 148 * Try to find an annotation by its name 149 * 150 * @param annotationName the name of the annotation 151 * @return null if not found, the annotation otherwise 152 */ 153 public default AnnotationExpr getAnnotationByName(String annotationName) { 154 return getAnnotations().stream().filter(a -> a.getName().getName().equals(annotationName)).findFirst() 155 .orElse(null); 156 } 157 158 /** 159 * Try to find an annotation by its class 160 * 161 * @param annotationClass the class of the annotation 162 * @return null if not found, the annotation otherwise 163 */ 164 public default AnnotationExpr getAnnotationByClass(Class<? extends Annotation> annotationClass) { 165 return getAnnotationByName(annotationClass.getSimpleName()); 166 } 167 } 168