001 /* 002 * Created on May 27, 2007 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 005 * the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 011 * specific language governing permissions and limitations under the License. 012 * 013 * Copyright @2007-2010 the original author or authors. 014 */ 015 package org.fest.swing.annotation; 016 017 import java.lang.reflect.AnnotatedElement; 018 import java.lang.reflect.Method; 019 020 /** 021 * Understands utility methods related to GUI tests. A GUI test is a class or method annotated with 022 * <code>{@link org.fest.swing.annotation.GUITest}</code>. 023 * 024 * @author Alex Ruiz 025 */ 026 public final class GUITestFinder { 027 028 /** 029 * Returns <code>true</code> if the given class and/or method are annotated with <code>{@link GUITest}</code>. This 030 * method also searches in super-classes and overridden methods. 031 * @param type the class to check. 032 * @param method the method to check. 033 * @return <code>true</code> if the given class and/or method are annotated with <code>{@link GUITest}</code>. 034 */ 035 public static boolean isGUITest(Class<?> type, Method method) { 036 return isGUITest(type) || isGUITest(method) || isSuperClassGUITest(type, method); 037 } 038 039 private static boolean isSuperClassGUITest(Class<?> type, Method method) { 040 Class<?> superclass = type.getSuperclass(); 041 while (superclass != null) { 042 if (isGUITest(superclass)) return true; 043 Method overriden = method(superclass, method.getName(), method.getParameterTypes()); 044 if (overriden != null && isGUITest(overriden)) return true; 045 superclass = superclass.getSuperclass(); 046 } 047 return false; 048 } 049 050 private static Method method(Class<?> type, String methodName, Class<?>[] parameterTypes) { 051 try { 052 return type.getDeclaredMethod(methodName, parameterTypes); 053 } catch (SecurityException e) { 054 return null; 055 } catch (NoSuchMethodException e) { 056 return null; 057 } catch (RuntimeException e) { 058 return null; 059 } 060 } 061 062 private static boolean isGUITest(AnnotatedElement annotatedElement) { 063 return annotatedElement.isAnnotationPresent(GUITest.class); 064 } 065 066 private GUITestFinder() {} 067 }