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

moosetechnology / VerveineJ / 22185271598

19 Feb 2026 02:12PM UTC coverage: 51.954% (+0.4%) from 51.544%
22185271598

push

github

web-flow
Merge pull request #199 from moosetechnology/initializers

Introducing initializers

1972 of 3976 branches covered (49.6%)

Branch coverage included in aggregate %.

105 of 127 new or added lines in 26 files covered. (82.68%)

1 existing line in 1 file now uncovered.

4343 of 8179 relevant lines covered (53.1%)

2.15 hits per line

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

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

3
import java.util.*;
4

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

49
import ch.akuhn.fame.Repository;
50
import fr.inria.verveine.extractor.java.utils.ImplicitVarBinding;
51
import fr.inria.verveine.extractor.java.utils.Util;
52

53
/**
54
 * A dictionary of FamixJava entities to help create them and find them back
55
 * Entities are mapped to keys which are the "binding" provided by the JDT pars
56
 * 
57
 * @author anquetil
58
 */
59
public class EntityDictionary {
60

61
        /**
62
         * A property added to CompilationUnits to record the name of the source file they belong to.
63
         * Used to create FileAnchors
64
         */
65
        public static final String SOURCE_FILENAME_PROPERTY = "verveine-source-filename";
66

67
        public static final String DEFAULT_PCKG_NAME = "<Default Package>";
68
        public static final String STUB_METHOD_CONTAINER_NAME = "<StubMethodContainer>";
69
        public static final String THIS_NAME = "this";
70
        public static final String SUPER_NAME = "super";
71
        
72
        public static final String OBJECT_NAME = "Object";
73
        public static final String METACLASS_NAME = "Class";
74
        public static final String OBJECT_PACKAGE_NAME = "java.lang";
75
        public static final String ARRAYS_NAME = "default[]";
76
        public static final String INIT_BLOCK_NAME = "<Initializer>";
77
        public static final String ANONYMOUS_NAME_PREFIX = "_Anonymous";
78

79
        public static final int UNKNOWN_MODIFIERS = 0;
80
        public static final String MODIFIER_PUBLIC   = "public";
81
        public static final String MODIFIER_PRIVATE  = "private";
82
        public static final String MODIFIER_PROTECTED= "protected";
83
        public static final String MODIFIER_PACKAGE = "package";
84

85
    /**
86
     * The symbol kind to use to define that a method is a default implementation in an interface
87
     */
88
    public static final String DEFAULT_IMPLEMENTATION_KIND_MARKER = "default";
89

90
        /** name of the entity representing the "unknown" type 'var'
91
         * The entity is intended to be unique, see {@link #ensureFamixUniqEntity(java.lang.Class, IBinding , String )}
92
         */
93
        public static final String IMPLICIT_VAR_TYPE_NAME = "<ImplicitVarType>";
94

95
        /**
96
         * The FAMIX repository where all FAMIX entities are created and stored
97
         */
98
        protected Repository famixRepo;
99

100
        /**
101
         * A dictionary to map a key (provided by the user) to FAMIX Entity
102
         */
103
        protected Map<IBinding,TNamedEntity> keyToEntity;
104
        /**
105
         * A reverse dictionary (see {@link #keyToEntity}) to find the key of an entity.
106
         */
107
        protected Map<TNamedEntity,IBinding> entityToKey;
108

109
        /**
110
         * Another dictionary to map a name to FAMIX Entities with this name
111
         */
112
        protected Map<String,Collection<TNamedEntity>> nameToEntity;
113

114
        /**
115
         * Yet another dictionary for implicit variables ('self' and 'super')
116
         * Because they are implicit, they may not have a binding provided by the parser,
117
         * or may have the same binding as their associated type so they can't be kept easily in {@link #keyToEntity}
118
         */
119
        @Deprecated
120
        protected Map<Type,ImplicitVars> typeToImpVar;
121

122
        /**
123
         * Used to keep the two possible ImplicitVariable for a given Class binding
124
         * @author anquetil
125
         */
126
        @Deprecated
127
        protected class ImplicitVars {
×
128
                public ImplicitVariable self_iv;
129
                public ImplicitVariable super_iv;
130
        }
131
        
132
        /**
133
         * Result of utility methods for checking matching between two entities
134
         */
135
        private enum CheckResult {
3✔
136
                MATCH, UNDECIDED, FAIL;
18✔
137
        }
138

139

140
        /** Constructor taking a FAMIX repository
141
         * @param famixRepo
142
         */
143
        public EntityDictionary(Repository famixRepo) {
2✔
144
                        this.famixRepo = famixRepo;
3✔
145
                        
146
                        this.keyToEntity = new Hashtable<IBinding,TNamedEntity>();
5✔
147
                        this.entityToKey = new Hashtable<TNamedEntity,IBinding>();
5✔
148
                        this.nameToEntity = new Hashtable<String,Collection<TNamedEntity>>();
5✔
149
                        this.typeToImpVar = new Hashtable<Type,ImplicitVars>();
5✔
150
                        
151
                        if (! this.famixRepo.isEmpty()) {
4✔
152
                                recoverExistingRepository();
2✔
153
                        }
154
                }
1✔
155

156
    /**
157
         * Resets the dictionnary in a proper state after loading entities from an existing MSE file:
158
         * <UL>
159
         * <li>map all named entities to their names in <b>mapName</b></li>
160
         * <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>
161
         * </ul>
162
         */
163
        protected void recoverExistingRepository() {
164
                for (NamedEntity ent : famixRepo.all(NamedEntity.class)) {
13✔
165
                        try {
166
                                mapEntityToName(ent.getName(), ent);
5✔
NEW
167
                        } catch (java.lang.Exception e) {
×
NEW
168
                                System.err.println("Error recovering entity " + ent.getName() + " from repository " + famixRepo);
×
169
                        };
1✔
170
                        // for the Exception to be raised, the return value must be tested
171
                        try { if (((TCanBeStub) ent).getIsStub()) {} }
5✔
172
                        catch (NullPointerException e) { ((TCanBeStub)ent).setIsStub(Boolean.FALSE); }
6✔
173
                }
1✔
174

175
                for (Access acc : famixRepo.all(Access.class)) {
13✔
176
                        // for the Exception to be raised, the return value must be tested
177
                        try { if (acc.getIsWrite()) {}        }
4✔
178
                        catch (NullPointerException e) { acc.setIsWrite(Boolean.FALSE); }
5✔
179
                }
1✔
180
        }
1✔
181

182
        protected void mapEntityToName(String name, TNamedEntity ent) {
183
                
184
                Collection<TNamedEntity> l_ent = nameToEntity.get(name);
6✔
185
                if (l_ent == null) {
2✔
186
                        l_ent = new LinkedList<>();
4✔
187
                }
188
                l_ent.add(ent);
4✔
189
                nameToEntity.put(name, l_ent);
6✔
190
        }
1✔
191

192
        public void removeEntity( NamedEntity ent) {
193
                IBinding key;
194
                key = entityToKey.get(ent);
6✔
195
                if (key != null) {
2✔
196
                        entityToKey.remove(ent);
5✔
197
                        keyToEntity.remove(key);
5✔
198
                }
199

200
                Collection<TNamedEntity> l_ent = nameToEntity.get(ent.getName());
7✔
201
                if (l_ent != null) {
2✔
202
                        l_ent.remove(ent);
4✔
203
                }
204

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

236
                return ret;
2✔
237
        }
238

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

256
        /**
257
         * Returns the key associated to a Famix Entity.
258
         * @param e -- the Named entity
259
         * @return the key associated to this entity or null if none
260
         */
261
        public IBinding getEntityKey(TNamedEntity e) {
262
                return entityToKey.get(e);
6✔
263
        }
264

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

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

292
                        mapEntityToName(name, fmx);
4✔
293
                        
294
                        // put new entity in Famix repository
295
                        famixRepoAdd((Entity) fmx);
4✔
296
                }
297

298
                return fmx;
2✔
299
        }
300
        
301
        /**
302
         * Returns a Famix Entity of the type <b>fmxjava.lang.Class</b> and maps it to its binding <b>bnd</b> (if not null).
303
         * The Entity is created if it did not exist.
304
         * @param fmxClass -- the Famix class of the instance to create
305
         * @param bnd -- the binding to map to the new instance
306
         * @param name -- the name of the new instance (used if <pre>{@code bnd == null}</pre>)
307
         * @return the Famix Entity or null if <b>bnd</b> was null or in case of a Famix error
308
         */
309
        @SuppressWarnings("unchecked")
310
        protected <T extends TNamedEntity & TSourceEntity> T ensureFamixEntity(java.lang.Class<T> fmxClass, IBinding bnd, String name) {
311
                T fmx = null;
2✔
312
                
313
                /* 
314
                 * Unfortunately different entities with the same name and same type may exist
315
                 * e.g. 2 parameters of 2 different methods but having the same name
316
                 * so we cannot recover just from the name
317
                 */
318

319
                if (bnd != null) {
2✔
320
                        fmx = (T) getEntityByKey(bnd);
4✔
321
                        if (fmx != null) {
2✔
322
                                return fmx;
2✔
323
                        }
324
                }
325

326
                // else
327
                fmx = createFamixEntity(fmxClass, name);
5✔
328
                if ( (bnd != null) && (fmx != null) ) {
4!
329
                        keyToEntity.put(bnd, fmx);
6✔
330
                        entityToKey.put(fmx, bnd);
6✔
331
                }
332
                
333
                return fmx;
2✔
334
        }
335

336
        /**
337
         * Adds an already created Entity to the Famix repository
338
         * Used mainly for non-NamedEntity, for example relationships
339
         * @param e -- the Famix entity to add to the repository
340
         */
341
        public void famixRepoAdd(Entity e) {
342
                this.famixRepo.add(e);
4✔
343
        }
1✔
344

345

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

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

391
        public AnnotationInstanceAttribute createFamixAnnotationInstanceAttribute(AnnotationTypeAttribute att, String value) {
392
                AnnotationInstanceAttribute fmx = null;
2✔
393
                if ( (att != null) && (value != null) ) {
4!
394
                        fmx = new AnnotationInstanceAttribute();
4✔
395
                        fmx.setAnnotationTypeAttribute(att);
3✔
396
                        fmx.setValue(value);
3✔
397
                        this.famixRepo.add(fmx);
4✔
398
                }
399
                return fmx;
2✔
400
        }
401

402
        public AnnotationInstance addFamixAnnotationInstance(TWithAnnotationInstances fmx, AnnotationType annType, Collection<AnnotationInstanceAttribute> annAtts) {
403
                AnnotationInstance inst = null;
2✔
404
                if ( (fmx != null) && (annType != null) ) {
4!
405
                        inst = new AnnotationInstance();
4✔
406
                        inst.setAnnotatedEntity(fmx);
3✔
407
                        inst.setAnnotationType(annType);
3✔
408
                        inst.addAttributes(annAtts);
3✔
409
                        this.famixRepo.add(inst);
4✔
410
                }
411
                return inst;
2✔
412
        }
413

414
        ///// ensure Famix Relationships /////
415

416
        /**
417
         * Returns a Famix Inheritance relationship between two Famix Classes creating it if needed
418
         * @param sup -- the super class
419
         * @param sub -- the sub class
420
         * @param prev -- previous inheritance relationship in the same context
421
         * @return the Inheritance relationship
422
         */
423
        public Inheritance ensureFamixInheritance(TWithInheritances sup, TWithInheritances sub, TAssociation prev, ITypeBinding supBnd) {
424
                if ( (sup == null) || (sub == null) ) {
4!
425
                        return null;
×
426
                }
427

428
                // Does the inheritance already exist?
429
                for (TInheritance i : (sup).getSubInheritances()) {                        
11✔
430
                        if (i.getSubclass() == sub) {
4✔
431
                                return (Inheritance) i;
3✔
432
                        }
433
                }
1✔
434

435
                Inheritance inh;
436
                if (supBnd != null && supBnd.isParameterizedType()) { // Needs checks and tests.
5✔
437
                        inh = (ParametricInheritance)buildFamixParametricAssociation(new ParametricInheritance(), supBnd.getErasure().getTypeParameters(), supBnd.getTypeArguments());
13✔
438
                } else {
439
                        inh = new Inheritance();
4✔
440
                }
441

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

463
                while (concreteIterator.hasNext() && genericIterator.hasNext()) {
6!
464
                        TTypeArgument typeArgument = (TTypeArgument)ensureFamixType(concreteIterator.next());
7✔
465
                        TypeParameter typeParameter = (TypeParameter)ensureFamixType(genericIterator.next());
7✔
466

467
                        Concretization concretization = ensureFamixConcretization(typeArgument, typeParameter);
5✔
468
                        association.addConcretization(concretization);
3✔
469
                }
1✔
470

471
                return association;
2✔
472
        }
473

474
        /**
475
         * Returns a Famix Concretization relationship between a Concrete Type and a ParameterType
476
         * @param typeArgument -- the concrete type
477
         * @param typeParameter -- the generic type parameter
478
         * @return the Concretization relationship
479
         */
480
        public Concretization ensureFamixConcretization(TTypeArgument typeArgument, TypeParameter typeParameter ) {
481
                if ( (typeArgument == null) || (typeParameter == null) ) {
4!
482
                        return null;
×
483
                }
484

485
                Concretization concretization = new Concretization();
4✔
486
                concretization.setTypeArgument(typeArgument);
3✔
487
                concretization.setTypeParameter(typeParameter);
3✔
488

489
                famixRepoAdd(concretization);
3✔
490
                return concretization;
2✔
491
        }
492
        
493
                /**
494
         * Returns a Famix Implementation relationship between two Famix Classes creating it if needed
495
         * @param myInterface -- the implemented interface
496
         * @param implementingClass -- the implementing class
497
         * @param prev -- previous inheritance relationship in the same context
498
         * @return the Inheritance relationship
499
         */
