/* * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product * that is described in this document. In particular, and without limitation, these intellectual property * rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or * more additional patents or pending patent applications in the U.S. and in other countries. * * U.S. Government Rights - Commercial software. Government users are subject to the Sun * Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its * supplements. * * Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or * registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks * are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the * U.S. and other countries. * * UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open * Company, Ltd. */ package com.sun.max; import java.lang.reflect.*; import java.util.*; import com.sun.max.lang.*; import com.sun.max.program.*; /** * Describes a package in the com.sun.max project, * providing programmatic package information manipulation, * which is lacking in java.lang.Package. *
* You must create a class called 'Package extends MaxPackage' * in each and every package in this project. *
* For example you can call:
*
* new com.sun.max.program.Package().superPackage()
*
* Also make sure that you have a file called 'package-info.java' in every package.
* This is where you can put package-related JavaDoc comments.
*
* @author Bernd Mathiske
* @author Doug Simon
*/
public abstract class MaxPackage implements Comparable void registerScheme(Class schemeType, Class extends S> schemeImplementation) {
assert schemeType.isInterface() || Modifier.isAbstract(schemeType.getModifiers());
assert schemeImplementation.getPackage().getName().equals(name()) : "cannot register implmentation class from another package: " + schemeImplementation;
if (schemeTypeToImplementation == null) {
schemeTypeToImplementation = new IdentityHashMap Class extends S> schemeTypeToImplementation(Class schemeType) {
if (schemeTypeToImplementation == null) {
return null;
}
final Class< ? extends Scheme> implementation = schemeTypeToImplementation.get(schemeType);
if (implementation == null) {
return null;
}
return implementation.asSubclass(schemeType);
}
public static boolean equal(MaxPackage p1, MaxPackage p2) {
if (p1 == null) {
return p2 == null;
}
return p1.equals(p2);
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (other instanceof MaxPackage) {
return packageName.equals(((MaxPackage) other).packageName);
}
return false;
}
@Override
public int hashCode() {
return name().hashCode();
}
public Set Class extends S> loadSchemeImplementation(Class schemeType) {
final Class extends S> schemeImplementation = schemeTypeToImplementation(schemeType);
if (schemeImplementation == null) {
ProgramError.unexpected("could not find subclass of " + schemeType + " in " + this);
} else {
final Class> loadedImplementation = Classes.load(schemeType.getClassLoader(), schemeImplementation.getName());
return loadedImplementation.asSubclass(schemeType);
}
return null;
}
/**
* Instantiates the scheme implementation class in this package implementing a given scheme type.
*
* @param schemeType the interface or abstract class defining a scheme type
* @return a new instance of the scheme implementation class
*/
public synchronized S loadAndInstantiateScheme(Class schemeType) {
final Class extends S> schemeImplementation = loadSchemeImplementation(schemeType);
try {
return schemeImplementation.newInstance();
} catch (Throwable throwable) {
throw ProgramError.unexpected("could not instantiate class: " + schemeImplementation.getName(), throwable);
}
}
@Override
public String toString() {
return name();
}
}