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

moosetechnology / VerveineJ / 24242785388

10 Apr 2026 12:25PM UTC coverage: 52.056% (-0.001%) from 52.057%
24242785388

push

github

web-flow
Merge pull request #204 from guillep/strict_mode

Add strict option

1975 of 3970 branches covered (49.75%)

Branch coverage included in aggregate %.

4 of 6 new or added lines in 2 files covered. (66.67%)

1 existing line in 1 file now uncovered.

4368 of 8215 relevant lines covered (53.17%)

2.15 hits per line

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

75.67
/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✔
167
                        } catch (java.lang.Exception e) {
×
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.isCapture()) {
3✔
951
                        if (bnd.getErasure().isInterface()) {
4!
952
                                return this.ensureFamixInterface(bnd.getErasure(), name, owner, /*isGeneric*/true, modifiers);
9✔
953
                        }
954
                        else {
955
                                return this.ensureFamixClass(bnd.getErasure(), name, (TNamedEntity) owner, /*isGeneric*/true, modifiers);
×
956
                        }
957
                }
958

959
                if (bnd.isAnnotation()) {
3!
UNCOV
960
                        return this.ensureFamixAnnotationType(bnd, name, (ContainerEntity) owner);
×
961
                }
962

963
                if (bnd.isInterface()) {
3✔
964
                        return this.ensureFamixInterface(bnd, name, owner, /*isGeneric*/bnd.isGenericType() || bnd.isParameterizedType() || bnd.isRawType(), modifiers);
19✔
965
                }
966

967
                if (isThrowable(bnd)) {
4✔
968
                        return this.ensureFamixException(bnd, name, owner, /*isGeneric*/false, modifiers);
8✔
969
                }
970
                if (bnd.isClass()) {
3✔
971
                        return this.ensureFamixClass(bnd, name, (TNamedEntity) owner, /*isGeneric*/bnd.isGenericType() || bnd.isParameterizedType() || bnd.isRawType(), modifiers);
20!
972
                }
973
                if(bnd.isWildcardType()) {
3✔
974
                        return this.ensureFamixWildcardType(bnd, name, (TParametricEntity)owner, ctxt);
8✔
975
                }
976

977
                //otherwise (none of the above)
978

979
                if (name == null) {
2✔
980
                        name = bnd.getName();
3✔
981
                }
982

983
                if (owner == null) {
2!
984
                        owner = (TWithTypes) this.ensureOwner(bnd);
5✔
985
                }
986

987
                if (bnd.isTypeVariable() ) {
3!
988
                        fmx = ensureFamixTypeParameter(bnd, name, owner);
6✔
989
                        return fmx;
2✔
990
                }
991

992
                fmx = ensureFamixEntity(Type.class, bnd, name);
×
993
                fmx.setTypeContainer(owner);
×
994
                return fmx;
×
995
        }
996

997
        public Type ensureFamixType(ITypeBinding bnd, TWithTypes context) {
998
        int modifiers = (bnd != null) ? bnd.getModifiers() : UNKNOWN_MODIFIERS;
7✔
999
                return ensureFamixType(bnd, /*name*/null, /*owner*/null, context, modifiers);
8✔
1000
        }
1001
        
1002
        public Type ensureFamixType(ITypeBinding bnd) {
1003
                return ensureFamixType(bnd, /*ctxt*/null);
5✔
1004
        }
1005

1006
        public boolean isThrowable(ITypeBinding bnd) {
1007
                if (bnd == null) {
2!
1008
                        return false;
×
1009
                }
1010
                if (bnd.getQualifiedName().equals("java.lang.Throwable")) {
5✔
1011
                        return true;
2✔
1012
                } else if (bnd.getQualifiedName().equals("java.lang.Object")) {
5✔
1013
                        return false;
2✔
1014
                }
1015
                else {
1016
                        return isThrowable(bnd.getSuperclass());
5✔
1017
                }
1018
        }
1019

1020
        /**
1021
         * Returns a Famix Class associated with the ITypeBinding.
1022
         * The Entity is created if it does not exist.
1023
         * @param name -- the name of the Famix Class (MUST NOT be null, but this is not checked)
1024
         * @param owner -- package defining the class (should not be null, but it will work if it is)
1025
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
1026
         */
1027
        @SuppressWarnings("deprecation")
1028
        public Class ensureFamixClass(ITypeBinding bnd, String name, TNamedEntity owner, boolean isGeneric, int modifiers) {
1029
                Class fmx;
1030

1031
                // --------------- some special cases
1032
                if (bnd != null) {
2✔
1033
                        if (bnd.isArray()) {
3!
1034
                                bnd = bnd.getElementType();
×
1035
                        }
1036

1037
                        // for inner classes defined in generics !!! For others should not change anything
1038
                        bnd = bnd.getErasure();
3✔
1039
                }
1040

1041
                // ---------------- to avoid useless computations if we can
1042
                fmx = (Class) getEntityByKey(bnd);
5✔
1043
                if (fmx != null) {
2✔
1044
                        return fmx;
2✔
1045
                }
1046

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

1070
        // If we have java.lang.Object we should ensure we create this class
1071
        if (bnd != null && bnd.getQualifiedName().equals("java.lang.Object")) {
7✔
1072
                        return ensureFamixClassObject();
3✔
1073
                }
1074

1075
                // --------------- owner
1076
                if (owner == null) {
2✔
1077
                        if (bnd != null) {
2✔
1078
                                owner = ensureOwner(bnd);
4✔
1079
                        }
1080
                        /*                                owner = ensureFamixPackageDefault();
1081
                        } else {*/
1082
                }
1083

1084
                // --------------- recover from name ?
1085
                if (owner != null) {
2✔
1086
                        for (Class candidate : this.getEntityByName(Class.class, name)) {
13✔
1087
                                if (matchAndMapClass(bnd, name, owner, candidate)) {
7✔
1088
                                        fmx = candidate;
2✔
1089
                                        break;
1✔
1090
                                }
1091
                        }
1✔
1092
                }
1093

1094
                // ---------------- create
1095
                if (fmx == null) {
2✔
1096
                        if (isGeneric) {
2✔
1097
                                fmx = ensureFamixParametricClass(bnd, name, (TWithTypes) owner);
8✔
1098
                        }
1099
                        else {
1100
                                fmx = ensureFamixEntity(Class.class, bnd, name);
7✔
1101
                                fmx.setTypeContainer((TWithTypes)owner);
4✔
1102
                        }
1103
                }
1104

1105
                // ---------------- modifiers and super-classes
1106
                if (fmx!=null) {
2!
1107
                        // we just created it, or it was not bound so we make sure it has the right information in it
1108
                        if (bnd != null) {
2✔
1109
                                setClassModifiers(fmx, bnd.getDeclaredModifiers());
5✔
1110
                        }
1111

1112
                        TAssociation lastAssoc = null;
2✔
1113

1114
                        if (bnd != null) {
2✔
1115
                                ITypeBinding supbnd = bnd.getSuperclass();
3✔
1116
                                if (supbnd != null) {
2✔
1117
                                        lastAssoc = ensureFamixInheritance((TWithInheritances) ensureFamixType(supbnd), fmx, lastAssoc, supbnd);
11✔
1118
                                }
1119
                                else {
1120
                                        lastAssoc = ensureFamixInheritance(ensureFamixClassObject(), fmx, lastAssoc, null);
8✔
1121
                                }
1122
                                ensureImplementedInterfaces(bnd, fmx, (TWithTypes) owner, lastAssoc);
7✔
1123
                        }
1124
                }
1125

1126
                return fmx;
2✔
1127
        }
1128

1129
        /**
1130
         * Returns a Famix Exception associated with the ITypeBinding.
1131
         * The Entity is created if it does not exist.
1132
         * @param name -- the name of the Famix Exception
1133
         * @param owner -- type defining the Exception (should not be null, but it will work if it is) 
1134
         *
1135
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
1136
         */
1137
        public <T extends TWithTypes & TNamedEntity> Exception ensureFamixException(ITypeBinding bnd, String name, TWithTypes owner, boolean isGeneric, int modifiers) {
1138
                Exception fmx;
1139

1140
                // --------------- some special cases
1141
                if (bnd != null) {
2✔
1142
                        if (bnd.isArray()) {
3!
1143
                                bnd = bnd.getElementType();
×
1144
                        }
1145

1146
                        // for inner classes defined in generics !!! For others should not change anything
1147
                        bnd = bnd.getErasure();
3✔
1148
                }
1149

1150
                // ---------------- to avoid useless computations if we can
1151
                fmx = (Exception) getEntityByKey(bnd);
5✔
1152
                if (fmx != null) {
2✔
1153
                        return fmx;
2✔
1154
                }
1155

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

1179
                // --------------- owner
1180
                if (owner == null) {
2✔
1181
                        if (bnd == null) {
2✔
1182
                                owner = ensureFamixPackageDefault();
4✔
1183
                        } else {
1184
                                owner = (TWithTypes) ensureOwner(bnd);
5✔
1185
                        }
1186
                }
1187

1188
                // --------------- recover from name ?
1189
                for (Exception candidate : this.getEntityByName(Exception.class, name)) {
13✔
1190
                        if (matchAndMapClass(bnd, name, (T) owner, candidate)) {
8!
1191
                                fmx = candidate;
×
1192
                                break;
×
1193
                        }
1194
                }
1✔
1195

1196
                // ---------------- create
1197
                if (fmx == null) {
2!
1198
                        fmx = ensureFamixEntity(Exception.class, bnd, name);
7✔
1199
                        fmx.setTypeContainer(owner);
3✔
1200
                }
1201

1202
        // we just created it or it was not bound, so we make sure it has the right information in it
1203
        TAssociation lastAssoc = null;
2✔
1204
        if (bnd != null) {
2✔
1205
            ITypeBinding supbnd = bnd.getSuperclass();
3✔
1206
            if (supbnd != null) {
2!
1207
                lastAssoc = ensureFamixInheritance((TWithInheritances) ensureFamixType(supbnd), fmx, lastAssoc, supbnd);
11✔
1208
            }
1209
            else {
1210
                lastAssoc = ensureFamixInheritance(ensureFamixClassObject(), fmx, lastAssoc, null);
×
1211
            }
1212
            ensureImplementedInterfaces(bnd, fmx, owner, lastAssoc);
6✔
1213
        }
1214

1215
        return fmx;
2✔
1216
        }
1217

1218
        /**
1219
         * Returns a FAMIX Interface with the given <b>name</b>, creating it if it does not exist yet.
1220
         * @param name -- the name of the FAMIX Method
1221
         * @param owner -- type defining the method (should not be null, but it will work if it is) 
1222
         * @return the FAMIX Class or null in case of a FAMIX error
1223
         */
1224
        public <T extends TWithTypes & TNamedEntity> Interface ensureFamixInterface(ITypeBinding bnd, String name, TWithTypes owner, boolean isGeneric, int modifiers) {
1225
                Interface fmx;
1226

1227
                // --------------- some special cases
1228
                if (bnd != null) {
2!
1229
                        if (bnd.isArray()) {
3!
1230
                                bnd = bnd.getElementType();
×
1231
                        }
1232

1233
                        // for inner classes defined in generics !!! For others should not change anything
1234
                        bnd = bnd.getErasure();
3✔
1235
                }
1236

1237
                // ---------------- to avoid useless computations if we can
1238
                fmx = (Interface) getEntityByKey(bnd);
5✔
1239
                if (fmx != null) {
2✔
1240
                        return fmx;
2✔
1241
                }
1242

1243
                // --------------- name
1244
                if (name == null) {
2✔
1245
                        if (bnd == null) {
2!
1246
                                return null;  // not much we can do
×
1247
                        } else if (!bnd.isAnonymous()) {
3!
1248
                                name = bnd.getErasure().getName();  // for generics, will give the "core" type name, for normal type, won't change anything
5✔
1249
                        } else { // anonymous class
1250
                                if (bnd.getSuperclass() != null) {
×
1251
                                        name = bnd.getSuperclass().getName();
×
1252
                                }
1253
                                if ((name == null) || name.equals(OBJECT_NAME)) {
×
1254
                                        ITypeBinding[] intfcs = bnd.getInterfaces();
×
1255
                                        if ((intfcs != null) && (intfcs.length > 0)) {
×
1256
                                                name = bnd.getInterfaces()[0].getName();
×
1257
                                        }
1258
                                        else {
1259
                                                name = "???";
×
1260
                                        }
1261
                                }
1262
                                name = ANONYMOUS_NAME_PREFIX + "(" + name + ")";
×
1263
                        }
1264
                }
1265

1266
                // --------------- owner
1267
                if (owner == null) {
2✔
1268
                        if (bnd == null) {
2!
1269
                                owner = ensureFamixPackageDefault();
×
1270
                        } else {
1271
                                owner = (TWithTypes) ensureOwner(bnd);
5✔
1272
                        }
1273
                }
1274

1275
                // --------------- recover from name ?
1276
                for (Interface candidate : this.getEntityByName(Interface.class, name)) {
13✔
1277
                        if (matchAndMapInterface(bnd, name, (T) owner, candidate)) {
8✔
1278
                                fmx = candidate;
2✔
1279
                                break;
1✔
1280
                        }
1281
                }
1✔
1282

1283
                // ---------------- create
1284
                if (fmx == null) {
2✔
1285
                        if (isGeneric) {
2✔
1286
                                fmx = ensureFamixParametricInterface(bnd, name, owner);
7✔
1287
                        }
1288
                        else {
1289
                                fmx = ensureFamixEntity(Interface.class, bnd, name);
7✔
1290
                                fmx.setTypeContainer(owner);
3✔
1291
                        }
1292
                }
1293

1294
                // ---------------- modifiers and "super interfaces"
1295
                if (fmx!=null) {
2!
1296
                        // we just created it or it was not bound, so we make sure it has the right information in it
1297
                        if (bnd != null) {
2!
1298
                                setInterfaceModifiers(fmx, bnd.getModifiers());
5✔
1299
                        }
1300
                        TAssociation lastAssociation = null;
2✔
1301
                        if (bnd != null) {
2!
1302
                                ensureImplementedInterfaces(bnd, fmx, owner, lastAssociation);
6✔
1303
                        }
1304
                }
1305
                return fmx;
2✔
1306
        }
1307

1308
        /**
1309
         * "Converts" (if needed) a TTYpe entity to be a TThrowable. Might involve removing the existing entity, recreating a new one and migrating
1310
         * all the relationship of the former to the later
1311
         */
1312
        public TThrowable asException(TType fmxType) {
1313
                if (fmxType instanceof Exception) {
3✔
1314
                        return (Exception) fmxType;
3✔
1315
                }
1316
                if(fmxType instanceof TypeParameter) {
3✔
1317
                        return (TypeParameter) fmxType;
3✔
1318
                }
1319

1320
                Exception fmxException = null;
2✔
1321
                IBinding key;
1322

1323
                try {
1324
                        key = entityToKey.get(fmxType);
6✔
1325

1326
                        /* Remove entity immediately so that its key and name are not "reassigned" in the various cache dictionaries
1327
                         * the object still exists and its properties are still accessible */
1328
                        removeEntity((NamedEntity) fmxType);
4✔
1329

1330
                        TWithTypes owner = fmxType.getTypeContainer();
3✔
1331
                        fmxType.setTypeContainer(null);
3✔
1332
                        fmxException = ensureFamixException((ITypeBinding) key, fmxType.getName(), owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
10✔
1333

1334
                        fmxException.addMethods( new ArrayList<>( ((TWithMethods)fmxType).getMethods() ) );
8✔
1335
                        if (fmxType instanceof TWithAttributes) {
3!
1336
                                fmxException.addAttributes( new ArrayList<>( ((TWithAttributes)fmxType).getAttributes() ) );
8✔
1337
                        }
1338

1339
                        if (fmxType instanceof TWithInheritances) {
3!
1340
                                fmxException.addSuperInheritances( new ArrayList<>( ((TWithInheritances) fmxType).getSuperInheritances() ) );
8✔
1341
                                fmxException.addSubInheritances( new ArrayList<>( ((TWithInheritances) fmxType).getSubInheritances() ) );
8✔
1342
                        }
1343
                        fmxException.setComments(new ArrayList<>( ((TWithComments) fmxType).getComments() ));
8✔
1344
                        fmxException.setSourceAnchor(fmxType.getSourceAnchor());
4✔
1345
                        fmxException.addIncomingTypings( new ArrayList<>( fmxType.getIncomingTypings() ) );
7✔
1346
                        fmxException.addAnnotationInstances( new ArrayList<>( ((NamedEntity)fmxType).getAnnotationInstances() ) );
8✔
1347
                        fmxException.addIncomingReferences( new ArrayList<>( fmxType.getIncomingReferences() ) );
7✔
1348
                        fmxException.setIsStub(fmxType.getIsStub());
4✔
1349
                        fmxException.addTypes( new ArrayList<>( ((ContainerEntity) fmxType).getTypes() ) );
8✔
1350
                }
1351
                catch( ConcurrentModificationException e) {
×
1352
                        e.printStackTrace();
×
1353
                }
1✔
1354

1355
                return fmxException;
2✔
1356
        }
1357

1358
        /**
1359
         * helper method, we know the type exists, ensureFamixClass will recover it
1360
         */
1361
        public Class getFamixClass(ITypeBinding bnd, String name, TNamedEntity owner) {
1362
                return ensureFamixClass(bnd, name, owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
8✔
1363
        }
1364

1365
        /**
1366
         * helper method, we know the type exists, ensureFamixInterface will recover it
1367
         */
1368
        public Interface getFamixInterface(ITypeBinding bnd, String name, ContainerEntity owner) {
1369
                return ensureFamixInterface(bnd, name, owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
8✔
1370
        }
1371

1372
        /**
1373
         * helper method, we know the type exists, ensureFamixInterface will recover it
1374
         */
1375
        public Exception getFamixException(ITypeBinding bnd, String name, TWithTypes owner) {
1376
                return ensureFamixException(bnd, name, owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
8✔
1377
        }
1378

1379
        /**
1380
         * Ensures a famix entity for the owner of a binding.<br>
1381
         * This owner can be a method, a class or a namespace
1382
         * @param bnd -- binding for the owned entity
1383
         * @return a famix entity for the owner
1384
         */
1385
        private TNamedEntity ensureOwner(ITypeBinding bnd) {
1386
                TNamedEntity owner;
1387
                IMethodBinding parentMtd = bnd.getDeclaringMethod();
3✔
1388
                if (parentMtd != null) {
2✔
1389
                        owner = this.ensureFamixMethod(parentMtd);  // cast needed to desambiguate the call
5✔
1390
                }
1391
                else {
1392
                        ITypeBinding parentClass = bnd.getDeclaringClass();
3✔
1393
                        if (parentClass != null) {
2✔
1394
                owner = this.ensureFamixType(parentClass);
5✔
1395
            }
1396
                        else {
1397
                                IPackageBinding parentPckg = bnd.getPackage();
3✔
1398
                                if (parentPckg != null) {
2!
1399
                                        owner = this.ensureFamixPackage(parentPckg, null);
6✔
1400
                                } else {
1401
                                        owner = this.ensureFamixPackageDefault();
×
1402
                                }
1403
                        }
1404
                }
1405
                return owner;
2✔
1406
        }
1407

1408

1409
        /**
1410
         * Returns a FAMIX PrimitiveType with the given <b>name</b>, creating it if it does not exist yet
1411
         * We assume that PrimitiveType must be uniq for a given name
1412
         * @param name -- the name of the FAMIX PrimitiveType
1413
         * @return the FAMIX PrimitiveType or null in case of a FAMIX error
1414
         */
1415
        public PrimitiveType ensureFamixPrimitiveType(ITypeBinding bnd, String name) {
1416
                if (name == null) {
2✔
1417
                        if (bnd == null) {
2!
1418
                                return null;
×
1419
                        } else {
1420
                                name = bnd.getName();
3✔
1421
                        }
1422
                }
1423
                return ensureFamixUniqEntity(PrimitiveType.class, bnd, name);
7✔
1424
        }
1425

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

1429
                // --------------- to avoid useless computations if we can
1430
                fmx = (org.moosetechnology.model.famix.famixjavaentities.Enum) getEntityByKey(bnd);
5✔
1431
                if (fmx != null) {
2✔
1432
                        return fmx;
2✔
1433
                }
1434

1435
                // --------------- name
1436
                if (name == null) {
2✔
1437
                        if (bnd == null) {
2!
1438
                                return null;
×
1439
                        }
1440
                        else {
1441
                                name = bnd.getName();
3✔
1442
                        }
1443
                }
1444

1445
                // --------------- owner
1446
                if (owner == null) {
2✔
1447
                        if (bnd == null) {
2!
1448
                                owner = ensureFamixPackageDefault();  // not really sure what to do here
×
1449
                        } else {
1450
                                owner = (TWithTypes) ensureOwner(bnd);
5✔
1451
                        }
1452
                }
1453

1454
                // --------------- recover from name ?
1455
                for (org.moosetechnology.model.famix.famixjavaentities.Enum candidate : getEntityByName(org.moosetechnology.model.famix.famixjavaentities.Enum.class, name)) {
9!
1456
                        if (matchAndMapType(bnd, name, (T) owner, candidate)) {
×
1457
                                fmx = candidate;
×
1458
                                break;
×
1459
                        }
1460
                }
×
1461

1462
                if (fmx == null) {
2!
1463
                        fmx = ensureFamixEntity(Enum.class, bnd, name);
7✔
1464
                        fmx.setTypeContainer(owner);
3✔
1465
                }
1466

1467
                if (bnd != null) {
2!
1468
                        setVisibility(fmx, bnd.getModifiers());
5✔
1469
                }
1470

1471
                return fmx;
2✔
1472
        }
1473

1474
        /**
1475
         * helper method, we know the type exists, ensureFamixEnum will recover it
1476
         */
1477
        public org.moosetechnology.model.famix.famixjavaentities.Enum getFamixEnum(ITypeBinding bnd, String name, TWithTypes owner) {
1478
                return ensureFamixEnum(bnd, name, owner);
6✔
1479
        }
1480

1481
        public EnumValue ensureFamixEnumValue(IVariableBinding bnd,        String name, Enum owner) {
1482
                EnumValue fmx;
1483

1484
                // --------------- to avoid useless computations if we can
1485
                fmx = (EnumValue)getEntityByKey(bnd);
5✔
1486
                if (fmx != null) {
2✔
1487
                        return fmx;
2✔
1488
                }
1489

1490
                // --------------- name
1491
                if (name == null) {
2!
1492
                        if (bnd == null) {
×
1493
                                return null;
×
1494
                        }
1495
                        else {
1496
                                name = bnd.getName();
×
1497
                        }
1498
                }
1499

1500
                // --------------- owner
1501
                if (owner == null) {
2✔
1502
                        if (bnd == null) {
2!
1503
                                return null;  // what would be the interest of creating an EnumValue without a declaring Enum type?
×
1504
                        }
1505
                        else {
1506
                                owner = ensureFamixEnum(bnd.getDeclaringClass(), null, null);
7✔
1507
                        }
1508
                }
1509

1510
                // --------------- recover from name ?
1511
                for (EnumValue candidate : getEntityByName(EnumValue.class, name) ) {
9!
1512
                        if ( matchAndMapVariable(bnd, name, owner, candidate) ) {
×
1513
                                fmx = candidate;
×
1514
                                break;
×
1515
                        }
1516
                }
×
1517
                if (fmx == null) {
2!
1518
                        fmx = ensureFamixEntity(EnumValue.class, bnd, name);
7✔
1519
                        fmx.setParentEnum(owner);
3✔
1520
                }
1521

1522
        fmx.setParentEnum(owner);
3✔
1523

1524
        return fmx;
2✔
1525
        }
1526

1527
    /**
1528
         * e.g. see {@link EntityDictionary#ensureFamixClass}
1529
         */
1530
        public AnnotationType ensureFamixAnnotationType(ITypeBinding bnd, String name, ContainerEntity owner) {
1531
                AnnotationType fmx;
1532

1533
                // --------------- to avoid useless computations if we can
1534
                fmx = (AnnotationType)getEntityByKey(bnd);
5✔
1535
                if (fmx != null) {
2✔
1536
                        return fmx;
2✔
1537
                }
1538

1539
                // --------------- name
1540
                if (name == null) {
2✔
1541
                        if (bnd == null) {
2!
1542
                                return null;
×
1543
                        }
1544
                        else {
1545
                                name = bnd.getName();
3✔
1546
                        }
1547
                }
1548

1549
                // --------------- owner
1550
                if (owner == null) {
2✔
1551
                        if (bnd == null) {
2!
1552
                                owner = ensureFamixPackageDefault();
×
1553
                        }
1554
                        else {
1555
                                IPackageBinding parentPckg = bnd.getPackage();
3✔
1556
                                if (parentPckg != null) {
2!
1557
                                        owner = this.ensureFamixPackage(parentPckg, null);
6✔
1558
                                } else {
1559
                                        owner = this.ensureFamixPackageDefault();
×
1560
                                }
1561
                        }
1562
                }
1563

1564
                // --------------- recover from name ?
1565
                for (AnnotationType candidate : getEntityByName(AnnotationType.class, name) ) {
13✔
1566
                        if ( matchAndMapType(bnd, name, owner, candidate) ) {
7✔
1567
                                fmx = candidate;
2✔
1568
                                break;
1✔
1569
                        }
1570
                }
1✔
1571

1572
                // --------------- create
1573
                if (fmx == null) {
2✔
1574
                        fmx = ensureFamixEntity(AnnotationType.class, bnd, name);
7✔
1575
                        fmx.setAnnotationTypesContainer(owner);
3✔
1576
                }
1577

1578
                if (bnd != null) {
2!
1579
                        // Not supported in Famix
1580

1581
                        // setVisibility(fmx, bnd.getModifiers());
1582
                }
1583

1584
                return fmx;
2✔
1585
        }
1586

1587
        /**
1588
         * helper method, we know the type exists, ensureFamixAnnotationType will recover it
1589
         */
1590
        public AnnotationType getFamixAnnotationType(ITypeBinding bnd, String name, ContainerEntity owner) {
1591
                return ensureFamixAnnotationType(bnd, name, owner);
6✔
1592
        }
1593

1594
        public AnnotationTypeAttribute ensureFamixAnnotationTypeAttribute(IMethodBinding bnd, String name, AnnotationType owner) {
1595
                AnnotationTypeAttribute fmx = null;
2✔
1596

1597
                // --------------- to avoid useless computations if we can
1598
                fmx = (AnnotationTypeAttribute)getEntityByKey(bnd);
5✔
1599
                if (fmx != null) {
2✔
1600
                        return fmx;
2✔
1601
                }
1602

1603
                // --------------- name
1604
                if (name == null) {
2!
1605
                        if (bnd == null) {
×
1606
                                return null;
×
1607
                        }
1608
                        else {
1609
                                name = bnd.getName();
×
1610
                        }
1611
                }
1612

1613
                // --------------- owner
1614
                if (owner == null) {
2!
1615
                        if (bnd == null) {
×
1616
                                return null;  // what would be the use of an AnnotationTypeAttribute without AnnotationType ?
×
1617
                        }
1618
                        else {
1619
                                ITypeBinding parentType = bnd.getDeclaringClass();
×
1620
                                if (parentType != null) {
×
1621
                                        owner = this.ensureFamixAnnotationType(parentType, null, null);
×
1622
                                }
1623
                                else  {
1624
                                        return null;  // what would be the use of an AnnotationTypeAttribute without AnnotationType ?
×
1625
                                }
1626
                        }
1627
                }
1628

1629
                // --------------- recover from name ?
1630
                for (AnnotationTypeAttribute candidate : getEntityByName(AnnotationTypeAttribute.class, name) ) {
13✔
1631
                        // JDT treats annotation type attributes as methods ...
1632
                        // checkAndMapMethod wants a signature as 2nd argument so we add empty param list
1633
                        if ( (bnd != null) && matchAndMapMethod(bnd, name+"()", null, owner, candidate) ) {
11!
1634
                                fmx = candidate;
×
1635
                                break;
×
1636
                        }
1637
                        // if the binding is null, the annotationTypeAttribute migth have been created
1638
                        else if ( (bnd == null) && matchAndMapVariable(null, name, owner, candidate)) {
2!
1639
                                fmx = candidate;
×
1640
                                break;
×
1641
                        }
1642
                }
1✔
1643

1644
                if (fmx == null) {
2!
1645
                        fmx = ensureFamixEntity(AnnotationTypeAttribute.class, bnd, name);
7✔
1646
                        fmx.setParentType(owner);
3✔
1647
                }
1648

1649
                if (bnd != null) {
2!
1650
                        // Not suopp
1651

1652
                        // setVisibility(fmx, bnd.getModifiers());
1653
                }
1654

1655
                return fmx;
2✔
1656
        }
1657

1658
        /**
1659
         * helper method, we know the attribute exists, ensureFamixAnnotationTypeAttribute will recover it
1660
         */
1661
        public AnnotationTypeAttribute getFamixAnnotationTypeAttribute(IMethodBinding bnd, String name, AnnotationType owner) {
1662
                return ensureFamixAnnotationTypeAttribute( bnd, name, owner);
6✔
1663
        }
1664
        
1665
        
1666
        /**
1667
         * Returns a FAMIX Wildcard with its bounds
1668
         * @param bnd
1669
         * @param name
1670
         * @param owner
1671
         * @return
1672
         */
1673
        public Wildcard ensureFamixWildcardType(ITypeBinding bnd, String name, TParametricEntity owner, TWithTypes ctxt) {
1674
                Wildcard fmx = this.ensureFamixEntity(Wildcard.class, bnd, bnd.getName());
8✔
1675
                if(bnd.getBound() != null) {
3✔
1676
                        Type bound = this.ensureFamixType(bnd.getBound());
5✔
1677
                        if(bnd.isUpperbound()) {
3✔
1678
                                fmx.setUpperBound(bound);
3✔
1679
                                bound.addUpperBoundedWildcards(fmx);
4✔
1680
                        }else{
1681
                                fmx.setLowerBound(bound);
3✔
1682
                                bound.addLowerBoundedWildcards(fmx);
3✔
1683
                        }
1684
                }
1685
                return fmx;
2✔
1686
        }
1687

1688
        /**
1689
         * Returns a Famix TypeParameter (created by a Famix ParametricEntity) with the given <b>name</b>, creating it if it does not exist yet
1690
         * In the second case, sets some default properties: not Abstract, not Final, not Private, not Protected, not Public
1691
         * @param name -- the name of the Famix TypeParameter
1692
         * @return the Famix TypeParameter or null in case of a Famix error
1693
         */
1694
        public TypeParameter ensureFamixTypeParameter(ITypeBinding bnd,        String name, TWithTypes owner) {
1695
                TypeParameter fmx;
1696

1697
                // --------------- to avoid useless computations if we can
1698
                fmx = (TypeParameter)getEntityByKey(bnd);
5✔
1699
                if (fmx != null) {
2✔
1700
                        return fmx;
2✔
1701
                }
1702

1703
                // --------------- name
1704
                if (name == null) {
2✔
1705
                        if (bnd == null) {
2!
1706
                                return null;
×
1707
                        }
1708
                        else {
1709
                                name = bnd.getName();
3✔
1710
                        }
1711
                }
1712

1713
                // --------------- owner
1714
                if (owner == null && bnd != null) {
2!
1715
            if (bnd.getDeclaringClass() != null) {
×
1716
                owner = this.ensureFamixType(bnd.getDeclaringClass());
×
1717
            } else if(bnd.getDeclaringMethod() != null) {
×
1718
                owner = this.ensureFamixMethod(bnd.getDeclaringMethod());
×
1719
            }
1720
                }
1721

1722
                // --------------- recover from name ?
1723
                for (Type candidate : this.getEntityByName(Type.class, name)) {
13✔
1724
                        if ( matchAndMapType(bnd, name, (ContainerEntity) owner, candidate) ) {
8✔
1725
                                fmx = (TypeParameter) candidate;
3✔
1726
                                break;
1✔
1727
                        }
1728
                }
1✔
1729

1730
                // --------------- create
1731
                if (fmx == null) {
2✔
1732
                        fmx = ensureFamixEntity(TypeParameter.class, bnd, name);
7✔
1733
                        if(bnd != null && bnd.getSuperclass() != null) {
5!
1734
                                Type upperBound = ensureFamixType(bnd.getSuperclass());
5✔
1735
                                fmx.setUpperBound(upperBound);
3✔
1736
                        }
1737
                        if(bnd != null) {
2✔
1738
                for (ITypeBinding intbnd : bnd.getInterfaces()) {
17✔
1739
                    Type upperBound = ensureFamixType(intbnd);
4✔
1740
                    fmx.setUpperBound(upperBound);
3✔
1741
                }
1742
            }
1743
                        fmx.setTypeContainer(owner);
3✔
1744
                }
1745

1746
                return fmx;
2✔
1747
        }
1748

1749
        /**
1750
         * Checks whether the existing unmapped Famix Namespace matches the binding.
1751
         * Checks that the candidate has the same name as the JDT bound package, and checks recursively that owners also match.
1752
         *
1753
         * @param bnd       -- a JDT binding that we are trying to match to the candidate
1754
         * @param name      of the package
1755
         * @param owner     of the package
1756
         * @param candidate -- a Famix Entity
1757
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1758
         */
1759
        private boolean matchAndMapPackage(IPackageBinding bnd, String name, Package owner, NamedEntity candidate) {
1760
                if (!(candidate instanceof Package)) {
3!
1761
                        return false;
×
1762
                }
1763

1764
                // check whether bnd and candidate are already bound
1765
                CheckResult res = checkKeyMatch(bnd, candidate);
5✔
1766
                if (res == CheckResult.MATCH) {
3✔
1767
                        return true;
2✔
1768
                } else if (res == CheckResult.FAIL) {
3!
1769
                        return false;
×
1770
                }
1771

1772
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7✔
1773
                        return false;
2✔
1774
                }
1775

1776
                // names match, not need to look at owner because names of Namespaces are their fully qualified name
1777
                conditionalMapToKey(bnd, candidate);
4✔
1778
                return true;
2✔
1779
        }
1780

1781
        /**
1782
         * Checks whether the existing unmapped Famix Type matches the binding.
1783
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
1784
         * We also check that the actual class of the candidate matches (can be a sub-class of FamixType).
1785
         * @param bnd -- a JDT binding that we are trying to match to the candidate
1786
         * @param name of the type
1787
         * @param owner of the type
1788
         * @param candidate -- a Famix NamedEntity (Class, Type, PrimitiveType, Enum, AnnotationType)
1789
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1790
         */
1791
        private <T extends TWithTypes & TNamedEntity> boolean matchAndMapType(ITypeBinding bnd, String name, TNamedEntity owner, TNamedEntity candidate) {
1792
                if (! (candidate instanceof Type) ) {
3!
1793
                        return false;
×
1794
                }
1795

1796
                // check whether bnd and candidate are already bound
1797
                CheckResult res = checkKeyMatch(bnd, candidate);
5✔
1798
                if (res == CheckResult.MATCH) {
3✔
1799
                        return true;
2✔
1800
                }
1801
                else if (res == CheckResult.FAIL) {
3✔
1802
                        return false;
2✔
1803
                }
1804

1805
                if ( (bnd != null) && (bnd.isArray()) ) {
5!
1806
                                bnd = bnd.getElementType();
×
1807
                }
1808

1809
                // checking names
1810
                if ( (bnd != null) && (bnd.isParameterizedType() || bnd.isRawType()) ) {
8!
1811
                        name = bnd.getErasure().getName();
×
1812
                }
1813
                else if (bnd != null) {
2✔
1814
                        name = bnd.getName();
3✔
1815
                }
1816
                // else name = name
1817
                if (checkNameMatch(null, name, candidate) == CheckResult.FAIL) {
7✔
1818
                        return false;
2✔
1819
                }
1820

1821
                // special case of primitive types
1822
                if (candidate instanceof PrimitiveType) {
3!
1823
                        if ( (bnd != null) && bnd.isPrimitive() ) {
×
1824
                                // names are equal so it's OK
1825
                                conditionalMapToKey(bnd, candidate);
×
1826
                                return true;
×
1827
                        }
1828
                        else if ( (bnd == null) && (owner == null) ) {
×
1829
                                return true;
×
1830
                        }
1831
                }
1832

1833
                // check owners without bnd
1834
                if (bnd == null) {
2✔
1835
                        return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
7✔
1836
                }
1837

1838
                // check owners with bnd
1839
                // type is an annotation
1840
                if (bnd.isAnnotation() && (candidate instanceof AnnotationType)) {
6!
1841
                        if (matchAndMapPackage(bnd.getPackage(), owner.getName(), (Package) Util.getOwner(owner), Util.getOwner(candidate))) {
13!
1842
                                conditionalMapToKey(bnd, candidate);
4✔
1843
                                return true;
2✔
1844
                        } else {
1845
                                return false;
×
1846
                        }
1847
                }
1848

1849
                // check owners with bnd
1850
                // type is a Parameterized type
1851
                if ((bnd.isParameterizedType() || bnd.isRawType()) && (candidate instanceof ParametricClass)) {
6!
1852
                        return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
×
1853
                }
1854

1855
                // check owners with bnd
1856
                // type is an Enum
1857
                if (bnd.isEnum() && (candidate instanceof Enum)) {
3!
1858
                        return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
×
1859
                }
1860

1861
                // check owners with bnd
1862
                // type is something elae (a class or interface)
1863
                // Annotation are interfaces too, so we should check this one after isAnnotation
1864
                if ( bnd.isClass()) {
3!
1865
                        return matchAndMapClass(bnd, name, owner, (Type) candidate);
×
1866
                }
1867

1868
                if(bnd.isInterface()) {
3!
1869
                        return matchAndMapInterface(bnd, name, owner, (Type) candidate);
×
1870
                }
1871

1872
                return false;
2✔
1873
        }
1874

1875
        /**
1876
         * Checks whether the existing unmapped Famix Class (or Interface) matches the binding.
1877
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
1878
         * @param bnd -- a JDT binding that we are trying to match to the candidate
1879
         * @param name of the class
1880
         * @param owner of the class
1881
         * @param candidate -- a Famix Entity
1882
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1883
         */
1884
        private boolean matchAndMapClass(ITypeBinding bnd, String name, TNamedEntity owner, TType candidate) {
1885
                if (!(candidate instanceof Class)) {
3!
1886
                        return false;
×
1887
                }
1888

1889
                // check whether bnd and candidate are already bound
1890
                CheckResult res = checkKeyMatch(bnd, candidate);
5✔
1891
                if (res == CheckResult.MATCH) {
3!
1892
                        return true;
×
1893
                } else if (res == CheckResult.FAIL) {
3✔
1894
                        return false;
2✔
1895
                }
1896

1897
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7!
1898
                        return false;
×
1899
                }
1900

1901
                // checking owner
1902
                return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
7✔
1903
        }
1904

1905
        /**
1906
         * Checks whether the existing unmapped Famix Class (or Interface) matches the binding.
1907
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
1908
         * @param bnd -- a JDT binding that we are trying to match to the candidate
1909
         * @param name of the class
1910
         * @param owner of the class
1911
         * @param candidate -- a Famix Entity
1912
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1913
         */
1914
        private boolean matchAndMapInterface(ITypeBinding bnd, String name, TNamedEntity owner, Type candidate) {
1915
                if (!(candidate instanceof Interface)) {
3!
1916
                        return false;
×
1917
                }
1918

1919
                // check whether bnd and candidate are already bound
1920
                CheckResult res = checkKeyMatch(bnd, candidate);
5✔
1921
                if (res == CheckResult.MATCH) {
3!
1922
                        return true;
×
1923
                } else if (res == CheckResult.FAIL) {
3✔
1924
                        return false;
2✔
1925
                }
1926

1927
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7!
1928
                        return false;
×
1929
                }
1930

1931
                // checking owner
1932
                return matchAndMapTypeOwner(bnd, owner, candidate);
6✔
1933
        }
1934

1935
        /**
1936
         * Checks whether the existing unmapped Famix "Method" matches the binding.
1937
         * Checks that the candidate has the same name and same signature as the JDT bound method, and checks recursively that owners also match.
1938
         * Note that AnnotationTypeAttribute are treated as methods by JDT, so they are checked here.
1939
         * @param bnd -- a JDT binding that we are trying to match to the candidate
1940
         * @param sig -- signature of the method
1941
         * @param retTyp -- return type of the method
1942
         * @param owner of the method
1943
         * @param candidate -- a Famix Entity (regular Method or AnnotationTypeAttribute)
1944
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1945
         */
1946
        private  boolean matchAndMapMethod(IMethodBinding bnd, String sig, TType retTyp, TNamedEntity owner, NamedEntity candidate) {
1947
                if (! (candidate instanceof Method) ) {
3✔
1948
                        return false;
2✔
1949
                }
1950

1951
                // check whether bnd and candidate are already bound
1952
                CheckResult res = checkKeyMatch(bnd, candidate);
5✔
1953
                if (res == CheckResult.MATCH) {
3!
1954
                        return true;
×
1955
                }
1956
                else if (res == CheckResult.FAIL) {
3✔
1957
                        return false;
2✔
1958
                }
1959

1960
                // checking names
1961
                String name = (sig != null) ? sig.substring(0, sig.indexOf('(')) : null;
10!
1962
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7!
1963
                        return false;
×
1964
                }
1965

1966
                // for methods, the name is not enough, we must test the signature also
1967
                // but not for AnnotationTypeAttribute
1968

1969
                        if (bnd != null) {
2✔
1970
                                sig = bnd.getName() + "(" + signatureParamsFromBinding(bnd) + ")";
7✔
1971
                        }
1972
                        if (! ((Method) candidate).getSignature().equals(sig)) {
6✔
1973
                                return false;
2✔
1974
                        }
1975

1976
                        // and still for method, must also check the return type
1977
                        if (bnd != null) {
2✔
1978
                                if (bnd.isConstructor()) {
3✔
1979
                                        if ( ((Method) candidate).getDeclaredType() != null ) {
4!
1980
                                                return false;
×
1981
                                        }
1982
                                        // else OK for now
1983
                                }
1984
                                else { // not a constructor
1985
                                        if ( ((Method) candidate).getDeclaredType() == null ) {
4!
1986
                                                return false;
×
1987
                                        }
1988
                                        else if (! matchAndMapType(bnd.getReturnType(), null, null, ((Method) candidate).getDeclaredType()) ) {
10!
1989
                                                return false;
×
1990
                                        }
1991
                                        // else OK for now
1992
                                }
1993
                        }
1994
                        else {  // bnd == null
1995
                                if (retTyp == null) { // similar to (bnd.isConstructor())
2!
1996
                                        if ( ((Method) candidate).getDeclaredType() != null ) {
4✔
1997
                                                return false;
2✔
1998
                                        }
1999
                                        // else OK for now
2000
                                } else { // (ret != null)  i.e. not a constructor
2001
                                        if (((Method) candidate).getDeclaredType() == null) {
×
2002
                                                return false;
×
2003
                                        } else if (!matchAndMapType(null, retTyp.getName(), Util.getOwner(retTyp), (NamedEntity) ((Method) candidate).getDeclaredType())) {
×
2004
                                                return false;
×
2005
                                        }
2006
                                        // else OK for now
2007
                                }
2008
                        }
