1818 */
1919package org .mapstruct .eclipse .internal ;
2020
21+ import java .util .ArrayList ;
22+ import java .util .Collection ;
2123import java .util .HashSet ;
2224import java .util .Set ;
2325
26+ import org .eclipse .jdt .core .dom .IAnnotationBinding ;
2427import org .eclipse .jdt .core .dom .IBinding ;
28+ import org .eclipse .jdt .core .dom .IMemberValuePairBinding ;
2529import org .eclipse .jdt .core .dom .IMethodBinding ;
2630import org .eclipse .jdt .core .dom .ITypeBinding ;
31+ import org .eclipse .jdt .core .dom .IVariableBinding ;
2732
2833/**
2934 * Helper class to inspect various {@link IBinding}s.
@@ -39,25 +44,26 @@ private Bindings() {
3944 * @param type the type
4045 * @return the method names declared in the class or a super type of it
4146 */
42- public static Set <String > findAllMethodNames (ITypeBinding type ) {
43- Set <String > result = new HashSet <String >();
47+ public static Collection <String > findAllMethodNames (ITypeBinding type ) {
48+ Collection <String > result = new HashSet <String >();
4449
4550 collectMethodNames ( type , new HashSet <ITypeBinding >(), result );
4651
4752 return result ;
4853 }
4954
50- private static void collectMethodNames (ITypeBinding curr , Set <ITypeBinding > visited , Set <String > methodNames ) {
51- if ( !isJavaLangObject ( curr ) && visited .add ( curr ) ) {
52- for ( IMethodBinding methodBinding : curr .getDeclaredMethods () ) {
55+ private static void collectMethodNames (ITypeBinding type , Set <ITypeBinding > visited ,
56+ Collection <String > methodNames ) {
57+ if ( !isJavaLangObject ( type ) && visited .add ( type ) ) {
58+ for ( IMethodBinding methodBinding : type .getDeclaredMethods () ) {
5359 methodNames .add ( methodBinding .getName () );
5460 }
5561
56- for ( ITypeBinding ifc : curr .getInterfaces () ) {
62+ for ( ITypeBinding ifc : type .getInterfaces () ) {
5763 collectMethodNames ( ifc , visited , methodNames );
5864 }
5965
60- ITypeBinding superClass = curr .getSuperclass ();
66+ ITypeBinding superClass = type .getSuperclass ();
6167 if ( superClass != null ) {
6268 collectMethodNames ( superClass , visited , methodNames );
6369 }
@@ -67,4 +73,49 @@ private static void collectMethodNames(ITypeBinding curr, Set<ITypeBinding> visi
6773 private static boolean isJavaLangObject (ITypeBinding curr ) {
6874 return curr .getQualifiedName ().equals ( "java.lang.Object" );
6975 }
76+
77+ /**
78+ * @param annotations the annotations
79+ * @param annotationName the fully qualified name of the annotation to look for
80+ * @return {@code true}, iff the given array of annotations contains an annotation with the given name
81+ */
82+ public static boolean containsAnnotation (IAnnotationBinding [] annotations , String annotationName ) {
83+ for ( IAnnotationBinding annotation : annotations ) {
84+ if ( annotation .getAnnotationType ().getQualifiedName ().equals ( annotationName ) ) {
85+ return true ;
86+ }
87+ }
88+ return false ;
89+ }
90+
91+ /**
92+ * @param type the enum type
93+ * @return the enum constant names of the given type
94+ */
95+ public static Collection <String > findAllEnumConstants (ITypeBinding type ) {
96+ IVariableBinding [] declaredFields = type .getDeclaredFields ();
97+
98+ Collection <String > result = new ArrayList <String >( declaredFields .length );
99+
100+ for ( IVariableBinding field : declaredFields ) {
101+ result .add ( field .getName () );
102+ }
103+
104+ return result ;
105+ }
106+
107+ /**
108+ * Returns the qualified name of the annotation which is associated with the given {@link IMemberValuePairBinding}.
109+ */
110+ public static String getAnnotationQualifiedName (IMemberValuePairBinding binding ) {
111+ IMethodBinding methodBinding = binding .getMethodBinding ();
112+ if ( methodBinding == null ) {
113+ return null ;
114+ }
115+ ITypeBinding declaringClass = methodBinding .getDeclaringClass ();
116+ if ( declaringClass == null ) {
117+ return null ;
118+ }
119+ return declaringClass .getQualifiedName ();
120+ }
70121}
0 commit comments