• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

moosetechnology / VerveineJ / 14665386629

25 Apr 2025 01:09PM UTC coverage: 50.655% (+0.7%) from 49.972%
14665386629

Pull #127

github

web-flow
Merge 9c1f1ce46 into 5207a6c7c
Pull Request #127: New generics implementation

1878 of 3942 branches covered (47.64%)

Branch coverage included in aggregate %.

481 of 1066 new or added lines in 52 files covered. (45.12%)

53 existing lines in 10 files now uncovered.

4267 of 8189 relevant lines covered (52.11%)

2.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.44
/app/src/main/java/fr/inria/verveine/extractor/java/EntityDictionary.java
1
package fr.inria.verveine.extractor.java;
2

3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.Collection;
6
import java.util.ConcurrentModificationException;
7
import java.util.Hashtable;
8
import java.util.Iterator;
9
import java.util.LinkedList;
10
import java.util.List;
11
import java.util.Map;
12
import java.util.stream.Collectors;
13

14
import org.eclipse.jdt.core.dom.ASTNode;
15
import org.eclipse.jdt.core.dom.CompilationUnit;
16
import org.eclipse.jdt.core.dom.IBinding;
17
import org.eclipse.jdt.core.dom.IMethodBinding;
18
import org.eclipse.jdt.core.dom.IPackageBinding;
19
import org.eclipse.jdt.core.dom.ITypeBinding;
20
import org.eclipse.jdt.core.dom.IVariableBinding;
21
import org.eclipse.jdt.core.dom.MethodDeclaration;
22
import org.eclipse.jdt.core.dom.Modifier;
23
import org.moosetechnology.model.famix.famixjavaentities.*;
24
import org.moosetechnology.model.famix.famixjavaentities.Class;
25
import org.moosetechnology.model.famix.famixjavaentities.Enum;
26
import org.moosetechnology.model.famix.famixjavaentities.Exception;
27
import org.moosetechnology.model.famix.famixjavaentities.Package;
28
import org.moosetechnology.model.famix.famixtraits.TAccessible;
29
import org.moosetechnology.model.famix.famixtraits.TAssociation;
30
import org.moosetechnology.model.famix.famixtraits.TCanBeClassSide;
31
import org.moosetechnology.model.famix.famixtraits.TCanBeFinal;
32
import org.moosetechnology.model.famix.famixtraits.TCanBeStub;
33
import org.moosetechnology.model.famix.famixtraits.TCanImplement;
34
import org.moosetechnology.model.famix.famixtraits.THasVisibility;
35
import org.moosetechnology.model.famix.famixtraits.TImplementable;
36
import org.moosetechnology.model.famix.famixtraits.TImplementation;
37
import org.moosetechnology.model.famix.famixtraits.TInheritance;
38
import org.moosetechnology.model.famix.famixtraits.TInvocationsReceiver;
39
import org.moosetechnology.model.famix.famixtraits.TMethod;
40
import org.moosetechnology.model.famix.famixtraits.TNamedEntity;
41
import org.moosetechnology.model.famix.famixtraits.TParametricEntity;
42
import org.moosetechnology.model.famix.famixtraits.TParametricAssociation;
43
import org.moosetechnology.model.famix.famixtraits.TReference;
44
import org.moosetechnology.model.famix.famixtraits.TSourceEntity;
45
import org.moosetechnology.model.famix.famixtraits.TStructuralEntity;
46
import org.moosetechnology.model.famix.famixtraits.TThrowable;
47
import org.moosetechnology.model.famix.famixtraits.TType;
48
import org.moosetechnology.model.famix.famixtraits.TTypeArgument;
49
import org.moosetechnology.model.famix.famixtraits.TTypedEntity;
50
import org.moosetechnology.model.famix.famixtraits.TWithAccesses;
51
import org.moosetechnology.model.famix.famixtraits.TWithAnnotationInstances;
52
import org.moosetechnology.model.famix.famixtraits.TWithAttributes;
53
import org.moosetechnology.model.famix.famixtraits.TWithComments;
54
import org.moosetechnology.model.famix.famixtraits.TWithInheritances;
55
import org.moosetechnology.model.famix.famixtraits.TWithLocalVariables;
56
import org.moosetechnology.model.famix.famixtraits.TWithMethods;
57
import org.moosetechnology.model.famix.famixtraits.TWithTypes;
58

59
import ch.akuhn.fame.Repository;
60
import fr.inria.verveine.extractor.java.utils.ImplicitVarBinding;
61
import fr.inria.verveine.extractor.java.utils.Util;
62

63
/**
64
 * A dictionary of FamixJava entities to help create them and find them back
65
 * Entities are mapped to keys which are the "binding" provided by the JDT pars
66
 * 
67
 * @author anquetil
68
 */
