Commit 72dd2e03 authored by Carlos Galindo's avatar Carlos Galindo
Browse files

remove unnecessary classes from Miscellanea

parent 56fc223e
Loading
Loading
Loading
Loading
+0 −119
Original line number Diff line number Diff line
package misc.java;

import java.io.File;
import java.io.Writer;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import javax.tools.JavaCompiler.CompilationTask;

import misc.Misc;

public final class JavaCompiler2
{
	/********************************************************************************************************************************/
	/************************************************************ STATIC ************************************************************/
	/********************************************************************************************************************************/
	public static void main(String[] args)
	{
		if (args.length != 1)
			throw new RuntimeException("The args parameter must be a project path");

		final String projectPath = args[0];

		JavaCompiler2.compileProject(projectPath);
	}

	/****************************************************************/
	/************************ Main functions ************************/
	/****************************************************************/
	public static boolean compileProject(String projectPath)
	{
		return JavaCompiler2.compileProject(null, projectPath, null);
	}
	public static boolean compileFiles(List<File> files)
	{
		return JavaCompiler2.compileFiles(null, files, null);
	}
	public static boolean compileFile(File file)
	{
		return JavaCompiler2.compileFile(null, file, null);
	}

	public static boolean compileProject(String classpath, String projectPath)
	{
		return JavaCompiler2.compileProject(classpath, projectPath, null);
	}
	public static boolean compileFiles(String classpath, List<File> files)
	{
		return JavaCompiler2.compileFiles(classpath, files, null);
	}
	public static boolean compileFile(String classpath, File file)
	{
		return JavaCompiler2.compileFile(classpath, file, null);
	}

	public static boolean compileProject(String projectPath, Writer errorsWriter)
	{
		return JavaCompiler2.compileProject(null, projectPath, errorsWriter);
	}
	public static boolean compileFiles(List<File> files, Writer errorsWriter)
	{
		return JavaCompiler2.compileFiles(null, files, errorsWriter);
	}
	public static boolean compileFile(File file, Writer errorsWriter)
	{
		return JavaCompiler2.compileFile(null, file, errorsWriter);
	}

	public static boolean compileProject(String classpath, String projectPath, Writer errorsWriter)
	{
		final File root = new File(projectPath);
		final String[] extensions = { ".java" };
		final List<File> files = Misc.getFiles(root, extensions, true);

		return JavaCompiler2.compileFiles(classpath, files, errorsWriter);
	}
	public static boolean compileFiles(String classpath, List<File> files, Writer errorsWriter)
	{
		boolean allCompiled = true;

		for (File file : files)
			allCompiled = allCompiled && JavaCompiler2.compileFile(classpath, file, errorsWriter);

		return allCompiled;
	}
	public static boolean compileFile(String classpath, File file, Writer errorsWriter)
	{
		try
		{
			final List<String> optionList = new LinkedList<String>();

			if (classpath != null)
				optionList.addAll(Arrays.asList("-classpath", classpath));

			final javax.tools.JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
			final StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
			final Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(file);
			final CompilationTask compilationTask = compiler.getTask(errorsWriter, fileManager, null, optionList, null, compilationUnits);

			return compilationTask.call();
		}
		catch (Throwable e)
		{
			return false;
		}
	}

	/********************************************************************************************************************************/
	/************************************************************ OBJECT ************************************************************/
	/********************************************************************************************************************************/
	private JavaCompiler2()
	{
		
	}
}
 No newline at end of file
+0 −100
Original line number Diff line number Diff line
package misc.java;

import misc.Misc;

import java.io.File;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.LinkedList;
import java.util.List;

public class MainSearcher
{
	public static void main(String[] args)
	{
		if (args.length != 1)
			throw new RuntimeException("The args parameter must contains a directory path");

		final String projectPath = args[0];
		final List<File> files = MainSearcher.search(projectPath, true);

		for (File file : files)
			System.out.println(file.getPath());
	}

	public static List<File> search(String projectPath, boolean recursive)
	{
		MainSearcher.checkProject(projectPath);

		final File root = new File(projectPath);
		final String[] extensions = { ".class" };
		final List<File> files = Misc.getFiles(root, extensions, recursive);
		final List<File> mainFiles = new LinkedList<File>();

		for (File file : files)
		{
			final int projectPathLength = projectPath.length();
			final String filePath = file.getPath().substring(projectPathLength);

			if (MainSearcher.checkMain(projectPath, filePath))
				mainFiles.add(file);
		}

		return mainFiles;
	}
	private static void checkProject(String projectPath)
	{
		if (projectPath == null)
			throw new RuntimeException("Null project path");

		final File file = new File(projectPath);
		if (!file.exists())
			throw new RuntimeException("The project path does not exist");
		if (!file.isDirectory())
			throw new RuntimeException("The project path is not a directory");
	}
	private static boolean checkMain(String projectPath, String filePath)
	{
		if (filePath == null || !filePath.endsWith(".class"))
			return false;

		try
		{
			final String separator = File.separator;
			final File file = new File(projectPath);
			final URL url = file.toURI().toURL();
			final URL[] urls = new URL[]{ url };
			final URLClassLoader ucl = new URLClassLoader(urls);
			final String fileName = filePath.substring(0, filePath.length() - ".class".length()).replaceAll(separator, ".");
			final Class<?> classFile = ucl.loadClass(fileName);
			final java.lang.reflect.Method[] methods = classFile.getDeclaredMethods();

			ucl.close();
			for (java.lang.reflect.Method method : methods)
			{
				// Static main method
				final int modifiers = method.getModifiers();
				final boolean publicMethod = Modifier.isPublic(modifiers);
				final boolean staticMethod = Modifier.isStatic(modifiers);
				final String methodName = method.getName();
				final Class<?>[] parameters = method.getParameterTypes();

				if (!(publicMethod && staticMethod && methodName.equals("main") && parameters != null && parameters.length == 1))
					continue;

				// Correct parameter
				final String type = parameters[0].getSimpleName();

				if (type.equals("String[]"))
					return true;
			}
		}
		catch (Throwable t)
		{
			t.printStackTrace();
		}

		return false;
	}
}
 No newline at end of file