2009

2010

2011
                // check owner
2012
                if (matchAndMapOwnerAsType(((bnd != null) ? bnd.getDeclaringClass() : null), owner, Util.getOwner(candidate)) == CheckResult.MATCH) {
13✔
2013
                        conditionalMapToKey(bnd, candidate);
4✔
2014
                        return true;
2✔
2015
                } else {
2016
                        return false;
2✔
2017
                }
2018
        }
2019

2020
        /**
2021
         * Checks whether the candidate (an existing unmapped Famix "Variable" like Attribute, Parameter, ...) matches the binding.
2022
         * Checks that the candidate has the same name as the JDT bound variable, and checks recursively that owners also match.
2023
         * The Famix candidate is a NamedEntity and not a StructuralEntity to allow dealing with Famix EnumValue that JDT treats as variables
2024
         * @param bnd -- a JDT binding that we are trying to match to the candidate
2025
         * @param name of the variable
2026
         * @param owner of the variable
2027
         * @param candidate -- a Famix Entity (a StructuralEntity or an EnumValue)
2028
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
2029
         */
2030
        private boolean matchAndMapVariable(IVariableBinding bnd, String name, TNamedEntity owner, TNamedEntity candidate) {
2031
                if (!(candidate instanceof TStructuralEntity)) {
3!
2032
                        return false;
×
2033
                }
2034

2035
                // check whether bnd and candidate are already bound
2036
                CheckResult keyMatch = checkKeyMatch(bnd, candidate);
5✔
2037
                if (keyMatch == CheckResult.MATCH) {
3!
2038
                        return true;
×
2039
                } else if (keyMatch == CheckResult.FAIL) {
3✔
2040
                        return false;
2✔
2041
                }
2042

2043
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7!
2044
                        return false;
×
2045
                }
2046

2047
                // check owner
2048
                TNamedEntity candidateOwner = Util.getOwner(candidate);
3✔
2049

2050
                // local variable or parameter ?
2051
                // owner is a Method? (for example in case of an anonymous class)
2052
                CheckResult res = matchAndMapOwnerAsMethod(((bnd != null) ? bnd.getDeclaringMethod() : null), owner, candidateOwner);
11✔
2053
                if (res == CheckResult.FAIL) {
3✔
2054
                        return false;
2✔
2055
                } else if (res == CheckResult.MATCH) {
3!
2056
                        conditionalMapToKey(bnd, candidate);
×
2057
                        return true;
×
2058
                }
2059

2060
                // check owner
2061
                // <anArray>.length field?
2062
                if (name.equals("length")) {
4!
2063
                        boolean isArrayLengthField = ((bnd != null) && (bnd.getDeclaringClass() == null)) ||
×
2064
                                                                                 ((bnd == null) && (owner.getName().equals(EntityDictionary.ARRAYS_NAME)));
×
2065
                        if (isArrayLengthField) {
×
2066
                                if (candidateOwner.getName().equals(EntityDictionary.ARRAYS_NAME)) {
×
2067
                                        conditionalMapToKey(bnd, candidate);
×
2068
                                        return true;
×
2069
                                }
2070
                                else {
2071
                                        return false;
×
2072
                                }
2073
                        }
2074
                }
2075

2076
                // check owner
2077
                // "normal" field?
2078
                res = matchAndMapOwnerAsType( ((bnd != null) ? bnd.getDeclaringClass() : null), owner, candidateOwner);
11✔
2079
                if (res == CheckResult.MATCH) {
3!
2080
                        conditionalMapToKey(bnd, candidate);
4✔
2081
                        return true;
2✔
2082
                }
2083
                return false;
×
2084
        }
2085

2086
        /**
2087
         * Checks whether the existing unmapped Famix Type's parent (or owner) matches the binding's owner.
2088
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
2089
         * @param bnd -- a JDT binding whose owner we are trying to match to the candidate's owner
2090
         * @param owner -- the owner of the type
2091
         * @param candidate -- a Famix Entity
2092
         * @return whether we found a match (if <b>true</b>, the mapping is recorded)
2093
         */
2094
        private boolean matchAndMapTypeOwner(ITypeBinding bnd, TNamedEntity owner, Type candidate) {
2095
                ContainerEntity candidateOwner = Util.getOwner(candidate);
4✔
2096

2097
                // owner is a Method? (for example in case of an anonymous class)
2098
                CheckResult res = matchAndMapOwnerAsMethod(((bnd != null) ? bnd.getDeclaringMethod() : null), owner, candidate);
11✔
2099
                if (res == CheckResult.MATCH) {
3!
2100
                        conditionalMapToKey(bnd, candidate);
×
2101
                        return true;
×
2102
                } else if (res == CheckResult.FAIL) {
3!
2103
                        return false;
×
2104
                }
2105

2106
                // owner is a class ?
2107
                res = matchAndMapOwnerAsType(((bnd != null) ? bnd.getDeclaringClass() : null), owner, candidateOwner);
11✔
2108
                if (res == CheckResult.MATCH) {
3✔
2109
                        conditionalMapToKey(bnd, candidate);
4✔
2110
                        return true;
2✔
2111
                }
2112
                else if (res == CheckResult.FAIL) {
3✔
2113
                        return false;
2✔
2114
                }
2115

2116
                // owner must be a package
2117
                if (matchAndMapOwnerAsNamespace( ((bnd != null)?bnd.getPackage():null), owner, candidateOwner) == CheckResult.MATCH) {
12✔
2118
                        conditionalMapToKey(bnd, candidate);
4✔
2119
                        return true;
2✔
2120
                }
2121
                return false;
2✔
2122
        }
2123

2124
        /**
2125
         * Check whether the owner of candidates is a method macthinf either methBnd or owner
2126
         * @param methBnd
2127
         * @param owner
2128
         * @param candidateOwner
2129
         * @return a {@link CheckResult}
2130
         */
2131
        private  <T extends TNamedEntity> CheckResult matchAndMapOwnerAsMethod(IMethodBinding methBnd, T owner, T candidateOwner) {
2132
                if ((methBnd != null) || (owner instanceof Method)) {
5!
2133
                        if (!(candidateOwner instanceof Method)) {
3!
2134
                                return CheckResult.FAIL;
×
2135
                        }
2136

2137
                        ContainerEntity ownerOwner = (owner != null) ? (ContainerEntity) Util.getOwner(owner) : null;
7!
2138
                        String ownerSig = (owner != null) ? ((Method) owner).getSignature() : null;
7!
2139
                        Type ownerReturn = (owner != null) ? (Type) ((Method) owner).getDeclaredType() : null;
8!
2140

2141
                        if (matchAndMapMethod(methBnd, ownerSig, ownerReturn, ownerOwner, (Method) candidateOwner)) {
9!
2142
                                return CheckResult.MATCH;
×
2143
                        } else {
2144
                                return CheckResult.FAIL;
2✔
2145
                        }
2146
                }
2147
                return CheckResult.UNDECIDED;
2✔
2148
        }
2149

2150
        /**
2151
         * @param typBnd
2152
         * @param owner
2153
         * @param candidateOwner
2154
         * @return a {@link CheckResult}
2155
         */
2156
        private CheckResult matchAndMapOwnerAsType(ITypeBinding typBnd, TNamedEntity owner, TNamedEntity candidateOwner) {
2157
                if ((typBnd != null) || (owner instanceof Type)) {
5✔
2158
                        if (!(candidateOwner instanceof Type)) {
3✔
2159
                                return CheckResult.FAIL;
2✔
2160
                        }
2161

2162
                        TNamedEntity ownerOwner = (owner != null) ? Util.getOwner(owner) : null;
6!
2163
                        String ownerName = (owner != null) ? owner.getName() : null;
6!
2164

2165
                        if (matchAndMapType(typBnd, ownerName, ownerOwner, candidateOwner)) {
7✔
2166
                                return CheckResult.MATCH;
2✔
2167
                        } else {
2168
                                return CheckResult.FAIL;
2✔
2169
                        }
2170
                }
2171
                return CheckResult.UNDECIDED;
2✔
2172
        }
2173

2174
        private CheckResult matchAndMapOwnerAsNamespace(IPackageBinding pckgBnd, TNamedEntity owner, ContainerEntity candidateOwner) {
2175
                if ((pckgBnd != null) || (owner instanceof Package)) {
5!
2176
                        if (!(candidateOwner instanceof Package)) {
3!
2177
                                return CheckResult.FAIL;
×
2178
                        }
2179

2180
                        Package ownerOwner = (owner != null) ? (Package) Util.getOwner(owner) : null;
7!
2181
                        String ownerName = (owner != null) ? owner.getName() : null;
6!
2182

2183
                        if (matchAndMapPackage(pckgBnd, ownerName, ownerOwner, candidateOwner)) {
7✔
2184
                                return CheckResult.MATCH;
2✔
2185
                        } else {
2186
                                return CheckResult.FAIL;
2✔
2187
                        }
2188
                }
2189
                return CheckResult.UNDECIDED;
×
2190
        }
2191

2192
        /**
2193
         * Checks whether the name and the candidate matches the name of the entity (given either by 'bnd' or 'name')<br>
2194
         * 'name' and 'bnd' cannot be null together
2195
         * @param bnd -- binding associated with the entity may be null
2196
         * @param name -- name of the entity may be null
2197
         * @param candidate
2198
         * @return true if names match, false if not
2199
         */
2200
        private CheckResult checkNameMatch(IBinding bnd, String name, TNamedEntity candidate) {
2201
                if ( (bnd != null) && (! bnd.getName().equals(candidate.getName())) ) {
8!
2202
                        return CheckResult.FAIL;
×
2203
                }
2204
                else if ( (bnd == null) && (name != null) && (! name.equals(candidate.getName())) ) {
9!
2205
                        return CheckResult.FAIL;
2✔
2206
                }
2207
                else {
2208
                        return CheckResult.MATCH;
2✔
2209
                }
2210
        }
2211

2212
        /**
2213
         * Check whether key and candidate are already bound together, whether either is bound to something else, or whether none is bound
2214
         * @param key
2215
         * @param candidate
2216
         * @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>
2217
         */
2218
        private CheckResult checkKeyMatch(IBinding key, TNamedEntity candidate) {
2219
                if (key == null) {
2✔
2220
                        return CheckResult.UNDECIDED;
2✔
2221
                }
2222

2223
                NamedEntity bound = (NamedEntity)getEntityByKey(key);
5✔
2224
                if (bound == candidate) {
3✔
2225
                        return CheckResult.MATCH;
2✔
2226
                }
2227
                else if (bound != null) {
2✔
2228
                        return CheckResult.FAIL;
2✔
2229
                }
2230
                else if (getEntityKey(candidate) != null) {
4✔
2231
                        // candidate already bound, and not to this binding
2232
                        return CheckResult.FAIL;
2✔
2233
                }
2234
                else {
2235
                        return CheckResult.UNDECIDED;
2✔
2236
                }
2237
        }
