22
33import java .util .ArrayList ;
44import java .util .Collection ;
5+ import java .util .HashSet ;
6+ import java .util .LinkedList ;
57import java .util .List ;
68import java .util .Map ;
79import java .util .Set ;
@@ -47,6 +49,7 @@ public class PCMDetector {
4749 private final RequirementsBuilder compositeRequirements = new RequirementsBuilder ();
4850 private final Map <CompUnitOrName , List <String >> weakComponents = new ConcurrentHashMap <>();
4951 private final Map <CompUnitOrName , String > separatingIdentifiers = new ConcurrentHashMap <>();
52+ private final Set <String > blacklist = new HashSet <>();
5053
5154 private static String getFullUnitName (final CompUnitOrName unit ) {
5255 // TODO this is potentially problematic, maybe restructure
@@ -76,18 +79,13 @@ private static String getFullUnitName(final CompUnitOrName unit) {
7679
7780 public void detectComponent (final CompUnitOrName unit ) {
7881 if (!unit .isUnit ()) {
79- if (this .components .get (unit ) == null ) {
80- this .components .put (unit , new ComponentBuilder (unit ));
81- }
82+ tryAddComponent (unit );
8283 return ;
8384 }
8485 for (final Object type : unit .compilationUnit ()
8586 .get ()
8687 .types ()) {
87- if (type instanceof TypeDeclaration ) {
88- if (this .components .get (unit ) == null ) {
89- this .components .put (unit , new ComponentBuilder (unit ));
90- }
88+ if (type instanceof TypeDeclaration && tryAddComponent (unit )) {
9189 final ITypeBinding binding = ((TypeDeclaration ) type ).resolveBinding ();
9290 this .detectProvidedInterfaceWeakly (unit , binding );
9391 }
@@ -100,8 +98,8 @@ public void detectRequiredInterface(final CompUnitOrName unit, final InterfaceNa
10098
10199 private void detectRequiredInterface (final CompUnitOrName unit , final InterfaceName interfaceName ,
102100 final boolean compositeRequired ) {
103- if (this . components . get (unit ) == null ) {
104- this . components . put ( unit , new ComponentBuilder ( unit )) ;
101+ if (! tryAddComponent (unit )) {
102+ return ;
105103 }
106104 final EntireInterface iface = new EntireInterface (interfaceName );
107105 this .detectRequiredInterface (unit , compositeRequired , false , iface );
@@ -117,8 +115,8 @@ public void detectRequiredInterfaceWeakly(final CompUnitOrName unit, final Field
117115
118116 private void detectRequiredInterface (final CompUnitOrName unit , final FieldDeclaration field ,
119117 final boolean compositeRequired , final boolean detectWeakly ) {
120- if (this . components . get (unit ) == null ) {
121- this . components . put ( unit , new ComponentBuilder ( unit )) ;
118+ if (! tryAddComponent (unit )) {
119+ return ;
122120 }
123121 @ SuppressWarnings ("unchecked" )
124122 final List <OperationInterface > ifaces = ((List <VariableDeclaration >) field .fragments ()).stream ()
@@ -136,17 +134,17 @@ public void detectCompositeRequiredInterfaceWeakly(final CompUnitOrName unit, fi
136134 return ;
137135 }
138136 final ITypeBinding type = method .getDeclaringClass ();
139- if (this . components . get (unit ) == null ) {
140- this . components . put ( unit , new ComponentBuilder ( unit )) ;
137+ if (! tryAddComponent (unit )) {
138+ return ;
141139 }
142140 final EntireInterface iface = new EntireInterface (type ,
143141 new JavaInterfaceName (NameConverter .toPCMIdentifier (type )));
144142 this .detectRequiredInterface (unit , true , true , iface );
145143 }
146144
147145 public void detectRequiredInterface (final CompUnitOrName unit , final SingleVariableDeclaration parameter ) {
148- if (this . components . get (unit ) == null ) {
149- this . components . put ( unit , new ComponentBuilder ( unit )) ;
146+ if (! tryAddComponent (unit )) {
147+ return ;
150148 }
151149 final IVariableBinding parameterBinding = parameter .resolveBinding ();
152150 if (parameterBinding == null ) {
@@ -245,8 +243,8 @@ public void detectProvidedOperation(final CompUnitOrName unit, final IMethodBind
245243
246244 private void detectProvidedOperation (final CompUnitOrName unit , final IMethodBinding method ,
247245 final OperationName name , final boolean compositeProvided , final boolean detectWeakly ) {
248- if (this . components . get (unit ) == null ) {
249- this . components . put ( unit , new ComponentBuilder ( unit )) ;
246+ if (! tryAddComponent (unit )) {
247+ return ;
250248 }
251249 final OperationInterface provision = new Operation (method , name );
252250 this .detectProvidedInterface (unit , provision , compositeProvided , detectWeakly );
@@ -290,8 +288,8 @@ public void detectSeparatingIdentifier(final CompUnitOrName unit, final String s
290288 }
291289
292290 public void detectPartOfComposite (final CompUnitOrName unit , final String compositeName ) {
293- if (this . components . get (unit ) == null ) {
294- this . components . put ( unit , new ComponentBuilder ( unit )) ;
291+ if (! tryAddComponent (unit )) {
292+ return ;
295293 }
296294 if (!this .composites .containsKey (compositeName )) {
297295 this .composites .put (compositeName , new CompositeBuilder (compositeName ));
@@ -380,4 +378,31 @@ public boolean isPartOfComposite(CompUnitOrName name) {
380378 }
381379 return false ;
382380 }
381+
382+ public void addToBlacklist (String string ) {
383+ this .blacklist .add (string );
384+
385+ // Clean up already added but now blacklisted components
386+ List <CompUnitOrName > toDelete = new LinkedList <>();
387+ for (CompUnitOrName unit : this .components .keySet ()) {
388+ if (unit .name ()
389+ .equals (string )) {
390+ toDelete .add (unit );
391+ }
392+ }
393+ for (CompUnitOrName unit : toDelete ) {
394+ this .components .remove (unit );
395+ }
396+ }
397+
398+ private boolean tryAddComponent (CompUnitOrName unit ) {
399+ if (this .components .get (unit ) != null ) {
400+ return true ;
401+ }
402+ if (this .blacklist .contains (unit .name ())) {
403+ return false ;
404+ }
405+ this .components .put (unit , new ComponentBuilder (unit ));
406+ return true ;
407+ }
383408}
0 commit comments