+0 −101
Original line number Diff line number Diff line
package misc.java;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class Reflection
{
	public static Class<?> getClass(String className)
	{
		switch (className)
		{
			case "String":
				return String.class;
			case "boolean":
				return boolean.class;
			case "byte":
				return byte.class;
			case "int":
				return int.class;
			case "long":
				return long.class;
			case "float":
				return float.class;
			case "double":
				return double.class;
			case "char":
				return char.class;
		}

		try
		{
			final ClassLoader classLoader = Reflection.class.getClassLoader();
			final Class<?> clazz = classLoader.loadClass(className);

			return clazz;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return null;
	}
	public static Object getInstance(Class<?> clazz, Class<?>[] parameterTypes, Object[] parameterValues)
	{
		try
		{
			final Constructor<?> constructor = clazz.getConstructor(parameterTypes);

			constructor.setAccessible(true);

			return constructor.newInstance(parameterValues);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return null;
	}
	public static Method getMethod(Class<?> clazz, String name, Class<?>[] parameterTypes)
	{
		try
		{
			final Method method = clazz.getMethod(name, parameterTypes);

			method.setAccessible(true);

			return method;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return null;
	}
	public static Object getValue(String type, String value)
	{
		switch (type)
		{
			case "String":
				return value;
			case "boolean":
				return Boolean.parseBoolean(value);
			case "byte":
				return Byte.parseByte(value);
			case "int":
				return Integer.parseInt(value);
			case "long":
				return Long.parseLong(value);
			case "float":
				return Float.parseFloat(value);
			case "double":
				return Double.parseDouble(value);
			case "char":
				return value.charAt(0);
		}
		return null;
	}
}
 No newline at end of file
+0 −24
Original line number Diff line number Diff line
package misc.math;

public abstract class Permutator
{
	protected int size;
	protected int limit;
	protected Integer[] numbers;

	public Permutator(int size, int limit)
	{
		this.size = size;
		this.limit = limit;

		this.restart();
	}

	public abstract void restart();

	public abstract Integer[] first();
	public abstract Integer[] previous();
	public abstract Integer[] current();
	public abstract Integer[] next();
	public abstract Integer[] last();
}
 No newline at end of file
+0 −112
Original line number Diff line number Diff line
package misc.math;

public class PermutatorWithRepetitions extends Permutator
{
	public static int LEFTWARDS = 0;
	public static int RIGHTWARDS = 1;

	private int direction;

	public PermutatorWithRepetitions(int size, int limit)
	{
		this(size, limit, PermutatorWithRepetitions.RIGHTWARDS);
	}
	public PermutatorWithRepetitions(int size, int limit, int direction)
	{
		super(size, limit);

		this.direction = direction;
		this.restart();
	}

	private int firstPosition()
	{
		if (this.direction == PermutatorWithRepetitions.LEFTWARDS)
			return 0;
		if (this.direction == PermutatorWithRepetitions.RIGHTWARDS)
			return super.size - 1;
		return 0;
	}
	private int nextPosition(int position)
	{
		if (this.direction == PermutatorWithRepetitions.LEFTWARDS)
			return position + 1;
		if (this.direction == PermutatorWithRepetitions.RIGHTWARDS)
			return position - 1;
		return position + 1;
	}
	private boolean lastPosition(int position)
	{
		if (this.direction == PermutatorWithRepetitions.LEFTWARDS)
			return position >= super.size;
		if (this.direction == PermutatorWithRepetitions.RIGHTWARDS)
			return position < 0;
		return position >= super.size;
	}

	public void restart()
	{
		int size = super.size - 1;

		super.numbers = new Integer[super.size];
		for (int position = 0; position <= size; position++)
			super.numbers[position] = 0;
		super.numbers[this.firstPosition()] = -1;
	}

	public Integer[] first()
	{
		for (int position = 0; position < super.size; position++)
			super.numbers[position] = 0;

		return super.numbers;
	}
	public Integer[] previous()
	{
		int position = this.firstPosition();

		super.numbers[position]--;
		while (super.numbers[position] < 0)
		{
			super.numbers[position] = super.limit - 1;
			position = this.nextPosition(position);
			if (this.lastPosition(position))
				return null;
			super.numbers[position]--;
		}

		return super.numbers;
	}
	public Integer[] current()
	{
		if (super.numbers[this.firstPosition()] == -1)
			super.numbers[this.firstPosition()] = 0;

		return super.numbers;
	}
	public Integer[] next()
	{
		int position = this.firstPosition();

		super.numbers[position]++;
		while (super.numbers[position] == super.limit + 1)
		{
			super.numbers[position] = 0;
			position = this.nextPosition(position);
			if (this.lastPosition(position))
				return null;
			super.numbers[position]++;
		}

		return super.numbers;
	}
	public Integer[] last()
	{
		int limit = super.limit - 1;

		for (int position = 0; position < super.size; position++)
			super.numbers[position] = limit;

		return super.numbers;
	}
}
 No newline at end of file
Loading