2238

2239
        private void conditionalMapToKey(IBinding bnd, TNamedEntity ent) {
2240
                if (bnd != null) {
2✔
2241
                        mapEntityToKey(bnd, ent);
4✔
2242
                }
2243
        }
1✔
2244

2245
        public Method ensureFamixMethod(IMethodBinding bnd) {
2246
                return ensureFamixMethod(
8✔
2247
                                bnd,
2248
                                /*name*/null,
2249
                                /*paramsType*/null,
2250
                                /*returnType*/null,
2251
                                /*owner*/null,
2252
                                (bnd == null) ? UNKNOWN_MODIFIERS : bnd.getModifiers());
6✔
2253
        }
2254

2255
        /**
2256
         * Returns a Famix Method associated with the IMethodBinding. The Entity is created if it does not exist.
2257
         * The Entity is created if it does not exist.
2258
         * @param name -- the name of the Famix Method (MUST NOT be null, but this is not checked)
2259
         * @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)
2260
         * @param owner -- type defining the method (should not be null, but it will work if it is)
2261
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
2262
         */
2263
        public Method ensureFamixMethod(IMethodBinding bnd, String name, Collection<String> paramTypes, Type ret, TWithMethods owner, int modifiers) {
2264
                Method fmx;
2265
                String signature;
2266
                boolean delayedRetTyp;
2267

2268
                // --------------- to avoid useless computations if we can
2269
                fmx = (Method)getEntityByKey(bnd);
5✔
2270
                if (fmx != null) {
2✔
2271
                        return fmx;
2✔
2272
                }
2273

2274
                // --------------- name
2275
                if (name == null) {
2✔
2276
                        if (bnd == null) {
2✔
2277
                                return null;
2✔
2278
                        }
2279
                        else {
2280
                                name = bnd.getName();
3✔
2281
                        }
2282
                }
2283

2284
                // --------------- signature
2285
                signature = name + "(";
3✔
2286
                 if (bnd != null) {
2✔
2287
                    signature += signatureParamsFromBinding(bnd);
7✔
2288
                }
2289
        else if (paramTypes != null) {
2!
2290
                        signature += signatureParamsFromStringCollection(paramTypes);
7✔
2291
                }
2292
                else {
2293
                        signature += "???";
×
2294
                }
2295
                signature += ")";
3✔
2296

2297
                // --------------- return type
2298
                delayedRetTyp = false;
2✔
2299
                ITypeBinding retTypBnd = null;
2✔
2300
                if (ret == null) {
2!
2301
                        if (bnd != null) {
2✔
2302
                // must create the return type
2303
                // but for method like "<T> T mtd()" where T belongs to mtd and mtd returns T,
2304
                // we need T to create the method and the method to create T ...
2305
                // so we need to test the situation and deal with it
2306
                retTypBnd = bnd.getReturnType();
3✔
2307
                if (retTypBnd != null) {
2✔
2308
                    if (retTypBnd.isArray()) {
3✔
2309
                        retTypBnd = retTypBnd.getElementType();
3✔
2310
                    }
2311
                }
2312

2313
                if ( (retTypBnd != null) && retTypBnd.isTypeVariable() && (retTypBnd.getDeclaringMethod() == bnd) ) {
9✔
2314
                    delayedRetTyp = true;
3✔
2315
                }
2316
                else {
2317
                    ret = this.ensureFamixType(retTypBnd,(TWithTypes) /*ctxt*/owner);
6✔
2318
                }
2319
                        }
2320
                }
2321

2322
                // --------------- owner
2323
                if (owner == null) {
2✔
2324
                        if (bnd == null) {
2✔
2325
                                owner = ensureFamixClassStubOwner();
4✔
2326
                        }
2327
                        else {
2328
                                ITypeBinding classBnd = bnd.getDeclaringClass().getErasure();
4✔
2329
                                if (classBnd != null) {
2!
2330
                                        owner = ensureFamixType(classBnd);
5✔
2331
                                }
2332
                                else {
2333
                                        owner = ensureFamixClassStubOwner();
×
2334
                                }
2335
                        }
2336
                }
2337

2338
                // --------------- recover from name ?
2339
                for (Method candidate : this.getEntityByName(Method.class, name)) {
13✔
2340
                        if (matchAndMapMethod(bnd, signature, ret, (TNamedEntity) owner, candidate)) {
9✔
2341
                                fmx = candidate;
2✔
2342
                                break;
1✔
2343
                        }
2344
                }
1✔
2345

2346
                if (fmx == null) {
2✔
2347
                        if(bnd != null && bnd.isGenericMethod()) {
5✔
2348
                                fmx = ensureFamixEntity(ParametricMethod.class, bnd, name);
7✔
2349
                                for(ITypeBinding param: bnd.getTypeParameters()) {
18✔
2350
                                        TypeParameter fmxParam = this.ensureFamixTypeParameter(param, null, fmx);
6✔
2351
                                        fmxParam.setGenericEntity((ParametricMethod)fmx);
4✔
2352
                                }
2353
                        // Parameterized method binding = when the method is the target of an invocation.
2354
                        } else if (bnd != null && bnd.isParameterizedMethod()) {
5!
2355
                                fmx = this.ensureFamixMethod(bnd.getMethodDeclaration());
×
2356
                        } else {
2357
                if (bnd != null && bnd.isConstructor()) {
5✔
2358
                    fmx = ensureFamixEntity(Initializer.class, bnd, name);
8✔
2359
                } else {
2360
                    fmx = ensureFamixEntity(Method.class, bnd, name);
7✔
2361
                }
2362
            }
2363

2364
                        fmx.setSignature(signature);
3✔
2365
                        ITypeBinding returnTypeBnd = (bnd == null) ? null : bnd.getReturnType();
7✔
2366
                        ensureFamixEntityTyping(returnTypeBnd, fmx, ret);
6✔
2367
                        fmx.setParentType(owner);
3✔
2368
                }
2369

2370
                if (fmx != null) {
2!
2371
                        setMethodModifiers(fmx, modifiers);
4✔
2372
                }
2373

2374
        //If it has the #default keywork, we mark it as default implementation
2375
        if (Modifier.isDefault(modifiers)) {
3✔
2376
            fmx.setKind(DEFAULT_IMPLEMENTATION_KIND_MARKER);
3✔
2377
        }
2378

2379
        if (delayedRetTyp) {
2✔
2380
                        int retTypModifiers = retTypBnd.getModifiers();
3✔
2381
                        ITypeBinding returnTypeBnd = bnd.getReturnType();
3✔
2382
                        ensureFamixEntityTyping(returnTypeBnd, fmx, this.ensureFamixType(retTypBnd, /*name*/null, /*owner*/fmx, /*ctxt*/(ContainerEntity) owner, retTypModifiers));
13✔
2383
                }
2384

2385
                return fmx;
2✔
2386
        }
2387

2388

2389
        /**
2390
         * Creates or recovers the initializer method containing the attribute initializations of a type.
2391
         * @param owner Type containing the initializer
2392
         * @param isStatic Modifier of the initializer. A type can have 2 initializers for attribute initialization: 1 static and 1 not.
2393
         * @param isInitializationBlock True if the entity is an initialization block. False for the artificial method containing all field initializations.
2394
         * @return the FamixInitializer
2395
         */
2396
        public Initializer ensureFamixInitializer(TWithMethods owner, Boolean isStatic, Boolean isInitializationBlock) {
2397
                Initializer fmx = null;
2✔
2398

2399
                if (owner != null) {
2!
2400
                        Optional<TMethod> existingInitializer = owner.getMethods().stream()
6✔
2401
                                        .filter(meth ->
1✔
2402
                                                        ((Method) meth).getIsInitializer() &&
8✔
2403
                                                        ((Method) meth).getIsConstructor().equals(false) &&
7✔
2404
                                                        ((Method) meth).getIsClassSide().equals(isStatic) &&
6✔
2405
                                                        ((Initializer) meth).getIsInitializationBlock().equals(isInitializationBlock))
7✔
2406
                                        .findFirst();
2✔
2407
                        if (existingInitializer.isPresent()) {
3✔
2408
                                fmx = (Initializer) existingInitializer.get();
4✔
2409
                        }
2410
                }
2411

2412
                if (fmx == null) {
2✔
2413
                        fmx = new Initializer();
4✔
2414
                        fmx.setName(INIT_BLOCK_NAME);
3✔
2415
                        fmx.setSignature(INIT_BLOCK_NAME + "()" );
3✔
2416
                        fmx.setVisibility(MODIFIER_PRIVATE);
3✔
2417
                        fmx.setParentType(owner);
3✔
2418
                        fmx.setIsClassSide(isStatic);
3✔
2419
                        fmx.setIsInitializationBlock(isInitializationBlock);
3✔
2420
                }
2421

2422
                return fmx;
2✔
2423
        }
2424

2425

2426
        public Initializer ensureImplicitConstructor(TWithMethods owner, String name, Collection<String> parameterTypesNames) {
2427
                Initializer fmx = null;
2✔
2428

2429
                if (owner != null) {
2!
2430
                        Optional<TMethod> existingInitializer = owner.getMethods().stream()
5✔
2431
                                        .filter(meth ->
1✔
2432
                                                        ((Method) meth).getIsInitializer() &&
8✔
2433
                                                                        ((Method) meth).getIsConstructor()
4✔
2434
                                                                        && meth.getParameters().size() == parameterTypesNames.size()
6✔
2435
                                                                        && (parameterTypesNames.stream().allMatch(parameterName -> meth.getSignature().contains(parameterName))) )
13!
2436
                                        .findFirst();
2✔
2437
                        if (existingInitializer.isPresent()) {
3✔
2438
                                fmx = (Initializer) existingInitializer.get();
4✔
2439
                        }
2440
                }
2441

2442
                if (fmx == null) {
2✔
2443
                        fmx = ensureFamixEntity(Initializer.class, null, name);
7✔
2444
                        fmx.setParentType(owner);
3✔
2445
                        fmx.setSignature(name + "()");
4✔
2446

2447
                }
2448

2449
                return fmx;
2✔
2450
        }
2451

2452
        /**
2453
         * Creates or recovers a stub Famix Method
2454
         * @param name of the method
2455
         * @return the Famix Method
2456
         */
2457
        public Method ensureFamixStubMethod(String name) {
2458
                return ensureFamixMethod(null, name, /*paramType*/null, /*returnType*/null, ensureFamixClassStubOwner(), /*modifiers*/0);
×
2459
        }
2460

2461
        public void setAttributeModifiers(Attribute fmx, int mod) {
2462
                setCommonModifiers(fmx, mod);
4✔
2463
                fmx.setIsTransient(Modifier.isTransient(mod));
5✔
2464
                fmx.setIsVolatile(Modifier.isVolatile(mod));
5✔
2465
        }
