All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.aspectj.asm.internal.RelationshipMap Maven / Gradle / Ivy

Go to download

The AspectJ weaver applies aspects to Java classes. It can be used as a Java agent in order to apply load-time weaving (LTW) during class-loading and also contains the AspectJ runtime classes.

There is a newer version: 1.9.22.1
Show newest version
/* *******************************************************************
 * Copyright (c) 2003,2010 Contributors
 * All rights reserved.
 * This program and the accompanying materials are made available
 * under the terms of the Eclipse Public License v 2.0
 * which accompanies this distribution and is available at
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
 *
 * Contributors:
 *     Mik Kersten     initial implementation
 *     Andy Clement
 * ******************************************************************/

package org.aspectj.asm.internal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.aspectj.asm.IProgramElement;
import org.aspectj.asm.IRelationship;
import org.aspectj.asm.IRelationship.Kind;
import org.aspectj.asm.IRelationshipMap;

/**
 * @author Mik Kersten
 * @author Andy Clement
 */
public class RelationshipMap extends HashMap> implements IRelationshipMap {

	private static final long serialVersionUID = 496638323566589643L;

	public RelationshipMap() {
	}

	public List get(String handle) {
		List relationships = super.get(handle);
		if (relationships == null) {
			return null;
		} else {
			return relationships;
		}
	}

	public List get(IProgramElement source) {
		return get(source.getHandleIdentifier());
	}

	public IRelationship get(String source, IRelationship.Kind kind, String relationshipName, boolean runtimeTest,
			boolean createIfMissing) {
		List relationships = get(source);
		if (relationships == null) {
			if (!createIfMissing) {
				return null;
			}
			relationships = new ArrayList<>();
			IRelationship rel = new Relationship(relationshipName, kind, source, new ArrayList<>(), runtimeTest);
			relationships.add(rel);

			super.put(source, relationships);
			return rel;
		} else {
			for (IRelationship curr : relationships) {
				if (curr.getKind() == kind && curr.getName().equals(relationshipName) && curr.hasRuntimeTest() == runtimeTest) {
					return curr;
				}
			}
			if (createIfMissing) {
				// At this point we did find some relationships for 'source' but not one that looks like what we are
				// after (either the kind or the name or the dynamictests setting don't match)
				IRelationship rel = new Relationship(relationshipName, kind, source, new ArrayList<>(), runtimeTest);
				relationships.add(rel);
				return rel;
			}
		}
		return null;
	}

	public IRelationship get(IProgramElement source, IRelationship.Kind kind, String relationshipName, boolean runtimeTest,
			boolean createIfMissing) {
		return get(source.getHandleIdentifier(), kind, relationshipName, runtimeTest, createIfMissing);
	}

	public IRelationship get(IProgramElement source, Kind kind, String relationshipName) {
		return get(source, kind, relationshipName, false, true);
	}

	public boolean remove(String source, IRelationship relationship) {
		List list = super.get(source);
		if (list != null) {
			return list.remove(relationship);
			// boolean matched = false;
			// for (Iterator it = list.iterator(); it.hasNext(); ) {
			// IRelationship curr = (IRelationship)it.next();
			// if (curr.getName().equals(relationship.getName())) {
			// curr.getTargets().addAll(relationship.getTargets());
			// matched = true;
			// }
			// }
			// if (!matched) list.remove(relationship);
		}
		return false;
	}

	public void removeAll(String source) {
		super.remove(source);
	}

	public void put(String source, IRelationship relationship) {
		List existingRelationships = super.get(source);
		if (existingRelationships == null) {
			// new entry
			existingRelationships = new ArrayList<>();
			existingRelationships.add(relationship);
			super.put(source, existingRelationships);
		} else {
			boolean matched = false;
			for (IRelationship existingRelationship : existingRelationships) {
				if (existingRelationship.getName().equals(relationship.getName())
						&& existingRelationship.getKind() == relationship.getKind()) {
					existingRelationship.getTargets().addAll(relationship.getTargets());
					matched = true;
				}
			}
			if (matched) {
				// bug?
				System.err.println("matched = true");
			}
			if (matched) {
				existingRelationships.add(relationship); // Is this a bug, will it give us double entries?
			}
		}
	}

	public void put(IProgramElement source, IRelationship relationship) {
		put(source.getHandleIdentifier(), relationship);
	}

	public void clear() {
		super.clear();
	}

	public Set getEntries() {
		return keySet();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy