You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RelationshipMap.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* *******************************************************************
  2. * Copyright (c) 2003,2010 Contributors
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Mik Kersten initial implementation
  11. * Andy Clement
  12. * ******************************************************************/
  13. package org.aspectj.asm.internal;
  14. import java.util.ArrayList;
  15. import java.util.HashMap;
  16. import java.util.Iterator;
  17. import java.util.List;
  18. import java.util.Set;
  19. import org.aspectj.asm.IProgramElement;
  20. import org.aspectj.asm.IRelationship;
  21. import org.aspectj.asm.IRelationship.Kind;
  22. import org.aspectj.asm.IRelationshipMap;
  23. /**
  24. * @author Mik Kersten
  25. * @author Andy Clement
  26. */
  27. public class RelationshipMap extends HashMap<String, List<IRelationship>> implements IRelationshipMap {
  28. private static final long serialVersionUID = 496638323566589643L;
  29. public RelationshipMap() {
  30. }
  31. public List<IRelationship> get(String handle) {
  32. List<IRelationship> relationships = super.get(handle);
  33. if (relationships == null) {
  34. return null;
  35. } else {
  36. return relationships;
  37. }
  38. }
  39. public List<IRelationship> get(IProgramElement source) {
  40. return get(source.getHandleIdentifier());
  41. }
  42. public IRelationship get(String source, IRelationship.Kind kind, String relationshipName, boolean runtimeTest,
  43. boolean createIfMissing) {
  44. List<IRelationship> relationships = get(source);
  45. if (relationships == null) {
  46. if (!createIfMissing) {
  47. return null;
  48. }
  49. relationships = new ArrayList<IRelationship>();
  50. IRelationship rel = new Relationship(relationshipName, kind, source, new ArrayList<String>(), runtimeTest);
  51. relationships.add(rel);
  52. super.put(source, relationships);
  53. return rel;
  54. } else {
  55. for (IRelationship curr : relationships) {
  56. if (curr.getKind() == kind && curr.getName().equals(relationshipName) && curr.hasRuntimeTest() == runtimeTest) {
  57. return curr;
  58. }
  59. }
  60. if (createIfMissing) {
  61. // At this point we did find some relationships for 'source' but not one that looks like what we are
  62. // after (either the kind or the name or the dynamictests setting don't match)
  63. IRelationship rel = new Relationship(relationshipName, kind, source, new ArrayList<String>(), runtimeTest);
  64. relationships.add(rel);
  65. return rel;
  66. }
  67. }
  68. return null;
  69. }
  70. public IRelationship get(IProgramElement source, IRelationship.Kind kind, String relationshipName, boolean runtimeTest,
  71. boolean createIfMissing) {
  72. return get(source.getHandleIdentifier(), kind, relationshipName, runtimeTest, createIfMissing);
  73. }
  74. public IRelationship get(IProgramElement source, Kind kind, String relationshipName) {
  75. return get(source, kind, relationshipName, false, true);
  76. }
  77. public boolean remove(String source, IRelationship relationship) {
  78. List<IRelationship> list = super.get(source);
  79. if (list != null) {
  80. return list.remove(relationship);
  81. // boolean matched = false;
  82. // for (Iterator it = list.iterator(); it.hasNext(); ) {
  83. // IRelationship curr = (IRelationship)it.next();
  84. // if (curr.getName().equals(relationship.getName())) {
  85. // curr.getTargets().addAll(relationship.getTargets());
  86. // matched = true;
  87. // }
  88. // }
  89. // if (!matched) list.remove(relationship);
  90. }
  91. return false;
  92. }
  93. public void removeAll(String source) {
  94. super.remove(source);
  95. }
  96. public void put(String source, IRelationship relationship) {
  97. List<IRelationship> existingRelationships = super.get(source);
  98. if (existingRelationships == null) {
  99. // new entry
  100. existingRelationships = new ArrayList<IRelationship>();
  101. existingRelationships.add(relationship);
  102. super.put(source, existingRelationships);
  103. } else {
  104. boolean matched = false;
  105. for (IRelationship existingRelationship : existingRelationships) {
  106. if (existingRelationship.getName().equals(relationship.getName())
  107. && existingRelationship.getKind() == relationship.getKind()) {
  108. existingRelationship.getTargets().addAll(relationship.getTargets());
  109. matched = true;
  110. }
  111. }
  112. if (matched) {
  113. // bug?
  114. System.err.println("matched = true");
  115. }
  116. if (matched) {
  117. existingRelationships.add(relationship); // Is this a bug, will it give us double entries?
  118. }
  119. }
  120. }
  121. public void put(IProgramElement source, IRelationship relationship) {
  122. put(source.getHandleIdentifier(), relationship);
  123. }
  124. public void clear() {
  125. super.clear();
  126. }
  127. public Set<String> getEntries() {
  128. return keySet();
  129. }
  130. }