com.sun.jini.tool
Class PreferredListGen.Graph

java.lang.Object
  extended by com.sun.jini.tool.PreferredListGen.Graph
Enclosing class:
PreferredListGen

private class PreferredListGen.Graph
extends Object

A representation of the graph of classes contained in the set of JARs being analyzed. This class implements the search algorithm to find the optimal (smallest) set of preferred list entries which describes the preferred state of entries in the graph. Nodes in the graph can be one of seven types:

All non-terminal nodes represent a component in the package name of a class. All leaf nodes represent classes or resources.


Field Summary
(package private) static int CLASS
          type value for a class node
(package private) static int DEFAULT
          type value for the default preference (root) node
(package private)  Boolean impliedPref
          the preferred value implied for child nodes
(package private) static int INHERIT
          type value when non-leaf node inherits preference from its parent
(package private)  String name
          the name of this node (e.g. com or sun, not com.sun)
(package private) static int NAMESPACE
          type value for namespace wildcard nodes
(package private)  HashSet nodes
          the set of child nodes of this node
(package private)  PreferredListGen.Graph parent
          the parent of this node, or null for the root node
(package private) static int PKG
          type value for package wildcard nodes
(package private)  boolean preferred
          the preferred state, only meaningful to leaf (class) nodes
(package private) static int RESOURCE
          type value for a resource (non-class JAR entry) node
(package private)  File sourceJar
          the source JAR causing creation of this node, or null
(package private) static int STALE
          type value for a stale node (marked for deletion)
(package private)  int type
          the type of the node
 
Constructor Summary
PreferredListGen.Graph()
          Create the root node of the graph, setting implied pref to TRUE
PreferredListGen.Graph(PreferredListGen.Graph parent, String name, int type, boolean preferred, File sourceJar)
          Create a node of the graph having the given name, parent, type and preferred value.
PreferredListGen.Graph(PreferredListGen.Graph parent, String name, int type, File sourceJar)
          Create a node of the graph having the given name and parent.
 
Method Summary
(package private)  void add(String name, int type, File sourceJar)
          Add a child node to this node having the given name. the name may have multiple '.' separated components, in which case a hierarchy of child nodes will be added.
(package private)  void addEntries(Collection entries)
          Add preferred list entries (represented by PrefData objects) to the given Collection.
(package private)  void addLeaves(Collection leaves)
           
(package private)  void addRoots(Collection roots)
          Add roots from this subtree of the graph to the given collection of roots.
(package private)  void addWithPreference(String name, int type, boolean preferred, File sourceJar)
          Add a child node to this node having the given name. the name may have multiple '.' separated components, in which case a hierarchy of child nodes will be added.
(package private)  boolean childPref()
          Return the implied preference of the parent node if the parent is a package wildcard.
(package private)  boolean contains(String name)
          Return true if the graph contains a node having the given name.
(package private)  boolean containsLeavesOnly()
          Test whether all of the immediate child nodes of this node are leaf (class) nodes.
(package private)  int countEntries(int type, Boolean pref)
          Count the number of entries which would be created by this node for the given node type and preference.
(package private)  int countImpliedFailures()
          Count the number of child leaf (class) nodes which have a preferred state which differs from the value implied by * its ancestors.
(package private)  int countLeafNodes()
          Count the number of child leaf nodes.
(package private)  int countPreferred(boolean value)
          Count the number of child leaf nodes which have a preferred state matching the given value.
(package private)  void deleteStaleNodes()
          Recursively remove child nodes of type STALE.
(package private)  PreferredListGen.Graph getChild(String name)
          Get the child node having the given name.
(package private)  String getFullName()
          Return the full name of this node expressed as a relative path.
(package private)  PreferredListGen.Graph getOuter()
          Return the node for the outer class for this class if this is a nested class.
(package private)  boolean impliedChildPref()
          Return the implied preference as determined by the nearest parent namespace wildcard, or the default preference if there are no parent namespace wildcards.
(package private)  void initialize(String arg, boolean preferred, File sourceJar)
          Determines whether arg represents a path reference to a class, a resource, a package wildcard, or a namespace wild card, and add the value, converted to a '.' separated name, to the graph with the given preferred value and appropriate type.
(package private)  boolean isFrozen(String name)
          Return the frozen state of the class node having the given name.
(package private)  void markStaleInnerClasses()
          If this node is of type CLASS and is an inner class, mark this node as STALE if the outer class has the same preferred value.
(package private)  void merge(PreferredListGen.Graph g)
           
(package private)  void reset()
          For non-leaf nodes, Reset the type and implied preference value for this node and all child nodes recursively.
(package private)  void setDefaultPref(boolean value)
          Set the default preferred value for the graph.
