Subversion Repository Public Repository

litesoft

Diff Revisions 293 vs 294 for /trunk/Java/ScarPlus/src/com/esotericsoftware/scar/Scar.java

Diff revisions: vs.
  @@ -1753,10 +1753,59 @@
1753 1753 }
1754 1754 }
1755 1755
1756 - protected void executeMethod( String pMethodName )
1756 + protected Runnable createRunnableFor( String pMethodName )
1757 1757 throws IOException
1758 1758 {
1759 - // todo: use reflection to call method
1759 + Runnable zRunnable = createRunnableFor( this, pMethodName );
1760 + return (zRunnable != null) ? zRunnable : createRunnableFor( mLaunchProject, pMethodName );
1761 + }
1762 +
1763 + protected Runnable createRunnableFor( final Object pObject, String pMethodName )
1764 + {
1765 + final Method zMethod = getMatchingMethod( pObject, pMethodName );
1766 + return (zMethod == null) ? null : new Runnable()
1767 + {
1768 + @Override public void run()
1769 + {
1770 + try
1771 + {
1772 + zMethod.invoke( pObject );
1773 + }
1774 + catch ( Exception e )
1775 + {
1776 + throw new RuntimeException( e );
1777 + }
1778 + }
1779 + };
1780 + }
1781 +
1782 + protected Method getMatchingMethod( Object pObject, String pMethodName )
1783 + {
1784 + List<Method> zFound = new ArrayList<Method>();
1785 + Method[] zMethods = pObject.getClass().getMethods();
1786 + for ( Method zMethod : zMethods )
1787 + {
1788 + if ( zMethod.getReturnType().equals( Void.class ) && (zMethod.getParameterTypes().length == 0) )
1789 + {
1790 + if ( pMethodName.equals( zMethod.getName() ) )
1791 + {
1792 + return zMethod;
1793 + }
1794 + if ( pMethodName.equalsIgnoreCase( zMethod.getName() ) )
1795 + {
1796 + zFound.add( zMethod );
1797 + }
1798 + }
1799 + }
1800 + if ( zFound.size() == 0 )
1801 + {
1802 + return null;
1803 + }
1804 + if ( zFound.size() == 1 )
1805 + {
1806 + return zFound.get( 0 );
1807 + }
1808 + throw new IllegalArgumentException( "Multiple Methods " + zFound + " found on '" + pObject.getClass().getSimpleName() + "' than match: " + pMethodName );
1760 1809 }
1761 1810
1762 1811 protected void createLaunchProject()
  @@ -1765,18 +1814,50 @@
1765 1814 mLaunchProject = project( mArgs.get( "file", "." ) );
1766 1815 }
1767 1816
1768 - protected void run()
1817 + protected int run()
1769 1818 throws IOException
1770 1819 {
1771 1820 if ( mArgs.count() == 0 )
1772 1821 {
1773 1822 mLaunchProject.build();
1774 - return;
1823 + return 0;
1824 + }
1825 + List<Runnable> zToExecute = getArgsBasedRunnables();
1826 + for ( Runnable zRunnable : zToExecute )
1827 + {
1828 + zRunnable.run();
1829 + }
1830 + return 0;
1831 + }
1832 +
1833 + private ArrayList<Runnable> getArgsBasedRunnables()
1834 + throws IOException
1835 + {
1836 + ArrayList<Runnable> zRunnables = new ArrayList<Runnable>();
1837 + List<String> zUnrecognizedNames = new ArrayList<String>();
1838 + for ( Arguments.NameValuePair zPair; null != (zPair = mArgs.getNext()); )
1839 + {
1840 +
1841 + Runnable zRunnable = createRunnableFor( zPair.getName() );
1842 + if ( zRunnable != null )
1843 + {
1844 + zRunnables.add( zRunnable );
1845 + }
1846 + else
1847 + {
1848 + zUnrecognizedNames.add( zPair.getName() );
1849 + }
1775 1850 }
1776 - for ( Arguments.NameValuePair zPair ; null != (zPair = mArgs.getNext()); )
1851 + if ( !zUnrecognizedNames.isEmpty() )
1777 1852 {
1778 - executeMethod( zPair.getName() );
1853 + System.err.println( "\nUnrecognized Command Line Args:" );
1854 + for ( String zName : zUnrecognizedNames )
1855 + {
1856 + System.err.println( " " + zName );
1857 + }
1858 + System.exit( 1 );
1779 1859 }
1860 + return zRunnables;
1780 1861 }
1781 1862
1782 1863 public static void main( String[] args )
  @@ -1786,6 +1867,6 @@
1786 1867 Scar scar = new Scar( arguments );
1787 1868 scar.initLoggerFactory();
1788 1869 scar.createLaunchProject();
1789 - scar.run( );
1870 + System.exit( scar.run() );
1790 1871 }
1791 1872 }