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.5KB

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