(package private)  boolean setPreferred(String name, boolean value, boolean force)
          Set the preferred state of the leaf (class) child node having the given '.' qualified name to the given value.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

INHERIT

static final int INHERIT
type value when non-leaf node inherits preference from its parent

See Also:
Constant Field Values

PKG

static final int PKG
type value for package wildcard nodes

See Also:
Constant Field Values

NAMESPACE

static final int NAMESPACE
type value for namespace wildcard nodes

See Also:
Constant Field Values

DEFAULT

static final int DEFAULT
type value for the default preference (root) node

See Also:
Constant Field Values

CLASS

static final int CLASS
type value for a class node

See Also:
Constant Field Values

RESOURCE

static final int RESOURCE
type value for a resource (non-class JAR entry) node

See Also:
Constant Field Values

STALE

static final int STALE
type value for a stale node (marked for deletion)

See Also:
Constant Field Values

name

String name
the name of this node (e.g. com or sun, not com.sun)


nodes

HashSet nodes
the set of child nodes of this node


preferred

boolean preferred
the preferred state, only meaningful to leaf (class) nodes


parent

PreferredListGen.Graph parent
the parent of this node, or null for the root node


type

int type
the type of the node


impliedPref

Boolean impliedPref
the preferred value implied for child nodes


sourceJar

File sourceJar
the source JAR causing creation of this node, or null

Constructor Detail

PreferredListGen.Graph

PreferredListGen.Graph()
Create the root node of the graph, setting implied pref to TRUE


PreferredListGen.Graph

PreferredListGen.Graph(PreferredListGen.Graph parent,
                       String name,
                       int type,
                       File sourceJar)
Create a node of the graph having the given name and parent. If the name contains multiple '.' separated tokens, the name is taken from the first token, and another node is constructed using the remainder of the name and this node as the parent. The final (leaf) node created is given a preferred value inherited from its ancestors.

Parameters:
parent - the parent of this node, or null if this is the root node.
name - the (possibly dot-separated) name for this node (and child nodes).
type - the node type, must be CLASS or RESOURCE
sourceJar - source of this node definition, or null defined through the -api or -impl options

PreferredListGen.Graph

PreferredListGen.Graph(PreferredListGen.Graph parent,
                       String name,
                       int type,
                       boolean preferred,
                       File sourceJar)
Create a node of the graph having the given name, parent, type and preferred value. If the name contains multiple '.' separated tokens, the name is taken from the first token, and another node is constructed using the remainder of the name and this node as the parent. The type and preferred value are only set on the last node created in the chain.

Parameters:
parent - the parent of this node, or null if this is the root node.
name - the (possibly dot-separated) name for this node (and child nodes).
type - the node type, must not be INHERIT
preferred - the preferred state of this node
sourceJar - source of this node definition, or null defined through the -api or -impl options
Throws:
IllegalStateException - if type is INHERIT
Method Detail

initialize

void initialize(String arg,
                boolean preferred,
                File sourceJar)
Determines whether arg represents a path reference to a class, a resource, a package wildcard, or a namespace wild card, and add the value, converted to a '.' separated name, to the graph with the given preferred value and appropriate type. If arg contains at least one '.' character and no '/' characters, assume it is a class name and add the value to the graph with the given preferred value and a type of Graph.CLASS.

Parameters:
arg - the name of the component to add to the graph
preferred - the preferred value of the component
Throws:
IllegalArgumentException - if arg is not a recognized form.

addRoots

void addRoots(Collection roots)
Add roots from this subtree of the graph to the given collection of roots. If this is a CLASS node and this node has a preferred value of false and was defined by a command line option or was supplied by the first JAR file, add this node to the collection.

Parameters:
roots - the collection to add to

reset

void reset()
For non-leaf nodes, Reset the type and implied preference value for this node and all child nodes recursively. If this is a leaf node, do nothing.


impliedChildPref

boolean impliedChildPref()
Return the implied preference as determined by the nearest parent namespace wildcard, or the default preference if there are no parent namespace wildcards. This method should only be called indirectly as a side-effect of calling childPref.

Returns:
the implied preferred value

childPref

boolean childPref()
Return the implied preference of the parent node if the parent is a package wildcard. Otherwise, return the implied preference as determined by the nearest parent namespace wildcard, or the default preference if there are no parent namespace wildcards.

Returns:
the implied preferred value

setDefaultPref

void setDefaultPref(boolean value)
Set the default preferred value for the graph. This method may only be called on the root node of the graph.

Parameters:
value - the default preferred value, which may be null to indicate that no default is defined

toString

public String toString()
Overrides:
toString in class Object

add

void add(String name,
         int type,
         File sourceJar)
Add a child node to this node having the given name. the name may have multiple '.' separated components, in which case a hierarchy of child nodes will be added. Only child nodes which do not already exist are created. The preferred value for the child will be set based on the value inherited from its ancestors. Nodes added by this method must always be leaf nodes.