500
        public Implementation ensureFamixImplementation(TImplementable myInterface, TCanImplement implementingClass, TAssociation prev, ITypeBinding supBnd) {
501
                if ( (myInterface == null) || (implementingClass == null) ) {
4!
502
                        return null;
×
503
                }
504

505
                for (TImplementation imp : myInterface.getImplementations()) {
11✔
506
                        if (imp.getImplementingClass() == implementingClass) {
4✔
507
                                return (Implementation) imp;
3✔
508
                        }
509
                }
1✔
510
                
511
                Implementation implementation;
512
                if (supBnd != null && supBnd.isParameterizedType()) { // Needs checks and tests.
5!
513
                        implementation = (ParametricImplementation)buildFamixParametricAssociation(new ParametricImplementation(), supBnd.getErasure().getTypeParameters(), supBnd.getTypeArguments());
13✔
514
                } else {
515
                        implementation = new Implementation();
4✔
516
                }
517

518
                implementation.setImplementingClass(implementingClass);
3✔
519
                implementation.setMyInterface(myInterface);
3✔
520
                chainPrevNext(prev, implementation);
4✔
521
                famixRepoAdd(implementation);
3✔
522
                return implementation;
2✔
523
        }
524

525
        public void ensureImplementedInterfaces(ITypeBinding bnd, TType fmx, TWithTypes owner, TAssociation lastAssociation) {
526
                for (ITypeBinding intbnd : bnd.getInterfaces()) {
17✔
527
                        Type superTyp;
528
                        if(intbnd.isClass()){
3✔
529
                                superTyp = this.ensureFamixInterface(intbnd, intbnd.getName(), null, intbnd.isGenericType() || intbnd.isParameterizedType() || intbnd.isRawType(), intbnd.getModifiers());
20!
530
                        }else {
531
                                superTyp = this.ensureFamixType(intbnd);
4✔
532
                        }
533
                        
534
                        if (bnd.isInterface()) {
3✔
535
                                // in Java "subtyping" link between 2 interfaces is call inheritance 
536
                                lastAssociation = ensureFamixInheritance((TWithInheritances)superTyp, (TWithInheritances)fmx, lastAssociation, intbnd);
10✔
537
                        }
538
                        else {
539
                                lastAssociation = ensureFamixImplementation((TImplementable)superTyp, (TCanImplement)fmx, lastAssociation, intbnd);
9✔
540
                        }
541
                }
542
        }
1✔
543

544
        /**
545
         * Returns a Famix Reference between two Famix Entities creating it if needed.<br>
546
         * 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
547
         * @param src -- source of the reference
548
         * @param tgt -- target of the reference
549
         * @param prev -- previous reference relationship in the same context
550
         * @return the FamixReference
551
         */
552
        public Reference addFamixReference(Method src, TType tgt, TAssociation prev, ITypeBinding referredTypeBnd) {
553
                Reference ref;
554
                
555
                if ( (src == null) || (tgt == null) ) {
4!
556
                        return null;
×
557
                }
558

559
                if (prev == null) {
2✔
560
                        for (TReference existingRef : src.getOutgoingReferences()) {
7!
561
                                if (existingRef.getReferredEntity() == tgt) {
×
562
                                        return (Reference) existingRef;
×
563
                                }
564
                        }
×
565
                }
566

567
                /* issue <href="https://github.com/moosetechnology/VerveineJ/issues/146">146</href> an expression like <code>char[].class</code> is a <code>Class</code>
568
                 * gives referredTypeBnd.isParameterizedType() == true.
569
                 * We test <code>tgt instanceof PrimitiveType</code> to avoid this case */
570
                if (referredTypeBnd != null && referredTypeBnd.isParameterizedType() && ! (tgt instanceof PrimitiveType)) { // Needs checks and tests.
8✔
571
                        ref = (ParametricReference)buildFamixParametricAssociation(new ParametricReference(), referredTypeBnd.getErasure().getTypeParameters(), referredTypeBnd.getTypeArguments());
13✔
572
                } else {
573
                        ref = new Reference();
4✔
574
                }
575

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.addCandidates((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
                meth.getDeclaredExceptions().add(excep);
5✔
655
                return excep;
2✔
656
        }
657

658
        /**
659
         * Returns a Famix CaughtException between a method and an Exception that is caught
660
         * @param meth -- the method catching the exception
661
         * @param excep -- the exception caught
662
         * @return the CaughtException
663
         */
664
        public TThrowable createFamixCaughtException(Method meth, TThrowable excep) {
665
                if ( (meth == null) || (excep == null) ) {
4!
666
                        return null;
×
667
                }
668
                meth.getCaughtExceptions().add(excep);
5✔
669
                return excep;
2✔
670
        }
671

672
        /**
673
         * Returns a Famix ThrownException between a method and an Exception that it (actually) throws.
674
         * Note: DeclaredException indicates that the method declares it can throw the exception,
675
         * here we state that the exception is actually thrown
676
         * @param meth -- the method throwing the exception
677
         * @param excep -- the exception thrown
678
         * @return the ThrownException
679
         */
680
        public TThrowable createFamixThrownException(Method meth, TThrowable excep) {
681
                if ( (meth == null) || (excep == null) ) {
4!
682
                        return null;
×
683
                }
684
                meth.getThrownExceptions().add(excep);
5✔
685
                return excep;
2✔
686
        }
687

688

689
        /**
690
         * Returns a Famix EntityTyping between a typed entity and a type.
691
         * @param typedEntity -- the typed entity
692
         * @param declaredType -- the declared type
693
         * @return the FamixEntityTyping
694
         */
695
        public EntityTyping ensureFamixEntityTyping(ITypeBinding declaredTypeBnd, TTypedEntity typedEntity, TType declaredType) {
696
                if ( (typedEntity == null) || (declaredType == null) ) {
4!
697
                        return null;
2✔
698
                }
699
                EntityTyping typing;
700
                if (declaredTypeBnd != null && declaredTypeBnd.isParameterizedType()) {
5✔
701
                        typing = (ParametricEntityTyping)buildFamixParametricAssociation(new ParametricEntityTyping(), declaredTypeBnd.getErasure().getTypeParameters(), declaredTypeBnd.getTypeArguments());
13✔
702
                } else {
703
                        typing = new EntityTyping();
4✔
704
                }
705
                typing.setTypedEntity(typedEntity);
3✔
706
                typing.setDeclaredType(declaredType);
3✔
707
                famixRepoAdd(typing);
3✔
708
                
709
                return typing;
2✔
710
        }
711

712

713
        ///// Special Case: ImplicitVariables /////
714

715
        /**
716
         * Returns the Famix ImplicitVariable associated to the given binding and name (self or super).
717
         * See also {@link #getEntityByKey(IBinding)}
718
         * @param bnd -- the binding
719
         * @return the Famix Entity associated to the binding or null if not found
720
         */
721
        @Deprecated
722
        public ImplicitVariable getImplicitVariableByBinding(IBinding bnd, String iv_name) {
723
                return getImplicitVariableByType((Class)getEntityByKey(bnd), iv_name);
×
724
        }
725
        
726
        /**
727
         * Returns the Famix ImplicitVariable associated to the given FamixType.
728
         * @param type -- the FamixType
729
         * @param name -- name of the ImplicitVariable (should be Dictionary.SELF_NAME or Dictionary.SUPER_NAME)
730
         * @return the Famix ImplicitVariable associated to the Type or null if not found
731
         */
732
        @Deprecated
733
        public ImplicitVariable getImplicitVariableByType(Type type, String name) {
734
                ImplicitVars iv = typeToImpVar.get(type);
×
735
                ImplicitVariable ret = null;
×
736
                
737
                if (iv == null) {
×
738
                        iv = new ImplicitVars();
×
739
                }
740
                
741
                if (name.equals(THIS_NAME)) {
×
742
                        ret = iv.self_iv;
×
743
                }
744
                else if (name.equals(SUPER_NAME)) {
×
745
                        ret = iv.super_iv;
×
746
                }
747

748
                return ret;
×
749
        }
750

751
        ///// Special Case: "Uniq" Entities /////
752

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

790
                return fmx;
2✔
791
        }
792

793
        /**
794
         * Creates or recovers the Famix Class that will own all stub methods (for which the real owner is unknown)
795
         *
796
         * @return a Famix class
797
         */
798
        public Class ensureFamixClassStubOwner() {
799
                Class fmx =  ensureFamixUniqEntity(Class.class, null, STUB_METHOD_CONTAINER_NAME);
7✔
800
                if (fmx != null) {
2!
801
                        fmx.setTypeContainer( ensureFamixPackageDefault());
4✔
802
                }
803
                ensureFamixInheritance(ensureFamixClassObject(), fmx, /*prev*/null, null);
8✔
804

805
                return fmx;
2✔
806
        }
807

808
        public Type searchTypeInContext(String name, TWithTypes ctxt) {
809
                if (ctxt == null) {
×
810
                        return null;
×
811
                }
812
                
813
                for (TType candidate : ctxt.getTypes()) {
×
814
                        if (candidate.getName().equals(name) ) {
×
815
                                return (Type) candidate;
×
816
                        }
817
                }
×
818
                
819
                return searchTypeInContext(name, Util.getOwner((TNamedEntity)ctxt));
×
820
        }
821

822
        /**
823
         * Returns a Famix Package associated with its IPackageBinding and/or fully qualified name.
824
         * The Entity is created if it does not exist.
825
         * We assume that Namespaces must be uniq for a given name
826
         * Also creates or recovers recusively it's parent namespaces.<br>
827
         * At least one of <b>bnd</b> and <b>name</b> must be non null.
828
         *
829
         * @param bnd  -- the JDT Binding that may be used as a uniq key to recover this namespace
830
         * @param name -- fully qualified name of the namespace (e.g. 'java.lang')
831
         * @return the Famix Namespace found or created. May return null in case of a Famix error
832
         */
833
        public Package ensureFamixPackage(IPackageBinding bnd, String name) {
834
                Package fmx;
835
                Package parent;
836

837
                if ((name == null) && (bnd != null)) {
4!
838
                        name = bnd.getName();
3✔
839
                }
840

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

859
                return fmx;
2✔
860
        }
861

862
        /**
863
         * Creates or recovers a default Famix Package.
864
         * Because this package does not really exist, it has no binding.
865
         *
866
         * @return a Famix Namespace
867
         */
868
        public Package ensureFamixPackageDefault() {
869
        return ensureFamixUniqEntity(Package.class, null, DEFAULT_PCKG_NAME);
7✔
870
        }
871

872
        /**
873
         * Creates or recovers a Famix Package for the package of Java class "Object" (i.e. "java.lang").
874
         * Because "Object" is the root of the inheritance tree, it needs to be treated differently.
875
         *
876
         * @param bnd -- a potential binding for the "java.lang" package
877
         * @return a Famix Namespace for "java.lang"
878
         */
879
        public Package ensureFamixPackageJavaLang(IPackageBinding bnd) {
880

881
        return this.ensureFamixPackage(bnd, OBJECT_PACKAGE_NAME);
5✔
882
        }
883

884
        /**
885
         * Returns the Package with {@link #DEFAULT_PCKG_NAME} or <code>null</code> if not found
886
         */
887
        public Package getFamixPackageDefault() {
888
                Collection<Package> l = getEntityByName(Package.class, DEFAULT_PCKG_NAME);
5✔
889
                if (l.size() > 0) {
3!
890
                        return l.iterator().next();
5✔
891
                } else {
892
                        return null;
×
893
                }
894
        }
895

896
        /**
897
         * Returns a Famix Type with the given <b>name</b>, creating it if it does not exist yet.
898
         * In the second case, sets some default properties: not Abstract, not Final, not Private, not Protected, not Public, not Interface
899
         * @param bnd -- binding for the type to create
900
         * @param name of the type
901
         * @param owner of the type
902
         * @param ctxt -- context of use of the type
903
         */
904
        public Type ensureFamixType(ITypeBinding bnd, String name, TWithTypes owner, TWithTypes ctxt, int modifiers) {
905
                
906
                Type fmx;
907

908
                if (bnd == null) {
2✔
909
                        if (name == null) {
2!
910
                                return null;
2✔
911
                        }
912
                        fmx = searchTypeInContext(name, ctxt); // WildCard Types don't have binding
×
913
                        if (fmx != null) {
×
914
                                return fmx;
×
915
                        }
916

917
                        if ((owner instanceof TParametricEntity)) {
×
918
                                return this.ensureFamixTypeParameter(null, name, owner);
×
919
                        }
920
                        else {
921
                                fmx = ensureFamixEntity(Type.class, bnd, name);
×
922
                                fmx.setTypeContainer(owner);
×
923
                                return fmx;
×
924
                        }
925
                }
926

927
                // bnd != null
928

929
                fmx = (Type) getEntityByKey(bnd);
5✔
930
                if (fmx != null) {
2✔
931
                        return fmx;
2✔
932
                }
933

934
                if (bnd.isArray()) {
3!
935
                        bnd = bnd.getElementType();
×
936
                }
937

938
                if (bnd.isPrimitive()) {
3✔
939
                        return this.ensureFamixPrimitiveType(bnd, name);
5✔
940
                }
941

942
                if (bnd.isEnum()) {
3!
943
                        return this.ensureFamixEnum(bnd, name, owner);
×
944
                }
945

946
                if ((bnd.isRawType() || bnd.isGenericType()) && !bnd.isInterface() ) {
9✔
947
                        return this.ensureFamixClass(bnd.getErasure(), name, (TNamedEntity) owner, /*isGeneric*/true, modifiers);
10✔
948
                }
949
                
950
                if (bnd.isAnnotation()) {
3!
951
                        return this.ensureFamixAnnotationType(bnd, name, (ContainerEntity) owner);
×
952
                }
953

954
                if (bnd.isInterface()) {
3✔
955
                        return this.ensureFamixInterface(bnd, name, owner, /*isGeneric*/bnd.isGenericType() || bnd.isParameterizedType() || bnd.isRawType(), modifiers);
19✔
956
                }
957

958
                if (isThrowable(bnd)) {
4✔
959
                        return this.ensureFamixException(bnd, name, owner, /*isGeneric*/false, modifiers);
8✔
960
                }
961
                if (bnd.isClass()) {
3✔
962
                        return this.ensureFamixClass(bnd, name, (TNamedEntity) owner, /*isGeneric*/bnd.isGenericType() || bnd.isParameterizedType() || bnd.isRawType(), modifiers);
20!
963
                }
964
                if(bnd.isWildcardType()) {
3✔
965
                        return this.ensureFamixWildcardType(bnd, name, (TParametricEntity)owner, ctxt);
8✔
966
                }
967

968
                //otherwise (none of the above)
969

970
                if (name == null) {
2✔
971
                        name = bnd.getName();
3✔
972
                }
973

974
                if (owner == null) {
2!
975
                        owner = (TWithTypes) this.ensureOwner(bnd);
5✔
976
                }
977

978
                if (bnd.isTypeVariable() ) {
3!
979
                        fmx = ensureFamixTypeParameter(bnd, name, owner);
6✔
980
                        return fmx;
2✔
981
                }
982

983
                fmx = ensureFamixEntity(Type.class, bnd, name);
×
984
                fmx.setTypeContainer(owner);
×
985
                return fmx;
×
986
        }
987

988
        public Type ensureFamixType(ITypeBinding bnd, TWithTypes context) {
989
        int modifiers = (bnd != null) ? bnd.getModifiers() : UNKNOWN_MODIFIERS;
7✔
990
                return ensureFamixType(bnd, /*name*/null, /*owner*/null, context, modifiers);
8✔
991
        }
992
        
993
        public Type ensureFamixType(ITypeBinding bnd) {
994
                return ensureFamixType(bnd, /*ctxt*/null);
5✔
995
        }
996

997
        public boolean isThrowable(ITypeBinding bnd) {
998
                if (bnd == null) {
2!
999
                        return false;
×
1000
                }
1001
                if (bnd.getQualifiedName().equals("java.lang.Throwable")) {
5✔
1002
                        return true;
2✔
1003
                } else if (bnd.getQualifiedName().equals("java.lang.Object")) {
5✔
1004
                        return false;
2✔
1005
                }
1006
                else {
1007
                        return isThrowable(bnd.getSuperclass());
5✔
1008
                }
1009
        }
1010

1011
        /**
1012
         * Returns a Famix Class associated with the ITypeBinding.
1013
         * The Entity is created if it does not exist.
1014
         * @param name -- the name of the Famix Class (MUST NOT be null, but this is not checked)
1015
         * @param owner -- package defining the class (should not be null, but it will work if it is)
1016
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
1017
         */
1018
        @SuppressWarnings("deprecation")
1019
        public Class ensureFamixClass(ITypeBinding bnd, String name, TNamedEntity owner, boolean isGeneric, int modifiers) {
1020
                Class fmx;
1021

1022
                // --------------- some special cases
1023
                if (bnd != null) {
2✔
1024
                        if (bnd.isArray()) {
3!
1025
                                bnd = bnd.getElementType();
×
1026
                        }
1027

1028
                        // for inner classes defined in generics !!! For others should not change anything
1029
                        bnd = bnd.getErasure();
3✔
1030
                }
1031

1032
                // ---------------- to avoid useless computations if we can
1033
                fmx = (Class) getEntityByKey(bnd);
5✔
1034
                if (fmx != null) {
2✔
1035
                        return fmx;
2✔
1036
                }
1037

1038
                // --------------- name
1039
                if (name == null) {
2✔
1040
                        if (bnd == null) {
2!
1041
                                return null;  // not much we can do
×
1042
                        } else if (!bnd.isAnonymous()) {
3!
1043
                                name = bnd.getErasure().getName();  // for generics, will give the "core" type name, for normal type, won't change anything
5✔
1044
                        } else { // anonymous class
1045
                                if (bnd.getSuperclass() != null) {
×
1046
                                        name = bnd.getSuperclass().getName();
×
1047
                                }
1048
                                if ((name == null) || name.equals(OBJECT_NAME)) {
×
1049
                                        ITypeBinding[] intfcs = bnd.getInterfaces();
×
1050
                                        if ((intfcs != null) && (intfcs.length > 0)) {
×
1051
                                                name = bnd.getInterfaces()[0].getName();
×
1052
                                        }
1053
                                        else {
1054
                                                name = "???";
×
1055
                                        }
1056
                                }
1057
                                name = ANONYMOUS_NAME_PREFIX + "(" + name + ")";
×
1058
                        }
1059
                }
1060

1061
        // If we have java.lang.Object we should ensure we create this class
1062
        if (bnd != null && bnd.getQualifiedName().equals("java.lang.Object")) {
7✔
1063
                        return ensureFamixClassObject();
3✔
1064
                }
1065

1066
                // --------------- owner
1067
                if (owner == null) {
2✔
1068
                        if (bnd != null) {
2✔
1069
                                owner = ensureOwner(bnd);
4✔
1070
                        }
1071
                        /*                                owner = ensureFamixPackageDefault();
1072
                        } else {*/
1073
                }
1074

1075
                // --------------- recover from name ?
1076
                if (owner != null) {
2✔
1077
                        for (Class candidate : this.getEntityByName(Class.class, name)) {
13✔
1078
                                if (matchAndMapClass(bnd, name, owner, candidate)) {
7✔
1079
                                        fmx = candidate;
2✔
1080
                                        break;
1✔
1081
                                }
1082
                        }
1✔
1083
                }
1084

1085
                // ---------------- create
1086
                if (fmx == null) {
2✔
1087
                        if (isGeneric) {
2✔
1088
                                fmx = ensureFamixParametricClass(bnd, name, (TWithTypes) owner);
8✔
1089
                        }
1090
                        else {
1091
                                fmx = ensureFamixEntity(Class.class, bnd, name);
7✔
1092
                                fmx.setTypeContainer((TWithTypes)owner);
4✔
1093
                        }
1094
                }
1095

1096
                if (fmx!=null) {
2!
1097
                        // we just created it, or it was not bound so we make sure it has the right information in it
1098
                        if (bnd != null) {
2✔
1099
                                setClassModifiers(fmx, bnd.getDeclaredModifiers());
5✔
1100
                        }
1101

1102
                        TAssociation lastAssoc = null;
2✔
1103

1104
                        if (bnd != null) {
2✔
1105
                                ITypeBinding supbnd = bnd.getSuperclass();
3✔
1106
                                if (supbnd != null) {
2✔
1107
                                        lastAssoc = ensureFamixInheritance((TWithInheritances) ensureFamixType(supbnd), fmx, lastAssoc, supbnd);
11✔
1108
                                }
1109
                                else {
1110
                                        lastAssoc = ensureFamixInheritance(ensureFamixClassObject(), fmx, lastAssoc, null);
8✔
1111
                                }
1112
                                ensureImplementedInterfaces(bnd, fmx, (TWithTypes) owner, lastAssoc);
7✔
1113
                        }
1114
                }
1115

1116
                return fmx;
2✔
1117
        }
1118

1119
        /**
1120
         * Returns a Famix Exception associated with the ITypeBinding.
1121
         * The Entity is created if it does not exist.
1122
         * @param name -- the name of the Famix Exception (MUST NOT be null, but this is not checked)
1123
         * @param owner -- type defining the Exception (should not be null, but it will work if it is) 
1124
         *
1125
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
1126
         */
1127
        public <T extends TWithTypes & TNamedEntity> Exception ensureFamixException(ITypeBinding bnd, String name, TWithTypes owner, boolean isGeneric, int modifiers) {
1128
                Exception fmx;
1129

1130
                // --------------- some special cases
1131
                if (bnd != null) {
2✔
1132
                        if (bnd.isArray()) {
3!
1133
                                bnd = bnd.getElementType();
×
1134
                        }
1135

1136
                        // for inner classes defined in generics !!! For others should not change anything
1137
                        bnd = bnd.getErasure();
3✔
1138
                }
1139

1140
                // ---------------- to avoid useless computations if we can
1141
                fmx = (Exception) getEntityByKey(bnd);
5✔
1142
                if (fmx != null) {
2✔
1143
                        return fmx;
2✔
1144
                }
1145

1146
                // --------------- name
1147
                if (name == null) {
2✔
1148
                        if (bnd == null) {
2!
1149
                                return null;  // not much we can do
×
1150
                        } else if (!bnd.isAnonymous()) {
3!
1151
                                name = bnd.getErasure().getName();  // for generics, will give the "core" type name, for normal type, won't change anything
5✔
1152
                        } else { // anonymous class
1153
                                if (bnd.getSuperclass() != null) {
×
1154
                                        name = bnd.getSuperclass().getName();
×
1155
                                }
1156
                                if ((name == null) || name.equals(OBJECT_NAME)) {
×
1157
                                        ITypeBinding[] intfcs = bnd.getInterfaces();
×
1158
                                        if ((intfcs != null) && (intfcs.length > 0)) {
×
1159
                                                name = bnd.getInterfaces()[0].getName();
×
1160
                                        }
1161
                                        else {
1162
                                                name = "???";
×
1163
                                        }
1164
                                }
1165
                                name = ANONYMOUS_NAME_PREFIX + "(" + name + ")";
×
1166
                        }
1167
                }
1168

1169
                // --------------- owner
1170
                if (owner == null) {
2✔
1171
                        if (bnd == null) {
2✔
1172
                                owner = ensureFamixPackageDefault();
4✔
1173
                        } else {
1174
                                owner = (TWithTypes) ensureOwner(bnd);
5✔
1175
                        }
1176
                }
1177

1178
                // --------------- recover from name ?
1179
                for (Exception candidate : this.getEntityByName(Exception.class, name)) {
13✔
1180
                        if (matchAndMapClass(bnd, name, (T) owner, candidate)) {
8!
1181
                                fmx = candidate;
×
1182
                                break;
×
1183
                        }
1184
                }
1✔
1185

1186
                // ---------------- create
1187
                if (fmx == null) {
2!
1188
                        fmx = ensureFamixEntity(Exception.class, bnd, name);
7✔
1189
                        fmx.setTypeContainer(owner);
3✔
1190
                }
1191

1192
        // we just created it or it was not bound, so we make sure it has the right information in it
1193
        TAssociation lastAssoc = null;
2✔
1194
        if (bnd != null) {
2✔
1195
            ITypeBinding supbnd = bnd.getSuperclass();
3✔
1196
            if (supbnd != null) {
2!
1197
                lastAssoc = ensureFamixInheritance((TWithInheritances) ensureFamixType(supbnd), fmx, lastAssoc, supbnd);
11✔
1198
            }
1199
            else {
1200
                lastAssoc = ensureFamixInheritance(ensureFamixClassObject(), fmx, lastAssoc, null);
×
1201
            }
1202
            ensureImplementedInterfaces(bnd, fmx, owner, lastAssoc);
6✔
1203
        }
1204

1205
        return fmx;
2✔
1206
        }
1207

1208
        /**
1209
         * Returns a FAMIX Interface with the given <b>name</b>, creating it if it does not exist yet.
1210
         * @param name -- the name of the FAMIX Method (MUST NOT be null, but this is not checked)
1211
         * @param owner -- type defining the method (should not be null, but it will work if it is) 
1212
         * @return the FAMIX Class or null in case of a FAMIX error
1213
         */
1214
        public <T extends TWithTypes & TNamedEntity> Interface ensureFamixInterface(ITypeBinding bnd, String name, TWithTypes owner, boolean isGeneric, int modifiers) {
1215
                Interface fmx;
1216

1217
                // --------------- some special cases
1218
                if (bnd != null) {
2!
1219
                        if (bnd.isArray()) {
3!
1220
                                bnd = bnd.getElementType();
×
1221
                        }
1222

1223
                        // for inner classes defined in generics !!! For others should not change anything
1224
                        bnd = bnd.getErasure();
3✔
1225
                }
1226

1227
                // ---------------- to avoid useless computations if we can
1228
                fmx = (Interface) getEntityByKey(bnd);
5✔
1229
                if (fmx != null) {
2✔
1230
                        return fmx;
2✔
1231
                }
1232

1233
                // --------------- name
1234
                if (name == null) {
2✔
1235
                        if (bnd == null) {
2!
1236
                                return null;  // not much we can do
×
1237
                        } else if (!bnd.isAnonymous()) {
3!
1238
                                name = bnd.getErasure().getName();  // for generics, will give the "core" type name, for normal type, won't change anything
5✔
1239
                        } else { // anonymous class
1240
                                if (bnd.getSuperclass() != null) {
×
1241
                                        name = bnd.getSuperclass().getName();
×
1242
                                }
1243
                                if ((name == null) || name.equals(OBJECT_NAME)) {
×
1244
                                        ITypeBinding[] intfcs = bnd.getInterfaces();
×
1245
                                        if ((intfcs != null) && (intfcs.length > 0)) {
×
1246
                                                name = bnd.getInterfaces()[0].getName();
×
1247
                                        }
1248
                                        else {
1249
                                                name = "???";
×
1250
                                        }
1251
                                }
1252
                                name = ANONYMOUS_NAME_PREFIX + "(" + name + ")";
×
1253
                        }
1254
                }
1255

1256
                // --------------- owner
1257
                if (owner == null) {
2✔
1258
                        if (bnd == null) {
2!
1259
                                owner = ensureFamixPackageDefault();
×
1260
                        } else {
1261
                                owner = (TWithTypes) ensureOwner(bnd);
5✔
1262
                        }
1263
                }
1264

1265
                // --------------- recover from name ?
1266
                for (Interface candidate : this.getEntityByName(Interface.class, name)) {
13✔
1267
                        if (matchAndMapInterface(bnd, name, (T) owner, candidate)) {
8✔
1268
                                fmx = candidate;
2✔
1269
                                break;
1✔
1270
                        }
1271
                }
1✔
1272

1273
                // ---------------- create
1274
                if (fmx == null) {
2✔
1275
                        if (isGeneric) {
2✔
1276
                                fmx = ensureFamixParametricInterface(bnd, name, owner);
7✔
1277
                        }
1278
                        else {
1279
                                fmx = ensureFamixEntity(Interface.class, bnd, name);
7✔
1280
                                fmx.setTypeContainer(owner);
3✔
1281
                        }
1282
                }
1283

1284
                if (fmx!=null) {
2!
1285
                        // we just created it or it was not bound, so we make sure it has the right information in it
1286
                        if (bnd != null) {
2!
1287
                                setInterfaceModifiers(fmx, bnd.getModifiers());
5✔
1288
                        }
1289
                        TAssociation lastAssociation = null;
2✔
1290
                        if (bnd != null) {
2!
1291
                                ensureImplementedInterfaces(bnd, fmx, owner, lastAssociation);
6✔
1292
                        }
1293
                }
1294
                return fmx;
2✔
1295
        }
1296

1297
        public TThrowable asException(TType fmxType) {
1298
                if (fmxType instanceof Exception) {
3✔
1299
                        return (Exception) fmxType;
3✔
1300
                }
1301
                if(fmxType instanceof TypeParameter) {
3✔
1302
                        return (TypeParameter) fmxType;
3✔
1303
                }
1304

1305
                Exception fmxException = null;
2✔
1306
                IBinding key;
1307

1308
                try {
1309
                        key = entityToKey.get(fmxType);
6✔
1310

1311
                        /* Remove entity immediatly so that its key and name are not "reassigned" in the various cache dictionnaries
1312
                         * the object still exists and its properties are still accessible */
1313
                        removeEntity((NamedEntity) fmxType);
4✔
1314

1315
                        TWithTypes owner = fmxType.getTypeContainer();
3✔
1316
                        fmxType.setTypeContainer(null);
3✔
1317
                        fmxException = ensureFamixException((ITypeBinding) key, fmxType.getName(), owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
10✔
1318

1319
                        fmxException.addMethods( new ArrayList<>( ((TWithMethods)fmxType).getMethods() ) );
8✔
1320
                        if (fmxType instanceof TWithAttributes) {
3!
1321
                                fmxException.addAttributes( new ArrayList<>( ((TWithAttributes)fmxType).getAttributes() ) );
8✔
1322
                        }
1323

1324
                        if (fmxType instanceof TWithInheritances) {
3!
1325
                                fmxException.addSuperInheritances( new ArrayList<>( ((TWithInheritances) fmxType).getSuperInheritances() ) );
8✔
1326
                                fmxException.addSubInheritances( new ArrayList<>( ((TWithInheritances) fmxType).getSubInheritances() ) );
8✔
1327
                        }
1328
                        fmxException.setSourceAnchor(fmxType.getSourceAnchor());
4✔
1329
                        fmxException.addIncomingTypings( new ArrayList<>( fmxType.getIncomingTypings() ) );
7✔
1330
                        fmxException.addAnnotationInstances( new ArrayList<>( ((NamedEntity)fmxType).getAnnotationInstances() ) );
8✔
1331
                        fmxException.addIncomingReferences( new ArrayList<>( fmxType.getIncomingReferences() ) );
7✔
1332
                        fmxException.setIsStub(fmxType.getIsStub());
4✔
1333
                        fmxException.addTypes( new ArrayList<>( ((ContainerEntity) fmxType).getTypes() ) );
8✔
1334
                }
1335
                catch( ConcurrentModificationException e) {
×
1336
                        e.printStackTrace();
×
1337
                }
1✔
1338

1339
                return fmxException;
2✔
1340
        }
1341

1342
        /**
1343
         * helper method, we know the type exists, ensureFamixClass will recover it
1344
         */
1345
        public Class getFamixClass(ITypeBinding bnd, String name, TNamedEntity owner) {
1346
                return ensureFamixClass(bnd, name, owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
8✔
1347
        }
1348

1349
        /**
1350
         * helper method, we know the type exists, ensureFamixInterface will recover it
1351
         */
1352
        public Interface getFamixInterface(ITypeBinding bnd, String name, ContainerEntity owner) {
1353
                return ensureFamixInterface(bnd, name, owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
8✔
1354
        }
1355

1356
        /**
1357
         * helper method, we know the type exists, ensureFamixInterface will recover it
1358
         */
1359
        public Exception getFamixException(ITypeBinding bnd, String name, TWithTypes owner) {
1360
                return ensureFamixException(bnd, name, owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
8✔
1361
        }
1362

1363
        /**
1364
         * Ensures a famix entity for the owner of a binding.<br>
1365
         * This owner can be a method, a class or a namespace
1366
         * @param bnd -- binding for the owned entity
1367
         * @return a famix entity for the owner
1368
         */
1369
        private TNamedEntity ensureOwner(ITypeBinding bnd) {
1370
                TNamedEntity owner;
1371
                IMethodBinding parentMtd = bnd.getDeclaringMethod();
3✔
1372
                if (parentMtd != null) {
2✔
1373
                        owner = this.ensureFamixMethod(parentMtd);  // cast needed to desambiguate the call
5✔
1374
                }
1375
                else {
1376
                        ITypeBinding parentClass = bnd.getDeclaringClass();
3✔
1377
                        if (parentClass != null) {
2✔
1378
                owner = this.ensureFamixType(parentClass);
5✔
1379
            }
1380
                        else {
1381
                                IPackageBinding parentPckg = bnd.getPackage();
3✔
1382
                                if (parentPckg != null) {
2!
1383
                                        owner = this.ensureFamixPackage(parentPckg, null);
6✔
1384
                                } else {
1385
                                        owner = this.ensureFamixPackageDefault();
×
1386
                                }
1387
                        }
1388
                }
1389
                return owner;
2✔
1390
        }
1391

1392

1393
        /**
1394
         * Returns a FAMIX PrimitiveType with the given <b>name</b>, creating it if it does not exist yet
1395
         * We assume that PrimitiveType must be uniq for a given name
1396
         * @param name -- the name of the FAMIX PrimitiveType
1397
         * @return the FAMIX PrimitiveType or null in case of a FAMIX error
1398
         */
1399
        public PrimitiveType ensureFamixPrimitiveType(ITypeBinding bnd, String name) {
1400
                if (name == null) {
2✔
1401
                        if (bnd == null) {
2!
1402
                                return null;
×
1403
                        } else {
1404
                                name = bnd.getName();
3✔
1405
                        }
1406
                }
1407
                return ensureFamixUniqEntity(PrimitiveType.class, bnd, name);
7✔
1408
        }
1409

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

1413
                // --------------- to avoid useless computations if we can
1414
                fmx = (org.moosetechnology.model.famix.famixjavaentities.Enum) getEntityByKey(bnd);
5✔
1415
                if (fmx != null) {
2✔
1416
                        return fmx;
2✔
1417
                }
1418

1419
                // --------------- name
1420
                if (name == null) {
2✔
1421
                        if (bnd == null) {
2!
1422
                                return null;
×
1423
                        }
1424
                        else {
1425
                                name = bnd.getName();
3✔
1426
                        }
1427
                }
1428

1429
                // --------------- owner
1430
                if (owner == null) {
2✔
1431
                        if (bnd == null) {
2!
1432
                                owner = ensureFamixPackageDefault();  // not really sure what to do here
×
1433
                        } else {
1434
                                owner = (TWithTypes) ensureOwner(bnd);
5✔
1435
                        }
1436
                }
1437

1438
                // --------------- recover from name ?
1439
                for (org.moosetechnology.model.famix.famixjavaentities.Enum candidate : getEntityByName(org.moosetechnology.model.famix.famixjavaentities.Enum.class, name)) {
9!
1440
                        if (matchAndMapType(bnd, name, (T) owner, candidate)) {
×
1441
                                fmx = candidate;
×
1442
                                break;
×
1443
                        }
1444
                }
×
1445

1446
                if (fmx == null) {
2!
1447
                        fmx = ensureFamixEntity(Enum.class, bnd, name);
7✔
1448
                        fmx.setTypeContainer(owner);
3✔
1449
                }
1450

1451
                if (bnd != null) {
2!
1452
                        setVisibility(fmx, bnd.getModifiers());
5✔
1453
                }
1454

1455
                return fmx;
2✔
1456
        }
1457

1458
        /**
1459
         * helper method, we know the type exists, ensureFamixEnum will recover it
1460
         */
1461
        public org.moosetechnology.model.famix.famixjavaentities.Enum getFamixEnum(ITypeBinding bnd, String name, TWithTypes owner) {
1462
                return ensureFamixEnum(bnd, name, owner);
6✔
1463
        }
1464

1465
        public EnumValue ensureFamixEnumValue(IVariableBinding bnd,        String name, Enum owner) {
1466
                EnumValue fmx;
1467

1468
                // --------------- to avoid useless computations if we can
1469
                fmx = (EnumValue)getEntityByKey(bnd);
5✔
1470
                if (fmx != null) {
2✔
1471
                        return fmx;
2✔
1472
                }
1473

1474
                // --------------- name
1475
                if (name == null) {
2!
1476
                        if (bnd == null) {
×
1477
                                return null;
×
1478
                        }
1479
                        else {
1480
                                name = bnd.getName();
×
1481
                        }
1482
                }
1483

1484
                // --------------- owner
1485
                if (owner == null) {
2✔
1486
                        if (bnd == null) {
2!
1487
                                return null;  // what would be the interest of creating an EnumValue without a declaring Enum type?
×
1488
                        }
1489
                        else {
1490
                                owner = ensureFamixEnum(bnd.getDeclaringClass(), null, null);
7✔
1491
                        }
1492
                }
1493

1494
                // --------------- recover from name ?
1495
                for (EnumValue candidate : getEntityByName(EnumValue.class, name) ) {
9!
1496
                        if ( matchAndMapVariable(bnd, name, owner, candidate) ) {
×
1497
                                fmx = candidate;
×
1498
                                break;
×
1499
                        }
1500
                }
×
1501
                if (fmx == null) {
2!
1502
                        fmx = ensureFamixEntity(EnumValue.class, bnd, name);
7✔
1503
                        fmx.setParentEnum(owner);
3✔
1504
                }
1505

1506
        fmx.setParentEnum(owner);
3✔
1507

1508
        return fmx;
2✔
1509
        }
1510

1511
    /**
1512
         * e.g. see {@link EntityDictionary#ensureFamixClass}
1513
         */
1514
        public AnnotationType ensureFamixAnnotationType(ITypeBinding bnd, String name, ContainerEntity owner) {
1515
                AnnotationType fmx;
1516

1517
                // --------------- to avoid useless computations if we can
1518
                fmx = (AnnotationType)getEntityByKey(bnd);
5✔
1519
                if (fmx != null) {
2✔
1520
                        return fmx;
2✔
1521
                }
1522

1523
                // --------------- name
1524
                if (name == null) {
2✔
1525
                        if (bnd == null) {
2!
1526
                                return null;
×
1527
                        }
1528
                        else {
1529
                                name = bnd.getName();
3✔
1530
                        }
1531
                }
1532

1533
                // --------------- owner
1534
                if (owner == null) {
2✔
1535
                        if (bnd == null) {
2!
1536
                                owner = ensureFamixPackageDefault();
×
1537
                        }
1538
                        else {
1539
                                IPackageBinding parentPckg = bnd.getPackage();
3✔
1540
                                if (parentPckg != null) {
2!
1541
                                        owner = this.ensureFamixPackage(parentPckg, null);
6✔
1542
                                } else {
1543
                                        owner = this.ensureFamixPackageDefault();
×
1544
                                }
1545
                        }
1546
                }
1547

1548
                // --------------- recover from name ?
1549
                for (AnnotationType candidate : getEntityByName(AnnotationType.class, name) ) {
13✔
1550
                        if ( matchAndMapType(bnd, name, owner, candidate) ) {
7✔
1551
                                fmx = candidate;
2✔
1552
                                break;
1✔
1553
                        }
1554
                }
1✔
1555

1556
                // --------------- create
1557
                if (fmx == null) {
2✔
1558
                        fmx = ensureFamixEntity(AnnotationType.class, bnd, name);
7✔
1559
                        fmx.setAnnotationTypesContainer(owner);
3✔
1560
                }
1561

1562
                if (bnd != null) {
2!
1563
                        // Not supported in Famix
1564

1565
                        // setVisibility(fmx, bnd.getModifiers());
1566
                }
1567

1568
                return fmx;
2✔
1569
        }
1570

1571
        /**
1572
         * helper method, we know the type exists, ensureFamixAnnotationType will recover it
1573
         */
1574
        public AnnotationType getFamixAnnotationType(ITypeBinding bnd, String name, ContainerEntity owner) {
1575
                return ensureFamixAnnotationType(bnd, name, owner);
6✔
1576
        }
1577

1578
        public AnnotationTypeAttribute ensureFamixAnnotationTypeAttribute(IMethodBinding bnd, String name, AnnotationType owner) {
1579
                AnnotationTypeAttribute fmx = null;
2✔
1580

1581
                // --------------- to avoid useless computations if we can
1582
                fmx = (AnnotationTypeAttribute)getEntityByKey(bnd);
5✔
1583
                if (fmx != null) {
2✔
1584
                        return fmx;
2✔
1585
                }
1586

1587
                // --------------- name
1588
                if (name == null) {
2!
1589
                        if (bnd == null) {
×
1590
                                return null;
×
1591
                        }
1592
                        else {
1593
                                name = bnd.getName();
×
1594
                        }
1595
                }
1596

1597
                // --------------- owner
1598
                if (owner == null) {
2!
1599
                        if (bnd == null) {
×
1600
                                return null;  // what would be the use of an AnnotationTypeAttribute without AnnotationType ?
×
1601
                        }
1602
                        else {
1603
                                ITypeBinding parentType = bnd.getDeclaringClass();
×
1604
                                if (parentType != null) {
×
1605
                                        owner = this.ensureFamixAnnotationType(parentType, null, null);
×
1606
                                }
1607
                                else  {
1608
                                        return null;  // what would be the use of an AnnotationTypeAttribute without AnnotationType ?
×
1609
                                }
1610
                        }
1611
                }
1612

1613
                // --------------- recover from name ?
1614
                for (AnnotationTypeAttribute candidate : getEntityByName(AnnotationTypeAttribute.class, name) ) {
13✔
1615
                        // JDT treats annotation type attributes as methods ...
1616
                        // checkAndMapMethod wants a signature as 2nd argument so we add empty param list
1617
                        if ( (bnd != null) && matchAndMapMethod(bnd, name+"()", null, owner, candidate) ) {
11!
1618
                                fmx = candidate;
×
1619
                                break;
×
1620
                        }
1621
                        // if the binding is null, the annotationTypeAttribute migth have been created
1622
                        else if ( (bnd == null) && matchAndMapVariable(null, name, owner, candidate)) {
2!
1623
                                fmx = candidate;
×
1624
                                break;
×
1625
                        }
1626
                }
1✔
1627

1628
                if (fmx == null) {
2!
1629
                        fmx = ensureFamixEntity(AnnotationTypeAttribute.class, bnd, name);
7✔
1630
                        fmx.setParentType(owner);
3✔
1631
                }
1632

1633
                if (bnd != null) {
2!
1634
                        // Not suopp
1635

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

1639
                return fmx;
2✔
1640
        }
1641

1642
        /**
1643
         * helper method, we know the attribute exists, ensureFamixAnnotationTypeAttribute will recover it
1644
         */
1645
        public AnnotationTypeAttribute getFamixAnnotationTypeAttribute(IMethodBinding bnd, String name, AnnotationType owner) {
1646
                return ensureFamixAnnotationTypeAttribute( bnd, name, owner);
6✔
1647
        }
1648
        
1649
        
1650
        /**
1651
         * Returns a FAMIX Wildcard with its bounds
1652
         * @param bnd
1653
         * @param name
1654
         * @param owner
1655
         * @return
1656
         */
1657
        public Wildcard ensureFamixWildcardType(ITypeBinding bnd, String name, TParametricEntity owner, TWithTypes ctxt) {
1658
                Wildcard fmx = this.ensureFamixEntity(Wildcard.class, bnd, bnd.getName());
8✔
1659
                if(bnd.getBound() != null) {
3✔
1660
                        Type bound = this.ensureFamixType(bnd.getBound());
5✔
1661
                        if(bnd.isUpperbound()) {
3✔
1662
                                fmx.setUpperBound(bound);
3✔
1663
                                bound.addUpperBoundedWildcards(fmx);
4✔
1664
                        }else{
1665
                                fmx.setLowerBound(bound);
3✔
1666
                                bound.addLowerBoundedWildcards(fmx);
3✔
1667
                        }
1668
                }
1669
                return fmx;
2✔
1670
        }
1671

1672
        /**
1673
         * Returns a Famix TypeParameter (created by a Famix ParametricEntity) with the given <b>name</b>, creating it if it does not exist yet
1674
         * In the second case, sets some default properties: not Abstract, not Final, not Private, not Protected, not Public
1675
         * @param name -- the name of the Famix TypeParameter
1676
         * @return the Famix TypeParameter or null in case of a Famix error
1677
         */
1678
        public TypeParameter ensureFamixTypeParameter(ITypeBinding bnd,        String name, TWithTypes owner) {
1679
                TypeParameter fmx;
1680

1681
                // --------------- to avoid useless computations if we can
1682
                fmx = (TypeParameter)getEntityByKey(bnd);
5✔
1683
                if (fmx != null) {
2✔
1684
                        return fmx;
2✔
1685
                }
1686

1687
                // --------------- name
1688
                if (name == null) {
2✔
1689
                        if (bnd == null) {
2!
1690
                                return null;
×
1691
                        }
1692
                        else {
1693
                                name = bnd.getName();
3✔
1694
                        }
1695
                }
1696

1697
                // --------------- owner
1698
                if (owner == null && bnd != null) {
2!
1699
            if (bnd.getDeclaringClass() != null) {
×
1700
                owner = this.ensureFamixType(bnd.getDeclaringClass());
×
1701
            } else if(bnd.getDeclaringMethod() != null) {
×
1702
                owner = this.ensureFamixMethod(bnd.getDeclaringMethod());
×
1703
            }
1704
                }
1705

1706
                // --------------- recover from name ?
1707
                for (Type candidate : this.getEntityByName(Type.class, name)) {
13✔
1708
                        if ( matchAndMapType(bnd, name, (ContainerEntity) owner, candidate) ) {
8✔
1709
                                fmx = (TypeParameter) candidate;
3✔
1710
                                break;
1✔
1711
                        }
1712
                }
1✔
1713

1714
                // --------------- create
1715
                if (fmx == null) {
2✔
1716
                        fmx = ensureFamixEntity(TypeParameter.class, bnd, name);
7✔
1717
                        if(bnd != null && bnd.getSuperclass() != null) {
5!
1718
                                Type upperBound = ensureFamixType(bnd.getSuperclass());
5✔
1719
                                fmx.setUpperBound(upperBound);
3✔
1720
                        }
1721
                        if(bnd != null) {
2✔
1722
                for (ITypeBinding intbnd : bnd.getInterfaces()) {
17✔
1723
                    Type upperBound = ensureFamixType(intbnd);
4✔
1724
                    fmx.setUpperBound(upperBound);
3✔
1725
                }
1726
            }
1727
                        fmx.setTypeContainer(owner);
3✔
1728
                }
1729

1730
                return fmx;
2✔
1731
        }
1732

1733
        /**
1734
         * Checks whether the existing unmapped Famix Namespace matches the binding.
1735
         * Checks that the candidate has the same name as the JDT bound package, and checks recursively that owners also match.
1736
         *
1737
         * @param bnd       -- a JDT binding that we are trying to match to the candidate
1738
         * @param name      of the package
1739
         * @param owner     of the package
1740
         * @param candidate -- a Famix Entity
1741
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1742
         */
1743
        private boolean matchAndMapPackage(IPackageBinding bnd, String name, Package owner, NamedEntity candidate) {
1744
                if (!(candidate instanceof Package)) {
3!
1745
                        return false;
×
1746
                }
1747

1748
                // check whether bnd and candidate are already bound
1749
                CheckResult res = checkKeyMatch(bnd, candidate);
5✔
1750
                if (res == CheckResult.MATCH) {
3✔
1751
                        return true;
2✔
1752
                } else if (res == CheckResult.FAIL) {
3!
1753
                        return false;
×
1754
                }
1755

1756
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7✔
1757
                        return false;
2✔
1758
                }
1759

1760
                // names match, not need to look at owner because names of Namespaces are their fully qualified name
1761
                conditionalMapToKey(bnd, candidate);
4✔
1762
                return true;
2✔
1763
        }
1764

1765
        /**
1766
         * Checks whether the existing unmapped Famix Type matches the binding.
1767
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
1768
         * We also check that the actual class of the candidate matches (can be a sub-class of FamixType).
1769
         * @param bnd -- a JDT binding that we are trying to match to the candidate
1770
         * @param name of the type
1771
         * @param owner of the type
1772
         * @param candidate -- a Famix NamedEntity (Class, Type, PrimitiveType, Enum, AnnotationType)
1773
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1774
         */
1775
        private <T extends TWithTypes & TNamedEntity> boolean matchAndMapType(ITypeBinding bnd, String name, TNamedEntity owner, TNamedEntity candidate) {
1776
                if (! (candidate instanceof Type) ) {
3!
1777
                        return false;
×
1778
                }
1779

1780
                // check whether bnd and candidate are already bound
1781
                CheckResult res = checkKeyMatch(bnd, candidate);
5✔
1782
                if (res == CheckResult.MATCH) {
3✔
1783
                        return true;
2✔
1784
                }
1785
                else if (res == CheckResult.FAIL) {
3✔
1786
                        return false;
2✔
1787
                }
1788

1789
                if ( (bnd != null) && (bnd.isArray()) ) {
5!
1790
                                bnd = bnd.getElementType();
×
1791
                }
1792

1793
                // checking names
1794
                if ( (bnd != null) && (bnd.isParameterizedType() || bnd.isRawType()) ) {
8!
1795
                        name = bnd.getErasure().getName();
×
1796
                }
1797
                else if (bnd != null) {
2✔
1798
                        name = bnd.getName();
3✔
1799
                }
1800
                // else name = name
1801
                if (checkNameMatch(null, name, candidate) == CheckResult.FAIL) {
7✔
1802
                        return false;
2✔
1803
                }
1804

1805
                // special case of primitive types
1806
                if (candidate instanceof PrimitiveType) {
3!
1807
                        if ( (bnd != null) && bnd.isPrimitive() ) {
×
1808
                                // names are equal so it's OK
1809
                                conditionalMapToKey(bnd, candidate);
×
1810
                                return true;
×
1811
                        }
1812
                        else if ( (bnd == null) && (owner == null) ) {
×
1813
                                return true;
×
1814
                        }
1815
                }
1816

1817
                // check owners without bnd
1818
                if (bnd == null) {
2✔
1819
                        return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
7✔
1820
                }
1821

1822
                // check owners with bnd
1823
                // type is an annotation
1824
                if (bnd.isAnnotation() && (candidate instanceof AnnotationType)) {
6!
1825
                        if (matchAndMapPackage(bnd.getPackage(), owner.getName(), (Package) Util.getOwner(owner), Util.getOwner(candidate))) {
13!
1826
                                conditionalMapToKey(bnd, candidate);
4✔
1827
                                return true;
2✔
1828
                        } else {
1829
                                return false;
×
1830
                        }
1831
                }
1832

1833
                // check owners with bnd
1834
                // type is a Parameterized type
1835
                if ((bnd.isParameterizedType() || bnd.isRawType()) && (candidate instanceof ParametricClass)) {
6!
1836
                        return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
×
1837
                }
1838

1839
                // check owners with bnd
1840
                // type is an Enum
1841
                if (bnd.isEnum() && (candidate instanceof Enum)) {
3!
1842
                        return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
×
1843
                }
1844

1845
                // check owners with bnd
1846
                // type is something elae (a class or interface)
1847
                // Annotation are interfaces too, so we should check this one after isAnnotation
1848
                if ( bnd.isClass()) {
3!
1849
                        return matchAndMapClass(bnd, name, owner, (Type) candidate);
×
1850
                }
1851

1852
                if(bnd.isInterface()) {
3!
1853
                        return matchAndMapInterface(bnd, name, owner, (Type) candidate);
×
1854
                }
1855

1856
                return false;
2✔
1857
        }
1858

1859
        /**
1860
         * Checks whether the existing unmapped Famix Class (or Interface) matches the binding.
1861
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
1862
         * @param bnd -- a JDT binding that we are trying to match to the candidate
1863
         * @param name of the class
1864
         * @param owner of the class
1865
         * @param candidate -- a Famix Entity
1866
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1867
         */
1868
        private boolean matchAndMapClass(ITypeBinding bnd, String name, TNamedEntity owner, TType candidate) {
1869
                if (!(candidate instanceof Class)) {
3!
1870
                        return false;
×
1871
                }
1872

1873
                // check whether bnd and candidate are already bound
1874
                CheckResult res = checkKeyMatch(bnd, candidate);
5✔
1875
                if (res == CheckResult.MATCH) {
3!
1876
                        return true;
×
1877
                } else if (res == CheckResult.FAIL) {
3✔
1878
                        return false;
2✔
1879
                }
1880

1881
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7!
1882
                        return false;
×
1883
                }
1884

1885
                // checking owner
1886
                return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
7✔
1887
        }
1888

1889
        /**
1890
         * Checks whether the existing unmapped Famix Class (or Interface) matches the binding.
1891
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
1892
         * @param bnd -- a JDT binding that we are trying to match to the candidate
1893
         * @param name of the class
1894
         * @param owner of the class
1895
         * @param candidate -- a Famix Entity
1896
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1897
         */
1898
        private boolean matchAndMapInterface(ITypeBinding bnd, String name, TNamedEntity owner, Type candidate) {
1899
                if (!(candidate instanceof Interface)) {
3!
1900
                        return false;
×
1901
                }
1902

1903
                // check whether bnd and candidate are already bound
1904
                CheckResult res = checkKeyMatch(bnd, candidate);
5✔
1905
                if (res == CheckResult.MATCH) {
3!
1906
                        return true;
×
1907
                } else if (res == CheckResult.FAIL) {
3✔
1908
                        return false;
2✔
1909
                }
1910

1911
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7!
1912
                        return false;
×
1913
                }
1914

1915
                // checking owner
1916
                return matchAndMapTypeOwner(bnd, owner, candidate);
6✔
1917
        }
1918

1919
        /**
1920
         * Checks whether the existing unmapped Famix "Method" matches the binding.
1921
         * Checks that the candidate has the same name and same signature as the JDT bound method, and checks recursively that owners also match.
1922
         * Note that AnnotationTypeAttribute are treated as methods by JDT, so they are checked here.
1923
         * @param bnd -- a JDT binding that we are trying to match to the candidate
1924
         * @param sig -- signature of the method
1925
         * @param retTyp -- return type of the method
1926
         * @param owner of the method
1927
         * @param candidate -- a Famix Entity (regular Method or AnnotationTypeAttribute)
1928
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1929
         */
1930
        private  boolean matchAndMapMethod(IMethodBinding bnd, String sig, TType retTyp, TNamedEntity owner, NamedEntity candidate) {
1931
                if (! (candidate instanceof Method) ) {
3✔
1932
                        return false;
2✔
1933
                }
1934

1935
                // check whether bnd and candidate are already bound
1936
                CheckResult res = checkKeyMatch(bnd, candidate);
5✔
1937
                if (res == CheckResult.MATCH) {
3!
1938
                        return true;
×
1939
                }
1940
                else if (res == CheckResult.FAIL) {
3✔
1941
                        return false;
2✔
1942
                }
1943

1944
                // checking names
1945
                String name = (sig != null) ? sig.substring(0, sig.indexOf('(')) : null;
10!
1946
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7!
1947
                        return false;
×
1948
                }
1949

1950
                // for methods, the name is not enough, we must test the signature also
1951
                // but not for AnnotationTypeAttribute
1952

1953
                        if (bnd != null) {
2✔
1954
                                sig = bnd.getName() + "(" + signatureParamsFromBinding(bnd) + ")";
7✔
1955
                        }
1956
                        if (! ((Method) candidate).getSignature().equals(sig)) {
6✔
1957
                                return false;
2✔
1958
                        }
1959

1960
                        // and still for method, must also check the return type
1961
                        if (bnd != null) {
2✔
1962
                                if (bnd.isConstructor()) {
3✔
1963
                                        if ( ((Method) candidate).getDeclaredType() != null ) {
4!
1964
                                                return false;
×
1965
                                        }
1966
                                        // else OK for now
1967
                                }
1968
                                else { // not a constructor
1969
                                        if ( ((Method) candidate).getDeclaredType() == null ) {
4!
1970
                                                return false;
×
1971
                                        }
1972
                                        else if (! matchAndMapType(bnd.getReturnType(), null, null, ((Method) candidate).getDeclaredType()) ) {
10!
1973
                                                return false;
×
1974
                                        }
1975
                                        // else OK for now
1976
                                }
1977
                        }
1978
                        else {  // bnd == null
1979
                                if (retTyp == null) { // similar to (bnd.isConstructor())
2!
1980
                                        if ( ((Method) candidate).getDeclaredType() != null ) {
4✔
1981
                                                return false;
2✔
1982
                                        }
1983
                                        // else OK for now
1984
                                } else { // (ret != null)  i.e. not a constructor
1985
                                        if (((Method) candidate).getDeclaredType() == null) {
×
1986
                                                return false;
×
1987
                                        } else if (!matchAndMapType(null, retTyp.getName(), Util.getOwner(retTyp), (NamedEntity) ((Method) candidate).getDeclaredType())) {
×
1988
                                                return false;
×
1989
                                        }
1990
                                        // else OK for now
1991
                                }
1992
                        }
1993

1994

1995
                // check owner
1996
                if (matchAndMapOwnerAsType(((bnd != null) ? bnd.getDeclaringClass() : null), owner, Util.getOwner(candidate)) == CheckResult.MATCH) {
13✔
1997
                        conditionalMapToKey(bnd, candidate);
4✔
1998
                        return true;
2✔
1999
                } else {
2000
                        return false;
2✔
2001
                }
2002
        }
2003

2004
        /**
2005
         * Checks whether the candidate (an existing unmapped Famix "Variable" like Attribute, Parameter, ...) matches the binding.
2006
         * Checks that the candidate has the same name as the JDT bound variable, and checks recursively that owners also match.
2007
         * The Famix candidate is a NamedEntity and not a StructuralEntity to allow dealing with Famix EnumValue that JDT treats as variables
2008
         * @param bnd -- a JDT binding that we are trying to match to the candidate
2009
         * @param name of the variable
2010
         * @param owner of the variable
2011
         * @param candidate -- a Famix Entity (a StructuralEntity or an EnumValue)
2012
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
2013
         */
2014
        private boolean matchAndMapVariable(IVariableBinding bnd, String name, TNamedEntity owner, TNamedEntity candidate) {
2015
                if (!(candidate instanceof TStructuralEntity)) {
3!
2016
                        return false;
×
2017
                }
2018

2019
                // check whether bnd and candidate are already bound
2020
                CheckResult keyMatch = checkKeyMatch(bnd, candidate);
5✔
2021
                if (keyMatch == CheckResult.MATCH) {
3!
2022
                        return true;
×
2023
                } else if (keyMatch == CheckResult.FAIL) {
3✔
2024
                        return false;
2✔
2025
                }
2026

2027
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7!
2028
                        return false;
×
2029
                }
2030

2031
                // check owner
2032
                TNamedEntity candidateOwner = Util.getOwner(candidate);
3✔
2033

2034
                // local variable or parameter ?
2035
                // owner is a Method? (for example in case of an anonymous class)
2036
                CheckResult res = matchAndMapOwnerAsMethod(((bnd != null) ? bnd.getDeclaringMethod() : null), owner, candidateOwner);
11✔
2037
                if (res == CheckResult.FAIL) {
3✔
2038
                        return false;
2✔
2039
                } else if (res == CheckResult.MATCH) {
3!
2040
                        conditionalMapToKey(bnd, candidate);
×
2041
                        return true;
×
2042
                }
2043

2044
                // check owner
2045
                // <anArray>.length field?
2046
                if (name.equals("length")) {
4!
2047
                        boolean isArrayLengthField = ((bnd != null) && (bnd.getDeclaringClass() == null)) ||
×
2048
                                                                                 ((bnd == null) && (owner.getName().equals(EntityDictionary.ARRAYS_NAME)));
×
2049
                        if (isArrayLengthField) {
×
2050
                                if (candidateOwner.getName().equals(EntityDictionary.ARRAYS_NAME)) {
×
2051
                                        conditionalMapToKey(bnd, candidate);
×
2052
                                        return true;
×
2053
                                }
2054
                                else {
2055
                                        return false;
×
2056
                                }
2057
                        }
2058
                }
2059

2060
                // check owner
2061
                // "normal" field?
2062
                res = matchAndMapOwnerAsType( ((bnd != null) ? bnd.getDeclaringClass() : null), owner, candidateOwner);
11✔
2063
                if (res == CheckResult.MATCH) {
3!
2064
                        conditionalMapToKey(bnd, candidate);
4✔
2065
                        return true;
2✔
2066
                }
2067
                return false;
×
2068
        }
2069

2070
        /**
2071
         * Checks whether the existing unmapped Famix Type's parent (or owner) matches the binding's owner.
2072
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
2073
         * @param bnd -- a JDT binding whose owner we are trying to match to the candidate's owner
2074
         * @param owner -- the owner of the type
2075
         * @param candidate -- a Famix Entity
2076
         * @return whether we found a match (if <b>true</b>, the mapping is recorded)
2077
         */
2078
        private boolean matchAndMapTypeOwner(ITypeBinding bnd, TNamedEntity owner, Type candidate) {
2079
                ContainerEntity candidateOwner = Util.getOwner(candidate);
4✔
2080

2081
                // owner is a Method? (for example in case of an anonymous class)
2082
                CheckResult res = matchAndMapOwnerAsMethod(((bnd != null) ? bnd.getDeclaringMethod() : null), owner, candidate);
11✔
2083
                if (res == CheckResult.MATCH) {
3!
2084
                        conditionalMapToKey(bnd, candidate);
×
2085
                        return true;
×
2086
                } else if (res == CheckResult.FAIL) {
3!
2087
                        return false;
×
2088
                }
2089

2090
                // owner is a class ?
2091
                res = matchAndMapOwnerAsType(((bnd != null) ? bnd.getDeclaringClass() : null), owner, candidateOwner);
11✔
2092
                if (res == CheckResult.MATCH) {
3✔
2093
                        conditionalMapToKey(bnd, candidate);
4✔
2094
                        return true;
2✔
2095
                }
2096
                else if (res == CheckResult.FAIL) {
3✔
2097
                        return false;
2✔
2098
                }
2099

2100
                // owner must be a package
2101
                if (matchAndMapOwnerAsNamespace( ((bnd != null)?bnd.getPackage():null), owner, candidateOwner) == CheckResult.MATCH) {
12✔
2102
                        conditionalMapToKey(bnd, candidate);
4✔
2103
                        return true;
2✔
2104
                }
2105
                return false;
2✔
2106
        }
2107

2108
        /**
2109
         * Check whether the owner of candidates is a method macthinf either methBnd or owner
2110
         * @param methBnd
2111
         * @param owner
2112
         * @param candidateOwner
2113
         * @return a {@link CheckResult}
2114
         */
2115
        private  <T extends TNamedEntity> CheckResult matchAndMapOwnerAsMethod(IMethodBinding methBnd, T owner, T candidateOwner) {
2116
                if ((methBnd != null) || (owner instanceof Method)) {
5!
2117
                        if (!(candidateOwner instanceof Method)) {
3!
2118
                                return CheckResult.FAIL;
×
2119
                        }
2120

2121
                        ContainerEntity ownerOwner = (owner != null) ? (ContainerEntity) Util.getOwner(owner) : null;
7!
2122
                        String ownerSig = (owner != null) ? ((Method) owner).getSignature() : null;
7!
2123
                        Type ownerReturn = (owner != null) ? (Type) ((Method) owner).getDeclaredType() : null;
8!
2124

2125
                        if (matchAndMapMethod(methBnd, ownerSig, ownerReturn, ownerOwner, (Method) candidateOwner)) {
9!
2126
                                return CheckResult.MATCH;
×
2127
                        } else {
2128
                                return CheckResult.FAIL;
2✔
2129
                        }
2130
                }
2131
                return CheckResult.UNDECIDED;
2✔
2132
        }
