Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * OfficeFloor - http://www.officefloor.net
   * Copyright (C) 2005-2011 Daniel Sagenschneider
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 package net.officefloor.compile.impl.util;
 
 import java.util.List;
 import java.util.Map;
 
Utility methods to aid in compiling.

Author(s):
Daniel Sagenschneider
 
 public class CompileUtil {

Indicates whether the input java.lang.String is either null or empty.

Parameters:
value Value to check.
Returns:
true if blank.
 
 	public static boolean isBlank(String value) {
 		return ((value == null) || (value.trim().length() == 0));
 	}

Convenience method for java.util.List.toArray(java.lang.Object[]) to pass compiler warnings for generic typed array.

Parameters:
list List to transform into an array.
type Type of the array.
Returns:
List as an array.
 
 	@SuppressWarnings("unchecked")
 	public static <T> T[] toArray(Collection<T> listObject[] type) {
 		Object[] array = list.toArray(type);
 		return (T[]) array;
 	}

Convenience method to create a sorted array from a java.util.Collection that passes compiler warnings for generic typed array along with handling null compareTo values.

Parameters:
collection Collection to transform into a sorted array.
type Type of the array.
extractor StringExtractor to obtain compare key to sort.
Returns:
Collection as a sorted array.
 
 	public static <T> T[] toSortedArray(Collection<T> collection,
 			Object[] typefinal StringExtractor<? super T> extractor) {
 
 		// Create the array
 		T[] array = toArray(collectiontype);
 
 		// Sort the array
 		Arrays.sort(arraynew Comparator<T>() {
 			public int compare(T a, T b) {
 				// Obtain the string keys for comparing
 				String aKey = extractor.toString(a);
 				String bKey = extractor.toString(b);
 
 				// Return the comparison (handling possible null key)
 				return (aKey == null ? "" : aKey).compareTo(bKey);
 			}
 		});
 
 		// Return the sorted array
		return array;
	}

Obtains the java.lang.Class.

Parameters:
className Fully qualified name of the java.lang.Class to obtain.
expectedType Expected type of the java.lang.Class to return.
aliases Map of alias name to java.lang.Class. May be null.
classLoader java.lang.ClassLoader.
locationType net.officefloor.compile.issues.CompilerIssues.LocationType.
location Location.
assetType net.officefloor.frame.api.build.OfficeFloorIssues.AssetType.
assetName Name of asset.
issues net.officefloor.compile.issues.CompilerIssues.
Returns:
java.lang.Class.
	@SuppressWarnings("unchecked")
	public static <T> Class<? extends T> obtainClass(String className,
			Class<T> expectedTypeMap<StringClass<?>> aliases,
			ClassLoader classLoaderLocationType locationType,
			String locationAssetType assetTypeString assetName,
			CompilerIssues issues) {
		try {
			// Obtain the class (first checking for an alias)
			Class<?> clazz = (aliases != null ? aliases.get(className) : null);
			if (clazz == null) {
				// Not alias, so load the class
				clazz = classLoader.loadClass(className);
			}
			// Ensure class of expected type
			if (!expectedType.isAssignableFrom(clazz)) {
				issues.addIssue(locationTypelocationassetTypeassetName,
						"Must implement " + expectedType.getSimpleName()
" (class=" + clazz.getName() + ")");
				return null// instance not of type
			}
			// Return the obtained class
			return (Class<? extends T>) clazz;
catch (Throwable ex) {
			// Indicate issue
			issues.addIssue(locationTypelocationassetTypeassetName,
					"Failed to obtain class " + classNameex);
			return null// no class
		}
	}

Instantiates a new instance of the input java.lang.Class by its default constructor. If fails to instantiate, then reports issue via net.officefloor.compile.issues.CompilerIssues.

Parameters:
clazz java.lang.Class to instantiate.
expectedType Expected type that is to be instantiated.
locationType net.officefloor.compile.issues.CompilerIssues.LocationType.
location Location.
assetType net.officefloor.frame.api.build.OfficeFloorIssues.AssetType.
assetName Name of asset.
issues net.officefloor.compile.issues.CompilerIssues.
Returns:
New instance or null if not able to instantiate.
	public static <T, E> T newInstance(Class<T> clazzClass<E> expectedType,
			LocationType locationTypeString locationAssetType assetType,
			String assetNameCompilerIssues issues) {
		try {
			// Create the instance
instance = clazz.newInstance();
			// Ensure the instance is of the expected type
			if (!expectedType.isInstance(instance)) {
				// Indicate issue
				issues.addIssue(locationTypelocationassetTypeassetName,
						"Must implement " + expectedType.getSimpleName()
" (class=" + clazz.getName() + ")");
				return null// instance not of type
			}
			// Return the instance
			return instance;
catch (Throwable ex) {
			// Indicate issue (catching exception from constructor)
			issues.addIssue(locationTypelocationassetTypeassetName,
					"Failed to instantiate "
							+ (clazz != null ? clazz.getName() : null)
" by default constructor"ex);
			return null// no instance
		}
	}

Convenience method to instantiate and instance of a java.lang.Class from its fully qualified name.

Parameters:
className Fully qualified name of the java.lang.Class.
expectedType Expected type that java.lang.Class instance must be assignable.
aliases Map of alias name to java.lang.Class. May be null.
classLoader java.lang.ClassLoader.
locationType net.officefloor.compile.issues.CompilerIssues.LocationType.
location Location.
assetType net.officefloor.frame.api.build.OfficeFloorIssues.AssetType.
assetName Name of asset.
issues net.officefloor.compile.issues.CompilerIssues.
Returns:
New instance or null if not able to instantiate.
	public static <T> T newInstance(String classNameClass<T> expectedType,
			Map<StringClass<?>> aliasesClassLoader classLoader,
			LocationType locationTypeString locationAssetType assetType,
			String assetNameCompilerIssues issues) {
		// Obtain the class
		Class<? extends T> clazz = obtainClass(classNameexpectedType,
				aliasesclassLoaderlocationTypelocationassetType,
				assetNameissues);
		if (clazz == null) {
			return null// must have class
		}
		// Create an instance of the class
instance = newInstance(clazzexpectedTypelocationTypelocation,
				assetTypeassetNameissues);
		if (instance == null) {
			return null// must have instance
		}
		// Return the instance
		return instance;
	}

Convenience method to instantiate and instance of a java.lang.Class from its fully qualified name utilising a net.officefloor.compile.internal.structure.NodeContext.

Parameters:
className Fully qualified name of the java.lang.Class.
expectedType Expected type that java.lang.Class instance must be assignable.
aliases Map of alias name to java.lang.Class. May be null.
locationType net.officefloor.compile.issues.CompilerIssues.LocationType.
location Location.
assetType net.officefloor.frame.api.build.OfficeFloorIssues.AssetType.
assetName Name of asset.
context net.officefloor.compile.internal.structure.NodeContext.
Returns:
New instance or null if not able to instantiate.
	public static <T> T newInstance(String classNameClass<T> expectedType,
			Map<StringClass<?>> aliasesLocationType locationType,
			String locationAssetType assetTypeString assetName,
			NodeContext context) {
		// Return new instance
		return newInstance(classNameexpectedTypealiasescontext
				.getClassLoader(), locationTypelocationassetType,
				assetNamecontext.getCompilerIssues());
	}

All access via static methods.
	private CompileUtil() {
	}
New to GrepCode? Check out our FAQ X