69
public class EntityDictionary {
70

71
        /**
72
         * A property added to CompilationUnits to record the name of the source file they belong to.
73
         * Used to create FileAnchors
74
         */
75
        public static final String SOURCE_FILENAME_PROPERTY = "verveine-source-filename";
76

77
        public static final String DEFAULT_PCKG_NAME = "<Default Package>";
78
        public static final String STUB_METHOD_CONTAINER_NAME = "<StubMethodContainer>";
79
        public static final String SELF_NAME = "self";
80
        public static final String SUPER_NAME = "super";
81
        
82
        public static final String OBJECT_NAME = "Object";
83
        public static final String METACLASS_NAME = "Class";
84
        public static final String OBJECT_PACKAGE_NAME = "java.lang";
85
        public static final String ARRAYS_NAME = "default[]";
86
        public static final String INIT_BLOCK_NAME = "<Initializer>";
87
        public static final String ANONYMOUS_NAME_PREFIX = "_Anonymous";
88

89
        public static final int UNKNOWN_MODIFIERS = 0;
90
        public static final String MODIFIER_ABSTRACT = "abstract";
91
        public static final String MODIFIER_PUBLIC   = "public";
92
        public static final String MODIFIER_PRIVATE  = "private";
93
        public static final String MODIFIER_PROTECTED= "protected";
94
        public static final String MODIFIER_PACKAGE = "package";
95
        public static final String MODIFIER_FINAL    = "final";
96
        public static final String MODIFIER_STATIC    = "static";
97
        public static final String MODIFIER_TRANSIENT = "transient";
98
        public static final String MODIFIER_VOLATILE = "volatile";
99
        public static final String MODIFIER_SYNCHRONIZED = "synchronized";
100

101
        /**
102
         * An MSE marker for methods
103
         */
104
        public static final String CONSTRUCTOR_KIND_MARKER = "constructor";
105

106
        /**
107
         * The FAMIX repository where all FAMIX entities are created and stored
108
         */
109
        protected Repository famixRepo;
110

111
        /**
112
         * A dictionary to map a key (provided by the user) to FAMIX Entity
113
         */
114
        protected Map<IBinding,TNamedEntity> keyToEntity;
115
        /**
116
         * A reverse dictionary (see {@link #keyToEntity}) to find the key of an entity.
117
         */
118
        protected Map<TNamedEntity,IBinding> entityToKey;
119

120
        /**
121
         * Another dictionary to map a name to FAMIX Entities with this name
122
         */
123
        protected Map<String,Collection<TNamedEntity>> nameToEntity;
124

125
        /**
126
         * Yet another dictionary for implicit variables ('self' and 'super')
127
         * Because they are implicit, they may not have a binding provided by the parser,
128
         * or may have the same binding than their associated type so they can't be kept easily in {@link #keyToEntity}
129
         */
130
        @Deprecated
131
        protected Map<Type,ImplicitVars> typeToImpVar;
132

133
        /**
134
         * Used to keep the two possible ImplicitVariable for a given Class binding
135
         * @author anquetil
136
         */
137
        @Deprecated
138
        protected class ImplicitVars {
×
139
                public ImplicitVariable self_iv;
140
                public ImplicitVariable super_iv;
141
        }
142
        
143
        /**
144
         * Result of utility methods for checking matching between two entities
145
         */
146
        private enum CheckResult {
3✔
147
                MATCH, UNDECIDED, FAIL;
18✔
148
        }
149

150

151
        /** Constructor taking a FAMIX repository
152
         * @param famixRepo
153
         */
154
        public EntityDictionary(Repository famixRepo) {
2✔
155
                        this.famixRepo = famixRepo;
3✔
156
                        
157
                        this.keyToEntity = new Hashtable<IBinding,TNamedEntity>();
5✔
158
                        this.entityToKey = new Hashtable<TNamedEntity,IBinding>();
5✔
159
                        this.nameToEntity = new Hashtable<String,Collection<TNamedEntity>>();
5✔
160
                        this.typeToImpVar = new Hashtable<Type,ImplicitVars>();
5✔
161
                        
162
                        if (! this.famixRepo.isEmpty()) {
4✔
163
                                recoverExistingRepository();
2✔
164
                        }
165
                }
1✔
166

167
          public void mapKey(IBinding bnd, NamedEntity fmx) {
168
                mapEntityToKey(bnd, fmx);
×
169
        }
×
170

171
        /**
172
         * Resets the dictionnary in a proper state after loading entities from an existing MSE file:
173
         * <UL>
174
         * <li>map all named entities to their names in <b>mapName</b></li>
175
         * <li>reset some boolean properties (e.g. <b>isStub</b>) that are false (they are not saved in the mse file and therefore not initialized)</li>
176
         * </ul>
177
         */
178
        protected void recoverExistingRepository() {
179
                for (NamedEntity ent : famixRepo.all(NamedEntity.class)) {
13✔
180
                        mapEntityToName( ent.getName(), ent);
5✔
181
                        // for the Exception to be raised, the return value must be tested
182
                        try { if (((TCanBeStub) ent).getIsStub()) {} }
5✔
183
                        catch (NullPointerException e) { ((TCanBeStub)ent).setIsStub(Boolean.FALSE); }
6✔
184
                }
1✔
185

186
                for (Access acc : famixRepo.all(Access.class)) {
13✔
187
                        // for the Exception to be raised, the return value must be tested
188
                        try { if (acc.getIsWrite()) {}        }
4✔
189
                        catch (NullPointerException e) { acc.setIsWrite(Boolean.FALSE); }
5✔
190
                }
1✔
191
        }
1✔
192

193
        protected void mapEntityToName(String name, TNamedEntity ent) {
194
                
195
                Collection<TNamedEntity> l_ent = nameToEntity.get(name);
6✔
196
                if (l_ent == null) {
2✔
197
                        l_ent = new LinkedList<TNamedEntity>();
4✔
198
                }
199
                l_ent.add(ent);
4✔
200
                nameToEntity.put(name, l_ent);
6✔
201
        }
1✔
202

203
        public void removeEntity( NamedEntity ent) {
204
                IBinding key;
205
                key = entityToKey.get(ent);
6✔
206
                entityToKey.remove(ent);
5✔
207
                keyToEntity.remove(key);
5✔
208

209
                Collection<TNamedEntity> l_ent = nameToEntity.get(ent.getName());
7✔
210
                l_ent.remove(ent);
4✔
211

212
                famixRepo.getElements().remove(ent);
6✔
213
        }
1✔
214
        
215
        protected void mapEntityToKey(IBinding key, TNamedEntity ent) {
216
                TNamedEntity old = keyToEntity.get(key);
6✔
217
                if (old != null) {
2!
218
                        entityToKey.remove(old);
×
219
                }
220
                keyToEntity.put(key, ent);
6✔
221
                entityToKey.put(ent, key);
6✔
222
        }
1✔
223
        
224
        /**
225
         * Returns all the Famix Entity with the given name and class 
226
         * @param fmxClass -- the subtype of Famix Entity we are looking for
227
         * @param name -- the name of the entity
228
         * @return the Collection of Famix Entities with the given name and class (possibly empty)
229
         */
230
        @SuppressWarnings("unchecked")
231
        public <T extends TNamedEntity> Collection<T> getEntityByName(java.lang.Class<T> fmxClass, String name) {
232
                Collection<T> ret = new LinkedList<T>();
4✔
233
                Collection<TNamedEntity> l_name = nameToEntity.get(name);
6✔
234
                
235
                if (l_name != null ) {
2✔
236
                        for (TNamedEntity obj : l_name) {
10✔
237
                                if (fmxClass.isInstance(obj)) {
4✔
238
                                        ret.add((T) obj);
4✔
239
                                }
240
                        }
1✔
241
                }
242

243
                return ret;
2✔
244
        }
245

246
        /**
247
         * Returns the Famix Entity associated to the given key.
248
         * <b>Note</b>: Be careful that ImplicitVariables share the same binding as their associated Class and cannot be retrieved with this method.
249
         * In such a case, this method will always retrieve the Class associated to the key.
250
         * To get an ImplicitVariable from the key, use {@link #getImplicitVariableByBinding(IBinding, String)}
251
         * @param key -- the key
252
         * @return the Famix Entity associated to the binding or null if not found
253
         */
254
        public TNamedEntity getEntityByKey(IBinding key) {
255
                if (key == null) {
2✔
256
                        return null;
2✔
257
                }
258
                else {
259
                        return keyToEntity.get(key);
6✔
260
                }
261
        }
262

263
        /**
264
         * Returns the key associated to a Famix Entity.
265
         * @param e -- the Named entity
266
         * @return the key associated to this entity or null if none
267
         */
268
        public IBinding getEntityKey(TNamedEntity e) {
269
                return entityToKey.get(e);
6✔
270
        }
271

272
        /**
273
         * Creates and returns a FAMIX Entity of the type <b>fmxjava.lang.Class</b>.
274
         * The Entity is always created.
275
         * @param fmxClass -- the FAMIX class of the instance to create
276
         * @param name -- the name of the new instance must not be null (and this is not tested)
277
         * @return the FAMIX Entity or null in case of a FAMIX error
278
         */
279
        protected <T extends TNamedEntity & TSourceEntity> T createFamixEntity(java.lang.Class<T> fmxClass, String name) {
280
                T fmx = null;
2✔
281

282
                if (name == null) {
2!
283
                        return null;
×
284
                }
285
                
286
                try {
287
                        fmx = fmxClass.getDeclaredConstructor().newInstance();
9✔
288
                } catch (java.lang.Exception e) {
×
289
                        System.err.println("Unexpected error, could not create a FAMIX entity: "+e.getMessage());
×
290
                        e.printStackTrace();
×
291
                }
1✔
292
                
293
                if (fmx != null) {
2!
294
                        fmx.setName(name);
3✔
295
                        if (fmx instanceof TCanBeStub) {
3!
296
                                ((TCanBeStub)fmx).setIsStub(Boolean.TRUE);
4✔
297
                        }
298

299
                        mapEntityToName(name, fmx);
4✔
300
                        
301
                        // put new entity in Famix repository
302
                        famixRepoAdd((Entity) fmx);
4✔
303
                }
304

305
                return fmx;
2✔
306
        }
307
        
308
        /**
309
         * Returns a Famix Entity of the type <b>fmxjava.lang.Class</b> and maps it to its binding <b>bnd</b> (if not null).
310
         * The Entity is created if it did not exist.
311
         * @param fmxClass -- the Famix class of the instance to create
312
         * @param bnd -- the binding to map to the new instance
313
         * @param name -- the name of the new instance (used if <pre>{@code bnd == null}</pre>)
314
         * @return the Famix Entity or null if <b>bnd</b> was null or in case of a Famix error
315
         */
316
        @SuppressWarnings("unchecked")
317
        protected <T extends TNamedEntity & TSourceEntity> T ensureFamixEntity(java.lang.Class<T> fmxClass, IBinding bnd, String name) {
318
                T fmx = null;
2✔
319
                
320
                /* 
321
                 * Unfortunately different entities with the same name and same type may exist
322
                 * e.g. 2 parameters of 2 different methods but having the same name
323
                 * so we cannot recover just from the name
324
                 */
325
                
326
                if (bnd != null) {
2✔
327
                        fmx = (T) getEntityByKey(bnd);
4✔
328
                        if (fmx != null) {
2✔
329
                                return fmx;
2✔
330
                        }
331
                }
332
                // else
333
                fmx = createFamixEntity(fmxClass, name);
5✔
334
                if ( (bnd != null) && (fmx != null) ) {
4!
335
                        keyToEntity.put(bnd, fmx);
6✔
336
                        entityToKey.put(fmx, bnd);
6✔
337
                }
338
                
339
                return fmx;
2✔
340
        }
341

342
        /**
343
         * Adds an already created Entity to the Famix repository
344
         * Used mainly for non-NamedEntity, for example relationships
345
         * @param e -- the Famix entity to add to the repository
346
         */
347
        public void famixRepoAdd(Entity e) {
348
                this.famixRepo.add(e);
4✔
349
        }
1✔
350

351

352
        /**
353
         * Returns a Famix ParametricClass with the given <b>name</b>, creating it if it does not exist yet
354
         * In the second case, sets some default properties: not Abstract, not Final, not Private, not Protected, not Public, not Interface
355
         * @param name -- the name of the Famix Class
356
         * @return the Famix Class or null in case of a Famix error
357
         */
358
        public ParametricClass ensureFamixParametricClass(ITypeBinding key, String name, TWithTypes owner) {
359
                ParametricClass fmx = ensureFamixEntity(ParametricClass.class, key, name);
7✔
360
                if(key != null) {
2✔
361
                        for (ITypeBinding tp : key.getErasure().getTypeParameters()) {
18✔
362
                                // If there is a type parameter, then fmx will be a Famix ParametricClass
363
                                // note: in Famix, the owner of the TypeParameter is the ParametricClass
364
                                TypeParameter fmxParam = ensureFamixTypeParameter(tp,
5✔
365
                                                tp.getName(), fmx);
2✔
366
                                fmxParam.setGenericEntity((TParametricEntity)fmx);
3✔
367
                                if (fmxParam != null) {
2!
368
                                        fmxParam.setIsStub(fmx.getIsStub());
4✔
369
                                }
370
                        }
371
                }
372
                
373
                fmx.setTypeContainer(owner);
3✔
374
                return fmx;
2✔
375
        }
376

377
        /**
378
         * Returns a Famix ParametricInterface with the given <b>name</b>, creating it if it does not exist yet
379
         * In the second case, sets some default properties: not Abstract, not Final, not Private, not Protected, not Public, not Interface
380
         * @param name -- the name of the Famix Class
381
         * @return the Famix Class or null in case of a Famix error
382
         */
383
        public ParametricInterface ensureFamixParametricInterface(ITypeBinding key, String name, TWithTypes owner) {
384
                ParametricInterface fmx = ensureFamixEntity(ParametricInterface.class, key, name);
7✔
385
                if(key != null) {
2!
386
                        for (ITypeBinding tp : key.getTypeParameters()) {
17✔
387
                                // If there is a type parameter, then fmx will be a Famix ParametricInterface
388
                                // note: in Famix, the owner of the TypeParameter is the ParametricInterface
389
                                TypeParameter fmxParam = ensureFamixTypeParameter(tp,
5✔
390
                                                tp.getName(), fmx);
2✔
391
                                fmxParam.setGenericEntity((ParametricInterface)fmx);
3✔
392
                                if (fmxParam != null) {
2!
393
                                        fmxParam.setIsStub(false);
4✔
394
                                }
395
                        }
396
                }
397
                fmx.setTypeContainer(owner);
3✔
398
                return fmx;
2✔
399
        }
400

401
        public AnnotationInstanceAttribute createFamixAnnotationInstanceAttribute(AnnotationTypeAttribute att, String value) {
402
                AnnotationInstanceAttribute fmx = null;
2✔
403
                if ( (att != null) && (value != null) ) {
4!
404
                        fmx = new AnnotationInstanceAttribute();
4✔
405
                        fmx.setAnnotationTypeAttribute(att);
3✔
406
                        fmx.setValue(value);
3✔
407
                        this.famixRepo.add(fmx);
4✔
408
                }
409
                return fmx;
2✔
410
        }
411

412
        public AnnotationInstance addFamixAnnotationInstance(TWithAnnotationInstances fmx, AnnotationType annType, Collection<AnnotationInstanceAttribute> annAtts) {
413
                AnnotationInstance inst = null;
2✔
414
                if ( (fmx != null) && (annType != null) ) {
4!
415
                        inst = new AnnotationInstance();
4✔
416
                        inst.setAnnotatedEntity(fmx);
3✔
417
                        inst.setAnnotationType(annType);
3✔
418
                        inst.addAttributes(annAtts);
3✔
419
                        this.famixRepo.add(inst);
4✔
420
                }
421
                return inst;
2✔
422
        }
423

424
        ///// ensure Famix Relationships /////
425

426
        /**
427
         * Returns a Famix Inheritance relationship between two Famix Classes creating it if needed
428
         * @param sup -- the super class
429
         * @param sub -- the sub class
430
         * @param prev -- previous inheritance relationship in the same context
431
         * @return the Inheritance relationship
432
         */
433
        public Inheritance ensureFamixInheritance(TWithInheritances sup, TWithInheritances sub, TAssociation prev, ITypeBinding supBnd) {
434
                if ( (sup == null) || (sub == null) ) {
4!
435
                        return null;
×
436
                }
437

438
                // Does the inheritance already exist?
439
                for (TInheritance i : (sup).getSubInheritances()) {                        
11✔
440
                        if (i.getSubclass() == sub) {
4✔
441
                                return (Inheritance) i;
3✔
442
                        }
443
                }
1✔
444

445
                Inheritance inh;
446
                if (supBnd != null && supBnd.isParameterizedType()) { // Needs checks and tests.
5✔
447
                        inh = (ParametricInheritance)buildFamixParametricAssociation(new ParametricInheritance(), supBnd.getErasure().getTypeParameters(), supBnd.getTypeArguments());
13✔
448
                } else {
449
                        inh = new Inheritance();
4✔
450
                }
451

452
                inh.setSuperclass(sup);
3✔
453
                inh.setSubclass(sub);
3✔
454
                chainPrevNext(prev, inh);
4✔
455
                famixRepoAdd(inh);
3✔
456
                return inh;
2✔
457
        }
458
        
459
        /**
460
         * Creates the concretization between the type parameters of the generic entity that is target of an association 
461
         * and the concrete types that concretize them in this association.
462
         * @param association -- the association that must be linked to # or several concretizations
463
         * @param genericTypes -- the collection of type parameters declared in the generic entity
464
         * @param typeArguments -- the collection of concrete types linked to this association
465
         * @return the parametric association
466
         */
467
        public  <T extends TParametricEntity> TParametricAssociation buildFamixParametricAssociation(TParametricAssociation association, ITypeBinding[] genericTypes, ITypeBinding[] typeArguments
468
        ) {
469
                
470
                Iterator<ITypeBinding> genericIterator = Arrays.asList(genericTypes).iterator();
4✔
471
                Iterator<ITypeBinding> concreteIterator = Arrays.asList(typeArguments).iterator();
4✔
472

473
                while (concreteIterator.hasNext() && genericIterator.hasNext()) {
6!
474
                        TTypeArgument typeArgument = (TTypeArgument)ensureFamixType(concreteIterator.next());
7✔
475
                        TypeParameter typeParameter = (TypeParameter)ensureFamixType(genericIterator.next());
7✔
476

477
                        Concretization concretization = ensureFamixConcretization(typeArgument, typeParameter);
5✔
478
                        association.addConcretization(concretization);
3✔
479
                }
1✔
480

481
                return association;
2✔
482
        }
483

484
        /**
485
         * Returns a Famix Concretization relationship between a Concrete Type and a ParameterType
486
         * @param typeArgument -- the concrete type
487
         * @param typeParameter -- the generic type parameter
488
         * @return the Concretization relationship
489
         */
490
        public Concretization ensureFamixConcretization(TTypeArgument typeArgument, TypeParameter typeParameter ) {
491
                if ( (typeArgument == null) || (typeParameter == null) ) {
4!
UNCOV
492
                        return null;
×
493
                }
494

495
                Concretization concretization = new Concretization();
4✔
496
                concretization.setTypeArgument(typeArgument);
3✔
497
                concretization.setTypeParameter(typeParameter);
3✔
498

499
                famixRepoAdd(concretization);
3✔
500
                return concretization;
2✔
501
        }
502
        
503
                /**
504
         * Returns a Famix Implementation relationship between two Famix Classes creating it if needed
505
         * @param myInterface -- the implemented interface
506
         * @param implementingClass -- the implementing class
507
         * @param prev -- previous inheritance relationship in the same context
508
         * @return the Inheritance relationship
509
         */
510
        public Implementation ensureFamixImplementation(TImplementable myInterface, TCanImplement implementingClass, TAssociation prev, ITypeBinding supBnd) {
511
                if ( (myInterface == null) || (implementingClass == null) ) {
4!
512
                        return null;
×
513
                }
514

515
                for (TImplementation imp : myInterface.getImplementations()) {
11✔
516
                        if (imp.getImplementingClass() == implementingClass) {
4✔
517
                                return (Implementation) imp;
3✔
518
                        }
519
                }
1✔
520
                
521
                Implementation implementation;
522
                if (supBnd != null && supBnd.isParameterizedType()) { // Needs checks and tests.
5!
523
                        implementation = (ParametricImplementation)buildFamixParametricAssociation(new ParametricImplementation(), supBnd.getErasure().getTypeParameters(), supBnd.getTypeArguments());
13✔
524
                } else {
525
                        implementation = new Implementation();
4✔
526
                }
527

528
                implementation.setImplementingClass(implementingClass);
3✔
529
                implementation.setMyInterface(myInterface);
3✔
530
                chainPrevNext(prev, implementation);
4✔
531
                famixRepoAdd(implementation);
3✔
532
                return implementation;
2✔
533
        }
534

535
        public void ensureImplementedInterfaces(ITypeBinding bnd, TType fmx, TWithTypes owner, TAssociation lastAssociation) {
536
                for (ITypeBinding intbnd : bnd.getInterfaces()) {
17✔
537
                        TType superTyp;
538
                        if(intbnd.isClass()){
3✔
539
                                superTyp = this.ensureFamixInterface(intbnd, intbnd.getName(), null, intbnd.isGenericType() || intbnd.isParameterizedType() || intbnd.isRawType(), intbnd.getModifiers());
20!
540
                        }else {
541
                                superTyp = this.ensureFamixType(intbnd);
4✔
542
                        }
543
                        
544
                        if (bnd.isInterface()) {
3✔
545
                                // in Java "subtyping" link between 2 interfaces is call inheritance 
546
                                lastAssociation = ensureFamixInheritance((TWithInheritances)superTyp, (TWithInheritances)fmx, lastAssociation, intbnd);
10✔
547
                        }
548
                        else {
549
                                lastAssociation = ensureFamixImplementation((TImplementable)superTyp, (TCanImplement)fmx, lastAssociation, intbnd);
9✔
550
                        }
551
                }
552
        }
1✔
553

554
        /**
555
         * Returns a Famix Reference between two Famix Entities creating it if needed.<br>
556
         * If <code>prev == null</code> and a similar reference already exist (same <code>src</code>, same <code>tgt</code>), does not create a new one
557
         * @param src -- source of the reference
558
         * @param tgt -- target of the reference
559
         * @param prev -- previous reference relationship in the same context
560
         * @return the FamixReference
561
         */
562
        public Reference addFamixReference(Method src, TType tgt, TAssociation prev) {
563
                if ( (src == null) || (tgt == null) ) {
4!
564
                        return null;
×
565
                }
566

567
                if (prev == null) {
2✔
568
                        for (TReference ref : src.getOutgoingReferences()) {
7!
569
                                if (ref.getReferredEntity() == tgt) {
×
570
                                        return (Reference) ref;
×
571
                                }
572
                        }
×
573
                }
574

575
                Reference ref = new Reference();
4✔
576
                ref.setReferredEntity(tgt);
3✔
577
                ref.setReferencer(src);
3✔
578
                chainPrevNext(prev,ref);
4✔
579
                famixRepoAdd(ref);
3✔
580

581
                return ref;
2✔
582
        }
583

584
        /**
585
         * Returns a Famix Invocation between two Famix Entities creating it if needed
586
         * @param tMethod of the invocation
587
         * @param invoked -- method invoked
588
         * @param receiver of the invocation
589
         * @param signature -- i.e. actual invocation code
590
         * @param prev -- previous invocation relationship in the same context
591
         * @return the FamixInvocation
592
         */
593
        public Invocation addFamixInvocation(TMethod tMethod, TMethod invoked, TInvocationsReceiver receiver, String signature, TAssociation prev, IMethodBinding invokedBnd) {
594
                if ( (tMethod == null) || (invoked == null) ) {
4!
595
                        return null;
×
596
                }
597
                Invocation invocation;
598
                if (invokedBnd != null && invokedBnd.isParameterizedMethod()) {
5✔
599
                        invocation = (ParametricInvocation)buildFamixParametricAssociation(new ParametricInvocation(), invokedBnd.getMethodDeclaration().getTypeParameters(), invokedBnd.getTypeArguments());
13✔
600
                } else if ( invokedBnd != null && invokedBnd.isConstructor() && invokedBnd.getMethodDeclaration().getDeclaringClass().isGenericType()) {
10✔
601
                        invocation = (ParametricInvocation)buildFamixParametricAssociation(new ParametricInvocation(), invokedBnd.getMethodDeclaration().getDeclaringClass().getTypeParameters(), invokedBnd.getDeclaringClass().getTypeArguments());
15✔
602
                } else {
603
                        invocation = new Invocation();
4✔
604
                }
605

606
                invocation.setReceiver(receiver);
3✔
607
                invocation.setSender(tMethod);
3✔
608
                invocation.setSignature((signature == null) ? invoked.getSignature() : signature);
5!
609
                invocation.addCandidates(invoked);
3✔
610
                chainPrevNext(prev,invocation);
4✔
611
                famixRepoAdd(invocation);
3✔
612
                
613
                return invocation;
2✔
614
        }
615

616
        /**
617
         * Returns a Famix Access between two Famix Entities creating it if needed
618
         * @param accessor -- the entity (presumably a method) accessing the attribute
619
         * @param var -- the variable accessed
620
         * @param isWrite -- whether this is an access for reading or writing in the variable
621
         * @param prev -- previous access relationship in the same context
622
         * @return the FamixAccess
623
         */
624
        public Access addFamixAccess(TWithAccesses accessor, TStructuralEntity var, boolean isWrite, TAssociation prev) {
625
                if ( (accessor == null) || (var == null) ) {
4!
626
                        return null;
×
627
                }
628
                Access acc = new Access();
4✔
629
                acc.setAccessor(accessor);
3✔
630
                acc.setVariable((TAccessible) var);
3✔
631
                acc.setIsWrite(isWrite);
4✔
632
                chainPrevNext(prev, acc);
4✔
633
                famixRepoAdd(acc);
3✔
634
                
635
                return acc;
2✔
636
        }
637

638
        protected void chainPrevNext(TAssociation prev, TAssociation next) {
639
                if (prev != null) {
2✔
640
                        next.setPrevious(prev);  // not yet implemented in importer
3✔
641
                }
642
        }
1✔
643
        
644
        /**
645
         * Returns a Famix DeclaredException between a method and an Exception that it declares to throw
646
         * @param meth -- the method throwing the exception
647
         * @param excep -- the exception declared to be thrown
648
         * @return the DeclaredException
649
         */
650
        public TThrowable createFamixDeclaredException(Method meth, TThrowable excep) {
651
                if ( (meth == null) || (excep == null) ) {
4!
652
                        return null;
×
653
                }
654
                //org.moosetechnology.model.famixjava.famixjavaentities.Exception decl = new org.moosetechnology.model.famixjava.famixjavaentities.Exception();
655
                // decl.setExceptionClass(excep);
656
                // excep.getDeclaringEntities().add(meth);
657
                // famixRepoAdd(excep);
658
                meth.getDeclaredExceptions().add(excep);
5✔
659
                return excep;
2✔
660
        }
661

662
        /**
663
         * Returns a Famix CaughtException between a method and an Exception that is caught
664
         * @param meth -- the method catching the exception
665
         * @param excep -- the exception caught
666
         * @return the CaughtException
667
         */
668
        public TThrowable createFamixCaughtException(Method meth, TThrowable excep) {
669
                if ( (meth == null) || (excep == null) ) {
4!
670
                        return null;
×
671
                }
672
                // CaughtException decl = new CaughtException();
673
                // decl.setExceptionClass(excep);
674
                // decl.setDefiningEntity(meth);
675
                // famixRepoAdd(decl);
676
                meth.getCaughtExceptions().add(excep);
5✔
677
                return excep;
2✔
678
        }
679

680
        /**
681
         * Returns a Famix ThrownException between a method and an Exception that it (actually) throws.
682
         * Note: DeclaredException indicates that the method declares it can throw the exception,
683
         * here we state that the exception is actually thrown
684
         * @param meth -- the method throwing the exception
685
         * @param excep -- the exception thrown
686
         * @return the ThrownException
687
         */
688
        public TThrowable createFamixThrownException(Method meth, TThrowable excep) {
689
                if ( (meth == null) || (excep == null) ) {
4!
690
                        return null;
×
691
                }
692
                // ThrownException decl = new ThrownException();
693
                // decl.setExceptionClass(excep);
694
                // decl.setDefiningEntity(meth);
695
                // famixRepoAdd(decl);
696
                meth.getThrownExceptions().add(excep);
5✔
697
                return excep;
2✔
698
        }
699

700

701
        /**
702
         * Returns a Famix EntityTyping between a typed entity and a type.
703
         * @param typedEntity -- the typed entity
704
         * @param declaredType -- the declared type
705
         * @return the FamixEntityTyping
706
         */
707
        public EntityTyping ensureFamixEntityTyping(ITypeBinding declaredTypeBnd, TTypedEntity typedEntity, TType declaredType) {
708
                if ( (typedEntity == null) || (declaredType == null) ) {
4!
709
                        return null;
2✔
710
                }
711
                EntityTyping typing;
712
                if (declaredTypeBnd != null && declaredTypeBnd.isParameterizedType()) {
5✔
713
                        typing = (ParametricEntityTyping)buildFamixParametricAssociation(new ParametricEntityTyping(), declaredTypeBnd.getErasure().getTypeParameters(), declaredTypeBnd.getTypeArguments());
13✔
714
                } else {
715
                        typing = new EntityTyping();
4✔
716
                }
717
                typing.setTypedEntity(typedEntity);
3✔
718
                typing.setDeclaredType(declaredType);
3✔
719
                famixRepoAdd(typing);
3✔
720
                
721
                return typing;
2✔
722
        }
723

724

725
        ///// Special Case: ImplicitVariables /////
726

727
        /**
728
         * Returns the Famix ImplicitVariable associated to the given binding and name (self or super).
729
         * See also {@link #getEntityByKey(IBinding)}
730
         * @param bnd -- the binding
731
         * @return the Famix Entity associated to the binding or null if not found
732
         */
733
        @Deprecated
734
        public ImplicitVariable getImplicitVariableByBinding(IBinding bnd, String iv_name) {
735
                return getImplicitVariableByType((Class)getEntityByKey(bnd), iv_name);
×
736
        }
737
        
738
        /**
739
         * Returns the Famix ImplicitVariable associated to the given FamixType.
740
         * @param type -- the FamixType
741
         * @param name -- name of the ImplicitVariable (should be Dictionary.SELF_NAME or Dictionary.SUPER_NAME)
742
         * @return the Famix ImplicitVariable associated to the Type or null if not found
743
         */
744
        @Deprecated
745
        public ImplicitVariable getImplicitVariableByType(Type type, String name) {
746
                ImplicitVars iv = typeToImpVar.get(type);
×
747
                ImplicitVariable ret = null;
×
748
                
749
                if (iv == null) {
×
750
                        iv = new ImplicitVars();
×
751
                }
752
                
753
                if (name.equals(SELF_NAME)) {
×
754
                        ret = iv.self_iv;
×
755
                }
756
                else if (name.equals(SUPER_NAME)) {
×
757
                        ret = iv.super_iv;
×
758
                }
759

760
                return ret;
×
761
        }
762

763
        ///// Special Case: "Uniq" Entities /////
764

765
        /**
766
         * Creates or recovers a Famix Named Entity uniq for the given name.
767
         * For some specific entities we don't allow two of them with the same name.
768
         * This is the case e.g. for the default package, or the Java class "Object" and its package "java.lang".
769
         * @param fmxClass -- the Famix class of the instance to create
770
         * @param key -- a potential binding for the entity
771
         * @param name -- the name of the new instance (used if <pre>{@code bnd == null}</pre>)
772
         * @return the uniq Famix Entity for this binding and/or name
773
         */
774
        @SuppressWarnings("unchecked")
775
        public <T extends NamedEntity> T ensureFamixUniqEntity(java.lang.Class<T> fmxClass, IBinding key, String name) {
776
                T fmx = null;
2✔
777
                
778
                if (name == null) {
2!
779
                        return null;
×
780
                }
781
                
782
                if (key != null) {
2✔
783
                        fmx = (T) getEntityByKey(key);
5✔
784
                }
785
                
786
                if (fmx == null) {
2✔
787
                        Collection<T> l = getEntityByName( fmxClass, name);
5✔
788
                        if (l.size() > 0) {
3✔
789
                                fmx = l.iterator().next();
6✔
790
                        }
791
                        else {
792
                                fmx = createFamixEntity(fmxClass, name);
6✔
793
                        }
794
                        
795
                        if (key != null) {
2✔
796
                                // may happen for example if the entity was first created without binding
797
                                // and we find a binding for it later
798
                                keyToEntity.put(key, fmx);
6✔
799
                        }
800
                }
801

802
                return fmx;
2✔
803
        }
804

805
        /**
806
         * Creates or recovers the Famix Class that will own all stub methods (for which the real owner is unknown)
807
         *
808
         * @return a Famix class
809
         */
810
        public Class ensureFamixClassStubOwner() {
811
                Class fmx =  ensureFamixUniqEntity(Class.class, null, STUB_METHOD_CONTAINER_NAME);
7✔
812
                if (fmx != null) {
2!
813
                        fmx.setTypeContainer( ensureFamixPackageDefault());
4✔
814
                }
815
                ensureFamixInheritance(ensureFamixClassObject(null), fmx, /*prev*/null, null);
9✔
816

817
                return fmx;
2✔
818
        }
819

820
        public Type searchTypeInContext(String name, TWithTypes ctxt) {
821
                if (ctxt == null) {
×
822
                        return null;
×
823
                }
824
                
825
                for (TType candidate : ctxt.getTypes()) {
×
826
                        if (((TNamedEntity)candidate).getName().equals(name) ) {
×
827
                                return (Type) candidate;
×
828
                        }
829
                }
×
830
                
831
                return searchTypeInContext(name, Util.getOwner((TNamedEntity)ctxt));
×
832
        }
833

834
        /**
835
         * Returns a Famix Package associated with its IPackageBinding and/or fully qualified name.
836
         * The Entity is created if it does not exist.
837
         * We assume that Namespaces must be uniq for a given name
838
         * Also creates or recovers recusively it's parent namespaces.<br>
839
         * At least one of <b>bnd</b> and <b>name</b> must be non null.
840
         *
841
         * @param bnd  -- the JDT Binding that may be used as a uniq key to recover this namespace
842
         * @param name -- fully qualified name of the namespace (e.g. 'java.lang')
843
         * @return the Famix Namespace found or created. May return null in case of a Famix error
844
         */
845
        public Package ensureFamixPackage(IPackageBinding bnd, String name) {
846
                Package fmx = null;
2✔
847
                Package parent = null;
2✔
848

849
                if ((name == null) && (bnd != null)) {
4!
850
                        name = bnd.getName();
3✔
851
                }
852

853
                if ((name == null) || name.equals("")) {
6!
854
                        return ensureFamixPackageDefault();
3✔
855
                } else {
856
                        /* Note: Packages are created with their fully-qualified name to simplify recovering when we don't have a binding
857
                         * (for example when creating parent packages of a package we have a binding for).
858
                         * Because the preferred solution in Moose is to give their simple names to packages, they must be post-processed when
859
                         * all is said and done. */
860
                        fmx = ensureFamixUniqEntity(Package.class, bnd, name);
7✔
861
                        String parentName = removeLastPartOfPackageName(name);
4✔
862
                        if (parentName.length() > 0) {
3✔
863
                                parent = ensureFamixPackage(null, parentName);
5✔
864
                                // set the parentscope relationship
865
                                if ((parent != null) && (fmx != null) && (fmx.getParentPackage() == null)) {
7!
866
                                        parent.addChildEntities(fmx);
3✔
867
                                }
868
                        }
869
                }
870

871
                return fmx;
2✔
872
        }
873

874
        /**
875
         * Creates or recovers a default Famix Package.
876
         * Because this package does not really exist, it has no binding.
877
         *
878
         * @return a Famix Namespace
879
         */
880
        public Package ensureFamixPackageDefault() {
881
                Package fmx = ensureFamixUniqEntity(Package.class, null, DEFAULT_PCKG_NAME);
7✔
882

883
                return fmx;
2✔
884
        }
885

886
        /**
887
         * Creates or recovers a Famix Package for the package of Java class "Object" (i.e. "java.lang").
888
         * Because "Object" is the root of the inheritance tree, it needs to be treated differently.
889
         *
890
         * @param bnd -- a potential binding for the "java.lang" package
891
         * @return a Famix Namespace for "java.lang"
892
         */
893
        public Package ensureFamixPackageJavaLang(IPackageBinding bnd) {
894
                Package fmx = this.ensureFamixPackage(bnd, OBJECT_PACKAGE_NAME);
5✔
895

896
                return fmx;
2✔
897
        }
898

899
        /**
900
         * Returns the Package with {@link #DEFAULT_PCKG_NAME} or <code>null</code> if not found
901
         */
902
        public Package getFamixPackageDefault() {
903
                Collection<Package> l = getEntityByName(Package.class, DEFAULT_PCKG_NAME);
5✔
904
                if (l.size() > 0) {
3!
905
                        return l.iterator().next();
5✔
906
                } else {
907
                        return null;
×
908
                }
909
        }
910

911
        /**
912
         * Returns a Famix Type with the given <b>name</b>, creating it if it does not exist yet.
913
         * In the second case, sets some default properties: not Abstract, not Final, not Private, not Protected, not Public, not Interface
914
         * @param bnd -- binding for the type to create
915
         * @param name of the type
916
         * @param owner of the type
917
         * @param ctxt -- context of use of the type
918
         */
919
        public TType ensureFamixType(ITypeBinding bnd, String name, TWithTypes owner, TWithTypes ctxt, int modifiers) {
920
                
921
                TType fmx = null;
2✔
922

923
                if (bnd == null) {
2✔
924
                        if (name == null) {
2!
925
                                return null;
2✔
926
                        }
927
                        fmx = searchTypeInContext(name, ctxt); // WildCard Types don't have binding
×
928
                        if (fmx != null) {
×
929
                                return fmx;
×
930
                        }
931

932
                        if ( (owner != null) && (owner instanceof TParametricEntity) ) {
×
NEW
933
                                return this.ensureFamixTypeParameter(null, name, owner);
×
934
                        }
935
                        else {
936
                                fmx = ensureFamixEntity(Type.class, bnd, name);
×
937
                                fmx.setTypeContainer(owner);
×
938
                                return fmx;
×
939
                        }
940
                }
941

942
                // bnd != null
943

944
                fmx = (TType) getEntityByKey(bnd);
5✔
945
                if (fmx != null) {
2✔
946
                        return fmx;
2✔
947
                }
948

949
                if (bnd.isArray()) {
3!
950
                        bnd = bnd.getElementType();
×
951
                }
952

953
                if (bnd.isPrimitive()) {
3✔
954
                        return this.ensureFamixPrimitiveType(bnd, name);
5✔
955
                }
956

957
                if (bnd.isEnum()) {
3!
958
                        return this.ensureFamixEnum(bnd, name, (ContainerEntity) owner);
×
959
                }
960

961
                if ((bnd.isRawType() || bnd.isGenericType()) && !bnd.isInterface() ) {
9✔
962
                        return this.ensureFamixClass(bnd.getErasure(), name, (TNamedEntity) owner, /*isGeneric*/true, modifiers);
10✔
963
                }
964
                
965
                if (bnd.isAnnotation()) {
3!
966
                        return this.ensureFamixAnnotationType(bnd, name, (ContainerEntity) owner);
×
967
                }
968

969
                if (bnd.isInterface()) {
3✔
970
                        return this.ensureFamixInterface(bnd, name, owner, /*isGeneric*/bnd.isGenericType() || bnd.isParameterizedType() || bnd.isRawType(), modifiers);
19✔
971
                }
972

973
                if (isThrowable(bnd)) {
4✔
974
                        return this.ensureFamixException(bnd, name, owner, /*isGeneric*/false, modifiers);
8✔
975
                }
976
                if (bnd.isClass()) {
3✔
977
                        return this.ensureFamixClass(bnd, name, (TNamedEntity) owner, /*isGeneric*/bnd.isGenericType() || bnd.isParameterizedType() || bnd.isRawType(), modifiers);
20!
978
                }
979
                if(bnd.isWildcardType()) {
3✔
980
                        return this.ensureFamixWildcardType(bnd, name, (TParametricEntity)owner, ctxt);
8✔
981
                }
982

983
                //otherwise (none of the above)
984

985
                if (name == null) {
2✔
986
                        name = bnd.getName();
3✔
987
                }
988

989
                if (owner == null) {
2!
990
                        owner = (TWithTypes) this.ensureOwner(bnd);
5✔
991
                }
992

993
                if (bnd.isTypeVariable() ) {
3!
994
                        fmx = ensureFamixTypeParameter(bnd, name, owner);
6✔
995
                        return fmx;
2✔
996
                }
997

998
                fmx = ensureFamixEntity(Type.class, bnd, name);
×
999
                fmx.setTypeContainer(owner);
×
1000
                return fmx;
×
1001
        }
1002

1003
        public TType ensureFamixType(ITypeBinding bnd, TWithTypes context) {
1004
                int modifiers = extractModifierOfTypeFrom(bnd);
4✔
1005
                return ensureFamixType(bnd, /*name*/null, /*owner*/null, context, modifiers);
8✔
1006
        }
1007
        
1008
        public TType ensureFamixType(ITypeBinding bnd) {
1009
                return ensureFamixType(bnd, /*ctxt*/null);
5✔
1010
        }
1011

1012
        private int extractModifierOfTypeFrom(ITypeBinding bnd) {
1013
                int modifiers = (bnd != null) ? bnd.getModifiers() : UNKNOWN_MODIFIERS;
7✔
1014
                return modifiers;
2✔
1015
        }
1016

1017
        public boolean isThrowable(ITypeBinding bnd) {
1018
                if (bnd == null) {
2!
1019
                        return false;
×
1020
                }
1021
                if (bnd.getQualifiedName().equals("java.lang.Throwable")) {
5✔
1022
                        return true;
2✔
1023
                } else if (bnd.getQualifiedName().equals("java.lang.Object")) {
5✔
1024
                        return false;
2✔
1025
                }
1026
                else {
1027
                        return isThrowable(bnd.getSuperclass());
5✔
1028
                }
1029
        }
1030

1031
        /**
1032
         * Returns a Famix Class associated with the ITypeBinding.
1033
         * The Entity is created if it does not exist.
1034
         * @param name -- the name of the FAMIX Method (MUST NOT be null, but this is not checked)
1035
         * @param owner -- type defining the method (should not be null, but it will work if it is) 
1036
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
1037
         */
1038
        @SuppressWarnings("deprecation")
1039
        public Class ensureFamixClass(ITypeBinding bnd, String name, TNamedEntity owner, boolean isGeneric, int modifiers) {
1040
                Class fmx = null;
2✔
1041

1042
                // --------------- some special cases
1043
                if (bnd != null) {
2✔
1044
                        if (bnd.isArray()) {
3!
1045
                                bnd = bnd.getElementType();
×
1046
                        }
1047

1048
                        // for inner classes defined in generics !!! For others should not change anything
1049
                        bnd = bnd.getErasure();
3✔
1050
                }
1051

1052
                // ---------------- to avoid useless computations if we can
1053
                fmx = (Class) getEntityByKey(bnd);
5✔
1054
                if (fmx != null) {
2✔
1055
                        return fmx;
2✔
1056
                }
1057

1058
                // --------------- name
1059
                if (name == null) {
2✔
1060
                        if (bnd == null) {
2!
1061
                                return null;  // not much we can do
×
1062
                        } else if (!bnd.isAnonymous()) {
3!
1063
                                name = bnd.getErasure().getName();  // for generics, will give the "core" type name, for normal type, won't change anything
5✔
1064
                        } else { // anonymous class
1065
                                if (bnd.getSuperclass() != null) {
×
1066
                                        name = bnd.getSuperclass().getName();
×
1067
                                }
1068
                                if ((name == null) || name.equals(OBJECT_NAME)) {
×
1069
                                        ITypeBinding[] intfcs = bnd.getInterfaces();
×
1070
                                        if ((intfcs != null) && (intfcs.length > 0)) {
×
1071
                                                name = bnd.getInterfaces()[0].getName();
×
1072
                                        }
1073
                                        else {
1074
                                                name = "???";
×
1075
                                        }
1076
                                }
1077
                                name = ANONYMOUS_NAME_PREFIX + "(" + name + ")";
×
1078
                        }
1079
                }
1080

1081
                if (name.equals(OBJECT_NAME)) { // TODO && owner == java.lang
4✔
1082
                        return ensureFamixClassObject(bnd);
4✔
1083
                }
1084

1085
                // --------------- owner
1086
                if (owner == null) {
2✔
1087
                        if (bnd != null) {
2✔
1088
                                owner = ensureOwner(bnd);
4✔
1089
                        }
1090
                        /*                                owner = ensureFamixPackageDefault();
1091
                        } else {*/
1092
                }
1093

1094
                // --------------- recover from name ?
1095
                if (owner != null) {
2✔
1096
                        for (Class candidate : this.getEntityByName(Class.class, name)) {
13✔
1097
                                if (matchAndMapClass(bnd, name, (ContainerEntity) owner, candidate)) {
8✔
1098
                                        fmx = candidate;
2✔
1099
                                        break;
1✔
1100
                                }
1101
                        }
1✔
1102
                }
1103

1104
                // ---------------- create
1105
                if (fmx == null) {
2✔
1106
                        if (isGeneric) {
2✔
1107
                                fmx = ensureFamixParametricClass(bnd, name, (TWithTypes) owner);
8✔
1108
                        }
1109
                        else {
1110
                                fmx = ensureFamixEntity(Class.class, bnd, name);
7✔
1111
                                fmx.setTypeContainer((TWithTypes)owner);
4✔
1112
                        }
1113
                }
1114

1115
                if (fmx!=null) {
2!
1116
                        // we just created it or it was not bound, so we make sure it has the right information in it
1117
                        if (bnd != null) {
2✔
1118
                                setClassModifiers(fmx, bnd.getDeclaredModifiers());
5✔
1119
                        }
1120
                        TAssociation lastAssoc = null;
2✔
1121

1122
                        if (bnd != null) {
2✔
1123
                                ITypeBinding supbnd = bnd.getSuperclass();
3✔
1124
                                if (supbnd != null) {
2✔
1125
                                        lastAssoc = ensureFamixInheritance((TWithInheritances) ensureFamixType(supbnd), fmx, lastAssoc, supbnd);
11✔
1126
                                }
1127
                                else {
1128
                                        lastAssoc = ensureFamixInheritance((TWithInheritances) ensureFamixClassObject(null), fmx, lastAssoc, null);
9✔
1129
                                }
1130
                                ensureImplementedInterfaces(bnd, fmx, (TWithTypes) owner, lastAssoc);
7✔
1131
                        }
1132
                }
1133

1134
                return fmx;
2✔
1135
        }
1136

1137
        /**
1138
         * Returns a Famix Exception associated with the ITypeBinding.
1139
         * The Entity is created if it does not exist.
1140
         * @param name -- the name of the Famix Exception (MUST NOT be null, but this is not checked)
1141
         * @param owner -- type defining the Exception (should not be null, but it will work if it is) 
1142
         *
1143
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
1144
         */
1145
        public <T extends TWithTypes & TNamedEntity> Exception ensureFamixException(ITypeBinding bnd, String name, TWithTypes owner, boolean isGeneric, int modifiers) {
1146
                Exception fmx = null;
2✔
1147

1148
                // --------------- some special cases
1149
                if (bnd != null) {
2✔
1150
                        if (bnd.isArray()) {
3!
1151
                                bnd = bnd.getElementType();
×
1152
                        }
1153

1154
                        // for inner classes defined in generics !!! For others should not change anything
1155
                        bnd = bnd.getErasure();
3✔
1156
                }
1157

1158
                // ---------------- to avoid useless computations if we can
1159
                fmx = (Exception) getEntityByKey(bnd);
5✔
1160
                if (fmx != null) {
2✔
1161
                        return fmx;
2✔
1162
                }
1163

1164
                // --------------- name
1165
                if (name == null) {
2✔
1166
                        if (bnd == null) {
2!
1167
                                return null;  // not much we can do
×
1168
                        } else if (!bnd.isAnonymous()) {
3!
1169
                                name = bnd.getErasure().getName();  // for generics, will give the "core" type name, for normal type, won't change anything
5✔
1170
                        } else { // anonymous class
1171
                                if (bnd.getSuperclass() != null) {
×
1172
                                        name = bnd.getSuperclass().getName();
×
1173
                                }
1174
                                if ((name == null) || name.equals(OBJECT_NAME)) {
×
1175
                                        ITypeBinding[] intfcs = bnd.getInterfaces();
×
1176
                                        if ((intfcs != null) && (intfcs.length > 0)) {
×
1177
                                                name = bnd.getInterfaces()[0].getName();
×
1178
                                        }
1179
                                        else {
1180
                                                name = "???";
×
1181
                                        }
1182
                                }
1183
                                name = ANONYMOUS_NAME_PREFIX + "(" + name + ")";
×
1184
                        }
1185
                }
1186

1187
                // --------------- owner
1188
                if (owner == null) {
2✔
1189
                        if (bnd == null) {
2✔
1190
                                owner = ensureFamixPackageDefault();
4✔
1191
                        } else {
1192
                                owner = (TWithTypes) ensureOwner(bnd);
5✔
1193
                        }
1194
                }
1195

1196
                // --------------- recover from name ?
1197
                for (Exception candidate : this.getEntityByName(Exception.class, name)) {
13✔
1198
                        if (matchAndMapClass(bnd, name, (T) owner, candidate)) {
8✔
1199
                                fmx = candidate;
2✔
1200
                                break;
1✔
1201
                        }
1202
                }
1✔
1203

1204
                // ---------------- create
1205
                if (fmx == null) {
2✔
1206
                        fmx = ensureFamixEntity(Exception.class, bnd, name);
7✔
1207
                        fmx.setTypeContainer(owner);
3✔
1208
                }
1209

1210
                if (fmx!=null) {
2!
1211
                        // we just created it or it was not bound, so we make sure it has the right information in it
1212
                        TAssociation lastAssoc = null;
2✔
1213
                        if (bnd != null) {
2✔
1214
                                ITypeBinding supbnd = bnd.getSuperclass();
3✔
1215
                                if (supbnd != null) {
2!
1216
                                        lastAssoc = ensureFamixInheritance((TWithInheritances) ensureFamixType(supbnd), fmx, lastAssoc, supbnd);
11✔
1217
                                }
1218
                                else {
NEW
1219
                                        lastAssoc = ensureFamixInheritance((TWithInheritances) ensureFamixClassObject(null), fmx, lastAssoc, null);
×
1220
                                }
1221
                                ensureImplementedInterfaces(bnd, fmx, owner, lastAssoc);
6✔
1222
                        }
1223
                }
1224

1225
                return fmx;
2✔
1226
        }
1227

1228
        /**
1229
         * Returns a FAMIX Interface with the given <b>name</b>, creating it if it does not exist yet.
1230
         * @param name -- the name of the FAMIX Method (MUST NOT be null, but this is not checked)
1231
         * @param owner -- type defining the method (should not be null, but it will work if it is) 
1232
         * @return the FAMIX Class or null in case of a FAMIX error
1233
         */
1234
        public <T extends TWithTypes & TNamedEntity> Interface ensureFamixInterface(ITypeBinding bnd, String name, TWithTypes owner, boolean isGeneric, int modifiers) {
1235
                Interface fmx = null;
2✔
1236

1237
                // --------------- some special cases
1238
                if (bnd != null) {
2!
1239
                        if (bnd.isArray()) {
3!
1240
                                bnd = bnd.getElementType();
×
1241
                        }
1242

1243
                        // for inner classes defined in generics !!! For others should not change anything
1244
                        bnd = bnd.getErasure();
3✔
1245
                }
1246

1247
                // ---------------- to avoid useless computations if we can
1248
                fmx = (Interface) getEntityByKey(bnd);
5✔
1249
                if (fmx != null) {
2✔
1250
                        return fmx;
2✔
1251
                }
1252

1253
                // --------------- name
1254
                if (name == null) {
2✔
1255
                        if (bnd == null) {
2!
1256
                                return null;  // not much we can do
×
1257
                        } else if (!bnd.isAnonymous()) {
3!
1258
                                name = bnd.getErasure().getName();  // for generics, will give the "core" type name, for normal type, won't change anything
5✔
1259
                        } else { // anonymous class
1260
                                if (bnd.getSuperclass() != null) {
×
1261
                                        name = bnd.getSuperclass().getName();
×
1262
                                }
1263
                                if ((name == null) || name.equals(OBJECT_NAME)) {
×
1264
                                        ITypeBinding[] intfcs = bnd.getInterfaces();
×
1265
                                        if ((intfcs != null) && (intfcs.length > 0)) {
×
1266
                                                name = bnd.getInterfaces()[0].getName();
×
1267
                                        }
1268
                                        else {
1269
                                                name = "???";
×
1270
                                        }
1271
                                }
1272
                                name = ANONYMOUS_NAME_PREFIX + "(" + name + ")";
×
1273
                        }
1274
                }
1275

1276
                // --------------- owner
1277
                if (owner == null) {
2✔
1278
                        if (bnd == null) {
2!
1279
                                owner = ensureFamixPackageDefault();
×
1280
                        } else {
1281
                                owner = (TWithTypes) ensureOwner(bnd);
5✔
1282
                        }
1283
                }
1284

1285
                // --------------- recover from name ?
1286
                for (Interface candidate : this.getEntityByName(Interface.class, name)) {
13✔
1287
                        if (matchAndMapInterface(bnd, name, (T) owner, candidate)) {
8✔
1288
                                fmx = candidate;
2✔
1289
                                break;
1✔
1290
                        }
1291
                }
1✔
1292

1293
                // ---------------- create
1294
                if (fmx == null) {
2✔
1295
                        if (isGeneric) {
2✔
1296
                                fmx = ensureFamixParametricInterface(bnd, name, owner);
7✔
1297
                        }
1298
                        else {
1299
                                fmx = ensureFamixEntity(Interface.class, bnd, name);
7✔
1300
                                fmx.setTypeContainer(owner);
3✔
1301
                        }
1302
                }
1303

1304
                if (fmx!=null) {
2!
1305
                        // we just created it or it was not bound, so we make sure it has the right information in it
1306
                        if (bnd != null) {
2!
1307
                                setInterfaceModifiers(fmx, bnd.getModifiers());
5✔
1308
                        }
1309
                        TAssociation lastAssociation = null;
2✔
1310
                        if (bnd != null) {
2!
1311
                                ensureImplementedInterfaces(bnd, fmx, owner, lastAssociation);
6✔
1312
                        }
1313
                }
1314
                return fmx;
2✔
1315
        }
1316

1317
        public TType asClass(TType excepFmx) {
1318
                Class tmp = null;
×
1319
                IBinding key = null;
×
1320
                try {
1321
                        TWithTypes owner = (TWithTypes) Util.getOwner(excepFmx);
×
1322
                        owner.getTypes().remove(excepFmx);
×
1323
                        removeEntity((NamedEntity) excepFmx);
×
1324

1325
                        key = entityToKey.get((NamedEntity) excepFmx);
×
1326
                        tmp = ensureFamixEntity(Class.class, key, excepFmx.getName());
×
1327
                        tmp.setTypeContainer((ContainerEntity)owner);
×
1328

1329
                        tmp.addMethods(((TWithMethods) excepFmx).getMethods());
×
1330
                        if (excepFmx instanceof TWithAttributes) {
×
1331
                                tmp.addAttributes(((TWithAttributes) excepFmx).getAttributes());
×
1332
                        }
1333

1334
                        if (key != null) {
×
1335
                                setClassModifiers(tmp, key.getModifiers());
×
1336
                        }
1337

1338
                        if (excepFmx instanceof TWithInheritances) {
×
1339
                                tmp.addSuperInheritances(((TWithInheritances) excepFmx).getSuperInheritances());
×
1340
                                tmp.addSubInheritances(((TWithInheritances) excepFmx).getSubInheritances());
×
1341
                        }
1342
                        tmp.setSourceAnchor(excepFmx.getSourceAnchor());
×
1343
                        tmp.addAnnotationInstances(((NamedEntity) excepFmx).getAnnotationInstances());
×
1344
                        // tmp.addComments(excepFmx.getComments());
1345
                        tmp.addIncomingReferences(excepFmx.getIncomingReferences());
×
1346
                        tmp.setIsStub(excepFmx.getIsStub());
×
1347
                        tmp.addTypes(((ContainerEntity) excepFmx).getTypes());
×
1348
                }
1349
                catch( ConcurrentModificationException e) {
×
1350
                        e.printStackTrace();
×
1351
                }
×
1352

1353
                return tmp;
×
1354
        }
1355

1356
        public TThrowable asException(TType excepFmx) {
1357
                if (excepFmx instanceof Exception) {
3✔
1358
                        return (Exception) excepFmx;
3✔
1359
                }
1360
                if(excepFmx instanceof TypeParameter) {
3✔
1361
                        return (TypeParameter) excepFmx;
3✔
1362
                }
1363
                Exception tmp = null;
2✔
1364
                IBinding key = null;
2✔
1365
                try {
1366
                        TWithTypes owner = (TWithTypes) Util.getOwner(excepFmx);
4✔
1367
                        owner.getTypes().remove(excepFmx);
5✔
1368
                        removeEntity((NamedEntity) excepFmx);
4✔
1369

1370
                        key = entityToKey.get((NamedEntity) excepFmx);
7✔
1371
                        tmp = ensureFamixException((ITypeBinding) key, excepFmx.getName(), owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
10✔
1372

1373
                        tmp.addMethods(((TWithMethods) excepFmx).getMethods());
5✔
1374
                        if (excepFmx instanceof TWithAttributes) {
3!
1375
                                tmp.addAttributes(((TWithAttributes) excepFmx).getAttributes());
5✔
1376
                        }
1377

1378
                        if (excepFmx instanceof TWithInheritances) {
3!
1379
                                tmp.addSuperInheritances(((TWithInheritances) excepFmx).getSuperInheritances());
5✔
1380
                                tmp.addSubInheritances(((TWithInheritances) excepFmx).getSubInheritances());
5✔
1381
                        }
1382
                        tmp.setSourceAnchor(excepFmx.getSourceAnchor());
4✔
1383
                        tmp.addAnnotationInstances(((NamedEntity) excepFmx).getAnnotationInstances());
5✔
1384
                        // tmp.addComments(excepFmx.getComments());
1385
                        List<TReference> newList = excepFmx.getIncomingReferences().stream().collect(Collectors.toList());
7✔
1386
                        tmp.addIncomingReferences(newList);
3✔
1387
                        tmp.setIsStub(excepFmx.getIsStub());
4✔
1388
                        tmp.addTypes(((ContainerEntity) excepFmx).getTypes());
5✔
1389
                }
1390
                catch( ConcurrentModificationException e) {
×
1391
                        e.printStackTrace();
×
1392
                }
1✔
1393

1394
                return (TThrowable)tmp;
2✔
1395
        }
1396

1397
        /**
1398
         * helper method, we know the type exists, ensureFamixClass will recover it
1399
         */
1400
        public Class getFamixClass(ITypeBinding bnd, String name, TNamedEntity owner) {
1401
                return ensureFamixClass(bnd, name, owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
8✔
1402
        }
1403

1404
        /**
1405
         * helper method, we know the type exists, ensureFamixInterface will recover it
1406
         */
1407
        public Interface getFamixInterface(ITypeBinding bnd, String name, ContainerEntity owner) {
1408
                return ensureFamixInterface(bnd, name, owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
8✔
1409
        }
1410

1411
        /**
1412
         * helper method, we know the type exists, ensureFamixInterface will recover it
1413
         */
1414
        public Exception getFamixException(ITypeBinding bnd, String name, TWithTypes owner) {
1415
                return ensureFamixException(bnd, name, owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
8✔
1416
        }
1417

1418
        /**
1419
         * Ensures a famix entity for the owner of a binding.<br>
1420
         * This owner can be a method, a class or a namespace
1421
         * @param bnd -- binding for the owned entity
1422
         * @return a famix entity for the owner
1423
         */
1424
        private TNamedEntity ensureOwner(ITypeBinding bnd) {
1425
                TNamedEntity owner = null;
2✔
1426
                IMethodBinding parentMtd = bnd.getDeclaringMethod();
3✔
1427
                if (parentMtd != null) {
2✔
1428
                        owner = this.ensureFamixMethod(parentMtd);  // cast needed to desambiguate the call
5✔
1429
                }
1430
                else {
1431
                        ITypeBinding parentClass = bnd.getDeclaringClass();
3✔
1432
                        if (parentClass != null) {
2✔
1433
                                TType tmpOwn = this.ensureFamixType(parentClass);
4✔
1434
                                if (tmpOwn instanceof ParametricClass) {
3✔
1435
                                        owner =  (TNamedEntity) ((ParametricClass) tmpOwn);
4✔
1436
                                }
1437
                                else {
1438
                                        owner = tmpOwn;
2✔
1439
                                }
1440
                        }
1✔
1441
                        else {
1442
                                IPackageBinding parentPckg = bnd.getPackage();
3✔
1443
                                if (parentPckg != null) {
2!
1444
                                        owner = this.ensureFamixPackage(parentPckg, null);
6✔
1445
                                } else {
1446
                                        owner = this.ensureFamixPackageDefault();
×
1447
                                }
1448
                        }
1449
                }
1450
                return owner;
2✔
1451
        }
1452

1453

1454
        /**
1455
         * Returns a FAMIX PrimitiveType with the given <b>name</b>, creating it if it does not exist yet
1456
         * We assume that PrimitiveType must be uniq for a given name
1457
         * @param name -- the name of the FAMIX PrimitiveType
1458
         * @return the FAMIX PrimitiveType or null in case of a FAMIX error
1459
         */
1460
        public PrimitiveType ensureFamixPrimitiveType(ITypeBinding bnd, String name) {
1461
                if (name == null) {
2✔
1462
                        if (bnd == null) {
2!
1463
                                return null;
×
1464
                        } else {
1465
                                name = bnd.getName();
3✔
1466
                        }
1467
                }
1468
                return ensureFamixUniqEntity(PrimitiveType.class, bnd, name);
7✔
1469
        }
1470

1471
        public <T extends TWithTypes & TNamedEntity> org.moosetechnology.model.famix.famixjavaentities.Enum ensureFamixEnum(ITypeBinding bnd, String name, TWithTypes owner) {
1472
                org.moosetechnology.model.famix.famixjavaentities.Enum fmx = null;
2✔
1473

1474
                // --------------- to avoid useless computations if we can
1475
                fmx = (org.moosetechnology.model.famix.famixjavaentities.Enum) getEntityByKey(bnd);
5✔
1476
                if (fmx != null) {
2✔
1477
                        return fmx;
2✔
1478
                }
1479

1480
                // --------------- name
1481
                if (name == null) {
2✔
1482
                        if (bnd == null) {
2!
1483
                                return null;
×
1484
                        }
1485
                        else {
1486
                                name = bnd.getName();
3✔
1487
                        }
1488
                }
1489

1490
                // --------------- owner
1491
                if (owner == null) {
2✔
1492
                        if (bnd == null) {
2!
1493
                                owner = ensureFamixPackageDefault();  // not really sure what to do here
×
1494
                        } else {
1495
                                owner = (TWithTypes) ensureOwner(bnd);
5✔
1496
                        }
1497
                }
1498

1499
                // --------------- recover from name ?
1500
                for (org.moosetechnology.model.famix.famixjavaentities.Enum candidate : getEntityByName(org.moosetechnology.model.famix.famixjavaentities.Enum.class, name)) {
9!
1501
                        if (matchAndMapType(bnd, name, (T) owner, candidate)) {
×
1502
                                fmx = candidate;
×
1503
                                break;
×
1504
                        }
1505
                }
×
1506

1507
                if (fmx == null) {
2!
1508
                        fmx = ensureFamixEntity(Enum.class, bnd, name);
7✔
1509
                        fmx.setTypeContainer(owner);
3✔
1510
                }
1511

1512
                if ((fmx != null) && (bnd != null) ) {
4!
1513
                        setVisibility(fmx, bnd.getModifiers());
5✔
1514
                }
1515

1516
                return fmx;
2✔
1517
        }
1518

1519
        /**
1520
         * helper method, we know the type exists, ensureFamixEnum will recover it
1521
         */
1522
        public org.moosetechnology.model.famix.famixjavaentities.Enum getFamixEnum(ITypeBinding bnd, String name, TWithTypes owner) {
1523
                return ensureFamixEnum(bnd, name, owner);
6✔
1524
        }
1525

1526
        public EnumValue ensureFamixEnumValue(IVariableBinding bnd,        String name, Enum owner) {
1527
                EnumValue fmx = null;
2✔
1528

1529
                // --------------- to avoid useless computations if we can
1530
                fmx = (EnumValue)getEntityByKey(bnd);
5✔
1531
                if (fmx != null) {
2✔
1532
                        return fmx;
2✔
1533
                }
1534

1535
                // --------------- name
1536
                if (name == null) {
2!
1537
                        if (bnd == null) {
×
1538
                                return null;
×
1539
                        }
1540
                        else {
1541
                                name = bnd.getName();
×
1542
                        }
1543
                }
1544

1545
                // --------------- owner
1546
                if (owner == null) {
2✔
1547
                        if (bnd == null) {
2!
1548
                                return null;  // what would be the interest of creating an EnumValue without a declaring Enum type?
×
1549
                        }
1550
                        else {
1551
                                owner = ensureFamixEnum(bnd.getDeclaringClass(), null, null);
7✔
1552
                        }
1553
                }
1554

1555
                // --------------- recover from name ?
1556
                for (EnumValue candidate : getEntityByName(EnumValue.class, name) ) {
9!
1557
                        if ( matchAndMapVariable(bnd, name, owner, candidate) ) {
×
1558
                                fmx = candidate;
×
1559
                                break;
×
1560
                        }
1561
                }
×
1562
                if (fmx == null) {
2!
1563
                        fmx = ensureFamixEntity(EnumValue.class, bnd, name);
7✔
1564
                        fmx.setParentEnum(owner);
3✔
1565
                }
1566

1567
                if (fmx!=null) {
2!
1568
                        fmx.setParentEnum(owner);
3✔
1569
                }
1570

1571
                return fmx;
2✔
1572
        }
1573

1574
        /**
1575
         * helper method, we know the type enumValue, ensureFamixEnumValue will recover it
1576
         */
1577
        public EnumValue getFamixEnumValue(IVariableBinding bnd, String name, Enum owner) {
1578
                return ensureFamixEnumValue(bnd, name, owner);
×
1579
        }
1580

1581
        /**
1582
         * e.g. see {@link EntityDictionary#ensureFamixClass}
1583
         */
1584
        public AnnotationType ensureFamixAnnotationType(ITypeBinding bnd, String name, ContainerEntity owner) {
1585
                AnnotationType fmx = null;
2✔
1586

1587
                // --------------- to avoid useless computations if we can
1588
                fmx = (AnnotationType)getEntityByKey(bnd);
5✔
1589
                if (fmx != null) {
2✔
1590
                        return fmx;
2✔
1591
                }
1592

1593
                // --------------- name
1594
                if (name == null) {
2✔
1595
                        if (bnd == null) {
2!
1596
                                return null;
×
1597
                        }
1598
                        else {
1599
                                name = bnd.getName();
3✔
1600
                        }
1601
                }
1602

1603
                // --------------- owner
1604
                if (owner == null) {
2✔
1605
                        if (bnd == null) {
2!
1606
                                owner = ensureFamixPackageDefault();
×
1607
                        }
1608
                        else {
1609
                                IPackageBinding parentPckg = bnd.getPackage();
3✔
1610
                                if (parentPckg != null) {
2!
1611
                                        owner = this.ensureFamixPackage(parentPckg, null);
6✔
1612
                                } else {
1613
                                        owner = this.ensureFamixPackageDefault();
×
1614
                                }
1615
                        }
1616
                }
1617

1618
                // --------------- recover from name ?
1619
                for (AnnotationType candidate : getEntityByName(AnnotationType.class, name) ) {
13✔
1620
                        if ( matchAndMapType(bnd, name, owner, candidate) ) {
7✔
1621
                                fmx = candidate;
2✔
1622
                                break;
1✔
1623
                        }
1624
                }
1✔
1625

1626
                // --------------- create
1627
                if (fmx == null) {
2✔
1628
                        fmx = ensureFamixEntity(AnnotationType.class, bnd, name);
7✔
1629
                        fmx.setAnnotationTypesContainer(owner);
3✔
1630
                }
1631

1632
                if ( (fmx!=null) && (bnd != null) ) {
4!
1633
                        // Not supported in Famix
1634

1635
                        // setVisibility(fmx, bnd.getModifiers());
1636
                }
1637

1638
                return fmx;
2✔
1639
        }
1640

1641
        /**
1642
         * helper method, we know the type exists, ensureFamixAnnotationType will recover it
1643
         */
1644
        public AnnotationType getFamixAnnotationType(ITypeBinding bnd, String name, ContainerEntity owner) {
1645
                return ensureFamixAnnotationType(bnd, name, owner);
6✔
1646
        }
1647

1648
        public AnnotationTypeAttribute ensureFamixAnnotationTypeAttribute(IMethodBinding bnd, String name, AnnotationType owner) {
1649
                AnnotationTypeAttribute fmx = null;
2✔
1650

1651
                // --------------- to avoid useless computations if we can
1652
                fmx = (AnnotationTypeAttribute)getEntityByKey(bnd);
5✔
1653
                if (fmx != null) {
2✔
1654
                        return fmx;
2✔
1655
                }
1656

1657
                // --------------- name
1658
                if (name == null) {
2!
1659
                        if (bnd == null) {
×
1660
                                return null;
×
1661
                        }
1662
                        else {
1663
                                name = bnd.getName();
×
1664
                        }
1665
                }
1666

1667
                // --------------- owner
1668
                if (owner == null) {
2!
1669
                        if (bnd == null) {
×
1670
                                return null;  // what would be the use of an AnnotationTypeAttribute without AnnotationType ?
×
1671
                        }
1672
                        else {
1673
                                ITypeBinding parentType = bnd.getDeclaringClass();
×
1674
                                if (parentType != null) {
×
1675
                                        owner = this.ensureFamixAnnotationType(parentType, null, null);
×
1676
                                }
1677
                                else  {
1678
                                        return null;  // what would be the use of an AnnotationTypeAttribute without AnnotationType ?
×
1679
                                }
1680
                        }
1681
                }
1682

1683
                // --------------- recover from name ?
1684
                for (AnnotationTypeAttribute candidate : getEntityByName(AnnotationTypeAttribute.class, name) ) {
13✔
1685
                        // JDT treats annotation type attributes as methods ...
1686
                        // checkAndMapMethod wants a signature as 2nd argument so we add empty param list
1687
                        if ( (bnd != null) && matchAndMapMethod(bnd, name+"()", null, owner, candidate) ) {
11!
1688
                                fmx = candidate;
×
1689
                                break;
×
1690
                        }
1691
                        // if the binding is null, the annotationTypeAttribute migth have been created
1692
                        else if ( (bnd == null) && matchAndMapVariable(null, name, owner, candidate)) {
2!
1693
                                fmx = candidate;
×
1694
                                break;
×
1695
                        }
1696
                }
1✔
1697

1698
                if (fmx == null) {
2!
1699
                        fmx = ensureFamixEntity(AnnotationTypeAttribute.class, bnd, name);
7✔
1700
                        fmx.setParentType(owner);
3✔
1701
                }
1702

1703
                if ( (fmx!=null) && (bnd != null) ) {
4!
1704
                        // Not suopp
1705

1706
                        // setVisibility(fmx, bnd.getModifiers());
1707
                }
1708

1709
                return fmx;
2✔
1710
        }
1711

1712
        /**
1713
         * helper method, we know the attribute exists, ensureFamixAnnotationTypeAttribute will recover it
1714
         */
1715
        public AnnotationTypeAttribute getFamixAnnotationTypeAttribute(IMethodBinding bnd, String name, AnnotationType owner) {
1716
                return ensureFamixAnnotationTypeAttribute( bnd, name, owner);
6✔
1717
        }
1718
        
1719
        
1720
        /**
1721
         * Returns a FAMIX Wildcard with its bounds
1722
         * @param bnd
1723
         * @param name
1724
         * @param owner
1725
         * @return
1726
         */
1727
        public Wildcard ensureFamixWildcardType(ITypeBinding bnd, String name, TParametricEntity owner, TWithTypes ctxt) {
1728
                Wildcard fmx = this.ensureFamixEntity(Wildcard.class, bnd, bnd.getName());
8✔
1729
                if(bnd.getBound() != null) {
3✔
1730
                        Type bound = (Type) this.ensureFamixType(bnd.getBound()); 
6✔
1731
                        if(bnd.isUpperbound()) {
3✔
1732
                                fmx.setUpperBound(bound);
3✔
1733
                                bound.addUpperBoundedWildcards(fmx);
4✔
1734
                        }else{
1735
                                fmx.setLowerBound(bound);
3✔
1736
                                bound.addLowerBoundedWildcards(fmx);
3✔
1737
                        }
1738
                }
1739
                return fmx;
2✔
1740
        }
1741

1742
        /**
1743
         * Returns a Famix TypeParameter (created by a Famix ParametricEntity) with the given <b>name</b>, creating it if it does not exist yet
1744
         * In the second case, sets some default properties: not Abstract, not Final, not Private, not Protected, not Public
1745
         * @param name -- the name of the Famix TypeParameter
1746
         * @return the Famix TypeParameter or null in case of a Famix error
1747
         */
1748
        public TypeParameter ensureFamixTypeParameter(ITypeBinding bnd,        String name, TWithTypes owner) {
1749
                TypeParameter fmx = null;
2✔
1750

1751
                // --------------- to avoid useless computations if we can
1752
                fmx = (TypeParameter)getEntityByKey(bnd);
5✔
1753
                if (fmx != null) {
2✔
1754
                        return fmx;
2✔
1755
                }
1756

1757
                // --------------- name
1758
                if (name == null) {
2✔
1759
                        if (bnd == null) {
2!
1760
                                return null;
×
1761
                        }
1762
                        else {
1763
                                name = bnd.getName();
3✔
1764
                        }
1765
                }
1766

1767
                // --------------- owner
1768
                if (owner == null) {
2!
UNCOV
1769
                        if (bnd == null) {
×
1770
                                owner = null;  // not really sure what to do here
×
1771
                        }
1772
                        else {
UNCOV
1773
                                if (bnd.getDeclaringClass() != null) {
×
NEW
1774
                                        owner = (TWithTypes) this.ensureFamixType(bnd.getDeclaringClass());
×
UNCOV
1775
                                }else if(bnd.getDeclaringMethod() != null) {
×
NEW
1776
                                        owner = (TWithTypes) this.ensureFamixMethod(bnd.getDeclaringMethod());
×
1777
                                }
1778
                                else {
1779
                                        owner = null;  // not really sure what to do here
×
1780
                                }
1781
                        }
1782
                }
1783

1784
                // --------------- recover from name ?
1785
                for (Type candidate : this.getEntityByName(Type.class, name)) {
13✔
1786
                        if ( matchAndMapType(bnd, name, (ContainerEntity) owner, candidate) ) {
8✔
1787
                                fmx = (TypeParameter) candidate;
3✔
1788
                                break;
1✔
1789
                        }
1790
                }
1✔
1791

1792
                // --------------- create
1793
                if (fmx == null) {
2✔
1794
                        fmx = ensureFamixEntity(TypeParameter.class, bnd, name);
7✔
1795
                        if(bnd != null && bnd.getSuperclass() != null) {
5!
1796
                                Type upperBound = (Type)ensureFamixType(bnd.getSuperclass());
6✔
1797
                                fmx.setUpperBound(upperBound);
3✔
1798
                        }
1799
                        if(bnd != null && bnd.getInterfaces().length > 0) {
6✔
1800
                                for(ITypeBinding intbnd: bnd.getInterfaces()) {
17✔
1801
                                        Type upperBound = (Type)ensureFamixType(intbnd);
5✔
1802
                                        fmx.setUpperBound(upperBound);
3✔
1803
                                }
1804
                        }
1805
                        fmx.setTypeContainer((ContainerEntity) owner);
4✔
1806
                }
1807

1808
                return fmx;
2✔
1809
        }
1810

1811

1812
        public IBinding getTypeParameterOwner(ITypeBinding typeParameterBinding, IBinding currentOwner) {
1813
                
1814

1815

NEW
1816
                return currentOwner;
×
1817
        }
1818

1819

1820

1821
        /**
1822
         * Checks whether the existing unmapped Famix Namespace matches the binding.
1823
         * Checks that the candidate has the same name as the JDT bound package, and checks recursively that owners also match.
1824
         *
1825
         * @param bnd       -- a JDT binding that we are trying to match to the candidate
1826
         * @param name      of the package
1827
         * @param owner     of the package
1828
         * @param candidate -- a Famix Entity
1829
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1830
         */
1831
        private boolean matchAndMapPackage(IPackageBinding bnd, String name, Package owner, NamedEntity candidate) {
1832
                if (!(candidate instanceof Package)) {
3!
1833
                        return false;
×
1834
                }
1835

1836
                // check whether bnd and candidate are already bound
1837
                CheckResult res = checkKeyMatch(bnd, candidate);
5✔
1838
                if (res == CheckResult.MATCH) {
3✔
1839
                        return true;
2✔
1840
                } else if (res == CheckResult.FAIL) {
3✔
1841
                        return false;
2✔
1842
                }
1843

1844
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7✔
1845
                        return false;
2✔
1846
                }
1847

1848
                // names match, not need to look at owner because names of Namespaces are their fully qualified name
1849
                conditionalMapToKey(bnd, candidate);
4✔
1850
                return true;
2✔
1851
        }
1852

1853
        /**
1854
         * Checks whether the existing unmapped Famix Type matches the binding.
1855
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
1856
         * We also check that the actual class of the candidate matches (can be a sub-class of FamixType).
1857
         * @param bnd -- a JDT binding that we are trying to match to the candidate
1858
         * @param name of the type
1859
         * @param owner of the type
1860
         * @param candidate -- a Famix NamedEntity (Class, Type, PrimitiveType, Enum, AnnotationType)
1861
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1862
         */
1863
        private <T extends TWithTypes & TNamedEntity> boolean matchAndMapType(ITypeBinding bnd, String name, TNamedEntity owner, TNamedEntity candidate) {
1864
                if (! (candidate instanceof Type) ) {
3!
1865
                        return false;
×
1866
                }
1867

1868
                // check whether bnd and candidate are already bound
1869
                CheckResult res = checkKeyMatch(bnd, candidate);
5✔
1870
                if (res == CheckResult.MATCH) {
3✔
1871
                        return true;
2✔
1872
                }
1873
                else if (res == CheckResult.FAIL) {
3✔
1874
                        return false;
2✔
1875
                }
1876

1877
                if ( (bnd != null) && (bnd.isArray()) ) {
5!
1878
                                bnd = bnd.getElementType();
×
1879
                }
1880

1881
                // checking names
1882
                if ( (bnd != null) && (bnd.isParameterizedType() || bnd.isRawType()) ) {
8!
1883
                        name = bnd.getErasure().getName();
×
1884
                }
1885
                else if (bnd != null) {
2✔
1886
                        name = bnd.getName();
3✔
1887
                }
1888
                // else name = name
1889
                if (checkNameMatch(null, name, candidate) == CheckResult.FAIL) {
7✔
1890
                        return false;
2✔
1891
                }
1892

1893
                // special case of primitive types
1894
                if (candidate instanceof PrimitiveType) {
3!
1895
                        if ( (bnd != null) && bnd.isPrimitive() ) {
×
1896
                                // names are equal so it's OK
1897
                                conditionalMapToKey(bnd, candidate);
×
1898
                                return true;
×
1899
                        }
1900
                        else if ( (bnd == null) && (owner == null) ) {
×
1901
                                return true;
×
1902
                        }
1903
                }
1904

1905
                // check owners without bnd
1906
                if (bnd == null) {
2✔
1907
                        return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
7✔
1908
                }
1909

1910
                // check owners with bnd
1911
                // type is an annotation
1912
                if (bnd.isAnnotation() && (candidate instanceof AnnotationType)) {
6!
1913
                        if (matchAndMapPackage(bnd.getPackage(), owner.getName(), (Package) Util.getOwner(owner), Util.getOwner(candidate))) {
13!
1914
                                conditionalMapToKey(bnd, candidate);
4✔
1915
                                return true;
2✔
1916
                        } else {
1917
                                return false;
×
1918
                        }
1919
                }
1920

1921
                // check owners with bnd
1922
                // type is a Parameterized type
1923
                if ((bnd.isParameterizedType() || bnd.isRawType()) && (candidate instanceof ParametricClass)) {
6!
1924
                        return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
×
1925
                }
1926

1927
                // check owners with bnd
1928
                // type is an Enum
1929
                if (bnd.isEnum() && (candidate instanceof Enum)) {
3!
1930
                        return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
×
1931
                }
1932

1933
                // check owners with bnd
1934
                // type is something elae (a class or interface)
1935
                // Annotation are interfaces too, so we should check this one after isAnnotation
1936
                if ( bnd.isClass()) {
3!
1937
                        return matchAndMapClass(bnd, name, owner, (Type) candidate);
×
1938
                }
1939

1940
                if(bnd.isInterface()) {
3!
1941
                        return matchAndMapInterface(bnd, name, owner, (Type) candidate);
×
1942
                }
1943

1944
                return false;
2✔
1945
        }
1946

1947
        /**
1948
         * Checks whether the existing unmapped Famix Class (or Interface) matches the binding.
1949
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
1950
         * @param bnd -- a JDT binding that we are trying to match to the candidate
1951
         * @param name of the class
1952
         * @param owner of the class
1953
         * @param candidate -- a Famix Entity
1954
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1955
         */
1956
        private boolean matchAndMapClass(ITypeBinding bnd, String name, TNamedEntity owner, TType candidate) {
1957
                if (!(candidate instanceof Class)) {
3!
1958
                        return false;
×
1959
                }
1960

1961
                // check whether bnd and candidate are already bound
1962
                CheckResult res = checkKeyMatch(bnd, (NamedEntity) candidate);
6✔
1963
                if (res == CheckResult.MATCH) {
3!
1964
                        return true;
×
1965
                } else if (res == CheckResult.FAIL) {
3✔
1966
                        return false;
2✔
1967
                }
1968

1969
                if (checkNameMatch(bnd, name, (NamedEntity) candidate) == CheckResult.FAIL) {
8!
1970
                        return false;
×
1971
                }
1972

1973
                // checking owner
1974
                return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
7✔
1975
        }
1976

1977
        /**
1978
         * Checks whether the existing unmapped Famix Class (or Interface) matches the binding.
1979
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
1980
         * @param bnd -- a JDT binding that we are trying to match to the candidate
1981
         * @param name of the class
1982
         * @param owner of the class
1983
         * @param candidate -- a Famix Entity
1984
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1985
         */
1986
        private boolean matchAndMapInterface(ITypeBinding bnd, String name, TNamedEntity owner, Type candidate) {
1987
                if (!(candidate instanceof Interface)) {
3!
1988
                        return false;
×
1989
                }
1990

1991
                // check whether bnd and candidate are already bound
1992
                CheckResult res = checkKeyMatch(bnd, candidate);
5✔
1993
                if (res == CheckResult.MATCH) {
3!
1994
                        return true;
×
1995
                } else if (res == CheckResult.FAIL) {
3✔
1996
                        return false;
2✔
1997
                }
1998

1999
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7!
2000
                        return false;
×
2001
                }
2002

2003
                // checking owner
2004
                return matchAndMapTypeOwner(bnd, owner, candidate);
6✔
2005
        }
2006

2007
        /**
2008
         * Checks whether the existing unmapped Famix "Method" matches the binding.
2009
         * Checks that the candidate has the same name and same signature as the JDT bound method, and checks recursively that owners also match.
2010
         * Note that AnnotationTypeAttribute are treated as methods by JDT, so they are checked here.
2011
         * @param bnd -- a JDT binding that we are trying to match to the candidate
2012
         * @param sig -- signature of the method
2013
         * @param retTyp -- return type of the method
2014
         * @param owner of the method
2015
         * @param candidate -- a Famix Entity (regular Method or AnnotationTypeAttribute)
2016
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
2017
         */
2018
        private  boolean matchAndMapMethod(IMethodBinding bnd, String sig, TType retTyp, TNamedEntity owner, NamedEntity candidate) {
2019
                if (! (candidate instanceof Method) ) {
3✔
2020
                        return false;
2✔
2021
                }
2022

2023
                // check whether bnd and candidate are already bound
2024
                CheckResult res = checkKeyMatch(bnd, candidate);
5✔
2025
                if (res == CheckResult.MATCH) {
3!
UNCOV
2026
                        return true;
×
2027
                }
2028
                else if (res == CheckResult.FAIL) {
3✔
2029
                        return false;
2✔
2030
                }
2031

2032
                // checking names
2033
                String name = (sig != null) ? sig.substring(0, sig.indexOf('(')) : null;
10!
2034
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7!
2035
                        return false;
×
2036
                }
2037

2038
                // for methods, the name is not enough, we must test the signature also
2039
                // but not for AnnotationTypeAttribute
2040

2041
                        if (bnd != null) {
2✔
2042
                                sig = bnd.getName() + "(" + signatureParamsFromBinding(bnd) + ")";
7✔
2043
                        }
2044
                        if (! ((Method) candidate).getSignature().equals(sig)) {
6✔
2045
                                return false;
2✔
2046
                        }
2047

2048
                        // and still for method, must also check the return type
2049
                        if (bnd != null) {
2✔
2050
                                if (bnd.isConstructor()) {
3!
2051
                                        if ( ((Method) candidate).getDeclaredType() != null ) {
×
2052
                                                return false;
×
2053
                                        }
2054
                                        // else OK for now
2055
                                }
2056
                                else { // not a constructor
2057
                                        if ( ((Method) candidate).getDeclaredType() == null ) {
4!
2058
                                                return false;
×
2059
                                        }
2060
                                        else if (! matchAndMapType(bnd.getReturnType(), null, null, ((Method) candidate).getDeclaredType()) ) {
10!
2061
                                                return false;
×
2062
                                        }
2063
                                        // else OK for now
2064
                                }
2065
                        }
2066
                        else {  // bnd == null
2067
                                if (retTyp == null) { // similar to (bnd.isConstructor())
2!
2068
                                        if ( ((Method) candidate).getDeclaredType() != null ) {
4!
2069
                                                return false;
×
2070
                                        }
2071
                                        // else OK for now
2072
                                } else { // (ret != null)  i.e. not a constructor
2073
                                        if (((Method) candidate).getDeclaredType() == null) {
×
2074
                                                return false;
×
2075
                                        } else if (!matchAndMapType(null, retTyp.getName(), Util.getOwner(retTyp), (NamedEntity) ((Method) candidate).getDeclaredType())) {
×
2076
                                                return false;
×
2077
                                        }
2078
                                        // else OK for now
2079
                                }
2080
                        }
2081

2082

2083
                // check owner
2084
                if (matchAndMapOwnerAsType(((bnd != null) ? bnd.getDeclaringClass() : null), owner, Util.getOwner(candidate)) == CheckResult.MATCH) {
13✔
2085
                        conditionalMapToKey(bnd, candidate);
4✔
2086
                        return true;
2✔
2087
                } else {
2088
                        return false;
2✔
2089
                }
2090
        }
2091

2092
        /**
2093
         * Checks whether the candidate (an existing unmapped Famix "Variable" like Attribute, Parameter, ...) matches the binding.
2094
         * Checks that the candidate has the same name as the JDT bound variable, and checks recursively that owners also match.
2095
         * The Famix candidate is a NamedEntity and not a StructuralEntity to allow dealing with Famix EnumValue that JDT treats as variables
2096
         * @param bnd -- a JDT binding that we are trying to match to the candidate
2097
         * @param name of the variable
2098
         * @param owner of the variable
2099
         * @param candidate -- a Famix Entity (a StructuralEntity or an EnumValue)
2100
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
2101
         */
2102
        private boolean matchAndMapVariable(IVariableBinding bnd, String name, TNamedEntity owner, TNamedEntity candidate) {
2103
                if (!(candidate instanceof TStructuralEntity)) {
3!
2104
                        return false;
×
2105
                }
2106

2107
                // check whether bnd and candidate are already bound
2108
                CheckResult keyMatch = checkKeyMatch(bnd, candidate);
5✔
2109
                if (keyMatch == CheckResult.MATCH) {
3!
2110
                        return true;
×
2111
                } else if (keyMatch == CheckResult.FAIL) {
3✔
2112
                        return false;
2✔
2113
                }
2114

2115
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7!
2116
                        return false;
×
2117
                }
2118

2119
                // check owner
2120
                TNamedEntity candidateOwner = Util.getOwner(candidate);
3✔
2121

2122
                // local variable or parameter ?
2123
                // owner is a Method? (for example in case of an anonymous class)
2124
                CheckResult res = matchAndMapOwnerAsMethod(((bnd != null) ? bnd.getDeclaringMethod() : null), owner, candidateOwner);
11✔
2125
                if (res == CheckResult.FAIL) {
3✔
2126
                        return false;
2✔
2127
                } else if (res == CheckResult.MATCH) {
3!
2128
                        conditionalMapToKey(bnd, candidate);
×
2129
                        return true;
×
2130
                }
2131

2132
                // check owner
2133
                // <anArray>.length field?
2134
                if (name.equals("length")) {
4!
2135
                        boolean isArrayLengthField = ((bnd != null) && (bnd.getDeclaringClass() == null)) ||
×
2136
                                                                                 ((bnd == null) && (owner.getName().equals(EntityDictionary.ARRAYS_NAME)));
×
2137
                        if (isArrayLengthField) {
×
2138
                                if (candidateOwner.getName().equals(EntityDictionary.ARRAYS_NAME)) {
×
2139
                                        conditionalMapToKey(bnd, candidate);
×
2140
                                        return true;
×
2141
                                }
2142
                                else {
2143
                                        return false;
×
2144
                                }
2145
                        }
2146
                }
2147

2148
                // check owner
2149
                // "normal" field?
2150
                res = matchAndMapOwnerAsType( ((bnd != null) ? bnd.getDeclaringClass() : null), owner, candidateOwner);
11✔
2151
                if (res == CheckResult.MATCH) {
3!
2152
                        conditionalMapToKey(bnd, candidate);
4✔
2153
                        return true;
2✔
2154
                }
2155
                return false;
×
2156
        }
2157

2158
        /**
2159
         * Checks whether the existing unmapped Famix Type's parent (or owner) matches the binding's owner.
2160
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
2161
         * @param bnd -- a JDT binding whose owner we are trying to match to the candidate's owner
2162
         * @param owner -- the owner of the type
2163
         * @param candidate -- a Famix Entity
2164
         * @return whether we found a match (if <b>true</b>, the mapping is recorded)
2165
         */
2166
        private boolean matchAndMapTypeOwner(ITypeBinding bnd, TNamedEntity owner, Type candidate) {
2167
                ContainerEntity candidateOwner = Util.getOwner(candidate);
4✔
2168

2169
                // owner is a Method? (for example in case of an anonymous class)
2170
                CheckResult res = matchAndMapOwnerAsMethod(((bnd != null) ? bnd.getDeclaringMethod() : null), owner, candidate);
11✔
2171
                if (res == CheckResult.MATCH) {
3!
2172
                        conditionalMapToKey(bnd, candidate);
×
2173
                        return true;
×
2174
                } else if (res == CheckResult.FAIL) {
3!
2175
                        return false;
×
2176
                }
2177

2178
                // owner is a class ?
2179
                res = matchAndMapOwnerAsType(((bnd != null) ? bnd.getDeclaringClass() : null), owner, candidateOwner);
11✔
2180
                if (res == CheckResult.MATCH) {
3✔
2181
                        conditionalMapToKey(bnd, candidate);
4✔
2182
                        return true;
2✔
2183
                }
2184
                else if (res == CheckResult.FAIL) {
3✔
2185
                        return false;
2✔
2186
                }
2187

2188
                // owner must be a package
2189
                if (matchAndMapOwnerAsNamespace( ((bnd != null)?bnd.getPackage():null), owner, candidateOwner) == CheckResult.MATCH) {
12✔
2190
                        conditionalMapToKey(bnd, candidate);
4✔
2191
                        return true;
2✔
2192
                }
2193
                return false;
2✔
2194
        }
2195

2196
        /**
2197
         * Check whether the owner of candidates is a method macthinf either methBnd or owner
2198
         * @param methBnd
2199
         * @param owner
2200
         * @param candidateOwner
2201
         * @return a {@link CheckResult}
2202
         */
2203
        private  <T extends TNamedEntity> CheckResult matchAndMapOwnerAsMethod(IMethodBinding methBnd, T owner, T candidateOwner) {
2204
                if ((methBnd != null) || ((owner != null) && (owner instanceof Method))) {
7!
2205
                        if (!(candidateOwner instanceof Method)) {
3!
2206
                                return CheckResult.FAIL;
×
2207
                        }
2208

2209
                        ContainerEntity ownerOwner = (owner != null) ? (ContainerEntity) Util.getOwner(owner) : null;
7!
2210
                        String ownerSig = (owner != null) ? ((Method) owner).getSignature() : null;
7!
2211
                        Type ownerReturn = (owner != null) ? (Type) ((Method) owner).getDeclaredType() : null;
8!
2212

2213
                        if (matchAndMapMethod(methBnd, ownerSig, ownerReturn, ownerOwner, (Method) candidateOwner)) {
9!
2214
                                return CheckResult.MATCH;
×
2215
                        } else {
2216
                                return CheckResult.FAIL;
2✔
2217
                        }
2218
                }
2219
                return CheckResult.UNDECIDED;
2✔
2220
        }
2221

2222
        /**
2223
         * @param typBnd
2224
         * @param owner
2225
         * @param candidateOwner
2226
         * @return a {@link CheckResult}
2227
         */
2228
        private CheckResult matchAndMapOwnerAsType(ITypeBinding typBnd, TNamedEntity owner, TNamedEntity candidateOwner) {
2229
                if ((typBnd != null) || ((owner != null) && (owner instanceof Type))) {
7!
2230
                        if (!(candidateOwner instanceof Type)) {
3✔
2231
                                return CheckResult.FAIL;
2✔
2232
                        }
2233

2234
                        TNamedEntity ownerOwner = (owner != null) ? Util.getOwner(owner) : null;
6!
2235
                        String ownerName = (owner != null) ? ((Type) owner).getName() : null;
7!
2236

2237
                        if (matchAndMapType(typBnd, ownerName, ownerOwner, candidateOwner)) {
7✔
2238
                                return CheckResult.MATCH;
2✔
2239
                        } else {
2240
                                return CheckResult.FAIL;
2✔
2241
                        }
2242
                }
2243
                return CheckResult.UNDECIDED;
2✔
2244
        }
2245

2246
        private CheckResult matchAndMapOwnerAsNamespace(IPackageBinding pckgBnd, TNamedEntity owner, ContainerEntity candidateOwner) {
2247
                if ((pckgBnd != null) || ((owner != null) && (owner instanceof Package))) {
7!
2248
                        if (!(candidateOwner instanceof Package)) {
3!
2249
                                return CheckResult.FAIL;
×
2250
                        }
2251

2252
                        Package ownerOwner = (owner != null) ? (Package) Util.getOwner(owner) : null;
7!
2253
                        String ownerName = (owner != null) ? ((Package) owner).getName() : null;
7!
2254

2255
                        if (matchAndMapPackage(pckgBnd, ownerName, ownerOwner, candidateOwner)) {
7✔
2256
                                return CheckResult.MATCH;
2✔
2257
                        } else {
2258
                                return CheckResult.FAIL;
2✔
2259
                        }
2260
                }
2261
                return CheckResult.UNDECIDED;
×
2262
        }
2263

2264
        /**
2265
         * Checks whether the name and the candidate matches the name of the entity (given either by 'bnd' or 'name')<br>
2266
         * 'name' and 'bnd' cannot be null together
2267
         * @param bnd -- binding associated with the entity may be null
2268
         * @param name -- name of the entity may be null
2269
         * @param candidate
2270
         * @return true if names match, false if not
2271
         */
2272
        private CheckResult checkNameMatch(IBinding bnd, String name, TNamedEntity candidate) {
2273
                if ( (bnd != null) && (! bnd.getName().equals(candidate.getName())) ) {
8!
2274
                        return CheckResult.FAIL;
×
2275
                }
2276
                else if ( (bnd == null) && (name != null) && (! name.equals(candidate.getName())) ) {
9!
2277
                        return CheckResult.FAIL;
2✔
2278
                }
2279
                else {
2280
                        return CheckResult.MATCH;
2✔
2281
                }
2282
        }
2283

2284
        /**
2285
         * Check whether key and candidate are already bound together, whether either is bound to something else, or whether none is bound
2286
         * @param key
2287
         * @param candidate
2288
         * @return <ul><li><b>-1</b>, if either is bound to something else</li><li><b>0</b>, if none is bound (or key is null)</li><li><b>1</b>, if they are bound to each other</li></ul>
2289
         */
2290
        private CheckResult checkKeyMatch(IBinding key, TNamedEntity candidate) {
2291
                if (key == null) {
2✔
2292
                        return CheckResult.UNDECIDED;
2✔
2293
                }
2294

2295
                NamedEntity bound = (NamedEntity)getEntityByKey(key);
5✔
2296
                if (bound == candidate) {
3✔
2297
                        return CheckResult.MATCH;
2✔
2298
                }
2299
                else if (bound != null) {
2✔
2300
                        return CheckResult.FAIL;
2✔
2301
                }
2302
                else if (getEntityKey(candidate) != null) {
4✔
2303
                        // candidate already bound, and not to this binding
2304
                        return CheckResult.FAIL;
2✔
2305
                }
2306
                else {
2307
                        return CheckResult.UNDECIDED;
2✔
2308
                }
2309
        }
2310

2311
        private void conditionalMapToKey(IBinding bnd, TNamedEntity ent) {
2312
                if (bnd != null) {
2✔
2313
                        mapEntityToKey(bnd, ent);
4✔
2314
                }
2315
        }
1✔
2316

2317
        public Method ensureFamixMethod(IMethodBinding bnd) {
2318
                return ensureFamixMethod(
9✔
2319
                                bnd,
2320
                                /*name*/null,
2321
                                /*paramsType*/(Collection<String>)null,
2322
                                /*returnType*/null,
2323
                                /*owner*/null,
2324
                                (bnd == null) ? UNKNOWN_MODIFIERS : bnd.getModifiers());
6✔
2325
        }
2326

2327
        /**
2328
         * Returns a Famix Method associated with the IMethodBinding. The Entity is created if it does not exist.
2329
         * The Entity is created if it does not exist.
2330
         * @param name -- the name of the FAMIX Method (MUST NOT be null, but this is not checked)
2331
         * @param ret -- Famix Type returned by the method (ideally should only be null in case of a constructor, but will accept it in any case)
2332
         * @param owner -- type defining the method (should not be null, but it will work if it is)
2333
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
2334
         */
2335
        public Method ensureFamixMethod(IMethodBinding bnd, String name, Collection<String> paramTypes, TType ret, TWithMethods owner, int modifiers) {
2336
                Method fmx = null;
2✔
2337
                String sig;
2338
                boolean delayedRetTyp;
2339

2340
                // --------------- to avoid useless computations if we can
2341
                fmx = (Method)getEntityByKey(bnd);
5✔
2342
                if (fmx != null) {
2✔
2343
                        return fmx;
2✔
2344
                }
2345

2346
                // --------------- name
2347
                if (name == null) {
2✔
2348
                        if (bnd == null) {
2✔
2349
                                return null;
2✔
2350
                        }
2351
                        else {
2352
                                name = bnd.getName();
3✔
2353
                        }
2354
                }
2355

2356
                // --------------- signature
2357
                sig = name + "(";
3✔
2358
                 if (bnd != null) {
2✔
2359
                    sig += signatureParamsFromBinding(bnd);
7✔
2360
                }
2361
        else if (paramTypes != null) {
2!
2362
                        sig += signatureParamsFromStringCollection(paramTypes);
7✔
2363
                }
2364
                else {
2365
                        sig += "???";
×
2366
                }
2367
                sig += ")";
3✔
2368

2369
                // --------------- return type
2370
                delayedRetTyp = false;
2✔
2371
                ITypeBinding retTypBnd = null;
2✔
2372
                if (ret == null) {
2!
2373
                        if (bnd == null) {
2✔
2374
                                ret = null;  // what else ?
3✔
2375
                        }
2376
                        else {
2377
                                if (bnd.isConstructor()) {
3✔
2378
                                        ret = null;
3✔
2379
                                }
2380
                                else {
2381

2382
                                        // must create the return type
2383
                                        // but for method like "<T> T mtd()" where T belongs to mtd and mtd returns T,
2384
                                        // we need T to create the method and the method to create T ...
2385
                                        // so we need to test the situation and deal with it
2386
                                        retTypBnd = bnd.getReturnType();
3✔
2387
                                        if (retTypBnd == null) {
2✔
2388
                                                ret = null;
3✔
2389
                                        }
2390
                                        else if (retTypBnd.isArray()) {
3✔
2391
                                                retTypBnd = retTypBnd.getElementType();
3✔
2392
                                        }
2393

2394
                                        if ( (retTypBnd != null) && retTypBnd.isTypeVariable() && (retTypBnd.getDeclaringMethod() == bnd) ) {
9✔
2395
                                                ret = null;
2✔
2396
                                                delayedRetTyp = true;
3✔
2397
                                        }
2398
                                        else {
2399
                                                ret = this.ensureFamixType(retTypBnd,(TWithTypes) /*ctxt*/owner);
6✔
2400
                                        }
2401
                                }
2402
                        }
2403
                }
2404

2405
                // --------------- owner
2406
                if (owner == null) {
2✔
2407
                        if (bnd == null) {
2✔
2408
                                owner = ensureFamixClassStubOwner();
4✔
2409
                        }
2410
                        else {
2411
                                ITypeBinding classBnd = bnd.getDeclaringClass().getErasure();
4✔
2412
                                if (classBnd != null) {
2!
2413
                                        TType tmpOwn = ensureFamixType(classBnd);
4✔
2414

2415
                                        owner = (TWithMethods) tmpOwn;
3✔
2416
                                        
2417
                                }
1✔
2418
                                else {
2419
                                        owner = ensureFamixClassStubOwner();
×
2420
                                }
2421
                        }
2422
                }
2423

2424
                // --------------- recover from name ?
2425
                for (Method candidate : this.getEntityByName(Method.class, name)) {
13✔
2426
                        if (matchAndMapMethod(bnd, sig, ret, (TNamedEntity) owner, candidate)) {
9✔
2427
                                fmx = candidate;
2✔
2428
                                break;
1✔
2429
                        }
2430
                }
1✔
2431

2432
                if (fmx == null) {
2✔
2433
                        if(bnd != null && bnd.isGenericMethod()) {
5✔
2434
                                fmx = ensureFamixEntity(ParametricMethod.class, bnd, name);
7✔
2435
                                for(ITypeBinding param: bnd.getTypeParameters()) {
18✔
2436
                                        TypeParameter fmxParam = this.ensureFamixTypeParameter(param, null, (TWithTypes)fmx);
6✔
2437
                                        fmxParam.setGenericEntity((ParametricMethod)fmx);
4✔
2438
                                }
2439
                        // parameterized method binding = when the method is the target of an invocation.
2440
                        } else if (bnd != null && bnd.isParameterizedMethod()) {
5!
NEW
2441
                                fmx = (ParametricMethod)this.ensureFamixMethod(bnd.getMethodDeclaration());
×
2442
                        }else{
2443
                                fmx = ensureFamixEntity(Method.class, bnd, name);
7✔
2444
                        }
2445
                        
2446
                        fmx.setSignature(sig);
3✔
2447
                        ITypeBinding returnTypeBnd = (bnd == null) ? null : bnd.getReturnType();
7✔
2448
                        ensureFamixEntityTyping(returnTypeBnd, fmx, ret);
6✔
2449
                        fmx.setParentType(owner);
3✔
2450
                }
2451

2452
                if (fmx != null) {
2!
2453
                        setMethodModifiers(fmx, modifiers);
4✔
2454
                        // if it's a constructor
2455
                        if (fmx.getName().equals(Util.getOwner(fmx).getName())) {
7✔
2456
                                fmx.setKind(CONSTRUCTOR_KIND_MARKER);
3✔
2457
                        }
2458
                }
2459

2460
                if ((fmx != null) && delayedRetTyp) {
4!
2461
                        int retTypModifiers = (retTypBnd != null) ? retTypBnd.getModifiers() : UNKNOWN_MODIFIERS;
6!
2462
                        ITypeBinding returnTypeBnd = (bnd == null) ? null : bnd.getReturnType();
5!
2463
                        ensureFamixEntityTyping(returnTypeBnd, fmx, this.ensureFamixType(retTypBnd, /*name*/null, /*owner*/fmx, /*ctxt*/(ContainerEntity) owner, retTypModifiers));
13✔
2464
                }
2465

2466
                return fmx;
2✔
2467
        }
2468

2469
        /**
2470
         * Creates or recovers a stub Famix Method
2471
         * @param name of the method
2472
         * @return the Famix Method
2473
         */
2474
        public Method ensureFamixStubMethod(String name) {
2475
                return ensureFamixMethod(null, name, /*paramType*/(Collection<String>)null, /*returnType*/null, ensureFamixClassStubOwner(), /*modifiers*/0);  // cast needed to desambiguate the call
×
2476
        }
2477

2478
        public void setAttributeModifiers(Attribute fmx, int mod) {
2479
                setCommonModifiers(fmx, mod);
4✔
2480
                fmx.setIsTransient(Modifier.isTransient(mod));
5✔
2481
                fmx.setIsVolatile(Modifier.isVolatile(mod));
5✔
2482
        }
1✔
2483

2484
        public void setMethodModifiers(Method fmx, int mod) {
2485
                setCommonModifiers(fmx, mod);
4✔
2486
                fmx.setIsAbstract(Modifier.isAbstract(mod));
5✔
2487
                fmx.setIsSynchronized(Modifier.isSynchronized(mod));
5✔
2488
        }
1✔
2489

2490
        public void setClassModifiers(Class fmx, int mod) {
2491
                setCommonModifiers(fmx, mod);
4✔
2492
                fmx.setIsAbstract(Modifier.isAbstract(mod));
5✔
2493
        }
1✔
2494

2495
        public void setInterfaceModifiers(Interface fmx, int mod) {
2496
                setCommonModifiers(fmx, mod);
4✔
2497
        }
1✔
2498

2499
        private void setCommonModifiers(Entity fmx, int mod) {
2500
                setVisibility((THasVisibility)fmx, mod);
5✔
2501
                ((TCanBeClassSide)fmx).setIsClassSide(Modifier.isStatic(mod));
6✔
2502
                ((TCanBeFinal)fmx).setIsFinal(Modifier.isFinal(mod));
6✔
2503
        }
1✔
2504

2505
        /**
2506
         * Sets the visibility of a FamixNamedEntity
2507
         *
2508
         * @param fmx -- the FamixNamedEntity
2509
         * @param mod -- a description of the modifiers as understood by org.eclipse.jdt.core.dom.Modifier
2510
         */
2511
        public void setVisibility(THasVisibility fmx, int mod) {
2512
                if (Modifier.isPublic(mod)) {
3✔
2513
                        fmx.setVisibility(MODIFIER_PUBLIC);
4✔
2514
                } else if (Modifier.isPrivate(mod)) {
3✔
2515
                        fmx.setVisibility(MODIFIER_PRIVATE);
4✔
2516
                } else if (Modifier.isProtected(mod)) {
3✔
2517
                        fmx.setVisibility(MODIFIER_PROTECTED);
4✔
2518
                } else {
2519
                        fmx.setVisibility(MODIFIER_PACKAGE);
3✔
2520
                }
2521
        }
1✔
2522

2523
        /**
2524
         * Returns a Famix Attribute associated with the IVariableBinding.
2525
         * The Entity is created if it does not exist.<br>
2526
         * @param name -- the name of the FAMIX Attribute (MUST NOT be null, but this is not checked)
2527
         * @param type -- Famix Type of the Attribute (should not be null, but it will work if it is)
2528
         * @param owner -- type defining the Attribute (should not be null, but it will work if it is)
2529
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
2530
         */
2531
        public Attribute ensureFamixAttribute(IVariableBinding bnd, String name, Type type, TWithAttributes owner) {
2532
                Attribute fmx = null;
2✔
2533

2534
                // --------------- to avoid useless computations if we can
2535
                fmx = (Attribute)getEntityByKey(bnd);
5✔
2536
                if (fmx != null) {
2✔
2537
                        return fmx;
2✔
2538
                }
2539

2540
                // --------------- name
2541
                if (name == null) {
2!
2542
                        if (bnd == null) {
×
2543
                                return null;
×
2544
                        }
2545
                        else {
2546
                                name = bnd.getName();
×
2547
                        }
2548
                }
2549

2550
                // --------------- owner
2551
                if (owner == null) {
2✔
2552
                        if (bnd == null) {
2!
2553
                                return null;  // what would be the interest of creating an attribute for which we ignore the declaring class?
×
2554
                        }
2555
                        else {
2556
                                if (bnd.getDeclaringClass() != null && bnd.getDeclaringClass().getErasure() != null) {
7!
2557
                                        // Declaring class is the generic one if the class is parametric.
2558
                                        owner = (TWithAttributes)ensureFamixType(bnd.getDeclaringClass().getErasure());
8✔
2559
                                } else {
2560
                                        return null;  // what would be the interest of creating an attribute for which we ignore the declaring class?
2✔
2561
                                }
2562
                        }
2563
                }
2564

2565
                // --------------- recover from name ?
2566
                for (Attribute candidate : getEntityByName(Attribute.class, name)) {
13✔
2567
                        if (matchAndMapVariable(bnd, name, (TNamedEntity) owner, candidate)) {
8✔
2568
                                fmx = candidate;
2✔
2569
                                break;
1✔
2570
                        }
2571
                }
1✔
2572

2573
                if (fmx == null) {
2✔
2574
                        fmx = ensureFamixEntity(Attribute.class, bnd, name);
7✔
2575
                        fmx.setParentType( owner);
3✔
2576
                }
2577

2578
                if (fmx != null) {
2!
2579
                        fmx.setParentType((TWithAttributes) owner);
3✔
2580
                        ITypeBinding declaredTypeBinding = (bnd == null) ? null : bnd.getType();
7✔
2581
                        ensureFamixEntityTyping(declaredTypeBinding, fmx, type);
6✔
2582
                        if (bnd != null) {
2✔
2583
                                int mod = bnd.getModifiers();
3✔
2584
                                setAttributeModifiers(fmx, mod);
4✔
2585
                        }
2586
                }
2587

2588
                return fmx;
2✔
2589
        }
2590

2591
        public Attribute ensureFamixAttribute(IVariableBinding bnd, String name, TWithAttributes owner) {
2592
                return ensureFamixAttribute(bnd, name, /*declared type*/null, owner);
7✔
2593
        }
2594

2595
        /**
2596
         * helper method, we know the var exists, ensureFamixAttribute will recover it
2597
         */
2598
        public Attribute getFamixAttribute(IVariableBinding bnd, String name, TWithAttributes owner) {
2599
                return ensureFamixAttribute(bnd, name, /*declared type*/null, owner);
7✔
2600
        }
2601

2602
        /**
2603
         * Returns a Famix Parameter associated with the IVariableBinding.
2604
         * The Entity is created if it does not exist.<br>
2605
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
2606
         */
2607
        public Parameter ensureFamixParameter(IVariableBinding bnd, String name, Type typ, TMethod tMethod) {
2608
                Parameter fmx = null;
2✔
2609

2610
                // --------------- to avoid useless computations if we can
2611
                try {
2612
                        fmx = (Parameter)getEntityByKey(bnd);
5✔
2613

2614
                }catch(Throwable e) {
×
2615
                        e.printStackTrace();
×
2616
                }
1✔
2617
                if (fmx != null) {
2✔
2618
                        return fmx;
2✔
2619
                }
2620

2621
                // --------------- name
2622
                if (name == null) {
2!
2623
                        if (bnd == null) {
×
2624
                                return null;
×
2625
                        }
2626
                        else {
2627
                                name = bnd.getName();
×
2628
                        }
2629
                }
2630

2631
                // --------------- owner
2632
                if (tMethod == null) {
2!
2633
                        if (bnd == null) {
×
2634
                                tMethod = ensureFamixStubMethod("<"+name+"_owner>");
×
2635
                        }
2636
                        else {
2637
                                tMethod = ensureFamixMethod(bnd.getDeclaringMethod());
×
2638
                        }
2639
                }
2640

2641
                // --------------- recover from name ?
2642
                for (Parameter candidate : getEntityByName(Parameter.class, name) ) {
13✔
2643
                        if ( matchAndMapVariable(bnd, name, tMethod, candidate) ) {
7!
2644
                                fmx = candidate;
×
2645
                                break;
×
2646
                        }
2647
                }
1✔
2648

2649
                if (fmx == null) {
2!
2650
                        fmx = ensureFamixEntity(Parameter.class, bnd, name);
7✔
2651
                        fmx.setParentBehaviouralEntity(tMethod);
3✔
2652
                }
2653

2654
                if (fmx != null) {
2!
2655
                        fmx.setParentBehaviouralEntity(tMethod);
3✔
2656
                        ITypeBinding declaredTypeBnd = (bnd == null) ? null : bnd.getType();
5!
2657
                        ensureFamixEntityTyping(declaredTypeBnd, fmx, typ);
6✔
2658
                }
2659

2660
                return fmx;
2✔
2661
        }
2662

2663
        /**
2664
         * helper method, we know the var exists, ensureFamixParameter will recover it
2665
         */
2666
        public Parameter getFamixParameter(IVariableBinding bnd, String name, TMethod tMethod) {
2667
                return ensureFamixParameter(bnd, name, /*declared type*/null, tMethod);
×
2668
        }
2669

2670
        /**
2671
         * Returns a Famix LocalVariable associated with the IVariableBinding.
2672
         * The Entity is created if it does not exist.<br>
2673
         * @param name -- the name of the FAMIX LocalVariable
2674
         * @return the Famix Entity found or created. May return null if <b>bnd</b> and <b>name</b> are null, or <b>bnd</b> and <b>owner</b> are null, or in case of a Famix error
2675
         */
2676
        public LocalVariable ensureFamixLocalVariable(IVariableBinding bnd, String name, TWithLocalVariables owner) {
2677
                LocalVariable fmx = null;
2✔
2678

2679
                // --------------- to avoid useless computations if we can
2680
                fmx = (LocalVariable)getEntityByKey(bnd);
5✔
2681
                if (fmx != null) {
2!
2682
                        return fmx;
×
2683
                }
2684

2685
                // --------------- name
2686
                if (name == null) {
2!
2687
                        if (bnd == null) {
×
2688
                                return null;
×
2689
                        }
2690
                        else {
2691
                                name = bnd.getName();
×
2692
                        }
2693
                }
2694

2695
                // --------------- owner
2696
                if (owner == null) {
2!
2697
                        if (bnd == null) {
×
2698
                                return null;  // what would be the interest of a local variable for which we ignore the declaring method?
×
2699
                        }
2700
                        else {
2701
                                owner = ensureFamixMethod(bnd.getDeclaringMethod());
×
2702
                        }
2703
                }
2704

2705
                // --------------- recover from name ?
2706
                for (LocalVariable candidate : getEntityByName(LocalVariable.class, name) ) {
13✔
2707
                        if ( matchAndMapVariable(bnd, name, (TNamedEntity) owner, candidate) ) {
8!
2708
                                fmx = candidate;
×
2709
                                break;
×
2710
                        }
2711
                }
1✔
2712

2713
                if (fmx == null) {
2!
2714
                        fmx = ensureFamixEntity(LocalVariable.class, bnd, name);
7✔
2715
                        fmx.setParentBehaviouralEntity(owner);
3✔
2716
                }
2717

2718
                if (fmx != null) {
2!
2719
                        // we just created it or it was not bound, so we make sure it has the right information in it
2720
                        fmx.setParentBehaviouralEntity(owner);
3✔
2721
                }
2722

2723
                return fmx;
2✔
2724
        }
2725

2726
        /**
2727
         * helper method, we know the var exists, ensureFamixLocalVariable will recover it
2728
         */
2729
        public LocalVariable getFamixLocalVariable(IVariableBinding bnd, String name, TWithLocalVariables owner) {
2730
                return ensureFamixLocalVariable(bnd, name, owner);
×
2731
        }
2732

2733
        /**
2734
         * Returns a FAMIX ImplicitVariable with the given <b>name</b> ("self" or "super") and corresponding to the <b>type</b>.
2735
         * If this ImplicitVariable does not exist yet, it is created
2736
         * @param name -- the name of the FAMIX ImplicitVariable (should be Dictionary.SELF_NAME or Dictionary.SUPER_NAME)
2737
         * @param type -- the Famix Type for this ImplicitVariable (should not be null)
2738
         * @param tMethod -- the ContainerEntity where the implicit variable appears (should be a method inside <b>type</b>)
2739
         * @return the FAMIX ImplicitVariable or null in case of a FAMIX error
2740
         */
2741
        public ImplicitVariable ensureFamixImplicitVariable(IBinding key, String name, TType type, TMethod tMethod) {
2742
                ImplicitVariable fmx;
2743
                fmx = ensureFamixEntity(ImplicitVariable.class, key, name);
7✔
2744
                fmx.setParentBehaviouralEntity(tMethod);
3✔
2745
                return fmx;
2✔
2746
        }
2747

2748
        public ImplicitVariable ensureFamixImplicitVariable(String name, TType tType, TMethod tMethod) {
2749
                IBinding bnd = ImplicitVarBinding.getInstance(tMethod, name);
4✔
2750
                return ensureFamixImplicitVariable(bnd, name, tType, tMethod);
7✔
2751
        }
2752

2753
        /**
2754
         * Creates and returns a Famix Comment and associates it with an Entity (ex: for Javadocs)
2755
         * @param jCmt -- the content (String) of the comment 
2756
         * @param owner -- the entity that is commented
2757
         * @return the Famix Comment
2758
         */
2759
        public Comment createFamixComment(org.eclipse.jdt.core.dom.Comment jCmt, TWithComments owner) {
2760
                Comment cmt = null;
2✔
2761

2762
                if ( (jCmt != null) && (owner != null) ) {
4!
2763
                        
2764
                        cmt = new Comment();
4✔
2765
                        addSourceAnchor(cmt, jCmt);
5✔
2766
                        famixRepoAdd(cmt);
3✔
2767
                        cmt.setCommentedEntity(owner);
3✔
2768
                }
2769

2770
                return cmt;
2✔
2771
        }
2772

2773
        /**
2774
         * Creates and returns a Famix Comment and associates it with an Entity
2775
         * @param jCmt -- the content (String) of the comment 
2776
         * @param owner -- the entity that is commented
2777
         * @param content -- the text of the comment
2778
         * @return the Famix Comment
2779
         */
2780
        public Comment createFamixComment(org.eclipse.jdt.core.dom.Comment jCmt, TWithComments owner, String content) {
2781
                Comment cmt = null;
2✔
2782

2783
                if ( (jCmt != null) && (owner != null) ) {
4!
2784
                        cmt = new Comment();
4✔
2785
                        cmt.setContent(content );
3✔
2786
                        famixRepoAdd(cmt);
3✔
2787
                        cmt.setCommentedEntity(owner);
3✔
2788
                }
2789

2790
                return cmt;
2✔
2791
        }
2792

2793
        /**
2794
         * Adds location information to a Famix Entity.
2795
         * Location informations are: <b>name</b> of the source file and <b>line</b> position in this file. They are found in the JDT ASTNode: ast.
2796
         * This method also creates some basic links between the entity and others (e.g. declaring container, return type, ...)
2797
         * @param fmx -- Famix Entity to add the anchor to
2798
         * @param node -- JDT ASTNode, where the information is extracted
2799
         * @return the Famix SourceAnchor added to fmx. May be null in case of incorrect parameter ('fmx' or 'ast' == null)
2800
         */
2801
        public SourceAnchor addSourceAnchor(TSourceEntity fmx, ASTNode node) {
2802
                IndexedFileAnchor fa = null;
2✔
2803

2804
                fa = createIndexedFileAnchor(node);
4✔
2805
                if ((fmx != null) && (fa != null)) {
4!
2806
                        fmx.setSourceAnchor(fa);
3✔
2807
                        famixRepoAdd(fa);
3✔
2808
                }
2809

2810
                return fa;
2✔
2811
        }
2812

2813
        /**
2814
         * Special case of  {@linkplain #addSourceAnchor(TSourceEntity, ASTNode)} to add location information to a Famix Method.
2815
         */
2816
        public SourceAnchor addSourceAnchor(Method fmx, MethodDeclaration node) {
2817
                IndexedFileAnchor fa = null;
2✔
2818

2819
                fa = createIndexedFileAnchor(node);
4✔
2820
                if ((fmx != null) && (fa != null)) {
4!
2821

2822
                        // may change the positions
2823
                        List<ASTNode> methodDeclarationModifiers = new ArrayList<>();
4✔
2824
                        methodDeclarationModifiers.addAll(node.modifiers());
5✔
2825
                        if (node.getName() != null) {
3!
2826
                                methodDeclarationModifiers.add(node.getName());
5✔
2827
                        }
2828
                        if (node.getReturnType2() != null) {
3✔
2829
                                methodDeclarationModifiers.add(node.getReturnType2());
5✔
2830
                        }
2831
                        int beg = (methodDeclarationModifiers.stream().mapToInt(el -> el.getStartPosition()).min().getAsInt()) + 1;
12✔
2832
                        int end = node.getStartPosition() + node.getLength();
6✔
2833

2834
                        ((IndexedFileAnchor)fa).setStartPos(beg);
4✔
2835
                        ((IndexedFileAnchor)fa).setEndPos(end);
4✔
2836

2837
                        fmx.setSourceAnchor(fa);
3✔
2838
                        famixRepoAdd(fa);
3✔
2839
                }
2840

2841
                return fa;
2✔
2842
        }
2843

2844
        /**
2845
         * Gets the file name holding <code>node</code> and its start and end positions in the file.
2846
         * Information returned in the form of an IndexedFileAnchor
2847
         */
2848
        protected IndexedFileAnchor createIndexedFileAnchor(ASTNode node) {
2849
                IndexedFileAnchor fa;
2850
                
2851
                if (node == null) {
2!
2852
                        return null;
×
2853
                }
2854

2855
                // position in source file
2856
                int beg = node.getStartPosition() + 1; // Java starts at 0, Moose at 1
5✔
2857
                int end = beg + node.getLength() - 1;
7✔
2858

2859
                // find source Compilation Unit
2860
                // there is a special case for the JDT Comment Nodes
2861
                if (node instanceof org.eclipse.jdt.core.dom.Comment) {
3✔
2862
                        node = ((org.eclipse.jdt.core.dom.Comment) node).getAlternateRoot();
5✔
2863
                } else {
2864
                        node = node.getRoot();
3✔
2865
                }
2866

2867
                fa = new IndexedFileAnchor();
4✔
2868
                ((IndexedFileAnchor)fa).setStartPos(beg);
4✔
2869
                ((IndexedFileAnchor)fa).setEndPos(end);
4✔
2870

2871
                fa.setFileName((String) ((CompilationUnit)node).getProperty(SOURCE_FILENAME_PROPERTY));
7✔
2872

2873
                return fa;
2✔
2874
        }
2875

2876
        /**
2877
         * Creates or recovers the Famix Class for "Object".
2878
         * Because "Object" is the root of the inheritance tree, it needs to be treated differently.
2879
         *
2880
         * @param bnd -- a potential binding for the java "Object" class
2881
         * @return a Famix class for "Object"
2882
         */
2883
        public Class ensureFamixClassObject(ITypeBinding bnd) {
2884
                Class fmx = ensureFamixUniqEntity(Class.class, bnd, OBJECT_NAME);
7✔
2885

2886
                if (fmx != null) {
2!
2887
                        fmx.setTypeContainer(ensureFamixPackageJavaLang(null));
5✔
2888
                }
2889
                // Note: "Object" has no superclass
2890

2891
                return fmx;
2✔
2892
        }
2893

2894
        /**
2895
         * Ensures the Java meta-class: <pre>{@code java.lang.Class<>}</pre>
2896
         */
2897
        public Class ensureFamixMetaClass(ITypeBinding bnd) {
2898
                Package javaLang = ensureFamixPackageJavaLang((bnd == null) ? null : bnd.getPackage());
7!
2899
                ParametricClass fmx = (ParametricClass) this.ensureFamixClass(null, METACLASS_NAME, javaLang, /*isGeneric*/true, Modifier.PUBLIC & Modifier.FINAL);
9✔
2900

2901
                if (fmx != null) {
2!
2902
                        fmx.addTypeParameters(ensureFamixTypeParameter(null, "T", fmx));
7✔
2903
                }
2904

2905
                if ((fmx != null) && (fmx.getSuperInheritances() == null)) {
5!
NEW
2906
                        ensureFamixInheritance(ensureFamixClassObject(null), fmx, null, null);
×
2907
                }
2908

2909
                return fmx;
2✔
2910
        }
2911

2912
        public Class getFamixMetaClass(ITypeBinding bnd) {
2913
                Package javaLang = ensureFamixPackageJavaLang((bnd == null) ? null : bnd.getPackage());
7!
2914
                return this.ensureFamixClass(null, METACLASS_NAME, javaLang, /*isGeneric*/true, UNKNOWN_MODIFIERS);
8✔
2915
        }
2916

2917
        /**
2918
         * Creates or recovers the Famix Class for all arrays (<pre>{@code <some-type> []}</pre>)
2919
         * In java arrays or objects of special classes (i.e. "I[" for an array of int).
2920
         * JDT does not create a binding for these classes, so we create a stub one here.
2921
         *
2922
         * @return a Famix class
2923
         */
2924
        public Class ensureFamixClassArray() {
2925
                Class fmx = ensureFamixUniqEntity(Class.class, null, ARRAYS_NAME);
×
2926
                if (fmx != null) {
×
NEW
2927
                        ensureFamixInheritance(ensureFamixClassObject(null), fmx, /*prev*/null, null);
×
2928
                        fmx.setTypeContainer(ensureFamixPackageDefault());
×
2929

2930
                        // may be not needed anymore now that we use modifiers
2931
                        /*fmx.setIsAbstract(Boolean.FALSE);
2932
                        fmx.setIsFinal(Boolean.FALSE);
2933
                        fmx.setIsInterface(Boolean.FALSE); 
2934
                        fmx.setIsPrivate(Boolean.FALSE);
2935
                        fmx.setIsProtected(Boolean.FALSE);*/
2936
                        fmx.setVisibility(MODIFIER_PUBLIC);
×
2937
                }
2938

2939
                return fmx;
×
2940
        }
2941

2942
        public String removeLastPartOfPackageName(String qualifiedName) {
2943
                String ret = null;
2✔
2944
                int last = qualifiedName.lastIndexOf('.');
4✔
2945
                if (last > 0) {
2✔
2946
                        // recursively creating the parent
2947
                        ret = qualifiedName.substring(0, last);
6✔
2948
                }
2949
                else {
2950
                        ret = "";
2✔
2951
                }
2952

2953
                return ret;
2✔
2954
        }
2955

2956
        /** Generates the list of parameters for a method signature
2957
         * @return a string
2958
         */
2959
        protected String signatureParamsFromBinding(IMethodBinding bnd) {
2960
                boolean first = true;
2✔
2961
                String sig = new String();
4✔
2962

2963
                for (ITypeBinding parBnd : bnd.getParameterTypes()) {
17✔
2964
                        if (first) {
2✔
2965
                                sig = parBnd.getName();
3✔
2966
                                first = false;
3✔
2967
                        }
2968
                        else {
2969
                                sig += "," + parBnd.getName();
5✔
2970
                        }
2971
                }
2972
                return sig;
2✔
2973
        }
2974

2975
        private String signatureParamsFromStringCollection(Collection<String> paramTypes) {
2976
                boolean first = true;
2✔
2977
                String sig = new String();
4✔
2978

2979
                for (String t : paramTypes) {
10✔
2980
                        if (first) {
2✔
2981
                                sig = t;
2✔
2982
                                first = false;
3✔
2983
                        }
2984
                        else {
2985
                                sig += "," + t;
4✔
2986
                        }
2987
                }
1✔
2988
                return sig;
2✔
2989
        }
2990

2991
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc