/* 
 * Mutability Detector
 *
 * Copyright 2009 Graham Allan
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.mutabilitydetector.cli;

import static com.google.classpath.RegExpResourceFilter.ANY;
import static com.google.classpath.RegExpResourceFilter.ENDS_WITH_CLASS;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

import org.mutabilitydetector.AnalysisSession;


import com.google.classpath.ClassPath;
import com.google.classpath.ClassPathFactory;
import com.google.classpath.RegExpResourceFilter;

public class RunMutabilityDetector {

	public static void main(String[] args) {
		CommandLineOptions options = createOptionsFromArgs(args);
		ClassPath cp = new ClassPathFactory().createFromPath(options.classpath());

		RegExpResourceFilter regExpResourceFilter = new RegExpResourceFilter(ANY, ENDS_WITH_CLASS);
		String[] findResources = cp.findResources("", regExpResourceFilter);

		setCustomClassLoader(options);

		AnalysisSession session = new AnalysisSession(cp);

		List<String> filtered = getNamesOfClassesToAnalyse(options, findResources);

		session.runAnalysis(filtered);

		StringBuilder output = new SessionResultsFormatter(options).format(session);
		System.out.println(output);

	}

	private static CommandLineOptions createOptionsFromArgs(String[] args) {
		CommandLineOptions options;
		try {
			options = new CommandLineOptions(args);
			return options;
		} catch (Throwable e) {
			System.out.println("Exiting...");
			System.exit(1);
		}
		return null; // impossible statement
	}

	private static void setCustomClassLoader(CommandLineOptions options) {
		String[] classPathUrls = options.classpath().split(":");

		List<URL> urlList = new ArrayList<URL>();

		for (String classPathUrl : classPathUrls) {
			try {
				URL toAdd = new File(classPathUrl).toURI().toURL();
				urlList.add(toAdd);
			} catch (MalformedURLException e) {
				System.err.printf("Classpath option %s is invalid.", classPathUrl);
			}
		}
		ClassLoader classLoader = new URLClassLoader(urlList.toArray(new URL[] {}));
		Thread.currentThread().setContextClassLoader(classLoader);
	}

	private static List<String> getNamesOfClassesToAnalyse(CommandLineOptions options, String[] findResources) {
		List<String> filtered = new ArrayList<String>();
		List<String> classNames = new ArrayList<String>();
		classNames.addAll(Arrays.asList(findResources));
		String matcher = options.match().replace(".", "\\.").replace("*", ".*"); 
		for (String className : classNames) {

			String dottedClassName = className.replace(".class", "").replace("/", ".");
			if (Pattern.matches(matcher, dottedClassName)) {
				filtered.add(className);
			}
		}
		return classNames;
	}

}

@@ -1961,32 +1961,41 @@
 tring%5B%5D args) %7B%0A
+%09%09try %7B%0A%09
 %09%09CommandLineOpt
@@ -2003,28 +2003,8 @@
 ons 
-options;%0A%09%09try %7B%0A%09%09%09
 opti
@@ -2141,19 +2141,16 @@
 it(1);%0A%09
-%09%7D%0A
 %09%09return
@@ -2180,16 +2180,20 @@
 atement%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09pri
@@ -3067,49 +3067,9 @@
 ch()
-.replace(%22.%22, %22%5C%5C.%22).replace(%22*%22, %22.*%22); 
+;
 %0A%09%09f
@@ -3284,26 +3284,24 @@
 %09return 
-classNames
+filtered
 ;%0A%09%7D%0A%0A%7D%0A

Rearrange around exception handling to improve readability. Properly handle the filtering of classes to analyse using the user supplied regex.