2133

2134
        /**
2135
         * @param typBnd
2136
         * @param owner
2137
         * @param candidateOwner
2138
         * @return a {@link CheckResult}
2139
         */
2140
        private CheckResult matchAndMapOwnerAsType(ITypeBinding typBnd, TNamedEntity owner, TNamedEntity candidateOwner) {
2141
                if ((typBnd != null) || (owner instanceof Type)) {
5✔
2142
                        if (!(candidateOwner instanceof Type)) {
3✔
2143
                                return CheckResult.FAIL;
2✔
2144
                        }
2145

2146
                        TNamedEntity ownerOwner = (owner != null) ? Util.getOwner(owner) : null;
6!
2147
                        String ownerName = (owner != null) ? owner.getName() : null;
6!
2148

2149
                        if (matchAndMapType(typBnd, ownerName, ownerOwner, candidateOwner)) {
7✔
2150
                                return CheckResult.MATCH;
2✔
2151
                        } else {
2152
                                return CheckResult.FAIL;
2✔
2153
                        }
2154
                }
2155
                return CheckResult.UNDECIDED;
2✔
2156
        }
2157

2158
        private CheckResult matchAndMapOwnerAsNamespace(IPackageBinding pckgBnd, TNamedEntity owner, ContainerEntity candidateOwner) {
2159
                if ((pckgBnd != null) || (owner instanceof Package)) {
5!
2160
                        if (!(candidateOwner instanceof Package)) {
3!
2161
                                return CheckResult.FAIL;
×
2162
                        }
2163

2164
                        Package ownerOwner = (owner != null) ? (Package) Util.getOwner(owner) : null;
7!
2165
                        String ownerName = (owner != null) ? owner.getName() : null;
6!
2166

2167
                        if (matchAndMapPackage(pckgBnd, ownerName, ownerOwner, candidateOwner)) {
7✔
2168
                                return CheckResult.MATCH;
2✔
2169
                        } else {
2170
                                return CheckResult.FAIL;
2✔
2171
                        }
2172
                }
2173
                return CheckResult.UNDECIDED;
×
2174
        }
2175

2176
        /**
2177
         * Checks whether the name and the candidate matches the name of the entity (given either by 'bnd' or 'name')<br>
2178
         * 'name' and 'bnd' cannot be null together
2179
         * @param bnd -- binding associated with the entity may be null
2180
         * @param name -- name of the entity may be null
2181
         * @param candidate
2182
         * @return true if names match, false if not
2183
         */
2184
        private CheckResult checkNameMatch(IBinding bnd, String name, TNamedEntity candidate) {
2185
                if ( (bnd != null) && (! bnd.getName().equals(candidate.getName())) ) {
8!
2186
                        return CheckResult.FAIL;
×
2187
                }
2188
                else if ( (bnd == null) && (name != null) && (! name.equals(candidate.getName())) ) {
9!
2189
                        return CheckResult.FAIL;
2✔
2190
                }
2191
                else {
2192
                        return CheckResult.MATCH;
2✔
2193
                }
2194
        }
2195

2196
        /**
2197
         * Check whether key and candidate are already bound together, whether either is bound to something else, or whether none is bound
2198
         * @param key
2199
         * @param candidate
2200
         * @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>
2201
         */
2202
        private CheckResult checkKeyMatch(IBinding key, TNamedEntity candidate) {
2203
                if (key == null) {
2✔
2204
                        return CheckResult.UNDECIDED;
2✔
2205
                }
2206

2207
                NamedEntity bound = (NamedEntity)getEntityByKey(key);
5✔
2208
                if (bound == candidate) {
3✔
2209
                        return CheckResult.MATCH;
2✔
2210
                }
2211
                else if (bound != null) {
2✔
2212
                        return CheckResult.FAIL;
2✔
2213
                }
2214
                else if (getEntityKey(candidate) != null) {
4✔
2215
                        // candidate already bound, and not to this binding
2216
                        return CheckResult.FAIL;
2✔
2217
                }
2218
                else {
2219
                        return CheckResult.UNDECIDED;
2✔
2220
                }
2221
        }
2222

2223
        private void conditionalMapToKey(IBinding bnd, TNamedEntity ent) {
2224
                if (bnd != null) {
2✔
2225
                        mapEntityToKey(bnd, ent);
4✔
2226
                }
2227
        }
1✔
2228

2229
        public Method ensureFamixMethod(IMethodBinding bnd) {
2230
                return ensureFamixMethod(
8✔
2231
                                bnd,
2232
                                /*name*/null,
2233
                                /*paramsType*/null,
2234
                                /*returnType*/null,
2235
                                /*owner*/null,
2236
                                (bnd == null) ? UNKNOWN_MODIFIERS : bnd.getModifiers());
6✔
2237
        }
2238

2239
        /**
2240
         * Returns a Famix Method associated with the IMethodBinding. The Entity is created if it does not exist.
2241
         * The Entity is created if it does not exist.
2242
         * @param name -- the name of the Famix Method (MUST NOT be null, but this is not checked)
2243
         * @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)
2244
         * @param owner -- type defining the method (should not be null, but it will work if it is)
2245
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
2246
         */
2247
        public Method ensureFamixMethod(IMethodBinding bnd, String name, Collection<String> paramTypes, Type ret, TWithMethods owner, int modifiers) {
2248
                Method fmx;
2249
                String signature;
2250
                boolean delayedRetTyp;
2251

2252
                // --------------- to avoid useless computations if we can
2253
                fmx = (Method)getEntityByKey(bnd);
5✔
2254
                if (fmx != null) {
2✔
2255
                        return fmx;
2✔
2256
                }
2257

2258
                // --------------- name
2259
                if (name == null) {
2✔
2260
                        if (bnd == null) {
2✔
2261
                                return null;
2✔
2262
                        }
2263
                        else {
2264
                                name = bnd.getName();
3✔
2265
                        }
2266
                }
2267

2268
                // --------------- signature
2269
                signature = name + "(";
3✔
2270
                 if (bnd != null) {
2✔
2271
                    signature += signatureParamsFromBinding(bnd);
7✔
2272
                }
2273
        else if (paramTypes != null) {
2!
2274
                        signature += signatureParamsFromStringCollection(paramTypes);
7✔
2275
                }
2276
                else {
2277
                        signature += "???";
×
2278
                }
2279
                signature += ")";
3✔
2280

2281
                // --------------- return type
2282
                delayedRetTyp = false;
2✔
2283
                ITypeBinding retTypBnd = null;
2✔
2284
                if (ret == null) {
2!
2285
                        if (bnd != null) {
2✔
2286
                // must create the return type
2287
                // but for method like "<T> T mtd()" where T belongs to mtd and mtd returns T,
2288
                // we need T to create the method and the method to create T ...
2289
                // so we need to test the situation and deal with it
2290
                retTypBnd = bnd.getReturnType();
3✔
2291
                if (retTypBnd != null) {
2✔
2292
                    if (retTypBnd.isArray()) {
3✔
2293
                        retTypBnd = retTypBnd.getElementType();
3✔
2294
                    }
2295
                }
2296

2297
                if ( (retTypBnd != null) && retTypBnd.isTypeVariable() && (retTypBnd.getDeclaringMethod() == bnd) ) {
9✔
2298
                    delayedRetTyp = true;
3✔
2299
                }
2300
                else {
2301
                    ret = this.ensureFamixType(retTypBnd,(TWithTypes) /*ctxt*/owner);
6✔
2302
                }
2303
                        }
2304
                }
2305

2306
                // --------------- owner
2307
                if (owner == null) {
2✔
2308
                        if (bnd == null) {
2✔
2309
                                owner = ensureFamixClassStubOwner();
4✔
2310
                        }
2311
                        else {
2312
                                ITypeBinding classBnd = bnd.getDeclaringClass().getErasure();
4✔
2313
                                if (classBnd != null) {
2!
2314
                                        owner = ensureFamixType(classBnd);
5✔
2315
                                }
2316
                                else {
2317
                                        owner = ensureFamixClassStubOwner();
×
2318
                                }
2319
                        }
2320
                }
2321

2322
                // --------------- recover from name ?
2323
                for (Method candidate : this.getEntityByName(Method.class, name)) {
13✔
2324
                        if (matchAndMapMethod(bnd, signature, ret, (TNamedEntity) owner, candidate)) {
9✔
2325
                                fmx = candidate;
2✔
2326
                                break;
1✔
2327
                        }
2328
                }
1✔
2329

2330
                if (fmx == null) {
2✔
2331
                        if(bnd != null && bnd.isGenericMethod()) {
5✔
2332
                                fmx = ensureFamixEntity(ParametricMethod.class, bnd, name);
7✔
2333
                                for(ITypeBinding param: bnd.getTypeParameters()) {
18✔
2334
                                        TypeParameter fmxParam = this.ensureFamixTypeParameter(param, null, fmx);
6✔
2335
                                        fmxParam.setGenericEntity((ParametricMethod)fmx);
4✔
2336
                                }
2337
                        // Parameterized method binding = when the method is the target of an invocation.
2338
                        } else if (bnd != null && bnd.isParameterizedMethod()) {
5!
2339
                                fmx = this.ensureFamixMethod(bnd.getMethodDeclaration());
×
2340
                        } else {
2341
                if (bnd != null && bnd.isConstructor()) {
5✔
2342
                    fmx = ensureFamixEntity(Initializer.class, bnd, name);
8✔
2343
                } else {
2344
                    fmx = ensureFamixEntity(Method.class, bnd, name);
7✔
2345
                }
2346
            }
2347

2348
                        fmx.setSignature(signature);
3✔
2349
                        ITypeBinding returnTypeBnd = (bnd == null) ? null : bnd.getReturnType();
7✔
2350
                        ensureFamixEntityTyping(returnTypeBnd, fmx, ret);
6✔
2351
                        fmx.setParentType(owner);
3✔
2352
                }
2353

2354
                if (fmx != null) {
2!
2355
                        setMethodModifiers(fmx, modifiers);
4✔
2356
                }
2357

2358
        //If it has the #default keywork, we mark it as default implementation
2359
        if (Modifier.isDefault(modifiers)) {
3✔
2360
            fmx.setKind(DEFAULT_IMPLEMENTATION_KIND_MARKER);
3✔
2361
        }
2362

2363
        if (delayedRetTyp) {
2✔
2364
                        int retTypModifiers = retTypBnd.getModifiers();
3✔
2365
                        ITypeBinding returnTypeBnd = bnd.getReturnType();
3✔
2366
                        ensureFamixEntityTyping(returnTypeBnd, fmx, this.ensureFamixType(retTypBnd, /*name*/null, /*owner*/fmx, /*ctxt*/(ContainerEntity) owner, retTypModifiers));
13✔
2367
                }
2368

2369
                return fmx;
2✔
2370
        }
2371

2372

2373
        /**
2374
         * Creates or recovers the initializer method containing the attribute initializations of a type.
2375
         * @param owner Type containing the initializer
2376
         * @param isStatic Modifier of the initializer. A type can have 2 initializers for attribute initialization: 1 static and 1 not.
2377
         * @param isInitializationBlock True if the entity is an initialization block. False for the artificial method containing all field initializations.
2378
         * @return the FamixInitializer
2379
         */
2380
        public Initializer ensureFamixInitializer(TWithMethods owner, Boolean isStatic, Boolean isInitializationBlock) {
2381
                Initializer fmx = null;
2✔
2382

2383
                if (owner != null) {
2!
2384
                        Optional<TMethod> existingInitializer = owner.getMethods().stream()
6✔
2385
                                        .filter(meth ->
1✔
2386
                                                        ((Method) meth).getIsInitializer() &&
8✔
2387
                                                        ((Method) meth).getIsConstructor().equals(false) &&
7✔
2388
                                                        ((Method) meth).getIsClassSide().equals(isStatic) &&
6✔
2389
                                                        ((Initializer) meth).getIsInitializationBlock().equals(isInitializationBlock))
7✔
2390
                                        .findFirst();
2✔
2391
                        if (existingInitializer.isPresent()) {
3✔
2392
                                fmx = (Initializer) existingInitializer.get();
4✔
2393
                        }
2394
                }
2395

2396
                if (fmx == null) {
2✔
2397
                        fmx = new Initializer();
4✔
2398
                        fmx.setName(INIT_BLOCK_NAME);
3✔
2399
                        fmx.setSignature(INIT_BLOCK_NAME + "()" );
3✔
2400
                        fmx.setVisibility(MODIFIER_PRIVATE);
3✔
2401
                        fmx.setParentType(owner);
3✔
2402
                        fmx.setIsClassSide(isStatic);
3✔
2403
                        fmx.setIsInitializationBlock(isInitializationBlock);
3✔
2404
                }
2405

2406
                return fmx;
2✔
2407
        }
2408

2409

2410
        public Initializer ensureImplicitConstructor(TWithMethods owner, String name, Collection<String> parameterTypesNames) {
2411
                Initializer fmx = null;
2✔
2412

2413
                if (owner != null) {
2!
2414
                        Optional<TMethod> existingInitializer = owner.getMethods().stream()
5✔
2415
                                        .filter(meth ->
1✔
2416
                                                        ((Method) meth).getIsInitializer() &&
8✔
2417
                                                                        ((Method) meth).getIsConstructor()
4✔
2418
                                                                        && meth.getParameters().size() == parameterTypesNames.size()
6✔
2419
                                                                        && (parameterTypesNames.stream().allMatch(parameterName -> meth.getSignature().contains(parameterName))) )
13!
2420
                                        .findFirst();
2✔
2421
                        if (existingInitializer.isPresent()) {
3✔
2422
                                fmx = (Initializer) existingInitializer.get();
4✔
2423
                        }
2424
                }
2425

2426
                if (fmx == null) {
2✔
2427
                        fmx = ensureFamixEntity(Initializer.class, null, name);
7✔
2428
                        fmx.setParentType(owner);
3✔
2429
                        fmx.setSignature(name + "()");
4✔
2430

2431
                }
2432

2433
                return fmx;
2✔
2434
        }
2435

2436
        /**
2437
         * Creates or recovers a stub Famix Method
2438
         * @param name of the method
2439
         * @return the Famix Method
2440
         */
2441
        public Method ensureFamixStubMethod(String name) {
2442
                return ensureFamixMethod(null, name, /*paramType*/null, /*returnType*/null, ensureFamixClassStubOwner(), /*modifiers*/0);
×
2443
        }
2444

2445
        public void setAttributeModifiers(Attribute fmx, int mod) {
2446
                setCommonModifiers(fmx, mod);
4✔
2447
                fmx.setIsTransient(Modifier.isTransient(mod));
5✔
2448
                fmx.setIsVolatile(Modifier.isVolatile(mod));
5✔
2449
        }
1✔
2450

2451
        public void setMethodModifiers(Method fmx, int mod) {
2452
                setCommonModifiers(fmx, mod);
4✔
2453
                fmx.setIsAbstract(Modifier.isAbstract(mod));
5✔
2454
                fmx.setIsSynchronized(Modifier.isSynchronized(mod));
5✔
2455
        }
1✔
2456

2457
        public void setClassModifiers(Class fmx, int mod) {
2458
                setCommonModifiers(fmx, mod);
4✔
2459
                fmx.setIsAbstract(Modifier.isAbstract(mod));
5✔
2460
        }
1✔
2461

2462
        public void setInterfaceModifiers(Interface fmx, int mod) {
2463
                setCommonModifiers(fmx, mod);
4✔
2464
        }
1✔
2465

2466
        private void setCommonModifiers(Entity fmx, int mod) {
2467
                setVisibility((THasVisibility)fmx, mod);
5✔
2468
                ((TCanBeClassSide)fmx).setIsClassSide(Modifier.isStatic(mod));
6✔
2469
                ((TCanBeFinal)fmx).setIsFinal(Modifier.isFinal(mod));
6✔
2470
        }
1✔
2471

2472
        /**
2473
         * Sets the visibility of a FamixNamedEntity
2474
         *
2475
         * @param fmx -- the FamixNamedEntity
2476
         * @param mod -- a description of the modifiers as understood by org.eclipse.jdt.core.dom.Modifier
2477
         */
2478
        public void setVisibility(THasVisibility fmx, int mod) {
2479
                if (Modifier.isPublic(mod)) {
3✔
2480
                        fmx.setVisibility(MODIFIER_PUBLIC);
4✔
2481
                } else if (Modifier.isPrivate(mod)) {
3✔
2482
                        fmx.setVisibility(MODIFIER_PRIVATE);
4✔
2483
                } else if (Modifier.isProtected(mod)) {
3✔
2484
                        fmx.setVisibility(MODIFIER_PROTECTED);
4✔
2485
                } else {
2486
                        fmx.setVisibility(MODIFIER_PACKAGE);
3✔
2487
                }
2488
        }
1✔
2489

2490
        /**
2491
         * Returns a Famix Attribute associated with the IVariableBinding.
2492
         * The Entity is created if it does not exist.<br>
2493
         * @param name -- the name of the FAMIX Attribute (MUST NOT be null, but this is not checked)
2494
         * @param type -- Famix Type of the Attribute (should not be null, but it will work if it is)
2495
         * @param owner -- type defining the Attribute (should not be null, but it will work if it is)
2496
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
2497
         */
2498
        public Attribute ensureFamixAttribute(IVariableBinding bnd, String name, Type type, TWithAttributes owner) {
2499
                Attribute fmx;
2500

2501
                // --------------- to avoid useless computations if we can
2502
                fmx = (Attribute)getEntityByKey(bnd);
5✔
2503
                if (fmx != null) {
2✔
2504
                        return fmx;
2✔
2505
                }
2506

2507
                // --------------- name
2508
                if (name == null) {
2!
2509
                        if (bnd == null) {
×
2510
                                return null;
×
2511
                        }
2512
                        else {
2513
                                name = bnd.getName();
×
2514
                        }
2515
                }
2516

2517
                // --------------- owner
2518
                if (owner == null) {
2✔
2519
                        if (bnd == null) {
2!
2520
                                return null;  // what would be the interest of creating an attribute for which we ignore the declaring class?
×
2521
                        }
2522
                        else {
2523
                                if (bnd.getDeclaringClass() != null && bnd.getDeclaringClass().getErasure() != null) {
7!
2524
                                        // Declaring class is the generic one if the class is parametric.
2525
                                        owner = (TWithAttributes)ensureFamixType(bnd.getDeclaringClass().getErasure());
8✔
2526
                                } else {
2527
                                        return null;  // what would be the interest of creating an attribute for which we ignore the declaring class?
2✔
2528
                                }
2529
                        }
2530
                }
2531

2532
                // --------------- recover from name ?
2533
                for (Attribute candidate : getEntityByName(Attribute.class, name)) {
13✔
2534
                        if (matchAndMapVariable(bnd, name, (TNamedEntity) owner, candidate)) {
8✔
2535
                                fmx = candidate;
2✔
2536
                                break;
1✔
2537
                        }
2538
                }
1✔
2539

2540
                if (fmx == null) {
2✔
2541
                        fmx = ensureFamixEntity(Attribute.class, bnd, name);
7✔
2542
                        fmx.setParentType( owner);
3✔
2543
                }
2544

2545
        fmx.setParentType(owner);
3✔
2546
        ITypeBinding declaredTypeBinding = (bnd == null) ? null : bnd.getType();
7✔
2547
        ensureFamixEntityTyping(declaredTypeBinding, fmx, type);
6✔
2548
        if (bnd != null) {
2✔
2549
            int mod = bnd.getModifiers();
3✔
2550
            setAttributeModifiers(fmx, mod);
4✔
2551
        }
2552

2553
        return fmx;
2✔
2554
        }
2555

2556
        public Attribute ensureFamixAttribute(IVariableBinding bnd, String name, TWithAttributes owner) {
2557
                return ensureFamixAttribute(bnd, name, /*declared type*/null, owner);
7✔
2558
        }
2559

2560
        /**
2561
         * helper method, we know the var exists, ensureFamixAttribute will recover it
2562
         */
2563
        public Attribute getFamixAttribute(IVariableBinding bnd, String name, TWithAttributes owner) {
2564
                return ensureFamixAttribute(bnd, name, /*declared type*/null, owner);
7✔
2565
        }
2566

2567
        /**
2568
         * Returns a Famix Parameter associated with the IVariableBinding.
2569
         * The Entity is created if it does not exist.<br>
2570
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
2571
         */
2572
        public Parameter ensureFamixParameter(IVariableBinding bnd, String name, Type typ, TMethod tMethod) {
2573
                Parameter fmx = null;
2✔
2574

2575
                // --------------- to avoid useless computations if we can
2576
                try {
2577
                        fmx = (Parameter)getEntityByKey(bnd);
5✔
2578
                }catch(Throwable e) {
×
2579
                        e.printStackTrace();
×
2580
                }
1✔
2581
                if (fmx != null) {
2✔
2582
                        return fmx;
2✔
2583
                }
2584

2585
                // --------------- name
2586
                if (name == null) {
2!
2587
                        if (bnd == null) {
×
2588
                                return null;
×
2589
                        }
2590
                        else {
2591
                                name = bnd.getName();
×
2592
                        }
2593
                }
2594

2595
                // --------------- owner
2596
                if (tMethod == null) {
2!
2597
                        if (bnd == null) {
×
2598
                                tMethod = ensureFamixStubMethod("<"+name+"_owner>");
×
2599
                        }
2600
                        else {
2601
                                tMethod = ensureFamixMethod(bnd.getDeclaringMethod());
×
2602
                        }
2603
                }
2604

2605
                // --------------- recover from name ?
2606
                for (Parameter candidate : getEntityByName(Parameter.class, name) ) {
13✔
2607
                        if ( matchAndMapVariable(bnd, name, tMethod, candidate) ) {
7!
2608
                                fmx = candidate;
×
2609
                                break;
×
2610
                        }
2611
                }
1✔
2612

2613
                if (fmx == null) {
2!
2614
                        fmx = ensureFamixEntity(Parameter.class, bnd, name);
7✔
2615
                }
2616

2617
                if (fmx != null) {
2!
2618
                        fmx.setParentBehaviouralEntity(tMethod);
3✔
2619
                        ITypeBinding declaredTypeBnd = (bnd == null) ? null : bnd.getType();
5!
2620
                        ensureFamixEntityTyping(declaredTypeBnd, fmx, typ);
6✔
2621
                }
2622

2623
                return fmx;
2✔
2624
        }
