Qualified association detection

eUML2 Reverse engineering engine can recognize the java.util.Map and its implementation classes such as java.util.HashMap and java.util.Hashtable. It can capture not only the key and value type, but also the element type in the associated value if it is a collection.

In the following illustration, we use the Company and Project class as example:

public class Company
{
	private Hashtable projects = new Hashtable ();
	public Company(Hashtable projects) {
		this.projects = projects;
	}

	public Hashtable getProjects() {
		return projects;
	}

	public void setProjects (Hashtable projects) {
		this.projects = projects;
	}
}

According the accessing type of this attribute, we distinguee the analyze mechanism in two categories:

  1. Getter category
  2. Setter category

The first one allows the forward analyze, the analyzer can follow the execution order. The typical example is the iteration method (see below). It is much easy than Setter category, which needs backward analyze mechanism.

1. Getter category

Each accessing method may have a specific way to capture the element type. So we classify all relevant methods as following groups according to the model capture mechanism:

1.1 Put, Remove and Test group

This group includes following methods:

Class

Method

Recognition

java.util.Map

Object put(Object key, Object value)

Key and value

Class

Method

Recognition

java.util.Map

Object remove(Object object)

Key

Class

Method

Recognition

java.util.Map

boolean containsKey (Object object)

Key

java.util.Map

boolean containsValue (Object object)

Value

java.util.Hashtable

boolean contains (Object object)

Value

The capture mechanism of this group is simplest one comparing others. We just need identify the method call and capture argument type. For example:

public void putProject(String name, Project project) {
	projects.put(name, project);
}

1.2 Get group

This group consists of following methods:

Class

Method

Recognition

java.util.Map

Object get(Object object)

Key and value

This group is a little bit difficult than previous one since we need analyse the next statements to capture the element type in type casting or the operator instanceof. For example,

public Project getProjectAt(String name) {
	return (Project) projectsMap.get(name);;
}

1.3 Iteration group

This group consists of following methods

Class

Method

Recognition

java.util.Map

Iterator values ()

Value

java.util.Map

java.util.Set keySet()

followed by iterator()

Key

java.util.Map

Collection values()

followed by iterator()

Value

java.util.Hashtable

Enumeration keys ()

Key

java.util.Hashtable

Enumeration elements ()

Value

This group is more difficult than previous one again since it is necessary to analyse the following cast and instanceof statements only after element retrieve call such as next() or nextElement(). For example,

Iterator iterator = company.getProjects().values().iterator();
while (iterator.hasNext()) {
	Project project = (Project) iterator.next();
}

or

for (Iterator iterator = company. getProjects().keySet().iterator(); iterator 
hasNext();) {
	String name = (String) iterator.next();
}

All casting and operator instanceof must be ignored without calling next(). Otherwise, the result will be wrong:

Iterator iterator = company.getProjects().values().iterator();
Address address = (Address) getAddress();
while (iterator.hasNext()) {
	Project project = (Project) iterator.next();
}

If the value is a collection, the element type mechanism will be used for deep analyze. Here is an example of the association teams between Employee and Project:

public class Project {
	private HashMap teams = new HashMap ();

	public void addTeamMember(String name, Employee member) {
		Collection team = (Collection) teams.get(name);
		if (team == null) {
			team = new ArrayList();
			teams.put(name, team);
		}
		team.add(member);
		member.setProject(this);
	}
}