1✔
2466

2467
        public void setMethodModifiers(Method fmx, int mod) {
2468
                setCommonModifiers(fmx, mod);
4✔
2469
                fmx.setIsAbstract(Modifier.isAbstract(mod));
5✔
2470
                fmx.setIsSynchronized(Modifier.isSynchronized(mod));
5✔
2471
        }
1✔
2472

2473
        public void setClassModifiers(Class fmx, int mod) {
2474
                setCommonModifiers(fmx, mod);
4✔
2475
                fmx.setIsAbstract(Modifier.isAbstract(mod));
5✔
2476
        }
1✔
2477

2478
        public void setInterfaceModifiers(Interface fmx, int mod) {
2479
                setCommonModifiers(fmx, mod);
4✔
2480
        }
1✔
2481

2482
        private void setCommonModifiers(Entity fmx, int mod) {
2483
                setVisibility((THasVisibility)fmx, mod);
5✔
2484
                ((TCanBeClassSide)fmx).setIsClassSide(Modifier.isStatic(mod));
6✔
2485
                ((TCanBeFinal)fmx).setIsFinal(Modifier.isFinal(mod));
6✔
2486
        }
1✔
2487

2488
        /**
2489
         * Sets the visibility of a FamixNamedEntity
2490
         *
2491
         * @param fmx -- the FamixNamedEntity
2492
         * @param mod -- a description of the modifiers as understood by org.eclipse.jdt.core.dom.Modifier
2493
         */
2494
        public void setVisibility(THasVisibility fmx, int mod) {
2495
                if (Modifier.isPublic(mod)) {
3✔
2496
                        fmx.setVisibility(MODIFIER_PUBLIC);
4✔
2497
                } else if (Modifier.isPrivate(mod)) {
3✔
2498
                        fmx.setVisibility(MODIFIER_PRIVATE);
4✔
2499
                } else if (Modifier.isProtected(mod)) {
3✔
2500
                        fmx.setVisibility(MODIFIER_PROTECTED);
4✔
2501
                } else {
2502
                        fmx.setVisibility(MODIFIER_PACKAGE);
3✔
2503
                }
2504
        }
1✔
2505

2506
        /**
2507
         * Returns a Famix Attribute associated with the IVariableBinding.
2508
         * The Entity is created if it does not exist.<br>
2509
         * @param name -- the name of the FAMIX Attribute (MUST NOT be null, but this is not checked)
2510
         * @param type -- Famix Type of the Attribute (should not be null, but it will work if it is)
2511
         * @param owner -- type defining the Attribute (should not be null, but it will work if it is)
2512
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
2513
         */
2514
        public Attribute ensureFamixAttribute(IVariableBinding bnd, String name, Type type, TWithAttributes owner) {
2515
                Attribute fmx;
2516

2517
                // --------------- to avoid useless computations if we can
2518
                fmx = (Attribute)getEntityByKey(bnd);
5✔
2519
                if (fmx != null) {
2✔
2520
                        return fmx;
2✔
2521
                }
2522

2523
                // --------------- name
2524
                if (name == null) {
2!
2525
                        if (bnd == null) {
×
2526
                                return null;
×
2527
                        }
2528
                        else {
2529
                                name = bnd.getName();
×
2530
                        }
2531
                }
2532

2533
                // --------------- owner
2534
                if (owner == null) {
2✔
2535
                        if (bnd == null) {
2!
2536
                                return null;  // what would be the interest of creating an attribute for which we ignore the declaring class?
×
2537
                        }
2538
                        else {
2539
                                if (bnd.getDeclaringClass() != null && bnd.getDeclaringClass().getErasure() != null) {
7!
2540
                                        // Declaring class is the generic one if the class is parametric.
2541
                                        owner = (TWithAttributes)ensureFamixType(bnd.getDeclaringClass().getErasure());
8✔
2542
                                } else {
2543
                                        return null;  // what would be the interest of creating an attribute for which we ignore the declaring class?
2✔
2544
                                }
2545
                        }
2546
                }
2547

2548
                // --------------- recover from name ?
2549
                for (Attribute candidate : getEntityByName(Attribute.class, name)) {
13✔
2550
                        if (matchAndMapVariable(bnd, name, (TNamedEntity) owner, candidate)) {
8✔
2551
                                fmx = candidate;
2✔
2552
                                break;
1✔
2553
                        }
2554
                }
1✔
2555

2556
                if (fmx == null) {
2✔
2557
                        fmx = ensureFamixEntity(Attribute.class, bnd, name);
7✔
2558
                        fmx.setParentType( owner);
3✔
2559
                }
2560

2561
        fmx.setParentType(owner);
3✔
2562
        ITypeBinding declaredTypeBinding = (bnd == null) ? null : bnd.getType();
7✔
2563
        ensureFamixEntityTyping(declaredTypeBinding, fmx, type);
6✔
2564
        if (bnd != null) {
2✔
2565
            int mod = bnd.getModifiers();
3✔
2566
            setAttributeModifiers(fmx, mod);
4✔
2567
        }
2568

2569
        return fmx;
2✔
2570
        }
2571

2572
        public Attribute ensureFamixAttribute(IVariableBinding bnd, String name, TWithAttributes owner) {
2573
                return ensureFamixAttribute(bnd, name, /*declared type*/null, owner);
7✔
2574
        }
2575

2576
        /**
2577
         * helper method, we know the var exists, ensureFamixAttribute will recover it
2578
         */
2579
        public Attribute getFamixAttribute(IVariableBinding bnd, String name, TWithAttributes owner) {
2580
                return ensureFamixAttribute(bnd, name, /*declared type*/null, owner);
7✔
2581
        }
2582

2583
        /**
2584
         * Returns a Famix Parameter associated with the IVariableBinding.
2585
         * The Entity is created if it does not exist.<br>
2586
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
2587
         */
2588
        public Parameter ensureFamixParameter(IVariableBinding bnd, String name, Type typ, TMethod tMethod) {
2589
                Parameter fmx = null;
2✔
2590

2591
                // --------------- to avoid useless computations if we can
2592
                try {
2593
                        fmx = (Parameter)getEntityByKey(bnd);
5✔
2594
                }catch(Throwable e) {
×
2595
                        e.printStackTrace();
×
2596
                }
1✔
2597
                if (fmx != null) {
2✔
2598
                        return fmx;
2✔
2599
                }
2600

2601
                // --------------- name
2602
                if (name == null) {
2!
2603
                        if (bnd == null) {
×
2604
                                return null;
×
2605
                        }
2606
                        else {
2607
                                name = bnd.getName();
×
2608
                        }
2609
                }
2610

2611
                // --------------- owner
2612
                if (tMethod == null) {
2!
2613
                        if (bnd == null) {
×
2614
                                tMethod = ensureFamixStubMethod("<"+name+"_owner>");
×
2615
                        }
2616
                        else {
2617
                                tMethod = ensureFamixMethod(bnd.getDeclaringMethod());
×
2618
                        }
2619
                }
2620

2621
                // --------------- recover from name ?
2622
                for (Parameter candidate : getEntityByName(Parameter.class, name) ) {
13✔
2623
                        if ( matchAndMapVariable(bnd, name, tMethod, candidate) ) {
7!
2624
                                fmx = candidate;
×
2625
                                break;
×
2626
                        }
2627
                }
1✔
2628

2629
                if (fmx == null) {
2!
2630
                        fmx = ensureFamixEntity(Parameter.class, bnd, name);
7✔
2631
                }
2632

2633
                if (fmx != null) {
2!
2634
                        fmx.setParentBehaviouralEntity(tMethod);
3✔
2635
                        ITypeBinding declaredTypeBnd = (bnd == null) ? null : bnd.getType();
5!
2636
                        ensureFamixEntityTyping(declaredTypeBnd, fmx, typ);
6✔
2637
                }
2638

2639
                return fmx;
2✔
2640
        }
2641

2642
    /**
2643
         * Returns a Famix LocalVariable associated with the IVariableBinding.
2644
         * The Entity is created if it does not exist.<br>
2645
         * @param name -- the name of the FAMIX LocalVariable
2646
         * @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
2647
         */
2648
        public LocalVariable ensureFamixLocalVariable(IVariableBinding bnd, String name, TWithLocalVariables owner) {
2649
                LocalVariable fmx;
2650

2651
                // --------------- to avoid useless computations if we can
2652
                fmx = (LocalVariable)getEntityByKey(bnd);
5✔
2653
                if (fmx != null) {
2!
2654
                        return fmx;
×
2655
                }
2656

2657
                // --------------- name
2658
                if (name == null) {
2!
2659
                        if (bnd == null) {
×
2660
                                return null;
×
2661
                        }
2662
                        else {
2663
                                name = bnd.getName();
×
2664
                        }
2665
                }
2666

2667
                // --------------- owner
2668
                if (owner == null) {
2!
2669
                        if (bnd == null) {
×
2670
                                return null;  // what would be the interest of a local variable for which we ignore the declaring method?
×
2671
                        }
2672
                        else {
2673
                                owner = ensureFamixMethod(bnd.getDeclaringMethod());
×
2674
                        }
2675
                }
2676

2677
                // --------------- recover from name ?
2678
                for (LocalVariable candidate : getEntityByName(LocalVariable.class, name) ) {
13✔
2679
                        if ( matchAndMapVariable(bnd, name, (TNamedEntity) owner, candidate) ) {
8!
2680
                                fmx = candidate;
×
2681
                                break;
×
2682
                        }
2683
                }
1✔
2684

2685
                if (fmx == null) {
2!
2686
                        fmx = ensureFamixEntity(LocalVariable.class, bnd, name);
7✔
2687
                        fmx.setParentBehaviouralEntity(owner);
3✔
2688
                }
2689

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

2693
        return fmx;
2✔
2694
        }
2695

2696
    /**
2697
         * Returns a FAMIX ImplicitVariable with the given <b>name</b> ("self" or "super") and corresponding to the <b>type</b>.
2698
         * If this ImplicitVariable does not exist yet, it is created
2699
         * @param name -- the name of the FAMIX ImplicitVariable (should be Dictionary.SELF_NAME or Dictionary.SUPER_NAME)
2700
         * @param type -- the Famix Type for this ImplicitVariable (should not be null)
2701
         * @param tMethod -- the ContainerEntity where the implicit variable appears (should be a method inside <b>type</b>)
2702
         * @return the FAMIX ImplicitVariable or null in case of a FAMIX error
2703
         */
2704
        public ImplicitVariable ensureFamixImplicitVariable(IBinding key, String name, TType type, TMethod tMethod) {
2705
                ImplicitVariable fmx;
2706
                fmx = ensureFamixEntity(ImplicitVariable.class, key, name);
7✔
2707
                fmx.setParentBehaviouralEntity(tMethod);
3✔
2708
                return fmx;
2✔
2709
        }
2710

2711
        public ImplicitVariable ensureFamixImplicitVariable(String name, TType tType, TMethod tMethod) {
2712
                IBinding bnd = ImplicitVarBinding.getInstance(tMethod, name);
4✔
2713
                return ensureFamixImplicitVariable(bnd, name, tType, tMethod);
7✔
2714
        }
2715

2716
        /**
2717
         * Creates and returns a Famix Comment and associates it with an Entity (ex: for Javadocs)
2718
         * @param jCmt -- the content (String) of the comment 
2719
         * @param owner -- the entity that is commented
2720
         * @return the Famix Comment
2721
         */
2722
        public Comment createFamixComment(org.eclipse.jdt.core.dom.Comment jCmt, TWithComments owner) {
2723
                Comment cmt = null;
2✔
2724

2725
                if ( (jCmt != null) && (owner != null) ) {
4!
2726
                        
2727
                        cmt = new Comment();
4✔
2728
                        addSourceAnchor(cmt, jCmt);
5✔
2729
                        famixRepoAdd(cmt);
3✔
2730
                        cmt.setCommentedEntity(owner);
3✔
2731
                }
2732

2733
                return cmt;
2✔
2734
        }
2735

2736
        /**
2737
         * Creates and returns a Famix Comment and associates it with an Entity
2738
         * @param jCmt -- the content (String) of the comment 
2739
         * @param owner -- the entity that is commented
2740
         * @param content -- the text of the comment
2741
         * @return the Famix Comment
2742
         */
2743
        public Comment createFamixComment(org.eclipse.jdt.core.dom.Comment jCmt, TWithComments owner, String content) {
2744
                Comment cmt = null;
2✔
2745

2746
                if ( (jCmt != null) && (owner != null) ) {
4!
2747
                        cmt = new Comment();
4✔
2748
                        cmt.setContent(content );
3✔
2749
                        famixRepoAdd(cmt);
3✔
2750
                        cmt.setCommentedEntity(owner);
3✔
2751
                }
2752

2753
                return cmt;
2✔
2754
        }
2755

2756
        /**
2757
         * Adds location information to a Famix Entity.
2758
         * 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.
2759
         * This method also creates some basic links between the entity and others (e.g. declaring container, return type, ...)
2760
         * @param fmx -- Famix Entity to add the anchor to
2761
         * @param node -- JDT ASTNode, where the information is extracted
2762
         * @return the Famix SourceAnchor added to fmx. May be null in case of incorrect parameter ('fmx' or 'ast' == null)
2763
         */
2764
        public SourceAnchor addSourceAnchor(TSourceEntity fmx, ASTNode node) {
2765
                IndexedFileAnchor fa;
2766

2767
                fa = createIndexedFileAnchor(node);
4✔
2768
                if ((fmx != null) && (fa != null)) {
4!
2769
                        fmx.setSourceAnchor(fa);
3✔
2770
                        famixRepoAdd(fa);
3✔
2771
                }
2772

2773
                return fa;
2✔
2774
        }
2775

2776
        /**
2777
         * Special case of  {@linkplain #addSourceAnchor(TSourceEntity, ASTNode)} to add location information to a Famix Method.
2778
         */
2779
        public SourceAnchor addSourceAnchor(Method fmx, MethodDeclaration node) {
2780
                IndexedFileAnchor fa;
2781

2782
                fa = createIndexedFileAnchor(node);
4✔
2783
                if ((fmx != null) && (fa != null)) {
4!
2784

2785
                        // may change the positions
2786
                        List<ASTNode> methodDeclarationModifiers = new ArrayList<>();
4✔
2787
                        methodDeclarationModifiers.addAll(node.modifiers());
5✔
2788
                        if (node.getName() != null) {
3!
2789
                                methodDeclarationModifiers.add(node.getName());
5✔
2790
                        }
2791
                        if (node.getReturnType2() != null) {
3✔
2792
                                methodDeclarationModifiers.add(node.getReturnType2());
5✔
2793
                        }
2794
                        int beg = (methodDeclarationModifiers.stream().mapToInt(el -> el.getStartPosition()).min().getAsInt()) + 1;
12✔
2795
                        int end = node.getStartPosition() + node.getLength();
6✔
2796

2797
                        fa.setStartPos(beg);
4✔
2798
                        fa.setEndPos(end);
4✔
2799

2800
                        fmx.setSourceAnchor(fa);
3✔
2801
                        famixRepoAdd(fa);
3✔
2802
                }
2803

2804
                return fa;
2✔
2805
        }
2806

2807
        /**
2808
         * Gets the file name holding <code>node</code> and its start and end positions in the file.
2809
         * Information returned in the form of an IndexedFileAnchor
2810
         */
2811
        protected IndexedFileAnchor createIndexedFileAnchor(ASTNode node) {
2812
                IndexedFileAnchor fa;
2813
                
2814
                if (node == null) {
2!
2815
                        return null;
×
2816
                }
2817

2818
                // position in source file
2819
                int beg = node.getStartPosition() + 1; // Java starts at 0, Moose at 1
5✔
2820
                int end = beg + node.getLength() - 1;
7✔
2821

2822
                // find source Compilation Unit
2823
                // there is a special case for the JDT Comment Nodes
2824
                if (node instanceof org.eclipse.jdt.core.dom.Comment) {
3✔
2825
                        node = ((org.eclipse.jdt.core.dom.Comment) node).getAlternateRoot();
5✔
2826
                } else {
2827
                        node = node.getRoot();
3✔
2828
                }
2829

2830
                fa = new IndexedFileAnchor();
4✔
2831
                fa.setStartPos(beg);
4✔
2832
                fa.setEndPos(end);
4✔
2833

2834
                fa.setFileName((String) node.getProperty(SOURCE_FILENAME_PROPERTY));
6✔
2835

2836
                return fa;
2✔
2837
        }
2838

2839
        /**
2840
         * Creates or recovers the Famix Class for "Object".
2841
         * Because "Object" is the root of the inheritance tree, it needs to be treated differently.
2842
         *
2843
         * @return a Famix class for "Object"
2844
         */
2845
        public Class ensureFamixClassObject() {
2846
        // 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...
2847
        Collection<Class> objects = getEntityByName(Class.class, "Object");
5✔
2848

2849
        for (Class entity : objects) {
10✔
2850
                //We need to cast because the type container is a FamixTWithType but all implementors of this should be named in Java...
2851
                if ("java.lang".equals(((TNamedEntity) entity.getTypeContainer()).getName())) {
7✔
2852
                    return entity;
2✔
2853
                }
2854
        }
1✔
2855

2856
        Class fmx = createFamixEntity(Class.class, "Object");
6✔
2857
        fmx.setTypeContainer(ensureFamixPackageJavaLang(null));
5✔
2858
                return fmx;
2✔
2859
        }
2860

2861
        /**
2862
         * Ensures the Java meta-class: <pre>{@code java.lang.Class<>}</pre>
2863
         */
2864
        public Class ensureFamixMetaClass(ITypeBinding bnd) {
2865
                Package javaLang = ensureFamixPackageJavaLang((bnd == null) ? null : bnd.getPackage());
7!
2866
                ParametricClass fmx = (ParametricClass) this.ensureFamixClass(null, METACLASS_NAME, javaLang, /*isGeneric*/true, Modifier.PUBLIC & Modifier.FINAL);
9✔
2867

2868
                if (fmx != null) {
2!
2869
                        fmx.addTypeParameters(ensureFamixTypeParameter(null, "T", fmx));
7✔
2870
                }
2871

2872
                if ((fmx != null) && (fmx.getSuperInheritances() == null)) {
5!
2873
                        ensureFamixInheritance(ensureFamixClassObject(), fmx, null, null);
×
2874
                }
2875

2876
                return fmx;
2✔
2877
        }
2878

2879
        public Class getFamixMetaClass(ITypeBinding bnd) {
2880
                Package javaLang = ensureFamixPackageJavaLang((bnd == null) ? null : bnd.getPackage());
7!
2881
                return this.ensureFamixClass(null, METACLASS_NAME, javaLang, /*isGeneric*/true, UNKNOWN_MODIFIERS);
8✔
2882
        }
2883

2884
        /**
2885
         * Creates or recovers the Famix Class for all arrays (<pre>{@code <some-type> []}</pre>)
2886
         * In java arrays or objects of special classes (i.e. "I[" for an array of int).
2887
         * JDT does not create a binding for these classes, so we create a stub one here.
2888
         *
2889
         * @return a Famix class
2890
         */
2891
        public Class ensureFamixClassArray() {
2892
                Class fmx = ensureFamixUniqEntity(Class.class, null, ARRAYS_NAME);
×
2893
                if (fmx != null) {
×
2894
                        ensureFamixInheritance(ensureFamixClassObject(), fmx, /*prev*/null, null);
×
2895
                        fmx.setTypeContainer(ensureFamixPackageDefault());
×
2896

2897
                        // may be not needed anymore now that we use modifiers
2898
                        /*fmx.setIsAbstract(Boolean.FALSE);
2899
                        fmx.setIsFinal(Boolean.FALSE);
2900
                        fmx.setIsInterface(Boolean.FALSE); 
2901
                        fmx.setIsPrivate(Boolean.FALSE);
2902
                        fmx.setIsProtected(Boolean.FALSE);*/
2903
                        fmx.setVisibility(MODIFIER_PUBLIC);
×
2904
                }
2905

2906
                return fmx;
×
2907
        }
2908

2909
        public String removeLastPartOfPackageName(String qualifiedName) {
2910
                String ret;
2911
                int last = qualifiedName.lastIndexOf('.');
4✔
2912
                if (last > 0) {
2✔
2913
                        // recursively creating the parent
2914
                        ret = qualifiedName.substring(0, last);
6✔
2915
                }
2916
                else {
2917
                        ret = "";
2✔
2918
                }
2919

2920
                return ret;
2✔
2921
        }
2922

2923
        /** Generates the list of parameters for a method signature
2924
         * @return a string
2925
         */
2926
        protected String signatureParamsFromBinding(IMethodBinding bnd) {
2927
                boolean first = true;
2✔
2928
                String sig = "";
2✔
2929

2930
                for (ITypeBinding parBnd : bnd.getParameterTypes()) {
17✔
2931
                        if (first) {
2✔
2932
                                sig = parBnd.getName();
3✔
2933
                                first = false;
3✔
2934
                        }
2935
                        else {
2936
                                sig += "," + parBnd.getName();
5✔
2937
                        }
2938
                }
2939
                return sig;
2✔
2940
        }
2941

2942
        private String signatureParamsFromStringCollection(Collection<String> paramTypes) {
2943
                boolean first = true;
2✔
2944
                String sig = "";
2✔
2945

2946
                for (String t : paramTypes) {
10✔
2947
                        if (first) {
2✔
2948
                                sig = t;
2✔
2949
                                first = false;
3✔
2950
                        }
2951
                        else {
2952
                                sig += "," + t;
4✔
2953
                        }
2954
                }
1✔
2955
                return sig;
2✔
2956
        }
2957

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