You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ml...@apache.org on 2006/07/25 10:54:56 UTC
svn commit: r425336 [2/3] - in
/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java:
java/beans/ java/beans/beancontext/ org/apache/harmony/beans/
Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/beancontext/BeanContextSupport.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/beancontext/BeanContextSupport.java?rev=425336&r1=425335&r2=425336&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/beancontext/BeanContextSupport.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/beancontext/BeanContextSupport.java Tue Jul 25 01:54:54 2006
@@ -36,40 +36,37 @@
import java.net.URL;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
-import java.util.List;
import java.util.Locale;
-import java.util.Set;
/**
* @author Sergei A. Krivenko
* @version $Revision: 1.17.2.3 $
*/
-public class BeanContextSupport extends BeanContextChildSupport
- implements BeanContext, Serializable, PropertyChangeListener,
+public class BeanContextSupport extends BeanContextChildSupport implements
+ BeanContext, Serializable, PropertyChangeListener,
VetoableChangeListener {
-
+
/**
* Nested class
- */
+ */
protected class BCSChild implements Serializable {
-
+
private static final long serialVersionUID = -5815286101609939109L;
-
+
/**
* @serial
*/
private Object child;
-
+
/**
* @serial
*/
private Object proxyPeer;
-
+
/**
* Construct an object not initially locked for editing
* with child and its peer
@@ -81,7 +78,7 @@
this.child = child;
this.proxyPeer = proxyPeer;
}
-
+
/**
* Return a child
*
@@ -91,17 +88,17 @@
return this.child;
}
}
-
+
/**
* Nested class
*/
protected static final class BCSIterator implements Iterator {
-
+
/**
*
*/
private Iterator it;
-
+
/**
* Construct an iterator
*
@@ -110,21 +107,21 @@
BCSIterator(HashMap map) {
this.it = map.values().iterator();
}
-
+
/**
*
*/
- public boolean hasNext() {
+ public boolean hasNext() {
return it.hasNext();
}
-
+
/**
*
*/
public Object next() {
return it.next();
}
-
+
/**
*
*/
@@ -137,21 +134,21 @@
*
*/
private static final long serialVersionUID = -4879613978649577204L;
-
+
/**
*
*/
- protected transient ArrayList bcmListeners;
+ protected transient ArrayList<BeanContextMembershipListener> bcmListeners;
/**
*
*/
- protected transient HashMap children;
+ protected transient HashMap<Object, BCSChild> children;
/**
* @serial
*/
- protected boolean designTime;
+ protected boolean designTime;
/**
* @serial
@@ -162,54 +159,54 @@
* @serial
*/
protected boolean okToUseGui;
-
+
/**
* @serial
*/
private int serializable;
-
+
/**
* A flag to show if this BeanContext is in a process of serializing
*/
private transient boolean serializing;
-
+
/**
* PropertyChangeListener of this BeanContext
*/
private transient PropertyChangeListener pcl;
-
+
/**
* VetoableChangeListener of this BeanContext
*/
private transient VetoableChangeListener vcl;
-
+
/**
* A flag to prevent the infinite roll backs
* while adding and removing children
*/
private boolean rollingBack = false;;
-
+
/**
* @com.intel.drl.spec_ref
*/
public BeanContextSupport() {
this(null, null, false, true);
}
-
+
/**
* @com.intel.drl.spec_ref
*/
public BeanContextSupport(BeanContext peer) {
this(peer, null, false, true);
}
-
+
/**
* @com.intel.drl.spec_ref
*/
public BeanContextSupport(BeanContext peer, Locale lcle) {
this(peer, lcle, false, true);
}
-
+
/**
* @com.intel.drl.spec_ref
*/
@@ -220,17 +217,17 @@
/**
* @com.intel.drl.spec_ref
*/
- public BeanContextSupport(BeanContext peer, Locale lcle, boolean dtime,
+ public BeanContextSupport(BeanContext peer, Locale lcle, boolean dtime,
boolean visible) {
-
+
super(peer);
-
+
// If locale is null, use default
this.locale = (lcle == null ? Locale.getDefault() : lcle);
-
+
this.designTime = dtime;
this.okToUseGui = visible;
-
+
// Initialize some values necessary for this class to operate
initialize();
}
@@ -239,41 +236,42 @@
* @com.intel.drl.spec_ref
*/
public boolean add(Object targetChild) {
-
+
// We must synchronize on BeanContext.globalHierarchyLock
- synchronized(BeanContext.globalHierarchyLock) {
-
+ synchronized (BeanContext.globalHierarchyLock) {
+
// Validate some values and states to check if we can proceed
if (!addChecks(targetChild)) {
return false;
}
-
+
try {
-
+
// If child is an instance of BeanContextChild or
// BeanContextProxy,
// invoke setBeanContext() method on this child
// and register BeanContext with its child
// on PropertyChangeListener and VetoableChangeListener.
BeanContextChild ch = getChildBeanContextChild(targetChild);
-
- if (ch != null) {
+
+ if (ch != null) {
ch.setBeanContext(getBeanContextPeer());
ch.addPropertyChangeListener(BEAN_CONTEXT, this.pcl);
ch.addVetoableChangeListener(BEAN_CONTEXT, this.vcl);
- }
- } catch(PropertyVetoException e) {
-
+ }
+ } catch (PropertyVetoException e) {
+
// Throw IllegalStateException, if PropertyVetoException occurs
throw new IllegalStateException(
- "PropertyVetoException was thrown while adding a child: " +
- targetChild + "; Original error message:" + e.getMessage());
- }
-
+ "PropertyVetoException was thrown while adding a child: "
+ + targetChild + "; Original error message:"
+ + e.getMessage());
+ }
+
// If child implements Visibility,
// set an appropriate type of ability to render GUI
Visibility vis = getChildVisibility(targetChild);
-
+
if (vis != null) {
if (this.okToUseGui) {
vis.okToUseGui();
@@ -281,17 +279,17 @@
vis.dontUseGui();
}
}
-
+
// Check if this child implements Serializable and increase
// the number of serializable children of the BeanContext
if (getChildSerializable(targetChild) != null) {
this.serializable++;
}
-
+
// Finally add child to the collection
addChild(targetChild);
}
-
+
return true;
}
@@ -299,19 +297,19 @@
* @com.intel.drl.spec_ref
*/
public boolean addAll(Collection c) {
-
- Collection col = new ArrayList();
-
+
+ Collection<Object> col = new ArrayList<Object>();
+
// Add children one by one
- for (Iterator i = c.iterator(); i.hasNext(); ) {
+ for (Iterator i = c.iterator(); i.hasNext();) {
try {
Object next = i.next();
-
+
if (add(next)) {
col.add(next);
}
- } catch(Exception e) {
-
+ } catch (Exception e) {
+
// Roll back changes but first check if it's already rolling
// back to avoid infinitive action
if (!this.rollingBack) {
@@ -320,30 +318,30 @@
} else {
this.rollingBack = false;
}
-
+
return false;
}
}
-
+
return true;
}
-
+
/**
* @com.intel.drl.spec_ref
*/
public void addBeanContextMembershipListener(
BeanContextMembershipListener bcml) {
-
+
// BeanContextMembershipListener canj not be null
if (bcml == null) {
throw new NullPointerException("Membership listener is null");
}
-
+
synchronized (this.bcmListeners) {
- this.bcmListeners.add(bcml);
+ this.bcmListeners.add(bcml);
}
}
-
+
/**
* Check if we can add this child to BeanContext
*
@@ -352,20 +350,20 @@
* @return true if we may continue
*/
private boolean addChecks(Object targetChild) {
-
+
// Child can not be null
if (targetChild == null) {
throw new IllegalArgumentException("Target child can not be null");
}
-
+
// Each child should appear only once in a given BeanContext
if (containsKey(targetChild)) {
return false;
}
-
- return validatePendingAdd(targetChild);
+
+ return validatePendingAdd(targetChild);
}
-
+
/**
* Add child to the collection
*
@@ -373,66 +371,66 @@
* @param fire - true if BeanContextMembershipEvent should be fired
*/
private void addChild(Object targetChild) {
-
+
// Add a new child using targetChild as a key and
// its BCSChild instance as an entry
- synchronized(this.children) {
+ synchronized (this.children) {
BCSChild ch = createBCSChild(targetChild, getBeanContextPeer());
- this.children.put(targetChild, ch);
+ this.children.put(targetChild, ch);
childJustAddedHook(targetChild, ch);
}
-
+
// Fire memebership event
fireChildrenAdded(getBCME(new Object[] { targetChild }));
}
-
+
/**
* @com.intel.drl.spec_ref
*/
public boolean avoidingGui() {
-
+
// Avoiding GUI means that
// GUI is needed but not allowed to use at this time
return (needsGui() && !this.okToUseGui);
}
-
+
/**
* @com.intel.drl.spec_ref
*/
protected Iterator bcsChildren() {
-
+
// Return Iterator containing children
// that are instances of BCSChild class
- synchronized(this.children) {
+ synchronized (this.children) {
return new BCSIterator(this.children);
}
}
-
+
/**
* @com.intel.drl.spec_ref
*/
- protected void bcsPreDeserializationHook(ObjectInputStream ois)
+ protected void bcsPreDeserializationHook(ObjectInputStream ois)
throws IOException, ClassNotFoundException {
-
- // Leave it for subclasses to implement
+
+ // Leave it for subclasses to implement
}
-
+
/**
* @com.intel.drl.spec_ref
*/
- protected void bcsPreSerializationHook(ObjectOutputStream oos)
+ protected void bcsPreSerializationHook(ObjectOutputStream oos)
throws IOException {
-
- // Leave it for subclasses to implement
+
+ // Leave it for subclasses to implement
}
/**
* @com.intel.drl.spec_ref
*/
- protected void childDeserializedHook(Object child,
+ protected void childDeserializedHook(Object child,
BeanContextSupport.BCSChild bcsc) {
-
- synchronized(this.children) {
+
+ synchronized (this.children) {
this.children.put(child, bcsc);
}
}
@@ -440,144 +438,141 @@
/**
* @com.intel.drl.spec_ref
*/
- protected void childJustAddedHook(Object child,
+ protected void childJustAddedHook(Object child,
BeanContextSupport.BCSChild bcsc) {
-
- // Leave it for subclasses to implement
+
+ // Leave it for subclasses to implement
}
-
+
/**
* @com.intel.drl.spec_ref
*/
- protected void childJustRemovedHook(Object child,
+ protected void childJustRemovedHook(Object child,
BeanContextSupport.BCSChild bcsc) {
-
- // Leave it for subclasses to implement
+
+ // Leave it for subclasses to implement
}
-
+
/**
* @com.intel.drl.spec_ref
*/
protected static final boolean classEquals(Class first, Class second) {
-
+
// Either classes themselves or their names should be equal
- return (first.equals(second) ?
- true :
- (first.getName().equals(second.getName()) ?
- true :
- false));
+ return (first.equals(second) ? true : (first.getName().equals(
+ second.getName()) ? true : false));
}
-
+
/**
* @com.intel.drl.spec_ref
*/
public void clear() {
throw new UnsupportedOperationException();
-
+
/*
- synchronized(BeanContext.globalHierarchyLock) {
- Collection col = new ArrayList();
-
- // Remove all children from BeanContext that are in the collection
- // one by one. This operation is successful if all the
- // removals succeeded
- for (Iterator i = iterator(); i.hasNext(); ) {
- try {
- Object next = i.next();
-
- if (remove(next)) {
- col.add(next);
- }
- } catch(Exception e) {
-
- // Roll back changes
- this.rollingBack = true;
- addAll(col);
- return;
- }
- }
- }*/
+ synchronized(BeanContext.globalHierarchyLock) {
+ Collection col = new ArrayList();
+
+ // Remove all children from BeanContext that are in the collection
+ // one by one. This operation is successful if all the
+ // removals succeeded
+ for (Iterator i = iterator(); i.hasNext(); ) {
+ try {
+ Object next = i.next();
+
+ if (remove(next)) {
+ col.add(next);
+ }
+ } catch(Exception e) {
+
+ // Roll back changes
+ this.rollingBack = true;
+ addAll(col);
+ return;
+ }
+ }
+ }*/
}
/**
* @com.intel.drl.spec_ref
*/
- public boolean contains(Object o) {
-
+ public boolean contains(Object o) {
+
// See if a given object can be found among
// the children's collection values
- synchronized(BeanContext.globalHierarchyLock) {
- synchronized (this.children) {
+ synchronized (BeanContext.globalHierarchyLock) {
+ synchronized (this.children) {
return this.children.containsKey(o);
}
}
}
-
+
/**
* @com.intel.drl.spec_ref
*/
public boolean containsAll(Collection c) {
-
+
// Iterate through the collection provided and find matches
// in the current BeanContext. If not found return false.
for (Iterator i = c.iterator(); i.hasNext();) {
Object next = i.next();
-
+
if (!contains(next)) {
return false;
}
}
-
+
// If we are here, all the elements of the collection are presented
// in this BeanContext
return true;
}
-
+
/**
* @com.intel.drl.spec_ref
*/
public boolean containsKey(Object o) {
-
+
// See if a given object can be found among
// the children's collection keys
- synchronized(BeanContext.globalHierarchyLock) {
+ synchronized (BeanContext.globalHierarchyLock) {
synchronized (this.children) {
return this.children.containsKey(o);
}
}
}
-
+
/**
* @com.intel.drl.spec_ref
*/
protected final Object[] copyChildren() {
-
+
// Make a copy of all children
- synchronized (this.children) {
+ synchronized (this.children) {
return this.children.entrySet().toArray();
}
}
-
+
/**
* @com.intel.drl.spec_ref
*/
protected BCSChild createBCSChild(Object targetChild, Object peer) {
-
+
// Create a child object with a reference to its peer
return new BCSChild(targetChild, peer);
}
-
+
/**
* @com.intel.drl.spec_ref
*/
- protected final void deserialize(ObjectInputStream ois, Collection coll)
+ protected final void deserialize(ObjectInputStream ois, Collection coll)
throws IOException, ClassNotFoundException {
-
+
// Read objects from output stream until "EOS" (the end of stream)
// mark is found. Place all the objects into collection provided
while (true) {
Object l = ois.readObject();
-
+
if (l != null && l.equals("EOS")) {
coll.add(l);
} else {
@@ -585,19 +580,19 @@
}
}
}
-
+
/**
* @com.intel.drl.spec_ref
*/
public synchronized void dontUseGui() {
-
+
// Command this BeanContext and its children not to use GUI
this.okToUseGui = false;
-
- for (Iterator it = iterator(); it.hasNext(); ) {
- Object next = it.next();
+
+ for (Iterator it = iterator(); it.hasNext();) {
+ Object next = it.next();
Visibility vis = getChildVisibility(next);
-
+
if (vis != null) {
vis.dontUseGui();
}
@@ -608,30 +603,30 @@
* @com.intel.drl.spec_ref
*/
protected final void fireChildrenAdded(BeanContextMembershipEvent bcme) {
-
+
// Notify all registered listenes about the change made
- for (Iterator i = bcmListeners.iterator(); i.hasNext(); ) {
- BeanContextMembershipListener cur =
- (BeanContextMembershipListener) i.next();
-
+ for (Iterator<BeanContextMembershipListener> i = bcmListeners
+ .iterator(); i.hasNext();) {
+ BeanContextMembershipListener cur = i.next();
+
cur.childrenAdded(bcme);
}
}
-
+
/**
* @com.intel.drl.spec_ref
*/
protected final void fireChildrenRemoved(BeanContextMembershipEvent bcme) {
-
+
// Notify all registered listenes about the change made
- for (Iterator i = bcmListeners.iterator(); i.hasNext(); ) {
- BeanContextMembershipListener cur =
- (BeanContextMembershipListener) i.next();
-
+ for (Iterator<BeanContextMembershipListener> i = bcmListeners
+ .iterator(); i.hasNext();) {
+ BeanContextMembershipListener cur = i.next();
+
cur.childrenRemoved(bcme);
}
}
-
+
/**
* Get BeanContextMembershipEvent class instance
*
@@ -642,7 +637,7 @@
private BeanContextMembershipEvent getBCME(Object[] changes) {
return new BeanContextMembershipEvent(getBeanContextPeer(), changes);
}
-
+
/**
* @com.intel.drl.spec_ref
*/
@@ -655,12 +650,12 @@
*/
protected static final BeanContextChild getChildBeanContextChild(
Object child) {
-
+
// There's nothing to do here if the child is null
if (child == null) {
return null;
}
-
+
// See if the child implements BeanContextChild or BeanContextProxy.
// Cast it to appropriate class or simply return null
if (child instanceof BeanContextChild) {
@@ -675,9 +670,9 @@
/**
* @com.intel.drl.spec_ref
*/
- protected static final BeanContextMembershipListener
- getChildBeanContextMembershipListener(Object child) {
-
+ protected static final BeanContextMembershipListener getChildBeanContextMembershipListener(
+ Object child) {
+
// See if child implements BeanContextMembershipListener.
// Cast it to BeanContextMembershipListener if it does
// or return null otherwise
@@ -691,9 +686,9 @@
/**
* @com.intel.drl.spec_ref
*/
- protected static final PropertyChangeListener
- getChildPropertyChangeListener(Object child) {
-
+ protected static final PropertyChangeListener getChildPropertyChangeListener(
+ Object child) {
+
// See if child implements PropertyChangeListener.
// Cast it to PropertyChangeListener if it does
// or return null otherwise
@@ -703,12 +698,12 @@
return null;
}
}
-
+
/**
* @com.intel.drl.spec_ref
*/
protected static final Serializable getChildSerializable(Object child) {
-
+
// See if child implements Serializable.
// Cast it to Serializable if it does
// or return null otherwise
@@ -718,13 +713,13 @@
return null;
}
}
-
+
/**
* @com.intel.drl.spec_ref
*/
- protected static final VetoableChangeListener
- getChildVetoableChangeListener(Object child) {
-
+ protected static final VetoableChangeListener getChildVetoableChangeListener(
+ Object child) {
+
// See if child implements VetoableChangeListener.
// Cast it to VetoableChangeListener if it does
// or return null otherwise
@@ -734,12 +729,12 @@
return null;
}
}
-
+
/**
* @com.intel.drl.spec_ref
*/
protected static final Visibility getChildVisibility(Object child) {
-
+
// See if child implements Visibility.
// Cast it to Visibility if it does
// or return null otherwise
@@ -749,14 +744,14 @@
return null;
}
}
-
+
/**
* @com.intel.drl.spec_ref
*/
public synchronized Locale getLocale() {
return this.locale;
}
-
+
/**
* Construct PropertyChangeEvent object and return
*
@@ -766,34 +761,33 @@
*
* @return PropertyChangeEvent object
*/
- private PropertyChangeEvent getPCE(String prop, Object o, Object n) {
- return new PropertyChangeEvent(getBeanContextPeer(), prop, o, n);
- }
-
+ // private PropertyChangeEvent getPCE(String prop, Object o, Object n) {
+ // return new PropertyChangeEvent(getBeanContextPeer(), prop, o, n);
+ // }
/**
* @com.intel.drl.spec_ref
*/
public URL getResource(String name, BeanContextChild bcc) {
-
+
// The resource name should not be null
- if(name == null) {
+ if (name == null) {
throw new NullPointerException("Resource name can not be null");
}
-
+
// The child should not be null
- if(bcc == null) {
+ if (bcc == null) {
throw new NullPointerException("The child can not be null");
}
-
+
// Load resource using the same ClassLoader as BeanContextChild specified
// If NullPointerException occurs try to load it as system resource
try {
return bcc.getClass().getClassLoader().getResource(name);
- } catch(NullPointerException e) {
+ } catch (NullPointerException e) {
try {
return ClassLoader.getSystemResource(name);
- } catch(Exception ex) {
-
+ } catch (Exception ex) {
+
// We tried our best but still failed
throw new IllegalArgumentException("Invalid resource");
}
@@ -804,58 +798,58 @@
* @com.intel.drl.spec_ref
*/
public InputStream getResourceAsStream(String name, BeanContextChild bcc) {
-
+
// The resource name should not be null
- if(name == null) {
+ if (name == null) {
throw new NullPointerException("Resource name can not be null");
}
-
+
// The child should not be null
- if(bcc == null) {
+ if (bcc == null) {
throw new NullPointerException("The child can not be null");
}
-
+
// Load resource using the same ClassLoader as BeanContextChild specified
// If NullPointerException occurs try to load it as system resource
try {
return bcc.getClass().getClassLoader().getResourceAsStream(name);
- } catch(NullPointerException e) {
+ } catch (NullPointerException e) {
try {
return ClassLoader.getSystemResourceAsStream(name);
- } catch(Exception ex) {
-
+ } catch (Exception ex) {
+
// No success at all
throw new IllegalArgumentException("Invalid resource");
}
}
}
-
+
/**
* @com.intel.drl.spec_ref
*/
protected synchronized void initialize() {
-
+
// Initialize some fields
- this.children = new HashMap();
- this.bcmListeners = new ArrayList();
-
+ this.children = new HashMap<Object, BCSChild>();
+ this.bcmListeners = new ArrayList<BeanContextMembershipListener>();
+
this.serializable = 0;
this.serializing = false;
-
+
// Initialize PropertyChangeListener
this.pcl = new PropertyChangeListener() {
-
+
public void propertyChange(PropertyChangeEvent pce) {
BeanContextSupport.this.propertyChange(pce);
}
};
-
+
// Initialize VetoableChangeListener
this.vcl = new VetoableChangeListener() {
-
- public void vetoableChange(PropertyChangeEvent pce)
+
+ public void vetoableChange(PropertyChangeEvent pce)
throws PropertyVetoException {
-
+
BeanContextSupport.this.vetoableChange(pce);
}
};
@@ -864,13 +858,12 @@
/**
* @com.intel.drl.spec_ref
*/
- public Object instantiateChild(String beanName)
- throws IOException, ClassNotFoundException {
-
+ public Object instantiateChild(String beanName) throws IOException,
+ ClassNotFoundException {
+
// Use Beans convenience method to instantiate the bean
- return Beans.instantiate(getBeanContextPeer().getClass().getClassLoader(),
- beanName,
- getBeanContextPeer());
+ return Beans.instantiate(getBeanContextPeer().getClass()
+ .getClassLoader(), beanName, getBeanContextPeer());
}
/**
@@ -884,15 +877,15 @@
* @com.intel.drl.spec_ref
*/
public boolean isEmpty() {
-
+
// See if there are any children in this BeanContext
- synchronized(BeanContext.globalHierarchyLock) {
- synchronized(this.children) {
+ synchronized (BeanContext.globalHierarchyLock) {
+ synchronized (this.children) {
return this.children.isEmpty();
}
}
}
-
+
/**
* @com.intel.drl.spec_ref
*/
@@ -904,38 +897,38 @@
* @com.intel.drl.spec_ref
*/
public Iterator iterator() {
-
- synchronized(BeanContext.globalHierarchyLock) {
- synchronized(this.children) {
+
+ synchronized (BeanContext.globalHierarchyLock) {
+ synchronized (this.children) {
return this.children.keySet().iterator();
}
}
}
-
+
/**
* @com.intel.drl.spec_ref
*/
public synchronized boolean needsGui() {
-
+
// BeanContext needs GUI if at least one its children needs it.
// We may check it by trying to cast each child to Visibility
// and see it needs GUI.
// A child definitely needs GUI if it implements Component
- for (Iterator it = iterator(); it.hasNext(); ) {
- Object next = it.next();
+ for (Iterator it = iterator(); it.hasNext();) {
+ Object next = it.next();
Visibility vis = getChildVisibility(next);
-
+
if (vis != null) {
if (vis.needsGui()) {
return true;
}
- }
-
+ }
+
if (next instanceof java.awt.Component) {
return true;
}
}
-
+
return false;
}
@@ -943,125 +936,125 @@
* @com.intel.drl.spec_ref
*/
public synchronized void okToUseGui() {
-
+
// Notify this BeanContext and its children that it's OK now to use GUI
this.okToUseGui = true;
-
- for (Iterator it = iterator(); it.hasNext(); ) {
- Object next = it.next();
+
+ for (Iterator it = iterator(); it.hasNext();) {
+ Object next = it.next();
Visibility vis = getChildVisibility(next);
-
+
if (vis != null) {
vis.okToUseGui();
}
}
}
-
+
/**
* @com.intel.drl.spec_ref
*/
public void propertyChange(PropertyChangeEvent pce) {
-
+
if (!pce.getPropertyName().equals(BEAN_CONTEXT)) {
return;
}
-
+
Object source = pce.getSource();
-
+
if (source instanceof BCSChild) {
BCSChild ch = (BCSChild) source;
- Object newValue = pce.getNewValue();
-
+ Object newValue = pce.getNewValue();
+
if (!newValue.equals(this.getBeanContextPeer())) {
remove(ch.getChild(), false);
}
}
}
-
+
/**
* @com.intel.drl.spec_ref
*/
- public final void readChildren(ObjectInputStream ois)
- throws IOException, ClassNotFoundException {
-
+ public final void readChildren(ObjectInputStream ois) throws IOException,
+ ClassNotFoundException {
+
// Deserialize children
- for (int i = 0; i < this.serializable; i++) {
+ for (int i = 0; i < this.serializable; i++) {
BCSChild bChild = (BCSChild) ois.readObject();
childDeserializedHook(bChild.getChild(), bChild);
}
}
-
+
/**
* @com.intel.drl.spec_ref
*/
- private void readObject(ObjectInputStream ois)
- throws IOException, ClassNotFoundException {
-
- synchronized(BeanContext.globalHierarchyLock) {
- ois.defaultReadObject();
- initialize();
+ private void readObject(ObjectInputStream ois) throws IOException,
+ ClassNotFoundException {
+
+ synchronized (BeanContext.globalHierarchyLock) {
+ ois.defaultReadObject();
+ initialize();
bcsPreDeserializationHook(ois);
-
+
// Deserialize children
if (!getBeanContextPeer().equals(this)) {
readChildren(ois);
}
-
+
// Deserialize listeners
- synchronized(this.bcmListeners) {
+ synchronized (this.bcmListeners) {
deserialize(ois, this.bcmListeners);
- }
+ }
}
}
-
+
/**
* @com.intel.drl.spec_ref
*/
public boolean remove(Object targetChild) {
return remove(targetChild, true);
}
-
+
/**
* @com.intel.drl.spec_ref
*/
protected boolean remove(Object targetChild, boolean callChildSetBC) {
-
- synchronized(BeanContext.globalHierarchyLock) {
-
+
+ synchronized (BeanContext.globalHierarchyLock) {
+
// Make necessary checks
if (!validatePendingRemove(targetChild)) {
return false;
}
-
+
Object removed = null;
-
- if (containsKey(targetChild)) {
-
+
+ if (containsKey(targetChild)) {
+
// Remove the reference to the BeanContext from the child
// if ordered to do so
if (callChildSetBC) {
removeFromContext(targetChild);
}
-
- synchronized(this.children) {
-
+
+ synchronized (this.children) {
+
// Just before removing save a reference to it for later use
// in childJustRemovedHook() method
- BCSChild ch = (BCSChild) this.children.get(targetChild);
+ BCSChild ch = this.children.get(targetChild);
removed = this.children.remove(targetChild);
childJustRemovedHook(targetChild, ch);
}
-
+
// Fire the event
fireChildrenRemoved(getBCME(new Object[] { targetChild }));
-
+
// Check if this child implements Serializable and decrease
// the number of serializable children of BeanContext
if (getChildSerializable(targetChild) != null) {
this.serializable--;
}
}
-
+
return (removed != null);
}
}
@@ -1072,54 +1065,54 @@
public boolean removeAll(Collection c) {
throw new UnsupportedOperationException();
/*
- synchronized(BeanContext.globalHierarchyLock) {
- Collection col = new ArrayList();
-
- // Remove all children from BeanContext that are in the collection
- // one by one. This operation is successful if all the
- // removals succeeded
- for (Iterator i = c.iterator(); i.hasNext(); ) {
- try {
- Object next = i.next();
-
- if (remove(next)) {
- col.add(next);
- }
- } catch(Exception e) {
-
- // Roll back changes but first check if it's already rolling
- // back to avoid infinitive action
- if (!this.rollingBack) {
- this.rollingBack = true;
- addAll(col);
- } else {
- this.rollingBack = false;
- }
-
- return false;
- }
- }
- }
-
- return true;*/
+ synchronized(BeanContext.globalHierarchyLock) {
+ Collection col = new ArrayList();
+
+ // Remove all children from BeanContext that are in the collection
+ // one by one. This operation is successful if all the
+ // removals succeeded
+ for (Iterator i = c.iterator(); i.hasNext(); ) {
+ try {
+ Object next = i.next();
+
+ if (remove(next)) {
+ col.add(next);
+ }
+ } catch(Exception e) {
+
+ // Roll back changes but first check if it's already rolling
+ // back to avoid infinitive action
+ if (!this.rollingBack) {
+ this.rollingBack = true;
+ addAll(col);
+ } else {
+ this.rollingBack = false;
+ }
+
+ return false;
+ }
+ }
+ }
+
+ return true;*/
}
-
+
/**
* @com.intel.drl.spec_ref
*/
public void removeBeanContextMembershipListener(
BeanContextMembershipListener bcml) {
-
+
// BeanContextMembershipListener can not be null
if (bcml == null) {
throw new NullPointerException("Membership listener is null");
}
-
- synchronized(this.bcmListeners) {
+
+ synchronized (this.bcmListeners) {
this.bcmListeners.remove(bcml);
}
}
-
+
/**
* Separate BeanContext and its child that had just been removed
* by removing all references to each other
@@ -1128,24 +1121,24 @@
*/
private void removeFromContext(Object targetChild) {
try {
-
+
// If child is an instance of BeanContextChild or BeanContextProxy,
// invoke setBeanContext() method on this child
// with null parameter
BeanContextChild ch = getChildBeanContextChild(targetChild);
-
- if (ch != null) {
- ch.setBeanContext(null);
+
+ if (ch != null) {
+ ch.setBeanContext(null);
ch.removePropertyChangeListener(BEAN_CONTEXT, this.pcl);
ch.removeVetoableChangeListener(BEAN_CONTEXT, this.vcl);
- }
- } catch(PropertyVetoException e) {
-
+ }
+ } catch (PropertyVetoException e) {
+
// Required by spec
throw new IllegalStateException(
- "PropertyVetoException was thrown while removing " +
- " a child: " + targetChild + "; " +
- "Original error message:" + e.getMessage());
+ "PropertyVetoException was thrown while removing "
+ + " a child: " + targetChild + "; "
+ + "Original error message:" + e.getMessage());
}
}
@@ -1154,120 +1147,120 @@
*/
public boolean retainAll(Collection c) {
throw new UnsupportedOperationException();
-
+
/*
- synchronized(BeanContext.globalHierarchyLock) {
- synchronized(this.children) {
-
- Collection col = new ArrayList();
-
- // Remove all children from BeanContext that are not in the
- // collection
- // This operation is successful if all the removals succeeded
- for (Iterator i = iterator(); i.hasNext(); ) {
- Object nextKey = i.next();
- Object nextValue = this.children.get(nextKey);
-
- if (!c.contains(nextKey) && !c.contains(nextValue)) {
- try {
- if (remove(nextKey)) {
- col.add(nextKey);
- }
- } catch(Exception e) {
-
- // Roll back changes
- this.rollingBack = true;
- addAll(col);
- return false;
- }
- }
- }
- }
- }
-
- return true;*/
+ synchronized(BeanContext.globalHierarchyLock) {
+ synchronized(this.children) {
+
+ Collection col = new ArrayList();
+
+ // Remove all children from BeanContext that are not in the
+ // collection
+ // This operation is successful if all the removals succeeded
+ for (Iterator i = iterator(); i.hasNext(); ) {
+ Object nextKey = i.next();
+ Object nextValue = this.children.get(nextKey);
+
+ if (!c.contains(nextKey) && !c.contains(nextValue)) {
+ try {
+ if (remove(nextKey)) {
+ col.add(nextKey);
+ }
+ } catch(Exception e) {
+
+ // Roll back changes
+ this.rollingBack = true;
+ addAll(col);
+ return false;
+ }
+ }
+ }
+ }
+ }
+
+ return true;*/
}
/**
* @com.intel.drl.spec_ref
*/
- protected final void serialize(ObjectOutputStream oos, Collection coll)
+ protected final void serialize(ObjectOutputStream oos, Collection coll)
throws IOException {
-
+
// Write the collection into ObjectOutputStream
- for (Iterator it = coll.iterator(); it.hasNext(); ) {
+ for (Iterator it = coll.iterator(); it.hasNext();) {
Object l = it.next();
-
+
if (getChildSerializable(l) != null) {
oos.writeObject(l);
}
}
-
+
// Mark the end of stream
oos.writeObject("EOS");
}
-
+
/**
* @com.intel.drl.spec_ref
*/
public synchronized void setDesignTime(boolean dTime) {
-
+
boolean old = this.designTime;
this.designTime = dTime;
-
+
// Notify BeanContext about this change
firePropertyChange("designTime", new Boolean(old), new Boolean(dTime));
}
-
+
/**
* @com.intel.drl.spec_ref
*/
- public synchronized void setLocale(Locale newLocale)
+ public synchronized void setLocale(Locale newLocale)
throws PropertyVetoException {
-
+
// Use default locale if a new value is null
- newLocale = (newLocale == null ? Locale.getDefault() : newLocale);
-
+ newLocale = (newLocale == null ? Locale.getDefault() : newLocale);
+
// Notify BeanContext about this change
Locale old = (Locale) this.locale.clone();
this.locale = newLocale;
firePropertyChange("locale", old, newLocale);
}
-
+
/**
* @com.intel.drl.spec_ref
*/
public int size() {
-
+
// Return the number of children of this BeanContext
- synchronized(BeanContext.globalHierarchyLock) {
- synchronized(this.children) {
+ synchronized (BeanContext.globalHierarchyLock) {
+ synchronized (this.children) {
return this.children.size();
}
}
}
-
+
/**
* @com.intel.drl.spec_ref
*/
public Object[] toArray() {
-
+
// Convert the collection of children to array
- synchronized(BeanContext.globalHierarchyLock) {
- synchronized(this.children) {
+ synchronized (BeanContext.globalHierarchyLock) {
+ synchronized (this.children) {
return this.children.keySet().toArray();
}
}
}
-
+
/**
* @com.intel.drl.spec_ref
*/
public Object[] toArray(Object[] arry) {
-
+
// Convert the collection of children to array
- synchronized(BeanContext.globalHierarchyLock) {
- synchronized(this.children) {
+ synchronized (BeanContext.globalHierarchyLock) {
+ synchronized (this.children) {
return this.children.keySet().toArray(arry);
}
}
@@ -1277,73 +1270,72 @@
* @com.intel.drl.spec_ref
*/
protected boolean validatePendingAdd(Object targetChild) {
-
-
+
return true;
}
-
+
/**
* @com.intel.drl.spec_ref
*/
protected boolean validatePendingRemove(Object targetChild) {
-
+
if (targetChild == null) {
throw new IllegalArgumentException("Target child is null");
}
-
+
return true;
}
-
+
/**
* @com.intel.drl.spec_ref
*/
- public void vetoableChange(PropertyChangeEvent pce)
+ public void vetoableChange(PropertyChangeEvent pce)
throws PropertyVetoException {
-
+
if (pce == null) {
throw new NullPointerException("The event is null");
}
-
+
}
-
+
/**
* @com.intel.drl.spec_ref
*/
public final void writeChildren(ObjectOutputStream oos) throws IOException {
-
+
// Write serializable children to ObjectOutputStream
- synchronized(this.children) {
- for (Iterator it = iterator(); it.hasNext(); ) {
+ synchronized (this.children) {
+ for (Iterator it = iterator(); it.hasNext();) {
Object next = it.next();
-
+
if (getChildSerializable(next) != null) {
oos.writeObject(this.children.get(next));
}
}
}
}
-
+
/**
* @com.intel.drl.spec_ref
*/
- private void writeObject(ObjectOutputStream oos)
- throws IOException, ClassNotFoundException {
-
- synchronized(BeanContext.globalHierarchyLock) {
- this.serializing = true;
- oos.defaultWriteObject();
-
- bcsPreSerializationHook(oos);
-
+ private void writeObject(ObjectOutputStream oos) throws IOException,
+ ClassNotFoundException {
+
+ synchronized (BeanContext.globalHierarchyLock) {
+ this.serializing = true;
+ oos.defaultWriteObject();
+
+ bcsPreSerializationHook(oos);
+
if (!this.getBeanContextPeer().equals(this)) {
- writeChildren(oos);
+ writeChildren(oos);
}
-
- synchronized(this.bcmListeners) {
+
+ synchronized (this.bcmListeners) {
serialize(oos, this.bcmListeners);
}
-
+
this.serializing = false;
}
- }
+ }
}
Modified: incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/Command.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/Command.java?rev=425336&r1=425335&r2=425336&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/Command.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/Command.java Tue Jul 25 01:54:54 2006
@@ -29,8 +29,6 @@
import java.beans.XMLDecoder;
import java.beans.Expression;
-import java.io.File;
-import java.io.FileOutputStream;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
@@ -46,33 +44,50 @@
*/
public class Command {
-
+
private static int INITIALIZED = 0;
+
private static int CHILDREN_FILTERED = 1;
+
private static int COMMAND_EXECUTED = 2;
+
private static int CHILDREN_PROCESSED = 3;
-
+
private String tagName; // tag name
+
private HashMap attrs; // set of attrs
+
private String data = null; // string data
- private Vector commands = new Vector(); // inner commands
- private Vector arguments = new Vector(); // arguments
- private Vector operations = new Vector(); // operations
- private Vector auxArguments = new Vector(); // additonal arguments placed before others
+
+ private Vector<Command> commands = new Vector<Command>(); // inner commands
+
+ private Vector<Command> arguments = new Vector<Command>(); // arguments
+
+ private Vector<Command> operations = new Vector<Command>(); // operations
+
+ // additonal arguments placed before others
+ private Vector<Argument> auxArguments = new Vector<Argument>();
+
private Argument result = null; // result argument
+
private Object target = null; // target to execute a command on
+
private String methodName = null; // method name
+
private Command ctx = null; // context for command
+
private int status; // commands
- private int tabCount = 0;
+
private XMLDecoder decoder;
-
+
+ //private int tabCount = 0;
+
public Command(String tagName, HashMap attrs) {
this.tagName = tagName;
this.attrs = attrs;
this.status = initializeStatus(tagName);
}
-
+
public Command(XMLDecoder decoder, String tagName, HashMap attrs) {
this.decoder = decoder;
this.tagName = tagName;
@@ -84,68 +99,74 @@
public void setData(String data) {
this.data = data;
}
-
+
// set tab count to display log messages
- public void setTabCount(int tabCount) {
- this.tabCount = tabCount;
- }
-
+ // public void setTabCount(int tabCount) {
+ // this.tabCount = tabCount;
+ // }
+
// set context - upper level command
public void setContext(Command ctx) {
this.ctx = ctx;
}
-
+
// add child command
public void addChild(Command cmd) {
- if(cmd != null) {
+ if (cmd != null) {
cmd.setContext(this);
commands.add(cmd);
}
}
-
+
// remove child command
public void removeChild(Command cmd) {
- if((cmd != null) && commands.remove(cmd))
+ if ((cmd != null) && commands.remove(cmd))
cmd.setContext(null);
}
-
+
// command status
public int getStatus() {
return status;
}
-
+
// check if one of arguments or operations is unresolved
private boolean isResolved() {
- if(getStatus() < Command.CHILDREN_PROCESSED) {
+ if (getStatus() < Command.CHILDREN_PROCESSED) {
return false;
} else {
- for(int i = 0; i < arguments.size(); ++i) {
- Command arg = (Command) arguments.elementAt(i);
- if(!arg.isResolved()) return false;
- }
- for(int j = 0; j < operations.size(); ++j) {
- Command opr = (Command) operations.elementAt(j);
- if(!opr.isResolved()) return false;
+ for (int i = 0; i < arguments.size(); ++i) {
+ Command arg = arguments.elementAt(i);
+
+ if (!arg.isResolved()) {
+ return false;
+ }
+ }
+ for (int j = 0; j < operations.size(); ++j) {
+ Command opr = operations.elementAt(j);
+
+ if (!opr.isResolved()) {
+ return false;
+ }
}
return true;
}
}
-
+
// execute command and return execution flags
public int exec(HashMap references) throws Exception {
//System.out.println("in exec() status = " + translateStatus(status) + "...");
- if(status < Command.CHILDREN_PROCESSED) {
- if(status < Command.COMMAND_EXECUTED) {
- if(status < Command.CHILDREN_FILTERED) {
+ if (status < Command.CHILDREN_PROCESSED) {
+ if (status < Command.COMMAND_EXECUTED) {
+ if (status < Command.CHILDREN_FILTERED) {
status = doBeforeRun(references);
//System.out.println("after doBeforeRun() status = " + translateStatus(status));
}
- if(status == Command.CHILDREN_FILTERED) {
+ if (status == Command.CHILDREN_FILTERED) {
status = doRun(references);
//System.out.println("after doRun() status = " + translateStatus(status));
}
}
- if(status == Command.COMMAND_EXECUTED) {
+ if (status == Command.COMMAND_EXECUTED) {
status = doAfterRun(references);
//System.out.println("after doAfterRun() status = " + translateStatus(status));
}
@@ -153,129 +174,135 @@
//System.out.println("...out of exec() status = " + translateStatus(status));
return status;
}
-
+
// execute commands in backtrack order
public boolean backtrack(HashMap references) throws Exception {
- for(int i = 0; i < arguments.size(); ++i) {
- Command arg = (Command) arguments.elementAt(i);
+ for (int i = 0; i < arguments.size(); ++i) {
+ Command arg = arguments.elementAt(i);
+
arg.backtrack(references);
}
- for(int i = 0; i < operations.size(); ++i) {
- Command opr = (Command) operations.elementAt(i);
+ for (int i = 0; i < operations.size(); ++i) {
+ Command opr = operations.elementAt(i);
+
opr.backtrack(references);
}
- if(status == Command.CHILDREN_FILTERED) {
+ if (status == Command.CHILDREN_FILTERED) {
status = doRun(references);
}
- if(status == Command.COMMAND_EXECUTED) {
+ if (status == Command.COMMAND_EXECUTED) {
status = doAfterRun(references);
return (getStatus() == Command.CHILDREN_PROCESSED);
} else {
return false;
}
}
-
- // put command in one of two collections - arguments ot operations
+
+ // put command in one of two collections - arguments or operations
private int doBeforeRun(HashMap references) throws Exception {
- if(status == Command.INITIALIZED) {
- for(int i = 0; i < commands.size(); ++i) {
- Command cmd = (Command) commands.elementAt(i);
- Vector dest = operations;
- if(cmd.isExecutable()) {
- dest = arguments;
+ if (status == Command.INITIALIZED) {
+ for (int i = 0; i < commands.size(); ++i) {
+ Command cmd = commands.elementAt(i);
+
+ // XXX is this correct?
+ if (cmd.isExecutable()) {
+ arguments.add(cmd);
+ } else {
+ operations.add(cmd);
}
- dest.add(cmd);
}
return Command.CHILDREN_FILTERED;
} else {
return status;
}
}
-
+
// run command
private int doRun(HashMap references) throws Exception {
- if(status == Command.CHILDREN_FILTERED) {
- if(isRoot()) {
- result = new Argument(decoder);
- //System.out.println("doRun(): result is decoder...");
- return Command.COMMAND_EXECUTED;
+ if (status == Command.CHILDREN_FILTERED) {
+ if (isRoot()) {
+ result = new Argument(decoder);
+ //System.out.println("doRun(): result is decoder...");
+ return Command.COMMAND_EXECUTED;
}
-
- if(isNull()) {
+
+ if (isNull()) {
result = new Argument(null);
//System.out.println("doRun(): result is null...");
return Command.COMMAND_EXECUTED;
}
-
- if(ctx != null && ctx.isArray() && (ctx.getResultValue() == null)
+
+ if (ctx != null && ctx.isArray() && (ctx.getResultValue() == null)
&& !isExecutable()) {
//System.out.println("doRun(): context is array...");
return status;
}
-
+
Object target = getTarget(references);
- if(target == null) {
+ if (target == null) {
//System.out.println("doRun(): target is null...");
return status;
} else {
- if(target instanceof Class) {
+ if (target instanceof Class) {
//System.out.println("doRun(): target = " + ((Class)target).getName());
} else {
//System.out.println("doRun(): target = " + target.getClass().getName());
}
- if(isReference()) {
+ if (isReference()) {
result = getReferencedArgument(references);
//System.out.println("doRun(): reference - result is " + result.getType());
} else {
String methodName = getMethodName(references);
//System.out.println("doRun(): methodName = " + methodName);
Argument[] arguments = getArguments();
- if(arguments == null) return status;
+ if (arguments == null)
+ return status;
//System.out.println("doRun(): args number is " + arguments.length);
- for(int i = 0; i < arguments.length; ++i) {
- if(arguments[i] != null) {
+ for (int i = 0; i < arguments.length; ++i) {
+ if (arguments[i] != null) {
//System.out.println("doRun(): arg [" + i + "] = " + arguments[i].getType());
} else {
//System.out.println("doRun(): arg [" + i + "] = null");
}
}
-
+
Expression expr = new Expression(target, methodName,
getArgumentsValues());
result = new Argument(expr.getValue());
-
- if(isPrimitiveClassName(getTagName()))
+
+ if (isPrimitiveClassName(getTagName()))
result.setType(getPrimitiveClass(tagName));
-
+
//System.out.println("doRun(): method call - result is " + result.getType());
}
return Command.COMMAND_EXECUTED;
}
- } else return status;
+ } else
+ return status;
}
-
+
// run child commands
private int doAfterRun(HashMap references) throws Exception {
- if(status == Command.COMMAND_EXECUTED) {
+ if (status == Command.COMMAND_EXECUTED) {
//System.out.println("doAfterRun(): command " + getResultType() + " processed...");
- Vector toBeRemoved = new Vector();
+ Vector<Command> toBeRemoved = new Vector<Command>();
try {
Statement[] statements = null;
-
- for(int i = 0; i < operations.size(); ++i) {
- Command cmd = (Command) operations.elementAt(i);
-
- if(cmd.isArrayElement()) {
-
- if(cmd.isResolved()) {
- if(statements == null) {
+
+ for (int i = 0; i < operations.size(); ++i) {
+ Command cmd = operations.elementAt(i);
+
+ if (cmd.isArrayElement()) {
+
+ if (cmd.isResolved()) {
+ if (statements == null) {
statements = new Statement[operations.size()];
}
statements[i] = new Statement(getResultValue(),
- "set", new Object[] {
- new Integer(i), cmd.getResultValue()} );
- if( (i + 1) == operations.size() ) {
- for(int j = 0; j < operations.size(); ++j) {
+ "set", new Object[] { new Integer(i),
+ cmd.getResultValue() });
+ if ((i + 1) == operations.size()) {
+ for (int j = 0; j < operations.size(); ++j) {
statements[j].execute();
}
toBeRemoved.addAll(operations);
@@ -283,59 +310,60 @@
} else {
break;
}
-
+
} else {
// since the call is Array.set()
- if(!isArray()) {
+ if (!isArray()) {
cmd.setTarget(getResultValue());
}
cmd.exec(references);
-
- if(cmd.isResolved()) {
+
+ if (cmd.isResolved()) {
//System.out.println("doAfterRun(): cmd = " + cmd.methodName + " is resolved");
toBeRemoved.add(cmd);
} else {
//System.out.println("doAfterRun(): cmd = " + cmd.methodName + " is unresolved");
break;
}
-
+
}
-
+
}
} catch (Exception e) {
throw new Exception(e);
} finally {
operations.removeAll(toBeRemoved);
}
-
- if(operations.size() == 0) {
+
+ if (operations.size() == 0) {
//System.out.println("doAfterRun(): command " + getResultType() + " completely processed.");
} else {
//System.out.println("doAfterRun(): command " + getResultType() + " contains incomplete " +
- //operations.size() + " commands.");
+ //operations.size() + " commands.");
}
return (operations.size() == 0) ? Command.CHILDREN_PROCESSED
: status;
- } else return status;
+ } else
+ return status;
}
-
+
// Result accessors
-
+
// Return result - Argument class
public Argument getResult() {
return result;
}
-
+
// Returns result value
public Object getResultValue() {
return (result != null) ? result.getValue() : null;
}
-
+
// Returns result type
public Class getResultType() throws ClassNotFoundException {
return (result != null) ? result.getType() : null;
}
-
+
// accessors to XML tags and attrs
public boolean hasAttr(String name) {
return attrs.get(name) != null;
@@ -361,101 +389,99 @@
private boolean isPrimitive() {
return isPrimitiveClassName(tagName);
}
-
+
// Checks if the object is constructor
private boolean isConstructor() {
return isPrimitive() || !isStaticMethod() && !isMethod()
&& !isProperty() && !isField() && !isArray() && !isReference();
}
-
+
// Checks if the command is static method
private boolean isStaticMethod() {
return isTag("object") && hasAttr("method") || isTag("class");
}
-
+
// Checks if the command is public method
private boolean isMethod() {
return isTag("void") && (hasAttr("method") || hasAttr("index"));
}
-
+
// Check if the command relates to property - getter ot setter depends on
// number of args
private boolean isProperty() {
return isTag("void") && hasAttr("property");
}
-
+
// Check if the command is field accessor
private boolean isField() {
return isTag("object") && hasAttr("field");
}
-
+
// Check if the command is array
private boolean isArray() {
return isTag("array");
}
-
+
// Check if the command is array element
private boolean isArrayElement() {
return (ctx != null) && (ctx.isArray()) && isExecutable();
}
-
+
private boolean isReference() {
return hasAttr("idref");
}
-
+
// Check if the command is root object
private boolean isRoot() {
return isTag("java");
}
-
+
// Check if the command is null
private boolean isNull() {
return isTag("null");
}
-
+
// Checks if the command could generate object
public boolean isExecutable() {
- boolean result = isTag("object") || isTag("void")
- && hasAttr("class") && hasAttr("method")
- || isTag("array") || isPrimitive()
+ boolean result = isTag("object") || isTag("void") && hasAttr("class")
+ && hasAttr("method") || isTag("array") || isPrimitive()
|| isTag("class") || isTag("null");
return result;
}
-
+
private Argument getReferencedArgument(HashMap references) {
return ((Command) references.get(getAttr("idref"))).getResult();
}
-
+
// get a target through tag and attrs analysis
private Object getTarget(HashMap references) throws Exception {
- if(target == null){
- if(isReference()) {
+ if (target == null) {
+ if (isReference()) {
Command cmd = (Command) references.get(getAttr("idref"));
target = (cmd != null) ? cmd.getResultValue() : null;
- } else if(isExecutable()) {
+ } else if (isExecutable()) {
String className = null;
-
- if(isPrimitive()) {
+
+ if (isPrimitive()) {
className = getPrimitiveClassName(tagName);
- } else if(isTag("class")) {
+ } else if (isTag("class")) {
className = getPrimitiveClassName(tagName);
- } else if(isConstructor() || isStaticMethod() || isField()) {
+ } else if (isConstructor() || isStaticMethod() || isField()) {
className = getAttr("class");
- } else if(isArray()) {
+ } else if (isArray()) {
className = getAttr("class");
- Class componentType = isPrimitiveClassName(className)
- ? getPrimitiveClass(className)
- : Class.forName(className, true,
- Thread.currentThread().getContextClassLoader());
- className = Array.newInstance(
- componentType, 0).getClass().getName();
- }
-
- if(className != null) {
- target = Class.forName(className, true,
- Thread.currentThread().getContextClassLoader());
-
- if(isField()) {
+ Class componentType = isPrimitiveClassName(className) ? getPrimitiveClass(className)
+ : Class.forName(className, true, Thread
+ .currentThread().getContextClassLoader());
+ className = Array.newInstance(componentType, 0).getClass()
+ .getName();
+ }
+
+ if (className != null) {
+ target = Class.forName(className, true, Thread
+ .currentThread().getContextClassLoader());
+
+ if (isField()) {
String fieldName = getAttr("field");
target = ((Class) target).getField(fieldName);
}
@@ -463,192 +489,184 @@
throw new Exception("target is not generated: classname "
+ className + " is not found");
}
- } else if(ctx.isArray()) {
+ } else if (ctx.isArray()) {
//target = ctx.getTarget(references);
target = Class.forName("java.lang.reflect.Array");
}
}
return target;
}
-
+
// set target to execute command on
private void setTarget(Object target) {
this.target = target;
}
-
+
// Return a method name of command
private String getMethodName(HashMap references)
- throws NoSuchMethodException, IntrospectionException, Exception
- {
- if(methodName == null) {
+ throws NoSuchMethodException, IntrospectionException, Exception {
+ if (methodName == null) {
String methodValue = null;
- if(isTag("class")) {
+ if (isTag("class")) {
addArgument(new Argument(String.class, data), 0);
methodValue = "forName";
- } else if(isPrimitive()) {
- if(isTag("char")) {
- if(data.length() != 1) {
- throw new IntrospectionException("Cannot convert" + data
- + " to char");
+ } else if (isPrimitive()) {
+ if (isTag("char")) {
+ if (data.length() != 1) {
+ throw new IntrospectionException("Cannot convert"
+ + data + " to char");
} else {
- addArgument(new Argument(char.class, new Character(
- data.charAt(0))), 0);
+ addArgument(new Argument(char.class, new Character(data
+ .charAt(0))), 0);
}
} else {
addArgument(new Argument(String.class, data), 0);
}
methodValue = "new";
- } else if(isConstructor() || hasAttr("method", "new")) {
+ } else if (isConstructor() || hasAttr("method", "new")) {
methodValue = "new";
- } else if(isArray()) {
+ } else if (isArray()) {
methodValue = "new";
- int length = hasAttr("length") ? Integer.parseInt(
- getAttr("length")) : getArgumentsNumber();
+ int length = hasAttr("length") ? Integer
+ .parseInt(getAttr("length")) : getArgumentsNumber();
copyArgumentsToCommands();
addArgument(new Argument(int.class, new Integer(length)), 0);
- } else if(hasAttr("property")) {
+ } else if (hasAttr("property")) {
String propertyValue = getAttr("property");
- if(hasAttr("index")) {
+ if (hasAttr("index")) {
addArgument(new Argument(int.class, new Integer(
getAttr("index"))), 0);
}
-
+
BeanInfo beanInfo = Introspector.getBeanInfo(getTarget(
references).getClass());
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
-
+
boolean methodFound = false;
Method method = null;
- for(int i = 0; i < pds.length; ++i) {
+ for (int i = 0; i < pds.length; ++i) {
PropertyDescriptor pd = pds[i];
- if(propertyValue.equals(pd.getName())) {
+ if (propertyValue.equals(pd.getName())) {
int argsNum = getArgumentsNumber();
- if(hasAttr("index")) {
- IndexedPropertyDescriptor ipd =
- (IndexedPropertyDescriptor) pd;
- if(argsNum == 1) {
+ if (hasAttr("index")) {
+ IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
+ if (argsNum == 1) {
method = ipd.getIndexedReadMethod();
- } else if(argsNum == 0) {
+ } else if (argsNum == 0) {
method = ipd.getReadMethod();
}
} else {
method = pd.getReadMethod();
}
-
- if(method != null)
+
+ if (method != null)
methodFound = matchMethodParams(method, references);
-
- if(methodFound == false) {
- if(hasAttr("index")) {
- IndexedPropertyDescriptor ipd =
- (IndexedPropertyDescriptor) pd;
+
+ if (methodFound == false) {
+ if (hasAttr("index")) {
+ IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
if (argsNum == 2) {
method = ipd.getIndexedWriteMethod();
- } else if(argsNum == 1) {
+ } else if (argsNum == 1) {
method = ipd.getWriteMethod();
}
} else {
method = pd.getWriteMethod();
}
}
-
- if(method != null)
+
+ if (method != null)
methodFound = matchMethodParams(method, references);
}
}
-
- if(method == null) {
+
+ if (method == null) {
throw new NoSuchMethodException("for property "
+ propertyValue + " no getter(setter) is found");
} else {
methodValue = method.getName();
}
- } else if(hasAttr("method")) {
- if(hasAttr("index"))
- addArgument(new Argument(int.class, Integer.valueOf(
- getAttr("index"))), 0);
+ } else if (hasAttr("method")) {
+ if (hasAttr("index"))
+ addArgument(new Argument(int.class, Integer
+ .valueOf(getAttr("index"))), 0);
methodValue = getAttr("method");
- } else if(hasAttr("index")) {
- addArgument(new Argument(int.class, Integer.valueOf(
- getAttr("index"))), 0);
+ } else if (hasAttr("index")) {
+ addArgument(new Argument(int.class, Integer
+ .valueOf(getAttr("index"))), 0);
methodValue = getArgumentsNumber() > 1 ? "set" : "get";
- if(ctx.isArray()) {
+ if (ctx.isArray()) {
addArgument(ctx.getResult(), 0);
}
- } else if(hasAttr("field")) {
- addArgument(
- new Argument(
- Class.forName(
- getAttr("class"), true,
- Thread.currentThread().getContextClassLoader()
- )
- ),
- 0
- );
-
+ } else if (hasAttr("field")) {
+ addArgument(new Argument(Class.forName(getAttr("class"), true,
+ Thread.currentThread().getContextClassLoader())), 0);
+
methodValue = "get";
} else {
- throw new Exception(
- "method name is not generated: error in "
+ throw new Exception("method name is not generated: error in "
+ "getMethodName()");
}
methodName = methodValue;
}
return methodName;
}
-
+
// return a list of arguments as of Argument type
private Argument[] getArguments() {
Argument[] args = new Argument[auxArguments.size() + arguments.size()];
- for(int i = 0; i < auxArguments.size(); ++i)
- args[i] = (Argument) auxArguments.elementAt(i);
- for(int j = 0; j < arguments.size(); ++j) {
- Command cmd = (Command) arguments.elementAt(j);
- if(cmd.getStatus() >= Command.COMMAND_EXECUTED) {
+
+ for (int i = 0; i < auxArguments.size(); ++i)
+ args[i] = auxArguments.elementAt(i);
+ for (int j = 0; j < arguments.size(); ++j) {
+ Command cmd = arguments.elementAt(j);
+
+ if (cmd.getStatus() >= Command.COMMAND_EXECUTED) {
args[auxArguments.size() + j] = cmd.getResult();
} else {
//System.out.println("arg: " + cmd.getResultValue());
- args = null; break;
+ args = null;
+ break;
}
}
return args;
}
-
+
// return argument values
private Object[] getArgumentsValues() {
Argument[] args = getArguments();
Object[] result = new Object[args.length];
- for(int i = 0; i < args.length; ++i)
+ for (int i = 0; i < args.length; ++i)
result[i] = args[i].getValue();
return result;
}
-
+
// copy arguments to treat as commands
private void copyArgumentsToCommands() {
- Iterator i = arguments.iterator();
- while(i.hasNext()) {
- Command cmd = (Command) i.next();
+ Iterator<Command> i = arguments.iterator();
+ while (i.hasNext()) {
+ Command cmd = i.next();
cmd.status = Command.CHILDREN_FILTERED;
operations.add(cmd);
}
arguments.clear();
}
-
+
// return number of arguments
private int getArgumentsNumber() {
return auxArguments.size() + arguments.size();
}
-
+
// return number of commands
- private int getOperationsNumber() {
- return operations.size();
- }
-
+ // private int getOperationsNumber() {
+ // return operations.size();
+ // }
+
// add argument to the beginning of arguments
private void addArgument(Argument argument, int idx) {
auxArguments.insertElementAt(argument, idx);
}
-
+
// Check if the name of class is primitive
public static boolean isPrimitiveClassName(String className) {
return className.equalsIgnoreCase("boolean")
@@ -661,157 +679,167 @@
|| className.equalsIgnoreCase("double")
|| className.equalsIgnoreCase("string");
}
-
+
// Transforms a primitive class name
private String getPrimitiveClassName(String data) {
String shortClassName = null;
- if(data.equals("int")) {
+ if (data.equals("int")) {
shortClassName = "Integer";
} else if (data.equals("char")) {
shortClassName = "Character";
} else {
- shortClassName = data.substring(0,1).toUpperCase()
+ shortClassName = data.substring(0, 1).toUpperCase()
+ data.substring(1, data.length());
}
return "java.lang." + shortClassName;
}
-
+
public static Class getPrimitiveClass(String className) {
Class result = null;
- if(className.equals("boolean")) {
+ if (className.equals("boolean")) {
result = boolean.class;
- } else if(className.equals("byte")) {
+ } else if (className.equals("byte")) {
result = byte.class;
- } else if(className.equals("char")) {
+ } else if (className.equals("char")) {
result = char.class;
- } else if(className.equals("short")) {
+ } else if (className.equals("short")) {
result = short.class;
- } else if(className.equals("int")) {
+ } else if (className.equals("int")) {
result = int.class;
- } else if(className.equals("long")) {
+ } else if (className.equals("long")) {
result = long.class;
- } else if(className.equals("float")) {
+ } else if (className.equals("float")) {
result = float.class;
- } else if(className.equals("double")) {
+ } else if (className.equals("double")) {
result = double.class;
- } else if(className.equals("string")) {
+ } else if (className.equals("string")) {
result = String.class;
}
return result;
}
-
+
private boolean matchMethodParams(Method method, HashMap references) {
- Class[] paramTypes = method.getParameterTypes();
+ Class<?>[] paramTypes = method.getParameterTypes();
Argument[] args = getArguments();
- if(args == null) return false;
+ if (args == null)
+ return false;
boolean result = true;
- if(paramTypes.length == args.length) {
- for(int j = 0; j < paramTypes.length; ++j) {
+ if (paramTypes.length == args.length) {
+ for (int j = 0; j < paramTypes.length; ++j) {
//System.out.println("paramTypes[j] = " + paramTypes[j]);
//System.out.println("args[j] = " + args[j].getType());
-
- boolean isAssignable = (args[j].getType() == null)
- ? !paramTypes[j].isPrimitive()
+
+ boolean isAssignable = (args[j].getType() == null) ? !paramTypes[j]
+ .isPrimitive()
: paramTypes[j].isAssignableFrom(args[j].getType());
-
+
//System.out.println("args[j] = " + args[j].getType());
-
- if(!isAssignable) {
+
+ if (!isAssignable) {
result = false;
break;
}
}
- } else result = false;
+ } else
+ result = false;
return result;
}
-
+
public static HashMap parseAttrs(String tagName, Attributes attrs) {
- HashMap result = new HashMap();
- if(tagName.equals("object")) {
- for(int i = 0; i < objectAttrNames.length; ++i) {
+ HashMap<String, String> result = new HashMap<String, String>();
+ if (tagName.equals("object")) {
+ for (int i = 0; i < objectAttrNames.length; ++i) {
String name = objectAttrNames[i];
String value = attrs.getValue(name);
- if(value != null) result.put(name, value);
+ if (value != null)
+ result.put(name, value);
}
- } else if(tagName.equals("void")) {
- for(int i = 0; i < voidAttrNames.length; ++i) {
+ } else if (tagName.equals("void")) {
+ for (int i = 0; i < voidAttrNames.length; ++i) {
String name = voidAttrNames[i];
String value = attrs.getValue(name);
- if(value != null) result.put(name, value);
+ if (value != null)
+ result.put(name, value);
}
- } else if(tagName.equals("array")) {
- for(int i = 0; i < arrayAttrNames.length; ++i) {
+ } else if (tagName.equals("array")) {
+ for (int i = 0; i < arrayAttrNames.length; ++i) {
String name = arrayAttrNames[i];
String value = attrs.getValue(name);
- if(value != null) result.put(name, value);
+ if (value != null)
+ result.put(name, value);
}
- } else if(tagName.equals("java")) {
- for(int i = 0; i < javaAttrNames.length; ++i) {
+ } else if (tagName.equals("java")) {
+ for (int i = 0; i < javaAttrNames.length; ++i) {
String name = javaAttrNames[i];
String value = attrs.getValue(name);
- if(value != null) result.put(name, value);
+ if (value != null)
+ result.put(name, value);
}
}
return result;
}
-
+
// Auxiliary logging with tabs functions
- public static void pr(String msg) {
- //System.out.print(msg);
+ public static void pr(String msg) {
+ //System.out.print(msg);
}
+
public static void pr(int tabCount, String msg) {
String result = "";
- for(int i = 0; i < tabCount; ++i) result += '\t';
+ for (int i = 0; i < tabCount; ++i)
+ result += '\t';
result += msg;
//System.out.print(result);
}
+
public static void prn(String msg) {
- //System.out.println(msg);
+ //System.out.println(msg);
}
+
public static void prn(int tabCount, String msg) {
String result = "";
- for(int i = 0; i < tabCount; ++i) result += '\t';
+ for (int i = 0; i < tabCount; ++i)
+ result += '\t';
result += msg;
//System.out.println(result);
}
-
- public static void printAttrs(
- int tabCount, String tagName, Attributes attrs) {
+
+ public static void printAttrs(int tabCount, String tagName, Attributes attrs) {
pr(tabCount, tabCount + ">in <" + tagName);
- for(int i = 0; i < attrs.getLength(); ++i) {
+ for (int i = 0; i < attrs.getLength(); ++i) {
String attrName = attrs.getQName(i);
String attrValue = attrs.getValue(i);
pr(" " + attrName + "=" + attrValue);
}
prn(">");
}
-
+
private static int initializeStatus(String tagName) {
// return tagName.equals("java") ? Command.COMMAND_EXECUTED : Command.INITIALIZED;
return Command.INITIALIZED;
}
-
- private static String translateStatus(int status) {
- String result = "unknown";
- if(status == Command.INITIALIZED) {
- result = "intialized";
- } else if(status == Command.CHILDREN_FILTERED) {
- result = "children filtered";
- } else if(status == Command.COMMAND_EXECUTED) {
- result = "executed";
- } else if(status == Command.CHILDREN_PROCESSED) {
- result = "children processed";
- }
- return result;
- }
-
- private static final String[] objectAttrNames =
- { "id", "idref", "class", "field", "method", "property", "index" };
-
- private static final String[] voidAttrNames =
- { "id", "class", "method", "property", "index" };
-
+
+ // private static String translateStatus(int status) {
+ // String result = "unknown";
+ // if(status == Command.INITIALIZED) {
+ // result = "intialized";
+ // } else if(status == Command.CHILDREN_FILTERED) {
+ // result = "children filtered";
+ // } else if(status == Command.COMMAND_EXECUTED) {
+ // result = "executed";
+ // } else if(status == Command.CHILDREN_PROCESSED) {
+ // result = "children processed";
+ // }
+ // return result;
+ // }
+
+ private static final String[] objectAttrNames = { "id", "idref", "class",
+ "field", "method", "property", "index" };
+
+ private static final String[] voidAttrNames = { "id", "class", "method",
+ "property", "index" };
+
private static final String[] arrayAttrNames = { "id", "class", "length" };
-
+
private static final String[] javaAttrNames = { "version", "class" };
}