package pkg_graphique; import java.io.File; import java.io.FileNotFoundException; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Scanner; /** * Classe utilitaire pour des tests utilisant la reflection * @author Denis BUREAU * @version 2010.03.14 * TODO: pb avec annotations dans isMethodP() */ public class TestUtil { public static boolean isConstantInt( Field pF, String pL, String pT, String pN, int pV ) { boolean vR; try { pF.setAccessible(true); // in case of private vR= isField( pF, pL, pT, pN ) && pV == ((Integer)(pF.get( null ))).intValue(); } catch ( IllegalAccessException iae ) { // valeur non accessible System.out.println( "pb d'acces a la constante"+pN ); vR= false; // String vName= pF.getDeclaringClass().getSimpleName() + ".java"; // Scanner vSc= null; // try { // vSc= new Scanner( new File( vName ) ); // while ( vSc.hasNext() && !vR ) { // String vWord= vSc.next(); // if ( vWord.equals( "@Override" ) && vSc.hasNext() ) { // vWord= vSc.next(); // System.out.println( "[2:" + vWord + "]" ); // } // if pT // } // while // } // catch ( FileNotFoundException fnfe ) { // System.out.println( "isConstantInt: " + vName + " not found !" ); // } // finally { // if ( vSc != null ) // vSc.close(); // } } // catch return vR; } // isConstantInt() public static boolean isField( Field pF, String pL, String pT, String pN ) { // pUblic prOtected pAckage prIvate ou anyProtection return hasModifiers( pF, pL ) && pF.getType().getSimpleName().equals( pT ) && pF.getName().equals( pN ); } // isField() public static boolean isConstructor( Constructor pC, String pL, int pN ) { // pUblic prOtected pAckage prIvate ou anyProtection return hasModifiers( pC, pL ) && pC.getParameterTypes().length == pN; } // isConstructor() public static boolean isMethod( Method pM, String pL, String pN, int pP ) { // pUblic prOtected pAckage prIvate ou anyProtection return hasModifiers( pM, pL ) && pM.getName().equals( pN ) && pM.getParameterTypes().length == pP; } // isMethod() public static boolean isMethodP( Method pM, String pL, String pT, String pN, String[] pP ) { // pUblic prOtected pAckage prIvate ou anyProtection boolean vR; vR = pM.getName().equals( pN ); if ( pL.contains( "r" ) ) { // vR = vR && (pM.getAnnotation( Override.class ) != null); // System.out.println(java.util.Arrays.toString(pM.getDeclaredAnnotations())); pL = pL.replace( "r", "" ); } // r vR = vR && hasModifiers( pM, pL ); vR = vR && pM.getReturnType().getName().equals( pT ); Class[] vP = pM.getParameterTypes(); vR = vR && (vP.length == pP.length); for ( int vI=0; vI 0 ) { vWord= vWord.substring( 0, vI ); } // if if ( vWord.equals( pN ) ) return true; //return true; } // if @Override } // while } catch ( FileNotFoundException fnfe ) { System.err.println( "hasMethodOver: " + vName + " not found !?" ); } catch ( Exception e ) { System.err.println( "hasMethodOver: syntaxe incorrecte !" ); } finally { if ( vSc != null ) vSc.close(); } // finally } // if } // for return false; } // hasMethodOver() } // TestUtil