2625

2626
    /**
2627
         * Returns a Famix LocalVariable associated with the IVariableBinding.
2628
         * The Entity is created if it does not exist.<br>
2629
         * @param name -- the name of the FAMIX LocalVariable
2630
         * @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
2631
         */
2632
        public LocalVariable ensureFamixLocalVariable(IVariableBinding bnd, String name, TWithLocalVariables owner) {
2633
                LocalVariable fmx;
2634

2635
                // --------------- to avoid useless computations if we can
2636
                fmx = (LocalVariable)getEntityByKey(bnd);
5✔
2637
                if (fmx != null) {
2!
2638
                        return fmx;
×
2639
                }
2640

2641
                // --------------- name
2642
                if (name == null) {
2!
2643
                        if (bnd == null) {
×
2644
                                return null;
×
2645
                        }
2646
                        else {
2647
                                name = bnd.getName();
×
2648
                        }
2649
                }
2650

2651
                // --------------- owner
2652
                if (owner == null) {
2!
2653
                        if (bnd == null) {
×
2654
                                return null;  // what would be the interest of a local variable for which we ignore the declaring method?
×
2655
                        }
2656
                        else {
2657
                                owner = ensureFamixMethod(bnd.getDeclaringMethod());
×
2658
                        }
2659
                }
2660

2661
                // --------------- recover from name ?
2662
                for (LocalVariable candidate : getEntityByName(LocalVariable.class, name) ) {
13✔
2663
                        if ( matchAndMapVariable(bnd, name, (TNamedEntity) owner, candidate) ) {
8!
2664
                                fmx = candidate;
×
2665
                                break;
×
2666
                        }
2667
                }
1✔
2668

2669
                if (fmx == null) {
2!
2670
                        fmx = ensureFamixEntity(LocalVariable.class, bnd, name);
7✔
2671
                        fmx.setParentBehaviouralEntity(owner);
3✔
2672
                }
2673

2674
        // we just created it or it was not bound, so we make sure it has the right information in it
2675
        fmx.setParentBehaviouralEntity(owner);
3✔
2676

2677
        return fmx;
2✔
2678
        }
2679

2680
    /**
2681
         * Returns a FAMIX ImplicitVariable with the given <b>name</b> ("self" or "super") and corresponding to the <b>type</b>.
2682
         * If this ImplicitVariable does not exist yet, it is created
2683
         * @param name -- the name of the FAMIX ImplicitVariable (should be Dictionary.SELF_NAME or Dictionary.SUPER_NAME)
2684
         * @param type -- the Famix Type for this ImplicitVariable (should not be null)
2685
         * @param tMethod -- the ContainerEntity where the implicit variable appears (should be a method inside <b>type</b>)
2686
         * @return the FAMIX ImplicitVariable or null in case of a FAMIX error
2687
         */
2688
        public ImplicitVariable ensureFamixImplicitVariable(IBinding key, String name, TType type, TMethod tMethod) {
2689
                ImplicitVariable fmx;
2690
                fmx = ensureFamixEntity(ImplicitVariable.class, key, name);
7✔
2691
                fmx.setParentBehaviouralEntity(tMethod);
3✔
2692
                return fmx;
2✔
2693
        }
2694

2695
        public ImplicitVariable ensureFamixImplicitVariable(String name, TType tType, TMethod tMethod) {
2696
                IBinding bnd = ImplicitVarBinding.getInstance(tMethod, name);
4✔
2697
                return ensureFamixImplicitVariable(bnd, name, tType, tMethod);
7✔
2698
        }
2699

2700
        /**
2701
         * Creates and returns a Famix Comment and associates it with an Entity (ex: for Javadocs)
2702
         * @param jCmt -- the content (String) of the comment 
2703
         * @param owner -- the entity that is commented
2704
         * @return the Famix Comment
2705
         */
2706
        public Comment createFamixComment(org.eclipse.jdt.core.dom.Comment jCmt, TWithComments owner) {
2707
                Comment cmt = null;
2✔
2708

2709
                if ( (jCmt != null) && (owner != null) ) {
4!
2710
                        
2711
                        cmt = new Comment();
4✔
2712
                        addSourceAnchor(cmt, jCmt);
5✔
2713
                        famixRepoAdd(cmt);
3✔
2714
                        cmt.setCommentedEntity(owner);
3✔
2715
                }
2716

2717
                return cmt;
2✔
2718
        }
2719

2720
        /**
2721
         * Creates and returns a Famix Comment and associates it with an Entity
2722
         * @param jCmt -- the content (String) of the comment 
2723
         * @param owner -- the entity that is commented
2724
         * @param content -- the text of the comment
2725
         * @return the Famix Comment
2726
         */
2727
        public Comment createFamixComment(org.eclipse.jdt.core.dom.Comment jCmt, TWithComments owner, String content) {
2728
                Comment cmt = null;
2✔
2729

2730
                if ( (jCmt != null) && (owner != null) ) {
4!
2731
                        cmt = new Comment();
4✔
2732
                        cmt.setContent(content );
3✔
2733
                        famixRepoAdd(cmt);
3✔
2734
                        cmt.setCommentedEntity(owner);
3✔
2735
                }
2736

2737
                return cmt;
2✔
2738
        }
2739

2740
        /**
2741
         * Adds location information to a Famix Entity.
2742
         * 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.
2743
         * This method also creates some basic links between the entity and others (e.g. declaring container, return type, ...)
2744
         * @param fmx -- Famix Entity to add the anchor to
2745
         * @param node -- JDT ASTNode, where the information is extracted
2746
         * @return the Famix SourceAnchor added to fmx. May be null in case of incorrect parameter ('fmx' or 'ast' == null)
2747
         */
2748
        public SourceAnchor addSourceAnchor(TSourceEntity fmx, ASTNode node) {
2749
                IndexedFileAnchor fa;
2750

2751
                fa = createIndexedFileAnchor(node);
4✔
2752
                if ((fmx != null) && (fa != null)) {
4!
2753
                        fmx.setSourceAnchor(fa);
3✔
2754
                        famixRepoAdd(fa);
3✔
2755
                }
2756

2757
                return fa;
2✔
2758
        }
2759

2760
        /**
2761
         * Special case of  {@linkplain #addSourceAnchor(TSourceEntity, ASTNode)} to add location information to a Famix Method.
2762
         */
2763
        public SourceAnchor addSourceAnchor(Method fmx, MethodDeclaration node) {
2764
                IndexedFileAnchor fa;
2765

2766
                fa = createIndexedFileAnchor(node);
4✔
2767
                if ((fmx != null) && (fa != null)) {
4!
2768

2769
                        // may change the positions
2770
                        List<ASTNode> methodDeclarationModifiers = new ArrayList<>();
4✔
2771
                        methodDeclarationModifiers.addAll(node.modifiers());
5✔
2772
                        if (node.getName() != null) {
3!
2773
                                methodDeclarationModifiers.add(node.getName());
5✔
2774
                        }
2775
                        if (node.getReturnType2() != null) {
3✔
2776
                                methodDeclarationModifiers.add(node.getReturnType2());
5✔
2777
                        }
2778
                        int beg = (methodDeclarationModifiers.stream().mapToInt(el -> el.getStartPosition()).min().getAsInt()) + 1;
12✔
2779
                        int end = node.getStartPosition() + node.getLength();
6✔
2780

2781
                        fa.setStartPos(beg);
4✔
2782
                        fa.setEndPos(end);
4✔
2783

2784
                        fmx.setSourceAnchor(fa);
3✔
2785
                        famixRepoAdd(fa);
3✔
2786
                }
2787

2788
                return fa;
2✔
2789
        }
2790

2791
        /**
2792
         * Gets the file name holding <code>node</code> and its start and end positions in the file.
2793
         * Information returned in the form of an IndexedFileAnchor
2794
         */
2795
        protected IndexedFileAnchor createIndexedFileAnchor(ASTNode node) {
2796
                IndexedFileAnchor fa;
2797
                
2798
                if (node == null) {
2!
2799
                        return null;
×
2800
                }
2801

2802
                // position in source file
2803
                int beg = node.getStartPosition() + 1; // Java starts at 0, Moose at 1
5✔
2804
                int end = beg + node.getLength() - 1;
7✔
2805

2806
                // find source Compilation Unit
2807
                // there is a special case for the JDT Comment Nodes
2808
                if (node instanceof org.eclipse.jdt.core.dom.Comment) {
3✔
2809
                        node = ((org.eclipse.jdt.core.dom.Comment) node).getAlternateRoot();
5✔
2810
                } else {
2811
                        node = node.getRoot();
3✔
2812
                }
2813

2814
                fa = new IndexedFileAnchor();
4✔
2815
                fa.setStartPos(beg);
4✔
2816
                fa.setEndPos(end);
4✔
2817

2818
                fa.setFileName((String) node.getProperty(SOURCE_FILENAME_PROPERTY));
6✔
2819

2820
                return fa;
2✔
2821
        }
2822

2823
        /**
2824
         * Creates or recovers the Famix Class for "Object".
2825
         * Because "Object" is the root of the inheritance tree, it needs to be treated differently.
2826
         *
2827
         * @return a Famix class for "Object"
2828
         */
2829
        public Class ensureFamixClassObject() {
2830
        // In the past we used #ensureFamixUniqEntity but that does not check that the parent package is right and we got some Object from other packages than java.lang...
2831
        Collection<Class> objects = getEntityByName(Class.class, "Object");
5✔
2832

2833
        for (Class entity : objects) {
10✔
2834
                //We need to cast because the type container is a FamixTWithType but all implementors of this should be named in Java...
2835
                if ("java.lang".equals(((TNamedEntity) entity.getTypeContainer()).getName())) {
7✔
2836
                    return entity;
2✔
2837
                }
2838
        }
1✔
2839

2840
        Class fmx = createFamixEntity(Class.class, "Object");
6✔
2841
        fmx.setTypeContainer(ensureFamixPackageJavaLang(null));
5✔
2842
                return fmx;
2✔
2843
        }
2844

2845
        /**
2846
         * Ensures the Java meta-class: <pre>{@code java.lang.Class<>}</pre>
2847
         */
2848
        public Class ensureFamixMetaClass(ITypeBinding bnd) {
2849
                Package javaLang = ensureFamixPackageJavaLang((bnd == null) ? null : bnd.getPackage());
7!
2850
                ParametricClass fmx = (ParametricClass) this.ensureFamixClass(null, METACLASS_NAME, javaLang, /*isGeneric*/true, Modifier.PUBLIC & Modifier.FINAL);
9✔
2851

2852
                if (fmx != null) {
2!
2853
                        fmx.addTypeParameters(ensureFamixTypeParameter(null, "T", fmx));
7✔
2854
                }
2855

2856
                if ((fmx != null) && (fmx.getSuperInheritances() == null)) {
5!
2857
                        ensureFamixInheritance(ensureFamixClassObject(), fmx, null, null);
×
2858
                }
2859

2860
                return fmx;
2✔
2861
        }
2862

2863
        public Class getFamixMetaClass(ITypeBinding bnd) {
2864
                Package javaLang = ensureFamixPackageJavaLang((bnd == null) ? null : bnd.getPackage());
7!
2865
                return this.ensureFamixClass(null, METACLASS_NAME, javaLang, /*isGeneric*/true, UNKNOWN_MODIFIERS);
8✔
2866
        }
2867

2868
        /**
2869
         * Creates or recovers the Famix Class for all arrays (<pre>{@code <some-type> []}</pre>)
2870
         * In java arrays or objects of special classes (i.e. "I[" for an array of int).
2871
         * JDT does not create a binding for these classes, so we create a stub one here.
2872
         *
2873
         * @return a Famix class
2874
         */
2875
        public Class ensureFamixClassArray() {
2876
                Class fmx = ensureFamixUniqEntity(Class.class, null, ARRAYS_NAME);
×
2877
                if (fmx != null) {
×
2878
                        ensureFamixInheritance(ensureFamixClassObject(), fmx, /*prev*/null, null);
×
2879
                        fmx.setTypeContainer(ensureFamixPackageDefault());
×
2880

2881
                        // may be not needed anymore now that we use modifiers
2882
                        /*fmx.setIsAbstract(Boolean.FALSE);
2883
                        fmx.setIsFinal(Boolean.FALSE);
2884
                        fmx.setIsInterface(Boolean.FALSE); 
2885
                        fmx.setIsPrivate(Boolean.FALSE);
2886
                        fmx.setIsProtected(Boolean.FALSE);*/
2887
                        fmx.setVisibility(MODIFIER_PUBLIC);
×
2888
                }
2889

2890
                return fmx;
×
2891
        }
2892

2893
        public String removeLastPartOfPackageName(String qualifiedName) {
2894
                String ret;
2895
                int last = qualifiedName.lastIndexOf('.');
4✔
2896
                if (last > 0) {
2✔
2897
                        // recursively creating the parent
2898
                        ret = qualifiedName.substring(0, last);
6✔
2899
                }
2900
                else {
2901
                        ret = "";
2✔
2902
                }
2903

2904
                return ret;
2✔
2905
        }
2906

2907
        /** Generates the list of parameters for a method signature
2908
         * @return a string
2909
         */
2910
        protected String signatureParamsFromBinding(IMethodBinding bnd) {
2911
                boolean first = true;
2✔
2912
                String sig = "";
2✔
2913

2914
                for (ITypeBinding parBnd : bnd.getParameterTypes()) {
17✔
2915
                        if (first) {
2✔
2916
                                sig = parBnd.getName();
3✔
2917
                                first = false;
3✔
2918
                        }
2919
                        else {
2920
                                sig += "," + parBnd.getName();
5✔
2921
                        }
2922
                }
2923
                return sig;
2✔
2924
        }
2925

2926
        private String signatureParamsFromStringCollection(Collection<String> paramTypes) {
2927
                boolean first = true;
2✔
2928
                String sig = "";
2✔
2929

2930
                for (String t : paramTypes) {
10✔
2931
                        if (first) {
2✔
2932
                                sig = t;
2✔
2933
                                first = false;
3✔
2934
                        }
2935
                        else {
2936
                                sig += "," + t;
4✔
2937
                        }
2938
                }
1✔
2939
                return sig;
2✔
2940
        }
2941

2942
}
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