Parameters:
name - the name of the child node to add
type - the node type, must be CLASS or RESOURCE
sourceJar - the JAR file causing this class or resource to be added

contains

boolean contains(String name)
Return true if the graph contains a node having the given name.

Parameters:
name - the name of the node to test for
Returns:
true if the node is found

isFrozen

boolean isFrozen(String name)
Return the frozen state of the class node having the given name. A node is frozen if it was created through the -api or -impl options.

Parameters:
name - the dot-separate name
Returns:
true if the node is frozen

addWithPreference

void addWithPreference(String name,
                       int type,
                       boolean preferred,
                       File sourceJar)
Add a child node to this node having the given name. the name may have multiple '.' separated components, in which case a hierarchy of child nodes will be added. Only child nodes which do not already exist are created. The final node created will be assigned the given type and preferred value. If the node already exists, new type and preferred values will be assigned unless the original source JAR is null, indicating node was created via the -api or -impl options. The source JAR is not changed from it's original value. If the node already exists, the original source JAR is non-null and is also not the first JAR, this is a leaf node, and preferred values differ, a warning message is written to System.err to identify the conflicting definitions; in this case, the state of the node is set to not-preferred. The first JAR is ignored in this case because the correct value for the preferred state of these classes is not known until the dependency analysis is done. Therefore in this special case the preferred state is reset to the new state.

Parameters:
name - the name of the child node to add

setPreferred

boolean setPreferred(String name,
                     boolean value,
                     boolean force)
Set the preferred state of the leaf (class) child node having the given '.' qualified name to the given value. If name is null, set the preferred value for this node. If the target name does not exist in the graph, this method is a noop.

This method enforces the requirement that the ultimate target node for the call must be a leaf node.

Parameters:
name - the name of the child node to set, or null to represent this node
value - the preferred value to set.
force - if true, ignore frozen state
Returns:
true if the preferred value matches value or is changed to value or if the named node does not exist in the graph. Returns false if the values don't match and the node is frozen and force is false.

countPreferred

int countPreferred(boolean value)
Count the number of child leaf nodes which have a preferred state matching the given value. If this is a leaf node, return 1 if the value matches the preferred state of this node.

Parameters:
value - the preferred value to search for
Returns:
the number of leaf nodes having the given value

countImpliedFailures

int countImpliedFailures()
Count the number of child leaf (class) nodes which have a preferred state which differs from the value implied by * its ancestors. If this is a leaf node and is also a nested class, return 1 if the outer class has a different preferred state than this class. If this is a leaf node and is not a nested class, return 1 if the implied value differs the preferred state for this node. If no value is implied by the ancestors, this is interpreted as a mismatch.

Returns:
the number of leaf nodes having preferred values which differ from that implied by its ancestors

countLeafNodes

int countLeafNodes()
Count the number of child leaf nodes.

Returns:
the number of leaf nodes which have this node as an ancestor.

getFullName

String getFullName()
Return the full name of this node expressed as a relative path. No suffixes (".class", "/-", "/*") are appended.

Returns:
the full path name representing this node

markStaleInnerClasses

void markStaleInnerClasses()
If this node is of type CLASS and is an inner class, mark this node as STALE if the outer class has the same preferred value. Print a warning to System.err if they do not have the same value. If there is no outer class corresponding to the inner class, this node is retained. Call this method on all children.


getOuter

PreferredListGen.Graph getOuter()
Return the node for the outer class for this class if this is a nested class.

Returns:
the graph for the outer class, or null if this is not a nested class, or if the outer class is not in the graph

deleteStaleNodes

void deleteStaleNodes()
Recursively remove child nodes of type STALE.


getChild

PreferredListGen.Graph getChild(String name)
Get the child node having the given name.

Parameters:
name - the child name
Returns:
the child Graph object, or null if the no child named name exists

containsLeavesOnly

boolean containsLeavesOnly()
Test whether all of the immediate child nodes of this node are leaf (class) nodes.

Returns:
true if all a the nodes are leaf nodes

addLeaves

void addLeaves(Collection leaves)

merge

void merge(PreferredListGen.Graph g)

addEntries

void addEntries(Collection entries)
Add preferred list entries (represented by PrefData objects) to the given Collection. One of the following cases is handled:


countEntries

int countEntries(int type,
                 Boolean pref)
Count the number of entries which would be created by this node for the given node type and preference. The type and impliedPref class attributes are side-affected by this method.

Parameters:
type - the node type to assign
pref - the node's implied preference value
Returns:
the number of entries generated by children


Copyright 2007-2010, multiple authors.
Licensed under the Apache License, Version 2.0, see the NOTICE file for attributions.