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

moosetechnology / VerveineJ / 42893019655

10 Nov 2026 08:12PM UTC coverage: 55.582% (+2.6%) from 52.984%
42893019655

Pull #275

github

web-flow
Merge cd2f95be4 into ec7f62630
Pull Request #275: Setup mooseci on every PR

2253 of 4218 branches covered (53.41%)

Branch coverage included in aggregate %.

4891 of 8635 relevant lines covered (56.64%)

2.34 hits per line

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

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

3
import java.util.*;
4
import java.util.logging.Level;
5
import java.util.logging.Logger;
6

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

54
import ch.akuhn.fame.Repository;
55
import fr.inria.verveine.extractor.java.utils.ImplicitVarBinding;
56
import fr.inria.verveine.extractor.java.utils.Util;
57

58
/**
59
 * A dictionary of FamixJava entities to help create them and find them back
60
 * Entities are mapped to keys which are the "binding" provided by the JDT pars
61
 * 
62
 * @author anquetil
63
 */
64
public class EntityDictionary {
65
        private static final Logger LOGGER = Logger.getLogger(EntityDictionary.class.getName());
5 ✔
66

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

73
        public static final String DEFAULT_PCKG_NAME = "<Default Package>";
74
        public static final String MISSING_PCKG_NAME= "<Missing Package>";
75
        public static final String STUB_METHOD_CONTAINER_NAME = "<StubMethodContainer>";
76
        public static final String THIS_NAME = "this";
77
        public static final String SUPER_NAME = "super";
78
        
79
        public static final String OBJECT_NAME = "Object";
80
        public static final String METACLASS_NAME = "Class";
81
        public static final String OBJECT_PACKAGE_NAME = "java.lang";
82
        public static final String ARRAYS_NAME = "default[]";
83
        public static final String INIT_BLOCK_NAME = "<Initializer>";
84
        public static final String ANONYMOUS_NAME_PREFIX = "_Anonymous";
85

86
        public static final int UNKNOWN_MODIFIERS = 0;
87
        public static final String MODIFIER_PUBLIC   = "public";
88
        public static final String MODIFIER_PRIVATE  = "private";
89
        public static final String MODIFIER_PROTECTED= "protected";
90
        public static final String MODIFIER_PACKAGE = "package";
91

92
    /**
93
     * The symbol kind to use to define that a method is a default implementation in an interface
94
     */
95
    public static final String DEFAULT_IMPLEMENTATION_KIND_MARKER = "default";
96

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

102
        /**
103
         * The FAMIX repository where all FAMIX entities are created and stored
104
         */
105
        protected Repository famixRepo;
106

107
        /**
108
         * A dictionary to map a key (provided by the user) to FAMIX Entity
109
         */
110
        protected Map<IBinding,TNamedEntity> keyToEntity;
111
        /**
112
         * A reverse dictionary (see {@link #keyToEntity}) to find the key of an entity.
113
         */
114
        protected Map<TNamedEntity,IBinding> entityToKey;
115
        /**
116
         * Associates an enum constant with the anonymous type declared in its class body.
117
         * This is extraction state only; the FAMIX metamodel has no such association.
118
         */
119
        protected Map<IVariableBinding,ITypeBinding> enumConstantAnonymousTypes;
120

121
        /**
122
         * Another dictionary to map a name to FAMIX Entities with this name
123
         */
124
        protected Map<String,Collection<TNamedEntity>> nameToEntity;
125
        
126
        /**
127
         * Yet another dictionary for implicit variables ('self' and 'super')
128
         * Because they are implicit, they may not have a binding provided by the parser,
129
         * or may have the same binding as their associated type so they can't be kept easily in {@link #keyToEntity}
130
         */
131
        @Deprecated
132
        protected Map<Type,ImplicitVars> typeToImpVar;
133

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

151
        /**
152
         * Option: If true, create stubs without package into a "Missing" package
153
         */
154
        protected boolean useMissingPackage;
155

156
        /** Constructor taking a FAMIX repository
157
         * @param famixRepo
158
         */
159
        public EntityDictionary(Repository famixRepo) {
2 ✔
160
                        this.famixRepo = famixRepo;
3 ✔
161
                        
162
                        this.keyToEntity = new Hashtable<IBinding,TNamedEntity>();
5 ✔
163
                        this.entityToKey = new Hashtable<TNamedEntity,IBinding>();
5 ✔
164
                        this.nameToEntity = new Hashtable<String,Collection<TNamedEntity>>();
5 ✔
165
                        this.typeToImpVar = new Hashtable<Type,ImplicitVars>();
5 ✔
166
                        this.enumConstantAnonymousTypes = new HashMap<>();
5 ✔
167
                        
168
                        if (! this.famixRepo.isEmpty()) {
4 ✔
169
                                recoverExistingRepository();
2 ✔
170
                        }
171
                }
1 ✔
172
        
173
        /** Constructor taking a FAMIX repository and the useMissingPackage option
174
         * @param famixRepo
175
         * @param useMissingPackage
176
         */
177
        public EntityDictionary(Repository famixRepo, boolean useMissingPackage) {
2 ✔
178
                        this.famixRepo = famixRepo;
3 ✔
179
                        this.useMissingPackage = useMissingPackage;
3 ✔
180
                        
181
                        this.keyToEntity = new Hashtable<IBinding,TNamedEntity>();
5 ✔
182
                        this.entityToKey = new Hashtable<TNamedEntity,IBinding>();
5 ✔
183
                        this.nameToEntity = new Hashtable<String,Collection<TNamedEntity>>();
5 ✔
184
                        this.typeToImpVar = new Hashtable<Type,ImplicitVars>();
5 ✔
185
                        this.enumConstantAnonymousTypes = new HashMap<>();
5 ✔
186
                        
187
                        if (! this.famixRepo.isEmpty()) {
4 ✔
188
                                recoverExistingRepository();
2 ✔
189
                        }
190
                }
1 ✔
191

192
    /**
193
         * Resets the dictionnary in a proper state after loading entities from an existing MSE file:
194
         * <UL>
195
         * <li>map all named entities to their names in <b>mapName</b></li>
196
         * <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>
197
         * </ul>
198
         */
199
        protected void recoverExistingRepository() {
200
                for (NamedEntity ent : famixRepo.all(NamedEntity.class)) {
13 ✔
201
                        try {
202
                                mapEntityToName(ent.getName(), ent);
5 ✔
203
                        } catch (java.lang.Exception e) {
×
204
                                LOGGER.log(Level.WARNING, "Error recovering entity " + ent.getName() + " from repository " + famixRepo, e);
×
205
                        };
1 ✔
206
                        // for the Exception to be raised, the return value must be tested
207
                        try { if (((TCanBeStub) ent).getIsStub()) {} }
5 ✔
208
                        catch (NullPointerException e) { ((TCanBeStub)ent).setIsStub(Boolean.FALSE); }
6 ✔
209
                }
1 ✔
210

211
                for (Access acc : famixRepo.all(Access.class)) {
13 ✔
212
                        // for the Exception to be raised, the return value must be tested
213
                        try { if (acc.getIsWrite()) {}        }
4 ✔
214
                        catch (NullPointerException e) { acc.setIsWrite(Boolean.FALSE); }
5 ✔
215
                }
1 ✔
216
        }
1 ✔
217

218
        protected void mapEntityToName(String name, TNamedEntity ent) {
219
                
220
                Collection<TNamedEntity> l_ent = nameToEntity.get(name);
6 ✔
221
                if (l_ent == null) {
2 ✔
222
                        l_ent = new LinkedList<>();
4 ✔
223
                }
224
                l_ent.add(ent);
4 ✔
225
                nameToEntity.put(name, l_ent);
6 ✔
226
        }
1 ✔
227

228
        public void removeEntity( NamedEntity ent) {
229
                IBinding key;
230
                key = entityToKey.get(ent);
6 ✔
231
                if (key != null) {
2 ✔
232
                        entityToKey.remove(ent);
5 ✔
233
                        keyToEntity.remove(key);
5 ✔
234
                }
235

236
                Collection<TNamedEntity> l_ent = nameToEntity.get(ent.getName());
7 ✔
237
                if (l_ent != null) {
2 ✔
238
                        l_ent.remove(ent);
4 ✔
239
                }
240

241
                famixRepo.getElements().remove(ent);
6 ✔
242
        }
1 ✔
243
        
244
        protected void mapEntityToKey(IBinding key, TNamedEntity ent) {
245
                TNamedEntity old = keyToEntity.get(key);
6 ✔
246
                if (old != null) {
2 ✔
247
                        entityToKey.remove(old);
5 ✔
248
                }
249
                keyToEntity.put(key, ent);
6 ✔
250
                entityToKey.put(ent, key);
6 ✔
251
        }
1 ✔
252
        
253
        /**
254
         * Returns all the Famix Entity with the given name and class 
255
         * @param fmxClass -- the subtype of Famix Entity we are looking for
256
         * @param name -- the name of the entity
257
         * @return the Collection of Famix Entities with the given name and class (possibly empty)
258
         */
259
        @SuppressWarnings("unchecked")
260
        public <T extends TNamedEntity> Collection<T> getEntityByName(java.lang.Class<T> fmxClass, String name) {
261
                Collection<T> ret = new LinkedList<T>();
4 ✔
262
                Collection<TNamedEntity> l_name = nameToEntity.get(name);
6 ✔
263
                
264
                if (l_name != null ) {
2 ✔
265
                        for (TNamedEntity obj : l_name) {
10 ✔
266
                                if (fmxClass.isInstance(obj)) {
4 ✔
267
                                        ret.add((T) obj);
4 ✔
268
                                }
269
                        }
1 ✔
270
                }
271

272
                return ret;
2 ✔
273
        }
274

275
        /**
276
         * Returns the Famix Entity associated to the given key.
277
         * <b>Note</b>: Be careful that ImplicitVariables share the same binding as their associated Class and cannot be retrieved with this method.
278
         * In such a case, this method will always retrieve the Class associated to the key.
279
         * To get an ImplicitVariable from the key, use {@link #getImplicitVariableByBinding(IBinding, String)}
280
         * @param key -- the key
281
         * @return the Famix Entity associated to the binding or null if not found
282
         */
283
        public TNamedEntity getEntityByKey(IBinding key) {
284
                if (key == null) {
2 ✔
285
                        return null;
2 ✔
286
                }
287
                else {
288
                        return keyToEntity.get(key);
6 ✔
289
                }
290
        }
291

292
        public void registerEnumConstantAnonymousType(IVariableBinding enumConstant, ITypeBinding anonymousType) {
293
                if (enumConstant != null && anonymousType != null) {
4 !
294
                        enumConstantAnonymousTypes.put(enumConstant.getVariableDeclaration(), anonymousType);
7 ✔
295
                }
296
        }
1 ✔
297

298
        public IMethodBinding getEnumConstantMethodOverride(EnumValue enumConstant, IMethodBinding calledMethod) {
299
                if (calledMethod == null) {
2 !
300
                        return null;
×
301
                }
302

303
                IBinding enumConstantBinding = getEntityKey(enumConstant);
4 ✔
304
                ITypeBinding anonymousType = enumConstantBinding instanceof IVariableBinding
3 !
305
                                ? enumConstantAnonymousTypes.get(((IVariableBinding) enumConstantBinding).getVariableDeclaration())
8 ✔
306
                                : null;
1 ✔
307
                if (anonymousType == null) {
2 ✔
308
                        return calledMethod;
2 ✔
309
                }
310

311
                // JDT resolves the enum declaration, but this receiver identifies the runtime override.
312
                IMethodBinding declaredMethod = calledMethod.getMethodDeclaration();
3 ✔
313
                for (IMethodBinding declaredInAnonymousType : anonymousType.getDeclaredMethods()) {
17 !
314
                        if (declaredInAnonymousType.overrides(declaredMethod)) {
4 ✔
315
                                return declaredInAnonymousType;
2 ✔
316
                        }
317
                }
318
                return calledMethod;
×
319
        }
320

321
        /**
322
         * Returns the Famix Entity associated to the given key if it is an instance of the given Famix class.
323
         * This avoids recovering a binding as another kind of entity than the one requested.
324
         * @param key -- the key
325
         * @param fmxClass -- the expected Famix class
326
         * @return the Famix Entity associated to the binding or null if not found or not an instance of <b>fmxClass</b>
327
         */
328
        protected <T extends TNamedEntity> T getEntityByKey(IBinding key, java.lang.Class<T> fmxClass) {
329
                TNamedEntity fmx = getEntityByKey(key);
4 ✔
330
                if (fmxClass.isInstance(fmx)) {
4 ✔
331
                        return fmxClass.cast(fmx);
5 ✔
332
                }
333
                return null;
2 ✔
334
        }
335

336
        /**
337
         * Returns the key associated to a Famix Entity.
338
         * @param e -- the Named entity
339
         * @return the key associated to this entity or null if none
340
         */
341
        public IBinding getEntityKey(TNamedEntity e) {
342
                return entityToKey.get(e);
6 ✔
343
        }
344

345
        /**
346
         * Creates and returns a FAMIX Entity of the type <b>fmxjava.lang.Class</b>.
347
         * The Entity is always created.
348
         * @param fmxClass -- the FAMIX class of the instance to create
349
         * @param name -- the name of the new instance must not be null (and this is not tested)
350
         * @return the FAMIX Entity or null in case of a FAMIX error
351
         */
352
        protected <T extends TNamedEntity & TSourceEntity> T createFamixEntity(java.lang.Class<T> fmxClass, String name) {
353
                T fmx = null;
2 ✔
354

355
                if (name == null) {
2 !
356
                        return null;
×
357
                }
358
                
359
                try {
360
                        fmx = fmxClass.getDeclaredConstructor().newInstance();
9 ✔
361
                } catch (java.lang.Exception e) {
×
362
                        LOGGER.log(Level.WARNING, "Unexpected error, could not create a FAMIX entity", e);
×
363
                }
1 ✔
364
                
365
                if (fmx != null) {
2 !
366
                        fmx.setName(name);
3 ✔
367
                        if (fmx instanceof TCanBeStub) {
3 !
368
                                ((TCanBeStub)fmx).setIsStub(Boolean.TRUE);
4 ✔
369
                        }
370

371
                        mapEntityToName(name, fmx);
4 ✔
372
                        
373
                        // put new entity in Famix repository
374
                        famixRepoAdd((Entity) fmx);
4 ✔
375
                }
376

377
                return fmx;
2 ✔
378
        }
379
        
380
        /**
381
         * Returns a Famix Entity of the type <b>fmxjava.lang.Class</b> and maps it to its binding <b>bnd</b> (if not null).
382
         * The Entity is created if it did not exist.
383
         * @param fmxClass -- the Famix class of the instance to create
384
         * @param bnd -- the binding to map to the new instance
385
         * @param name -- the name of the new instance (used if <pre>{@code bnd == null}</pre>)
386
         * @return the Famix Entity or null if <b>bnd</b> was null or in case of a Famix error
387
         */
388
        @SuppressWarnings("unchecked")
389
        protected <T extends TNamedEntity & TSourceEntity> T ensureFamixEntity(java.lang.Class<T> fmxClass, IBinding bnd, String name) {
390
                T fmx = null;
2 ✔
391
                
392
                /* 
393
                 * Unfortunately different entities with the same name and same type may exist
394
                 * e.g. 2 parameters of 2 different methods but having the same name
395
                 * so we cannot recover just from the name
396
                 */
397

398
                if (bnd != null) {
2 ✔
399
                        fmx = getEntityByKey(bnd, fmxClass);
5 ✔
400
                        if (fmx != null) {
2 ✔
401
                                return fmx;
2 ✔
402
                        }
403
                }
404

405
                // else
406
                fmx = createFamixEntity(fmxClass, name);
5 ✔
407
                if ( (bnd != null) && (fmx != null) ) {
4 !
408
                        mapEntityToKey(bnd, fmx);
4 ✔
409
                }
410
                
411
                return fmx;
2 ✔
412
        }
413

414
        /**
415
         * Adds an already created Entity to the Famix repository
416
         * Used mainly for non-NamedEntity, for example relationships
417
         * @param e -- the Famix entity to add to the repository
418
         */
419
        public void famixRepoAdd(Entity e) {
420
                this.famixRepo.add(e);
4 ✔
421
        }
1 ✔
422

423

424
        /**
425
         * Returns a Famix ParametricClass with the given <b>name</b>, creating it if it does not exist yet
426
         * In the second case, sets some default properties: not Abstract, not Final, not Private, not Protected, not Public, not Interface
427
         * @param name -- the name of the Famix Class
428
         * @return the Famix Class or null in case of a Famix error
429
         */
430
        public ParametricClass ensureFamixParametricClass(ITypeBinding key, String name, TWithTypes owner) {
431
                ParametricClass fmx = ensureFamixEntity(ParametricClass.class, key, name);
7 ✔
432
                if(key != null) {
2 ✔
433
                        for (ITypeBinding tp : key.getErasure().getTypeParameters()) {
18 ✔
434
                                // If there is a type parameter, then fmx will be a Famix ParametricClass
435
                                // note: in Famix, the owner of the TypeParameter is the ParametricClass
436
                                TypeParameter fmxParam = ensureFamixTypeParameter(tp,
5 ✔
437
                                                tp.getName(), fmx);
2 ✔
438
                                fmxParam.setGenericEntity((TParametricEntity)fmx);
3 ✔
439
                fmxParam.setIsStub(fmx.getIsStub());
4 ✔
440
            }
441
                }
442
                
443
                fmx.setTypeContainer(owner);
3 ✔
444
                return fmx;
2 ✔
445
        }
446

447
        /**
448
         * Returns a Famix ParametricInterface with the given <b>name</b>, creating it if it does not exist yet
449
         * In the second case, sets some default properties: not Abstract, not Final, not Private, not Protected, not Public, not Interface
450
         * @param name -- the name of the Famix Class
451
         * @return the Famix Class or null in case of a Famix error
452
         */
453
        public ParametricInterface ensureFamixParametricInterface(ITypeBinding key, String name, TWithTypes owner) {
454
                ParametricInterface fmx = ensureFamixEntity(ParametricInterface.class, key, name);
7 ✔
455
                if(key != null) {
2 !
456
                        for (ITypeBinding tp : key.getTypeParameters()) {
17 ✔
457
                                // If there is a type parameter, then fmx will be a Famix ParametricInterface
458
                                // note: in Famix, the owner of the TypeParameter is the ParametricInterface
459
                                TypeParameter fmxParam = ensureFamixTypeParameter(tp,
5 ✔
460
                                                tp.getName(), fmx);
2 ✔
461
                                fmxParam.setGenericEntity(fmx);
3 ✔
462
                fmxParam.setIsStub(false);
4 ✔
463
            }
464
                }
465
                fmx.setTypeContainer(owner);
3 ✔
466
                return fmx;
2 ✔
467
        }
468

469
        public AnnotationInstanceAttribute createFamixAnnotationInstanceAttribute(AnnotationTypeAttribute att, String value) {
470
                AnnotationInstanceAttribute fmx = null;
2 ✔
471
                if ( (att != null) && (value != null) ) {
4 !
472
                        fmx = new AnnotationInstanceAttribute();
4 ✔
473
                        fmx.setAnnotationTypeAttribute(att);
3 ✔
474
                        fmx.setValue(value);
3 ✔
475
                        this.famixRepo.add(fmx);
4 ✔
476
                }
477
                return fmx;
2 ✔
478
        }
479

480
        public AnnotationInstance addFamixAnnotationInstance(TWithAnnotationInstances fmx, AnnotationType annType, Collection<AnnotationInstanceAttribute> annAtts) {
481
                AnnotationInstance inst = null;
2 ✔
482
                if ( (fmx != null) && (annType != null) ) {
4 !
483
                        inst = new AnnotationInstance();
4 ✔
484
                        inst.setAnnotatedEntity(fmx);
3 ✔
485
                        inst.setAnnotationType(annType);
3 ✔
486
                        inst.addAttributes(annAtts);
3 ✔
487
                        this.famixRepo.add(inst);
4 ✔
488
                }
489
                return inst;
2 ✔
490
        }
491

492
        ///// ensure Famix Relationships /////
493

494
        /**
495
         * Returns a Famix Inheritance relationship between two Famix Classes creating it if needed
496
         * @param sup -- the super class
497
         * @param sub -- the sub class
498
         * @param prev -- previous inheritance relationship in the same context
499
         * @return the Inheritance relationship
500
         */
501
        public Inheritance ensureFamixInheritance(TWithInheritances sup, TWithInheritances sub, TAssociation prev, ITypeBinding supBnd) {
502
                if ( (sup == null) || (sub == null) ) {
4 !
503
                        return null;
×
504
                }
505

506
                // Does the inheritance already exist?
507
                for (TInheritance i : (sup).getSubInheritances()) {                        
11 ✔
508
                        if (i.getSubclass() == sub) {
4 ✔
509
                                return (Inheritance) i;
3 ✔
510
                        }
511
                }
1 ✔
512

513
                Inheritance inh;
514
                if (supBnd != null && supBnd.isParameterizedType()) { // Needs checks and tests.
5 ✔
515
                        inh = new ParametricInheritance();
5 ✔
516
                } else {
517
                        inh = new Inheritance();
4 ✔
518
                }
519

520
                inh.setSuperclass(sup);
3 ✔
521
                inh.setSubclass(sub);
3 ✔
522
                chainPrevNext(prev, inh);
4 ✔
523
                famixRepoAdd(inh);
3 ✔
524
                if (inh instanceof ParametricInheritance) {
3 ✔
525
                        buildFamixParametricAssociation((ParametricInheritance) inh,
6 ✔
526
                                        supBnd.getErasure().getTypeParameters(), supBnd.getTypeArguments());
4 ✔
527
                }
528
                return inh;
2 ✔
529
        }
530
        
531
        /**
532
         * Creates the concretization between the type parameters of the generic entity that is target of an association 
533
         * and the concrete types that concretize them in this association.
534
         * @param association -- the association that must be linked to # or several concretizations
535
         * @param typeParameters -- the collection of type parameters declared in the generic entity
536
         * @param typeArguments -- the collection of concrete types linked to this association
537
         * @return the parametric association
538
         */
539
        public  <T extends TParametricEntity> TParametricAssociation buildFamixParametricAssociation(TParametricAssociation association, ITypeBinding[] typeParameters, ITypeBinding[] typeArguments
540
        ) {
541
                
542
                Iterator<ITypeBinding> genericIterator = Arrays.asList(typeParameters).iterator();
4 ✔
543
                Iterator<ITypeBinding> concreteIterator = Arrays.asList(typeArguments).iterator();
4 ✔
544

545
                while (concreteIterator.hasNext() && genericIterator.hasNext()) {
6 ✔
546
                        TTypeArgument typeArgument = (TTypeArgument)ensureFamixType(concreteIterator.next());
7 ✔
547
                        TypeParameter typeParameter = (TypeParameter)ensureFamixType(genericIterator.next());
7 ✔
548

549
                        Concretization concretization = ensureFamixConcretization(typeArgument, typeParameter);
5 ✔
550
                        association.addConcretization(concretization);
3 ✔
551
                }
1 ✔
552

553
                return association;
2 ✔
554
        }
555

556
        /**
557
         * Returns a Famix Concretization relationship between a Concrete Type and a ParameterType
558
         * @param typeArgument -- the concrete type
559
         * @param typeParameter -- the generic type parameter
560
         * @return the Concretization relationship
561
         */
562
        public Concretization ensureFamixConcretization(TTypeArgument typeArgument, TypeParameter typeParameter ) {
563
                if ( (typeArgument == null) || (typeParameter == null) ) {
4 !
564
                        return null;
×
565
                }
566

567
                Concretization concretization = new Concretization();
4 ✔
568
                concretization.setTypeArgument(typeArgument);
3 ✔
569
                concretization.setTypeParameter(typeParameter);
3 ✔
570

571
                famixRepoAdd(concretization);
3 ✔
572
                return concretization;
2 ✔
573
        }
574
        
575
                /**
576
         * Returns a Famix Implementation relationship between two Famix Classes creating it if needed
577
         * @param myInterface -- the implemented interface
578
         * @param implementingClass -- the implementing class
579
         * @param prev -- previous inheritance relationship in the same context
580
         * @return the Inheritance relationship
581
         */
582
        public Implementation ensureFamixImplementation(TImplementable myInterface, TCanImplement implementingClass, TAssociation prev, ITypeBinding supBnd) {
583
                if ( (myInterface == null) || (implementingClass == null) ) {
4 !
584
                        return null;
×
585
                }
586

587
                for (TImplementation imp : myInterface.getImplementations()) {
11 ✔
588
                        if (imp.getImplementingClass() == implementingClass) {
4 ✔
589
                                return (Implementation) imp;
3 ✔
590
                        }
591
                }
1 ✔
592
                
593
                Implementation implementation;
594
                if (supBnd != null && supBnd.isParameterizedType()) { // Needs checks and tests.
5 !
595
                        implementation = new ParametricImplementation();
5 ✔
596
                } else {
597
                        implementation = new Implementation();
4 ✔
598
                }
599

600
                implementation.setImplementingClass(implementingClass);
3 ✔
601
                implementation.setMyInterface(myInterface);
3 ✔
602
                chainPrevNext(prev, implementation);
4 ✔
603
                famixRepoAdd(implementation);
3 ✔
604
                if (implementation instanceof ParametricImplementation) {
3 ✔
605
                        buildFamixParametricAssociation((ParametricImplementation) implementation,
6 ✔
606
                                        supBnd.getErasure().getTypeParameters(), supBnd.getTypeArguments());
4 ✔
607
                }
608
                return implementation;
2 ✔
609
        }
610

611
        public void ensureImplementedInterfaces(ITypeBinding bnd, TType fmx, TWithTypes owner, TAssociation lastAssociation) {
612
                for (ITypeBinding intbnd : bnd.getInterfaces()) {
17 ✔
613
                        Type superTyp;
614
                        if(intbnd.isClass()){
3 ✔
615
                                superTyp = this.ensureFamixInterface(intbnd, intbnd.getName(), null, intbnd.isGenericType() || intbnd.isParameterizedType() || intbnd.isRawType(), intbnd.getModifiers());
22 !
616
                        }else {
617
                                superTyp = this.ensureFamixType(intbnd);
4 ✔
618
                        }
619
                        
620
                        if (bnd.isInterface()) {
3 ✔
621
                                // in Java "subtyping" link between 2 interfaces is call inheritance 
622
                                lastAssociation = ensureFamixInheritance((TWithInheritances)superTyp, (TWithInheritances)fmx, lastAssociation, intbnd);
10 ✔
623
                        }
624
                        else {
625
                                lastAssociation = ensureFamixImplementation((TImplementable)superTyp, (TCanImplement)fmx, lastAssociation, intbnd);
9 ✔
626
                        }
627
                }
628
        }
1 ✔
629

630
        /**
631
         * Returns a Famix Reference between two Famix Entities creating it if needed.<br>
632
         * 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
633
         * @param src -- source of the reference
634
         * @param tgt -- target of the reference
635
         * @param prev -- previous reference relationship in the same context
636
         * @return the FamixReference
637
         */
638
        public Reference addFamixReference(Method src, TType tgt, TAssociation prev, ITypeBinding referredTypeBnd) {
639
                Reference ref = null;
2 ✔
640
                
641
                if ( (src == null) || (tgt == null) ) {
4 !
642
                        return null;
×
643
                }
644

645
                if (prev == null) {
2 ✔
646
                        for (TReference existingRef : src.getOutgoingReferences()) {
7 !
647
                                if (existingRef.getReferredEntity() == tgt) {
×
648
                                        return (Reference) existingRef;
×
649
                                }
650
                        }
×
651
                }
652

653
                if (referredTypeBnd != null) {
2 ✔
654
                        if (referredTypeBnd.isParameterizedType()) {
3 ✔
655
                                ref = (ParametricReference)buildFamixParametricAssociation(new ParametricReference(), referredTypeBnd.getErasure().getTypeParameters(), referredTypeBnd.getTypeArguments());
13 ✔
656
                        } else if (referredTypeBnd.isArray()) {
3 ✔
657
                                ref = new ParametricReference();
4 ✔
658
                        
659
                                TTypeArgument typeArgument  = (TTypeArgument)ensureFamixType(referredTypeBnd.getElementType());
6 ✔
660
                                TypeParameter typeParameter = (TypeParameter) ((TParametricEntity)tgt).getTypeParameters().iterator().next();
7 ✔
661

662
                                Concretization concretization = ensureFamixConcretization(typeArgument, typeParameter);
5 ✔
663
                                ((ParametricReference)ref).addConcretization(concretization);
4 ✔
664
                        }
665
                }
666

667
                if (ref == null){
2 ✔
668
                        ref = new Reference();
4 ✔
669
                }
670

671
                ref.setReferredEntity(tgt);
3 ✔
672
                ref.setReferencer(src);
3 ✔
673
                chainPrevNext(prev,ref);
4 ✔
674
                famixRepoAdd(ref);
3 ✔
675

676
                return ref;
2 ✔
677
        }
678

679
        /**
680
         * Returns a Famix Invocation between two Famix Entities creating it if needed
681
         * @param tMethod of the invocation
682
         * @param invoked -- method invoked
683
         * @param receiver of the invocation
684
         * @param signature -- i.e. actual invocation code
685
         * @param prev -- previous invocation relationship in the same context
686
         * @return the FamixInvocation
687
         */
688
        public Invocation addFamixInvocation(TMethod tMethod, TMethod invoked, TInvocationsReceiver receiver, String signature, TAssociation prev, IMethodBinding invokedBnd) {
689
                if ( (tMethod == null) || (invoked == null) ) {
4 !
690
                        return null;
×
691
                }
692
                Invocation invocation;
693
                if (invokedBnd != null && invokedBnd.isParameterizedMethod()) {
5 ✔
694
                        invocation = (ParametricInvocation)buildFamixParametricAssociation(new ParametricInvocation(), invokedBnd.getMethodDeclaration().getTypeParameters(), invokedBnd.getTypeArguments());
13 ✔
695
                } else if ( invokedBnd != null && isConstructorBinding(invokedBnd) && invokedBnd.getMethodDeclaration().getDeclaringClass().isGenericType()) {
11 ✔
696
                        invocation = (ParametricInvocation)buildFamixParametricAssociation(new ParametricInvocation(), invokedBnd.getMethodDeclaration().getDeclaringClass().getTypeParameters(), invokedBnd.getDeclaringClass().getTypeArguments());
15 ✔
697
                } else {
698
                        invocation = new Invocation();
4 ✔
699
                }
700

701
                invocation.setReceiver(receiver);
3 ✔
702
                invocation.setSender(tMethod);
3 ✔
703
                invocation.setSignature((signature == null) ? invoked.getSignature() : signature);
5 !
704
                invocation.addCandidates(invoked);
3 ✔
705
                chainPrevNext(prev,invocation);
4 ✔
706
                famixRepoAdd(invocation);
3 ✔
707
                
708
                return invocation;
2 ✔
709
        }
710

711
        /**
712
         * Returns a Famix Access between two Famix Entities creating it if needed
713
         * @param accessor -- the entity (presumably a method) accessing the attribute
714
         * @param var -- the variable accessed
715
         * @param isWrite -- whether this is an access for reading or writing in the variable
716
         * @param prev -- previous access relationship in the same context
717
         * @return the FamixAccess
718
         */
719
        public Access addFamixAccess(TWithAccesses accessor, TStructuralEntity var, boolean isWrite, TAssociation prev) {
720
                if ( (accessor == null) || (var == null) ) {
4 !
721
                        return null;
×
722
                }
723
                Access acc = new Access();
4 ✔
724
                acc.setAccessor(accessor);
3 ✔
725
                acc.addCandidates((TAccessible) var);
3 ✔
726
                acc.setIsWrite(isWrite);
4 ✔
727
                chainPrevNext(prev, acc);
4 ✔
728
                famixRepoAdd(acc);
3 ✔
729
                
730
                return acc;
2 ✔
731
        }
732

733
        protected void chainPrevNext(TAssociation prev, TAssociation next) {
734
                if (prev != null) {
2 ✔
735
                        next.setPrevious(prev);  // not yet implemented in importer
3 ✔
736
                }
737
        }
1 ✔
738
        
739
        /**
740
         * Returns a Famix DeclaredException between a method and an Exception that it declares to throw
741
         * @param meth -- the method throwing the exception
742
         * @param excep -- the exception declared to be thrown
743
         * @return the DeclaredException
744
         */
745
        public TThrowable createFamixDeclaredException(Method meth, TThrowable excep) {
746
                if ( (meth == null) || (excep == null) ) {
4 !
747
                        return null;
×
748
                }
749
                meth.getDeclaredExceptions().add(excep);
5 ✔
750
                return excep;
2 ✔
751
        }
752

753
        /**
754
         * Returns a Famix CaughtException between a method and an Exception that is caught
755
         * @param meth -- the method catching the exception
756
         * @param excep -- the exception caught
757
         * @return the CaughtException
758
         */
759
        public TThrowable createFamixCaughtException(Method meth, TThrowable excep) {
760
                if ( (meth == null) || (excep == null) ) {
4 !
761
                        return null;
×
762
                }
763
                meth.getCaughtExceptions().add(excep);
5 ✔
764
                return excep;
2 ✔
765
        }
766

767
        /**
768
         * Returns a Famix ThrownException between a method and an Exception that it (actually) throws.
769
         * Note: DeclaredException indicates that the method declares it can throw the exception,
770
         * here we state that the exception is actually thrown
771
         * @param meth -- the method throwing the exception
772
         * @param excep -- the exception thrown
773
         * @return the ThrownException
774
         */
775
        public TThrowable createFamixThrownException(Method meth, TThrowable excep) {
776
                if ( (meth == null) || (excep == null) ) {
4 !
777
                        return null;
×
778
                }
779
                meth.getThrownExceptions().add(excep);
5 ✔
780
                return excep;
2 ✔
781
        }
782

783

784
        /**
785
         * Returns a Famix EntityTyping between a typed entity and a type.
786
         * @param typedEntity -- the typed entity
787
         * @param declaredType -- the declared type
788
         * @return the FamixEntityTyping
789
         */
790
        public EntityTyping ensureFamixEntityTyping(ITypeBinding declaredTypeBnd, TTypedEntity typedEntity, TType declaredType) {
791
                if ( (typedEntity == null) || (declaredType == null) ) {
4 !
792
                        return null;
2 ✔
793
                }
794
                EntityTyping typing = null;
2 ✔
795
                
796
                if (declaredTypeBnd != null) {
2 ✔
797
                        if (declaredTypeBnd.isParameterizedType()) {
3 ✔
798
                                typing = (ParametricEntityTyping)buildFamixParametricAssociation(new ParametricEntityTyping(), declaredTypeBnd.getErasure().getTypeParameters(), declaredTypeBnd.getTypeArguments());
13 ✔
799
                        }else if (declaredTypeBnd.isArray()) {
3 ✔
800
                                typing = new ParametricEntityTyping();
4 ✔
801
                                
802
                                TTypeArgument typeArgument  = (TTypeArgument)ensureFamixType(declaredTypeBnd.getElementType());
6 ✔
803
                                TypeParameter typeParameter = (TypeParameter) ((ParametricClass)declaredType).getTypeParameters().iterator().next();
7 ✔
804

805
                                Concretization concretization = ensureFamixConcretization(typeArgument, typeParameter);
5 ✔
806
                                ((ParametricEntityTyping)typing).addConcretization(concretization);
4 ✔
807
                        }
808
                }
809

810
                // If we did not set a typing because not parameterized nor array, set a default one
811
                if (typing == null) {
2 ✔
812
                        typing = new EntityTyping();
4 ✔
813
                }
814
                
815
                typing.setTypedEntity(typedEntity);
3 ✔
816
                typing.setDeclaredType(declaredType);
3 ✔
817
                famixRepoAdd(typing);
3 ✔
818
                
819
                return typing;
2 ✔
820
        }
821

822

823
        ///// Special Case: ImplicitVariables /////
824

825
        /**
826
         * Returns the Famix ImplicitVariable associated to the given binding and name (self or super).
827
         * See also {@link #getEntityByKey(IBinding)}
828
         * @param bnd -- the binding
829
         * @return the Famix Entity associated to the binding or null if not found
830
         */
831
        @Deprecated
832
        public ImplicitVariable getImplicitVariableByBinding(IBinding bnd, String iv_name) {
833
                return getImplicitVariableByType((Class)getEntityByKey(bnd), iv_name);
×
834
        }
835
        
836
        /**
837
         * Returns the Famix ImplicitVariable associated to the given FamixType.
838
         * @param type -- the FamixType
839
         * @param name -- name of the ImplicitVariable (should be Dictionary.SELF_NAME or Dictionary.SUPER_NAME)
840
         * @return the Famix ImplicitVariable associated to the Type or null if not found
841
         */
842
        @Deprecated
843
        public ImplicitVariable getImplicitVariableByType(Type type, String name) {
844
                ImplicitVars iv = typeToImpVar.get(type);
×
845
                ImplicitVariable ret = null;
×
846
                
847
                if (iv == null) {
×
848
                        iv = new ImplicitVars();
×
849
                }
850
                
851
                if (name.equals(THIS_NAME)) {
×
852
                        ret = iv.self_iv;
×
853
                }
854
                else if (name.equals(SUPER_NAME)) {
×
855
                        ret = iv.super_iv;
×
856
                }
857

858
                return ret;
×
859
        }
860

861
        ///// Special Case: "Uniq" Entities /////
862

863
        /**
864
         * Creates or recovers a Famix Named Entity uniq for the given name.
865
         * For some specific entities we don't allow two of them with the same name.
866
         * This is the case e.g. for the default package, or the Java class "Object" and its package "java.lang".
867
         * @param fmxClass -- the Famix class of the instance to create
868
         * @param key -- a potential binding for the entity
869
         * @param name -- the name of the new instance (used if <pre>{@code bnd == null}</pre>)
870
         * @return the uniq Famix Entity for this binding and/or name
871
         */
872
        @SuppressWarnings("unchecked")
873
        public <T extends NamedEntity> T ensureFamixUniqEntity(java.lang.Class<T> fmxClass, IBinding key, String name) {
874
                T fmx = null;
2 ✔
875
                
876
                if (name == null) {
2 !
877
                        return null;
×
878
                }
879
                
880
                if (key != null) {
2 ✔
881
                        fmx = (T) getEntityByKey(key);
5 ✔
882
                }
883
                
884
                if (fmx == null) {
2 ✔
885
                        Collection<T> l = getEntityByName( fmxClass, name);
5 ✔
886
                        if (l.size() > 0) {
3 ✔
887
                                fmx = l.iterator().next();
6 ✔
888
                        }
889
                        else {
890
                                fmx = createFamixEntity(fmxClass, name);
6 ✔
891
                        }
892
                        
893
                        if (key != null) {
2 ✔
894
                                // may happen for example if the entity was first created without binding
895
                                // and we find a binding for it later
896
                                keyToEntity.put(key, fmx);
6 ✔
897
                        }
898
                }
899

900
                return fmx;
2 ✔
901
        }
902

903
        /**
904
         * Creates or recovers the Famix Class that will own all stub methods (for which the real owner is unknown)
905
         *
906
         * @return a Famix class
907
         */
908
        public Class ensureFamixClassStubOwner() {
909
                Class fmx =  ensureFamixUniqEntity(Class.class, null, STUB_METHOD_CONTAINER_NAME);
7 ✔
910
                if (fmx != null) {
2 !
911
                        /*if the option `useMissingPackage` is activated, we put all the stubs with unknown packages in a package `Missing*/
912
                        if(this.useMissingPackage) {
3 ✔
913
                                fmx.setTypeContainer(ensureFamixPackageMissing());
5 ✔
914
                        }
915
                        /*otherwise we put them in the default package*/
916
                        else {
917
                                fmx.setTypeContainer( ensureFamixPackageDefault());
4 ✔
918
                        }
919
                }
920
                ensureFamixInheritance(ensureFamixClassObject(), fmx, /*prev*/null, null);
8 ✔
921

922
                return fmx;
2 ✔
923
        }
924

925
        public Type searchTypeInContext(String name, TWithTypes ctxt) {
926
                if (ctxt == null) {
×
927
                        return null;
×
928
                }
929
                
930
                for (TType candidate : ctxt.getTypes()) {
×
931
                        if (candidate.getName().equals(name) ) {
×
932
                                return (Type) candidate;
×
933
                        }
934
                }
×
935
                
936
                return searchTypeInContext(name, Util.getOwner((TNamedEntity)ctxt));
×
937
        }
938

939
        /**
940
         * Returns a Famix Package associated with its IPackageBinding and/or fully qualified name.
941
         * The Entity is created if it does not exist.
942
         * We assume that Namespaces must be uniq for a given name
943
         * Also creates or recovers recusively it's parent namespaces.<br>
944
         * At least one of <b>bnd</b> and <b>name</b> must be non null.
945
         *
946
         * @param bnd  -- the JDT Binding that may be used as a uniq key to recover this namespace
947
         * @param name -- fully qualified name of the namespace (e.g. 'java.lang')
948
         * @return the Famix Namespace found or created. May return null in case of a Famix error
949
         */
950
        public Package ensureFamixPackage(IPackageBinding bnd, String name) {
951
                Package fmx;
952
                Package parent;
953

954
                if ((name == null) && (bnd != null)) {
4 !
955
                        name = bnd.getName();
3 ✔
956
                }
957

958
                if ((name == null) || name.equals("")) {
6 !
959
                        return ensureFamixPackageDefault();
3 ✔
960
                } else {
961
                        /* Note: Packages are created with their fully-qualified name to simplify recovering when we don't have a binding
962
                         * (for example when creating parent packages of a package we have a binding for).
963
                         * Because the preferred solution in Moose is to give their simple names to packages, they must be post-processed when
964
                         * all is said and done. */
965
                        fmx = ensureFamixUniqEntity(Package.class, bnd, name);
7 ✔
966
                        String parentName = removeLastPartOfPackageName(name);
4 ✔
967
                        if (parentName.length() > 0) {
3 ✔
968
                                parent = ensureFamixPackage(null, parentName);
5 ✔
969
                                // set the parentscope relationship
970
                                if ((parent != null) && (fmx != null) && (fmx.getParentPackage() == null)) {
7 !
971
                                        parent.addChildEntities(fmx);
3 ✔
972
                                }
973
                        }
974
                }
975

976
                return fmx;
2 ✔
977
        }
978

979
        /**
980
         * Creates or recovers a default Famix Package.
981
         * Because this package does not really exist, it has no binding.
982
         *
983
         * @return a Famix Namespace
984
         */
985
        public Package ensureFamixPackageDefault() {
986
        return ensureFamixUniqEntity(Package.class, null, DEFAULT_PCKG_NAME);
7 ✔
987
        }
988
        
989
        /**
990
         * Returns an artificial Package to contain stubs with an unknown package.
991
         * Always returns the same package. Create it lazily if not existing.
992
         * @return a Famix Namespace
993
         */
994
        public Package ensureFamixPackageMissing( ) {
995
                return ensureFamixUniqEntity(Package.class, null, MISSING_PCKG_NAME);
7 ✔
996
        }
997

998
        /**
999
         * Creates or recovers a Famix Package for the package of Java class "Object" (i.e. "java.lang").
1000
         * Because "Object" is the root of the inheritance tree, it needs to be treated differently.
1001
         *
1002
         * @param bnd -- a potential binding for the "java.lang" package
1003
         * @return a Famix Namespace for "java.lang"
1004
         */
1005
        public Package ensureFamixPackageJavaLang(IPackageBinding bnd) {
1006

1007
        return this.ensureFamixPackage(bnd, OBJECT_PACKAGE_NAME);
5 ✔
1008
        }
1009

1010
        /**
1011
         * Returns the Package with {@link #DEFAULT_PCKG_NAME} or <code>null</code> if not found
1012
         */
1013
        public Package getFamixPackageDefault() {
1014
                Collection<Package> l = getEntityByName(Package.class, DEFAULT_PCKG_NAME);
5 ✔
1015
                if (l.size() > 0) {
3 !
1016
                        return l.iterator().next();
5 ✔
1017
                } else {
1018
                        return null;
×
1019
                }
1020
        }
1021

1022
        /**
1023
         * Returns a Famix Type with the given <b>name</b>, creating it if it does not exist yet.
1024
         * In the second case, sets some default properties: not Abstract, not Final, not Private, not Protected, not Public, not Interface
1025
         * @param bnd -- binding for the type to create
1026
         * @param name of the type
1027
         * @param owner of the type
1028
         * @param ctxt -- context of use of the type
1029
         */
1030
        public Type ensureFamixType(ITypeBinding bnd, String name, TWithTypes owner, TWithTypes ctxt, int modifiers) {
1031
                
1032
                Type fmx;
1033

1034
                if (bnd == null) {
2 !
1035
                        if (name == null) {
×
1036
                                return null;
×
1037
                        }
1038
                        fmx = searchTypeInContext(name, ctxt); // WildCard Types don't have binding
×
1039
                        if (fmx != null) {
×
1040
                                return fmx;
×
1041
                        }
1042

1043
                        if ((owner instanceof TParametricEntity)) {
×
1044
                                return this.ensureFamixTypeParameter(null, name, owner);
×
1045
                        }
1046
                        else {
1047
                                fmx = ensureFamixEntity(Type.class, bnd, name);
×
1048
                                fmx.setTypeContainer(owner);
×
1049
                                return fmx;
×
1050
                        }
1051
                }
1052

1053
                // bnd != null
1054

1055
                fmx = (Type) getEntityByKey(bnd);
5 ✔
1056
                if (fmx != null) {
2 ✔
1057
                        return fmx;
2 ✔
1058
                }
1059

1060
                if (bnd.isArray()) {
3 !
1061
                        bnd = bnd.getElementType();
×
1062
                }
1063

1064
                if (bnd.isPrimitive()) {
3 ✔
1065
                        return this.ensureFamixPrimitiveType(bnd, name);
5 ✔
1066
                }
1067

1068
                if (bnd.isEnum()) {
3 !
1069
                        return this.ensureFamixEnum(bnd, name, owner);
×
1070
                }
1071
 
1072
                if ((bnd.isRawType() || bnd.isGenericType()) && !bnd.isInterface() ) {
9 ✔
1073
                        return this.ensureFamixClass(bnd.getErasure(), name, (TNamedEntity) owner, /*isGeneric*/true, modifiers);
10 ✔
1074
                }
1075

1076
                if (bnd.isCapture()) {
3 ✔
1077
                        if (bnd.getErasure().isInterface()) {
4 !
1078
                                return this.ensureFamixInterface(bnd.getErasure(), name, owner, /*isGeneric*/true, modifiers);
9 ✔
1079
                        }
1080
                        else {
1081
                                return this.ensureFamixClass(bnd.getErasure(), name, (TNamedEntity) owner, /*isGeneric*/true, modifiers);
×
1082
                        }
1083
                }
1084

1085
                if (bnd.isAnnotation()) {
3 ✔
1086
                        return this.ensureFamixAnnotationType(bnd, name, (ContainerEntity) owner);
7 ✔
1087
                }
1088

1089
                if (bnd.isInterface()) {
3 ✔
1090
                        return this.ensureFamixInterface(bnd, name, owner, /*isGeneric*/bnd.isGenericType() || bnd.isParameterizedType() || bnd.isRawType(), modifiers);
19 ✔
1091
                }
1092

1093
                if (isThrowable(bnd)) {
4 ✔
1094
                        return this.ensureFamixException(bnd, name, owner, /*isGeneric*/false, modifiers);
8 ✔
1095
                }
1096
                if (bnd.isClass()) {
3 ✔
1097
                        return this.ensureFamixClass(bnd, name, (TNamedEntity) owner, /*isGeneric*/bnd.isGenericType() || bnd.isParameterizedType() || bnd.isRawType(), modifiers);
20 !
1098
                }
1099
                if(bnd.isWildcardType()) {
3 ✔
1100
                        return this.ensureFamixWildcardType(bnd, name, (TParametricEntity)owner, ctxt);
8 ✔
1101
                }
1102

1103
                //otherwise (none of the above)
1104

1105
                if (name == null) {
2 ✔
1106
                        name = bnd.getName();
3 ✔
1107
                }
1108

1109
                if (owner == null) {
2 !
1110
                        owner = (TWithTypes) this.ensureOwner(bnd);
5 ✔
1111
                }
1112

1113
                if (bnd.isTypeVariable() ) {
3 !
1114
                        fmx = ensureFamixTypeParameter(bnd, name, owner);
6 ✔
1115
                        return fmx;
2 ✔
1116
                }
1117

1118
                fmx = ensureFamixEntity(Type.class, bnd, name);
×
1119
                fmx.setTypeContainer(owner);
×
1120
                return fmx;
×
1121
        }
1122

1123
        public Type ensureFamixType(ITypeBinding bnd, TWithTypes context) {
1124
        int modifiers = (bnd != null) ? bnd.getModifiers() : UNKNOWN_MODIFIERS;
6 !
1125
                return ensureFamixType(bnd, /*name*/null, /*owner*/null, context, modifiers);
8 ✔
1126
        }
1127
        
1128
        public Type ensureFamixType(ITypeBinding bnd) {
1129
                return ensureFamixType(bnd, /*ctxt*/null);
5 ✔
1130
        }
1131

1132
        public boolean isThrowable(ITypeBinding bnd) {
1133
                if (bnd == null) {
2 ✔
1134
                        return false;
2 ✔
1135
                }
1136
                if (bnd.getQualifiedName().equals("java.lang.Throwable")) {
5 ✔
1137
                        return true;
2 ✔
1138
                } else if (bnd.getQualifiedName().equals("java.lang.Object")) {
5 ✔
1139
                        return false;
2 ✔
1140
                }
1141
                else {
1142
                        return isThrowable(bnd.getSuperclass());
5 ✔
1143
                }
1144
        }
1145

1146
        /**
1147
         * Returns a Famix Class associated with the ITypeBinding.
1148
         * The Entity is created if it does not exist.
1149
         * @param name -- the name of the Famix Class (MUST NOT be null, but this is not checked)
1150
         * @param owner -- package defining the class (should not be null, but it will work if it is)
1151
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
1152
         */
1153
        @SuppressWarnings("deprecation")
1154
        public Class ensureFamixClass(ITypeBinding bnd, String name, TNamedEntity owner, boolean isGeneric, int modifiers) {
1155
                Class fmx;
1156

1157
                // --------------- some special cases
1158
                if (bnd != null) {
2 ✔
1159
                        if (bnd.isArray()) {
3 !
1160
                                bnd = bnd.getElementType();
×
1161
                        }
1162

1163
                        // for inner classes defined in generics !!! For others should not change anything
1164
                        bnd = bnd.getErasure();
3 ✔
1165
                }
1166

1167
                // ---------------- to avoid useless computations if we can
1168
                fmx = getEntityByKey(bnd, Class.class);
6 ✔
1169
                if (fmx != null) {
2 ✔
1170
                        // Famix cannot represent a parametric exception.
1171
                        if (isGeneric && !(fmx instanceof ParametricClass) && !(fmx instanceof Exception)) {
8 ✔
1172
                                return promoteFamixClass(fmx, bnd);
5 ✔
1173
                        }
1174
                        return fmx;
2 ✔
1175
                }
1176

1177
                // --------------- name
1178
                if (name == null) {
2 ✔
1179
                        if (bnd == null) {
2 !
1180
                                return null;  // not much we can do
×
1181
                        } else if (!bnd.isAnonymous()) {
3 !
1182
                                name = bnd.getErasure().getName();  // for generics, will give the "core" type name, for normal type, won't change anything
5 ✔
1183
                        } else { // anonymous class
1184
                                if (bnd.getSuperclass() != null) {
×
1185
                                        name = bnd.getSuperclass().getName();
×
1186
                                }
1187
                                if ((name == null) || name.equals(OBJECT_NAME)) {
×
1188
                                        ITypeBinding[] intfcs = bnd.getInterfaces();
×
1189
                                        if ((intfcs != null) && (intfcs.length > 0)) {
×
1190
                                                name = bnd.getInterfaces()[0].getName();
×
1191
                                        }
1192
                                        else {
1193
                                                name = "???";
×
1194
                                        }
1195
                                }
1196
                                name = ANONYMOUS_NAME_PREFIX + "(" + name + ")";
×
1197
                        }
1198
                }
1199

1200
        // If we have java.lang.Object we should ensure we create this class
1201
        if (bnd != null && bnd.getQualifiedName().equals("java.lang.Object")) {
7 ✔
1202
                        return ensureFamixClassObject();
3 ✔
1203
                }
1204

1205
                // --------------- owner
1206
                if (owner == null) {
2 ✔
1207
                        if (bnd != null) {
2 ✔
1208
                                owner = ensureOwner(bnd);
4 ✔
1209
                        }
1210
                        /*                                owner = ensureFamixPackageDefault();
1211
                        } else {*/
1212
                }
1213

1214
                // --------------- recover from name ?
1215
                if (owner != null) {
2 ✔
1216
                        for (Class candidate : this.getEntityByName(Class.class, name)) {
13 ✔
1217
                                if (matchAndMapClass(bnd, name, owner, candidate)) {
7 ✔
1218
                                        fmx = candidate;
2 ✔
1219
                                        break;
1 ✔
1220
                                }
1221
                        }
1 ✔
1222
                }
1223
                if (fmx == null && (bnd != null || owner != null)) {
6 ✔
1224
                        fmx = recoveredClassStub(name, (TWithTypes) owner);
6 ✔
1225
                }
1226
                // Famix cannot represent a parametric exception.
1227
                if (fmx != null && isGeneric && !(fmx instanceof ParametricClass) && !(fmx instanceof Exception)) {
10 !
1228
                        fmx = promoteFamixClass(fmx, bnd);
5 ✔
1229
                }
1230

1231
                // ---------------- create
1232
                if (fmx == null) {
2 ✔
1233
                        if (isGeneric) {
2 ✔
1234
                                fmx = ensureFamixParametricClass(bnd, name, (TWithTypes) owner);
8 ✔
1235
                        }
1236
                        else {
1237
                                fmx = ensureFamixEntity(Class.class, bnd, name);
7 ✔
1238
                                fmx.setTypeContainer((TWithTypes)owner);
4 ✔
1239
                        }
1240
                }
1241

1242
                // ---------------- modifiers and super-classes
1243
                if (fmx!=null) {
2 !
1244
                        // we just created it, or it was not bound so we make sure it has the right information in it
1245
                        if (bnd != null) {
2 ✔
1246
                                setClassModifiers(fmx, bnd.getDeclaredModifiers(), (TWithTypes) owner);
7 ✔
1247
                        }
1248

1249
                        TAssociation lastAssoc = null;
2 ✔
1250

1251
                        if (bnd != null) {
2 ✔
1252
                                ITypeBinding supbnd = bnd.getSuperclass();
3 ✔
1253
                                if (supbnd != null) {
2 ✔
1254
                                        lastAssoc = ensureFamixInheritance((TWithInheritances) ensureFamixType(supbnd), fmx, lastAssoc, supbnd);
11 ✔
1255
                                }
1256
                                else {
1257
                                        lastAssoc = ensureFamixInheritance(ensureFamixClassObject(), fmx, lastAssoc, null);
8 ✔
1258
                                }
1259
                                ensureImplementedInterfaces(bnd, fmx, (TWithTypes) owner, lastAssoc);
7 ✔
1260
                        }
1261
                }
1262

1263
                return fmx;
2 ✔
1264
        }
1265

1266
        /**
1267
         * Returns a Famix Exception associated with the ITypeBinding.
1268
         * The Entity is created if it does not exist.
1269
         * @param name -- the name of the Famix Exception
1270
         * @param owner -- type defining the Exception (should not be null, but it will work if it is) 
1271
         *
1272
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
1273
         */
1274
        public <T extends TWithTypes & TNamedEntity> Exception ensureFamixException(ITypeBinding bnd, String name, TWithTypes owner, boolean isGeneric, int modifiers) {
1275
                Exception fmx;
1276

1277
                // --------------- some special cases
1278
                if (bnd != null) {
2 ✔
1279
                        if (bnd.isArray()) {
3 !
1280
                                bnd = bnd.getElementType();
×
1281
                        }
1282

1283
                        // for inner classes defined in generics !!! For others should not change anything
1284
                        bnd = bnd.getErasure();
3 ✔
1285
                }
1286

1287
                // ---------------- to avoid useless computations if we can
1288
                fmx = (Exception) getEntityByKey(bnd);
5 ✔
1289
                if (fmx != null) {
2 ✔
1290
                        return fmx;
2 ✔
1291
                }
1292

1293
                // --------------- name
1294
                if (name == null) {
2 ✔
1295
                        if (bnd == null) {
2 !
1296
                                return null;  // not much we can do
×
1297
                        } else if (!bnd.isAnonymous()) {
3 !
1298
                                name = bnd.getErasure().getName();  // for generics, will give the "core" type name, for normal type, won't change anything
5 ✔
1299
                        } else { // anonymous class
1300
                                if (bnd.getSuperclass() != null) {
×
1301
                                        name = bnd.getSuperclass().getName();
×
1302
                                }
1303
                                if ((name == null) || name.equals(OBJECT_NAME)) {
×
1304
                                        ITypeBinding[] intfcs = bnd.getInterfaces();
×
1305
                                        if ((intfcs != null) && (intfcs.length > 0)) {
×
1306
                                                name = bnd.getInterfaces()[0].getName();
×
1307
                                        }
1308
                                        else {
1309
                                                name = "???";
×
1310
                                        }
1311
                                }
1312
                                name = ANONYMOUS_NAME_PREFIX + "(" + name + ")";
×
1313
                        }
1314
                }
1315

1316
                // --------------- owner
1317
                if (owner == null) {
2 ✔
1318
                        if (bnd == null) {
2 ✔
1319
                                owner = ensureFamixPackageDefault();
4 ✔
1320
                        } else {
1321
                                owner = (TWithTypes) ensureOwner(bnd);
5 ✔
1322
                        }
1323
                }
1324

1325
                // --------------- recover from name ?
1326
                for (Exception candidate : this.getEntityByName(Exception.class, name)) {
13 ✔
1327
                        if (matchAndMapClass(bnd, name, (T) owner, candidate)) {
8 !
1328
                                fmx = candidate;
×
1329
                                break;
×
1330
                        }
1331
                }
1 ✔
1332

1333
                // ---------------- create
1334
                if (fmx == null) {
2 !
1335
                        fmx = ensureFamixEntity(Exception.class, bnd, name);
7 ✔
1336
                        fmx.setTypeContainer(owner);
3 ✔
1337
                }
1338

1339
        // we just created it or it was not bound, so we make sure it has the right information in it
1340
        TAssociation lastAssoc = null;
2 ✔
1341
        if (bnd != null) {
2 ✔
1342
            ITypeBinding supbnd = bnd.getSuperclass();
3 ✔
1343
            if (supbnd != null) {
2 ✔
1344
                lastAssoc = ensureFamixInheritance((TWithInheritances) ensureFamixType(supbnd), fmx, lastAssoc, supbnd);
11 ✔
1345
            }
1346
            else {
1347
                lastAssoc = ensureFamixInheritance(ensureFamixClassObject(), fmx, lastAssoc, null);
8 ✔
1348
            }
1349
            ensureImplementedInterfaces(bnd, fmx, owner, lastAssoc);
6 ✔
1350
        }
1351

1352
        return fmx;
2 ✔
1353
        }
1354

1355
        /**
1356
         * Returns a FAMIX Interface with the given <b>name</b>, creating it if it does not exist yet.
1357
         * @param name -- the name of the FAMIX Method
1358
         * @param owner -- type defining the method (should not be null, but it will work if it is) 
1359
         * @return the FAMIX Class or null in case of a FAMIX error
1360
         */
1361
        public <T extends TWithTypes & TNamedEntity> Interface ensureFamixInterface(ITypeBinding bnd, String name, TWithTypes owner, boolean isGeneric, int modifiers) {
1362
                Interface fmx;
1363

1364
                // --------------- some special cases
1365
                if (bnd != null) {
2 !
1366
                        if (bnd.isArray()) {
3 !
1367
                                bnd = bnd.getElementType();
×
1368
                        }
1369

1370
                        // for inner classes defined in generics !!! For others should not change anything
1371
                        bnd = bnd.getErasure();
3 ✔
1372
                }
1373

1374
                // ---------------- to avoid useless computations if we can
1375
                fmx = getEntityByKey(bnd, Interface.class);
6 ✔
1376
                if (fmx != null) {
2 ✔
1377
                        if (isGeneric && !(fmx instanceof ParametricInterface)) {
5 ✔
1378
                                return promoteFamixInterface(fmx, bnd);
5 ✔
1379
                        }
1380
                        return fmx;
2 ✔
1381
                }
1382

1383
                // --------------- name
1384
                if (name == null) {
2 ✔
1385
                        if (bnd == null) {
2 !
1386
                                return null;  // not much we can do
×
1387
                        } else if (!bnd.isAnonymous()) {
3 !
1388
                                name = bnd.getErasure().getName();  // for generics, will give the "core" type name, for normal type, won't change anything
5 ✔
1389
                        } else { // anonymous class
1390
                                if (bnd.getSuperclass() != null) {
×
1391
                                        name = bnd.getSuperclass().getName();
×
1392
                                }
1393
                                if ((name == null) || name.equals(OBJECT_NAME)) {
×
1394
                                        ITypeBinding[] intfcs = bnd.getInterfaces();
×
1395
                                        if ((intfcs != null) && (intfcs.length > 0)) {
×
1396
                                                name = bnd.getInterfaces()[0].getName();
×
1397
                                        }
1398
                                        else {
1399
                                                name = "???";
×
1400
                                        }
1401
                                }
1402
                                name = ANONYMOUS_NAME_PREFIX + "(" + name + ")";
×
1403
                        }
1404
                }
1405

1406
                // --------------- owner
1407
                if (owner == null) {
2 ✔
1408
                        if (bnd == null) {
2 !
1409
                                owner = ensureFamixPackageDefault();
×
1410
                        } else {
1411
                                owner = (TWithTypes) ensureOwner(bnd);
5 ✔
1412
                        }
1413
                }
1414

1415
                // --------------- recover from name ?
1416
                for (Interface candidate : this.getEntityByName(Interface.class, name)) {
13 ✔
1417
                        if (matchAndMapInterface(bnd, name, (T) owner, candidate)) {
8 ✔
1418
                                fmx = candidate;
2 ✔
1419
                                break;
1 ✔
1420
                        }
1421
                }
1 ✔
1422
                if (fmx == null && (bnd != null || owner != null)) {
4 !
1423
                        fmx = recoveredInterfaceStub(name, owner);
5 ✔
1424
                }
1425
                if (fmx != null && isGeneric && !(fmx instanceof ParametricInterface)) {
7 !
1426
                        fmx = promoteFamixInterface(fmx, bnd);
×
1427
                }
1428

1429
                // ---------------- create
1430
                if (fmx == null) {
2 ✔
1431
                        if (isGeneric) {
2 ✔
1432
                                fmx = ensureFamixParametricInterface(bnd, name, owner);
7 ✔
1433
                        }
1434
                        else {
1435
                                fmx = ensureFamixEntity(Interface.class, bnd, name);
7 ✔
1436
                                fmx.setTypeContainer(owner);
3 ✔
1437
                        }
1438
                }
1439

1440
                // ---------------- modifiers and "super interfaces"
1441
                if (fmx!=null) {
2 !
1442
                        // we just created it or it was not bound, so we make sure it has the right information in it
1443
                        if (bnd != null) {
2 !
1444
                                setInterfaceModifiers(fmx, bnd.getModifiers());
5 ✔
1445
                        }
1446
                        TAssociation lastAssociation = null;
2 ✔
1447
                        if (bnd != null) {
2 !
1448
                                ensureImplementedInterfaces(bnd, fmx, owner, lastAssociation);
6 ✔
1449
                        }
1450
                }
1451
                return fmx;
2 ✔
1452
        }
1453

1454
        // JDT can assign distinct recovery bindings to the same unresolved type.
1455
        private Class recoveredClassStub(String name, TWithTypes owner) {
1456
                Class recovered = null;
2 ✔
1457
                for (Class candidate : getEntityByName(Class.class, name)) {
13 ✔
1458
                        if (sameTypeOwner(candidate.getTypeContainer(), owner) && Boolean.TRUE.equals(candidate.getIsStub())) {
11 ✔
1459
                                if (recovered != null) {
2 !
1460
                                        return null;
×
1461
                                }
1462
                                recovered = candidate;
2 ✔
1463
                        }
1464
                }
1 ✔
1465
                return recovered;
2 ✔
1466
        }
1467

1468
        private Interface recoveredInterfaceStub(String name, TWithTypes owner) {
1469
                Interface recovered = null;
2 ✔
1470
                for (Interface candidate : getEntityByName(Interface.class, name)) {
13 ✔
1471
                        if (sameTypeOwner(candidate.getTypeContainer(), owner) && Boolean.TRUE.equals(candidate.getIsStub())) {
11 !
1472
                                if (recovered != null) {
2 !
1473
                                        return null;
×
1474
                                }
1475
                                recovered = candidate;
2 ✔
1476
                        }
1477
                }
1 ✔
1478
                return recovered;
2 ✔
1479
        }
1480

1481
        private boolean sameTypeOwner(TWithTypes first, TWithTypes second) {
1482
                if (first == second) {
3 ✔
1483
                        return true;
2 ✔
1484
                }
1485
                if (!(first instanceof TNamedEntity) || !(second instanceof TNamedEntity)
8 !
1486
                                || !((TNamedEntity) first).getName().equals(((TNamedEntity) second).getName())) {
6 !
1487
                        return false;
2 ✔
1488
                }
1489
                if (first instanceof Type && second instanceof Type) {
×
1490
                        return sameTypeOwner(((Type) first).getTypeContainer(), ((Type) second).getTypeContainer());
×
1491
                }
1492
                return first.getClass() == second.getClass();
×
1493
        }
1494

1495
        private ParametricClass promoteFamixClass(Class former, ITypeBinding bnd) {
1496
                TWithTypes owner = former.getTypeContainer();
3 ✔
1497
                removeEntity(former);
3 ✔
1498
                former.setTypeContainer(null);
3 ✔
1499

1500
                ParametricClass promoted = ensureFamixParametricClass(bnd, former.getName(), owner);
7 ✔
1501
                transferTypeAssociations(former, promoted);
4 ✔
1502
                promoted.addAttributes(new ArrayList<>(former.getAttributes()));
7 ✔
1503
                promoted.addComments(new ArrayList<>(former.getComments()));
7 ✔
1504
                promoted.addImports(new ArrayList<>(former.getImports()));
7 ✔
1505
                promoted.addIncomingImports(new ArrayList<>(former.getIncomingImports()));
7 ✔
1506
                promoted.addInterfaceImplementations(new ArrayList<>(former.getInterfaceImplementations()));
7 ✔
1507
                promoted.addOutgoingConcretizations(new ArrayList<>(former.getOutgoingConcretizations()));
7 ✔
1508
                promoted.addReceivingInvocations(new ArrayList<>(former.getReceivingInvocations()));
7 ✔
1509
                promoted.addSubInheritances(new ArrayList<>(former.getSubInheritances()));
7 ✔
1510
                promoted.addSuperInheritances(new ArrayList<>(former.getSuperInheritances()));
7 ✔
1511
                promoted.setParentPackage(former.getParentPackage());
4 ✔
1512
                promoted.setIsAbstract(former.getIsAbstract());
4 ✔
1513
                promoted.setIsClassSide(former.getIsClassSide());
4 ✔
1514
                promoted.setIsFinal(former.getIsFinal());
4 ✔
1515
                promoted.setVisibility(former.getVisibility());
4 ✔
1516
                return promoted;
2 ✔
1517
        }
1518

1519
        private ParametricInterface promoteFamixInterface(Interface former, ITypeBinding bnd) {
1520
                TWithTypes owner = former.getTypeContainer();
3 ✔
1521
                removeEntity(former);
3 ✔
1522
                former.setTypeContainer(null);
3 ✔
1523

1524
                ParametricInterface promoted = ensureFamixParametricInterface(bnd, former.getName(), owner);
7 ✔
1525
                transferTypeAssociations(former, promoted);
4 ✔
1526
                promoted.addAttributes(new ArrayList<>(former.getAttributes()));
7 ✔
1527
                promoted.addComments(new ArrayList<>(former.getComments()));
7 ✔
1528
                promoted.addImplementations(new ArrayList<>(former.getImplementations()));
7 ✔
1529
                promoted.addImports(new ArrayList<>(former.getImports()));
7 ✔
1530
                promoted.addIncomingImports(new ArrayList<>(former.getIncomingImports()));
7 ✔
1531
                promoted.addOutgoingConcretizations(new ArrayList<>(former.getOutgoingConcretizations()));
7 ✔
1532
                promoted.addReceivingInvocations(new ArrayList<>(former.getReceivingInvocations()));
7 ✔
1533
                promoted.addSubInheritances(new ArrayList<>(former.getSubInheritances()));
7 ✔
1534
                promoted.addSuperInheritances(new ArrayList<>(former.getSuperInheritances()));
7 ✔
1535
                promoted.setParentPackage(former.getParentPackage());
4 ✔
1536
                promoted.setIsClassSide(former.getIsClassSide());
4 ✔
1537
                promoted.setIsFinal(former.getIsFinal());
4 ✔
1538
                promoted.setVisibility(former.getVisibility());
4 ✔
1539
                return promoted;
2 ✔
1540
        }
1541

1542
        private void transferTypeAssociations(Type former, Type promoted) {
1543
                promoted.addTypes(new ArrayList<>(former.getTypes()));
7 ✔
1544
                promoted.addDefinedAnnotationTypes(new ArrayList<>(former.getDefinedAnnotationTypes()));
7 ✔
1545
                promoted.addAnnotationInstances(new ArrayList<>(former.getAnnotationInstances()));
7 ✔
1546
                promoted.addIncomingReferences(new ArrayList<>(former.getIncomingReferences()));
7 ✔
1547
                promoted.addIncomingTypings(new ArrayList<>(former.getIncomingTypings()));
7 ✔
1548
                promoted.addLowerBoundedWildcards(new ArrayList<>(former.getLowerBoundedWildcards()));
7 ✔
1549
                promoted.addMethods(new ArrayList<>(former.getMethods()));
7 ✔
1550
                promoted.addUpperBoundedWildcards(new ArrayList<>(former.getUpperBoundedWildcards()));
7 ✔
1551
                promoted.setSourceAnchor(former.getSourceAnchor());
4 ✔
1552
                promoted.setNumberOfLinesOfCode(former.getNumberOfLinesOfCode());
4 ✔
1553
                promoted.setIsStub(former.getIsStub());
4 ✔
1554
        }
1 ✔
1555

1556
        /**
1557
         * "Converts" (if needed) a TTYpe entity to be a TThrowable. Might involve removing the existing entity, recreating a new one and migrating
1558
         * all the relationship of the former to the later
1559
         */
1560
        public TThrowable asException(TType fmxType) {
1561
                if (fmxType instanceof Exception) {
3 ✔
1562
                        return (Exception) fmxType;
3 ✔
1563
                }
1564
                if(fmxType instanceof TypeParameter) {
3 ✔
1565
                        return (TypeParameter) fmxType;
3 ✔
1566
                }
1567

1568
                Exception fmxException = null;
2 ✔
1569
                IBinding key;
1570

1571
                try {
1572
                        key = entityToKey.get(fmxType);
6 ✔
1573

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

1578
                        TWithTypes owner = fmxType.getTypeContainer();
3 ✔
1579
                        fmxType.setTypeContainer(null);
3 ✔
1580
                        fmxException = ensureFamixException((ITypeBinding) key, fmxType.getName(), owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
10 ✔
1581
                        transferTypeAssociations((Type) fmxType, fmxException);
5 ✔
1582
                        fmxException.setComments(new ArrayList<>( ((TWithComments) fmxType).getComments() ));
8 ✔
1583
                        if (fmxType instanceof TTypeArgument) {
3 !
1584
                                fmxException.addOutgoingConcretizations(new ArrayList<>( ((TTypeArgument) fmxType).getOutgoingConcretizations() ));
8 ✔
1585
                        }
1586

1587
                        if (fmxType instanceof TWithAttributes) {
3 !
1588
                                fmxException.addAttributes( new ArrayList<>( ((TWithAttributes)fmxType).getAttributes() ) );
8 ✔
1589
                        }
1590

1591
                        if (fmxType instanceof TWithInheritances) {
3 !
1592
                                fmxException.addSuperInheritances( new ArrayList<>( ((TWithInheritances) fmxType).getSuperInheritances() ) );
8 ✔
1593
                                fmxException.addSubInheritances( new ArrayList<>( ((TWithInheritances) fmxType).getSubInheritances() ) );
8 ✔
1594
                        }
1595
                }
1596
                catch( ConcurrentModificationException e) {
×
1597
                        e.printStackTrace();
×
1598
                }
1 ✔
1599

1600
                return fmxException;
2 ✔
1601
        }
1602

1603
        /**
1604
         * helper method, we know the type exists, ensureFamixClass will recover it
1605
         */
1606
        public Class getFamixClass(ITypeBinding bnd, String name, TNamedEntity owner) {
1607
                return ensureFamixClass(bnd, name, owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
8 ✔
1608
        }
1609

1610
        /**
1611
         * helper method, we know the type exists, ensureFamixInterface will recover it
1612
         */
1613
        public Interface getFamixInterface(ITypeBinding bnd, String name, ContainerEntity owner) {
1614
                return ensureFamixInterface(bnd, name, owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
8 ✔
1615
        }
1616

1617
        /**
1618
         * helper method, we know the type exists, ensureFamixInterface will recover it
1619
         */
1620
        public Exception getFamixException(ITypeBinding bnd, String name, TWithTypes owner) {
1621
                return ensureFamixException(bnd, name, owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
8 ✔
1622
        }
1623

1624
        /**
1625
         * Ensures a famix entity for the owner of a binding.<br>
1626
         * This owner can be a method, a class or a namespace
1627
         * @param bnd -- binding for the owned entity
1628
         * @return a famix entity for the owner
1629
         */
1630
        private TNamedEntity ensureOwner(ITypeBinding bnd) {
1631
                TNamedEntity owner;
1632
                IMethodBinding parentMtd = bnd.getDeclaringMethod();
3 ✔
1633
                if (parentMtd != null) {
2 ✔
1634
                        owner = this.ensureFamixMethod(parentMtd);  // cast needed to desambiguate the call
5 ✔
1635
                }
1636
                else {
1637
                        ITypeBinding parentClass = bnd.getDeclaringClass();
3 ✔
1638
                        if (parentClass != null) {
2 ✔
1639
                owner = this.ensureFamixType(parentClass);
5 ✔
1640
            }
1641
                        else {
1642
                                IPackageBinding parentPckg = bnd.getPackage();
3 ✔
1643
                                if (parentPckg != null) {
2 !
1644
                                        owner = this.ensureFamixPackage(parentPckg, null);
6 ✔
1645
                                } else {
1646
                                        owner = this.ensureFamixPackageDefault();
×
1647
                                }
1648
                        }
1649
                }
1650
                return owner;
2 ✔
1651
        }
1652

1653

1654
        /**
1655
         * Returns a FAMIX PrimitiveType with the given <b>name</b>, creating it if it does not exist yet
1656
         * We assume that PrimitiveType must be uniq for a given name
1657
         * @param name -- the name of the FAMIX PrimitiveType
1658
         * @return the FAMIX PrimitiveType or null in case of a FAMIX error
1659
         */
1660
        public PrimitiveType ensureFamixPrimitiveType(ITypeBinding bnd, String name) {
1661
                if (name == null) {
2 ✔
1662
                        if (bnd == null) {
2 !
1663
                                return null;
×
1664
                        } else {
1665
                                name = bnd.getName();
3 ✔
1666
                        }
1667
                }
1668
                return ensureFamixUniqEntity(PrimitiveType.class, bnd, name);
7 ✔
1669
        }
1670

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

1674
                // --------------- to avoid useless computations if we can
1675
                fmx = (org.moosetechnology.model.famix.famixjavaentities.Enum) getEntityByKey(bnd);
5 ✔
1676
                if (fmx != null) {
2 ✔
1677
                        return fmx;
2 ✔
1678
                }
1679

1680
                // --------------- name
1681
                if (name == null) {
2 ✔
1682
                        if (bnd == null) {
2 !
1683
                                return null;
×
1684
                        }
1685
                        else {
1686
                                name = bnd.getName();
3 ✔
1687
                        }
1688
                }
1689

1690
                // --------------- owner
1691
                if (owner == null) {
2 ✔
1692
                        if (bnd == null) {
2 !
1693
                                owner = ensureFamixPackageDefault();  // not really sure what to do here
×
1694
                        } else {
1695
                                owner = (TWithTypes) ensureOwner(bnd);
5 ✔
1696
                        }
1697
                }
1698

1699
                // --------------- recover from name ?
1700
                for (org.moosetechnology.model.famix.famixjavaentities.Enum candidate : getEntityByName(org.moosetechnology.model.famix.famixjavaentities.Enum.class, name)) {
9 !
1701
                        if (matchAndMapType(bnd, name, (T) owner, candidate)) {
×
1702
                                fmx = candidate;
×
1703
                                break;
×
1704
                        }
1705
                }
×
1706

1707
                if (fmx == null) {
2 !
1708
                        fmx = ensureFamixEntity(Enum.class, bnd, name);
7 ✔
1709
                        fmx.setTypeContainer(owner);
3 ✔
1710
                }
1711

1712
                if (bnd != null) {
2 !
1713
                        setVisibility(fmx, bnd.getModifiers(), owner);
6 ✔
1714
                }
1715

1716
                return fmx;
2 ✔
1717
        }
1718

1719
        /**
1720
         * helper method, we know the type exists, ensureFamixEnum will recover it
1721
         */
1722
        public org.moosetechnology.model.famix.famixjavaentities.Enum getFamixEnum(ITypeBinding bnd, String name, TWithTypes owner) {
1723
                return ensureFamixEnum(bnd, name, owner);
6 ✔
1724
        }
1725

1726
        public EnumValue ensureFamixEnumValue(IVariableBinding bnd,        String name, Enum owner) {
1727
                EnumValue fmx;
1728

1729
                // --------------- to avoid useless computations if we can
1730
                fmx = (EnumValue)getEntityByKey(bnd);
5 ✔
1731
                if (fmx != null) {
2 ✔
1732
                        return fmx;
2 ✔
1733
                }
1734

1735
                // --------------- name
1736
                if (name == null) {
2 !
1737
                        if (bnd == null) {
×
1738
                                return null;
×
1739
                        }
1740
                        else {
1741
                                name = bnd.getName();
×
1742
                        }
1743
                }
1744

1745
                // --------------- owner
1746
                if (owner == null) {
2 ✔
1747
                        if (bnd == null) {
2 !
1748
                                return null;  // what would be the interest of creating an EnumValue without a declaring Enum type?
×
1749
                        }
1750
                        else {
1751
                                owner = ensureFamixEnum(bnd.getDeclaringClass(), null, null);
7 ✔
1752
                        }
1753
                }
1754

1755
                // --------------- recover from name ?
1756
                for (EnumValue candidate : getEntityByName(EnumValue.class, name) ) {
9 !
1757
                        if ( matchAndMapVariable(bnd, name, owner, candidate) ) {
×
1758
                                fmx = candidate;
×
1759
                                break;
×
1760
                        }
1761
                }
×
1762
                if (fmx == null) {
2 !
1763
                        fmx = ensureFamixEntity(EnumValue.class, bnd, name);
7 ✔
1764
                        fmx.setParentEnum(owner);
3 ✔
1765
                }
1766

1767
        fmx.setParentEnum(owner);
3 ✔
1768

1769
        return fmx;
2 ✔
1770
        }
1771

1772
    /**
1773
         * e.g. see {@link EntityDictionary#ensureFamixClass}
1774
         */
1775
        public AnnotationType ensureFamixAnnotationType(ITypeBinding bnd, String name, ContainerEntity owner) {
1776
                AnnotationType fmx;
1777

1778
                // --------------- to avoid useless computations if we can
1779
                fmx = (AnnotationType)getEntityByKey(bnd);
5 ✔
1780
                if (fmx != null) {
2 ✔
1781
                        return fmx;
2 ✔
1782
                }
1783

1784
                // --------------- name
1785
                if (name == null) {
2 ✔
1786
                        if (bnd == null) {
2 !
1787
                                return null;
×
1788
                        }
1789
                        else {
1790
                                name = bnd.getName();
3 ✔
1791
                        }
1792
                }
1793

1794
                // --------------- owner
1795
                if (owner == null) {
2 ✔
1796
                        if (bnd == null) {
2 !
1797
                                owner = ensureFamixPackageDefault();
×
1798
                        }
1799
                        else {
1800
                                IPackageBinding parentPckg = bnd.getPackage();
3 ✔
1801
                                if (parentPckg != null) {
2 !
1802
                                        owner = this.ensureFamixPackage(parentPckg, null);
6 ✔
1803
                                } else {
1804
                                        owner = this.ensureFamixPackageDefault();
×
1805
                                }
1806
                        }
1807
                }
1808

1809
                // --------------- recover from name ?
1810
                for (AnnotationType candidate : getEntityByName(AnnotationType.class, name) ) {
13 ✔
1811
                        if ( matchAndMapType(bnd, name, owner, candidate) ) {
7 !
1812
                                fmx = candidate;
2 ✔
1813
                                break;
1 ✔
1814
                        }
1815
                }
×
1816

1817
                // --------------- create
1818
                if (fmx == null) {
2 ✔
1819
                        fmx = ensureFamixEntity(AnnotationType.class, bnd, name);
7 ✔
1820
                        fmx.setAnnotationTypesContainer(owner);
3 ✔
1821
                }
1822

1823
                if (bnd != null) {
2 !
1824
                        // Not supported in Famix
1825

1826
                        // setVisibility(fmx, bnd.getModifiers());
1827
                }
1828

1829
                return fmx;
2 ✔
1830
        }
1831

1832
        /**
1833
         * helper method, we know the type exists, ensureFamixAnnotationType will recover it
1834
         */
1835
        public AnnotationType getFamixAnnotationType(ITypeBinding bnd, String name, ContainerEntity owner) {
1836
                return ensureFamixAnnotationType(bnd, name, owner);
6 ✔
1837
        }
1838

1839
        public AnnotationTypeAttribute ensureFamixAnnotationTypeAttribute(IMethodBinding bnd, String name, AnnotationType owner) {
1840
                AnnotationTypeAttribute fmx = null;
2 ✔
1841

1842
                // --------------- to avoid useless computations if we can
1843
                fmx = (AnnotationTypeAttribute)getEntityByKey(bnd);
5 ✔
1844
                if (fmx != null) {
2 ✔
1845
                        return fmx;
2 ✔
1846
                }
1847

1848
                // --------------- name
1849
                if (name == null) {
2 !
1850
                        if (bnd == null) {
×
1851
                                return null;
×
1852
                        }
1853
                        else {
1854
                                name = bnd.getName();
×
1855
                        }
1856
                }
1857

1858
                // --------------- owner
1859
                if (owner == null) {
2 !
1860
                        if (bnd == null) {
×
1861
                                return null;  // what would be the use of an AnnotationTypeAttribute without AnnotationType ?
×
1862
                        }
1863
                        else {
1864
                                ITypeBinding parentType = bnd.getDeclaringClass();
×
1865
                                if (parentType != null) {
×
1866
                                        owner = this.ensureFamixAnnotationType(parentType, null, null);
×
1867
                                }
1868
                                else  {
1869
                                        return null;  // what would be the use of an AnnotationTypeAttribute without AnnotationType ?
×
1870
                                }
1871
                        }
1872
                }
1873

1874
                // --------------- recover from name ?
1875
                for (AnnotationTypeAttribute candidate : getEntityByName(AnnotationTypeAttribute.class, name) ) {
13 ✔
1876
                        // JDT treats annotation type attributes as methods ...
1877
                        // checkAndMapMethod wants a signature as 2nd argument so we add empty param list
1878
                        if ( (bnd != null) && matchAndMapMethod(bnd, name+"()", null, owner, candidate) ) {
11 !
1879
                                fmx = candidate;
×
1880
                                break;
×
1881
                        }
1882
                        // if the binding is null, the annotationTypeAttribute migth have been created
1883
                        else if ( (bnd == null) && matchAndMapVariable(null, name, owner, candidate)) {
2 !
1884
                                fmx = candidate;
×
1885
                                break;
×
1886
                        }
1887
                }
1 ✔
1888

1889
                if (fmx == null) {
2 !
1890
                        fmx = ensureFamixEntity(AnnotationTypeAttribute.class, bnd, name);
7 ✔
1891
                        fmx.setParentType(owner);
3 ✔
1892
                }
1893

1894
                if (bnd != null) {
2 !
1895
                        // Not suopp
1896

1897
                        // setVisibility(fmx, bnd.getModifiers());
1898
                }
1899

1900
                return fmx;
2 ✔
1901
        }
1902

1903
        /**
1904
         * helper method, we know the attribute exists, ensureFamixAnnotationTypeAttribute will recover it
1905
         */
1906
        public AnnotationTypeAttribute getFamixAnnotationTypeAttribute(IMethodBinding bnd, String name, AnnotationType owner) {
1907
                return ensureFamixAnnotationTypeAttribute( bnd, name, owner);
6 ✔
1908
        }
1909
        
1910
        
1911
        /**
1912
         * Returns a FAMIX Wildcard with its bounds
1913
         * @param bnd
1914
         * @param name
1915
         * @param owner
1916
         * @return
1917
         */
1918
        public Wildcard ensureFamixWildcardType(ITypeBinding bnd, String name, TParametricEntity owner, TWithTypes ctxt) {
1919
                Wildcard fmx = this.ensureFamixEntity(Wildcard.class, bnd, bnd.getName());
8 ✔
1920
                if(bnd.getBound() != null) {
3 ✔
1921
                        Type bound = this.ensureFamixType(bnd.getBound());
5 ✔
1922
                        if(bnd.isUpperbound()) {
3 ✔
1923
                                fmx.setUpperBound(bound);
3 ✔
1924
                                bound.addUpperBoundedWildcards(fmx);
4 ✔
1925
                        }else{
1926
                                fmx.setLowerBound(bound);
3 ✔
1927
                                bound.addLowerBoundedWildcards(fmx);
3 ✔
1928
                        }
1929
                }
1930
                return fmx;
2 ✔
1931
        }
1932

1933
        /**
1934
         * Returns a Famix TypeParameter (created by a Famix ParametricEntity) with the given <b>name</b>, creating it if it does not exist yet
1935
         * In the second case, sets some default properties: not Abstract, not Final, not Private, not Protected, not Public
1936
         * @param name -- the name of the Famix TypeParameter
1937
         * @return the Famix TypeParameter or null in case of a Famix error
1938
         */
1939
        public TypeParameter ensureFamixTypeParameter(ITypeBinding bnd,        String name, TWithTypes owner) {
1940
                TypeParameter fmx;
1941

1942
                // --------------- to avoid useless computations if we can
1943
                fmx = (TypeParameter)getEntityByKey(bnd);
5 ✔
1944
                if (fmx != null) {
2 ✔
1945
                        return fmx;
2 ✔
1946
                }
1947

1948
                // --------------- name
1949
                if (name == null) {
2 ✔
1950
                        if (bnd == null) {
2 !
1951
                                return null;
×
1952
                        }
1953
                        else {
1954
                                name = bnd.getName();
3 ✔
1955
                        }
1956
                }
1957

1958
                // --------------- owner
1959
                if (owner == null && bnd != null) {
2 !
1960
            if (bnd.getDeclaringClass() != null) {
×
1961
                owner = this.ensureFamixType(bnd.getDeclaringClass());
×
1962
            } else if(bnd.getDeclaringMethod() != null) {
×
1963
                owner = this.ensureFamixMethod(bnd.getDeclaringMethod());
×
1964
            }
1965
                }
1966

1967
                // --------------- recover from name ?
1968
                for (Type candidate : this.getEntityByName(Type.class, name)) {
13 ✔
1969
                        if ( matchAndMapType(bnd, name, (ContainerEntity) owner, candidate) ) {
8 ✔
1970
                                fmx = (TypeParameter) candidate;
3 ✔
1971
                                break;
1 ✔
1972
                        }
1973
                }
1 ✔
1974

1975
                // --------------- create
1976
                if (fmx == null) {
2 ✔
1977
                        fmx = ensureFamixEntity(TypeParameter.class, bnd, name);
7 ✔
1978
                        if(bnd != null && bnd.getSuperclass() != null) {
5 !
1979
                                Type upperBound = ensureFamixType(bnd.getSuperclass());
5 ✔
1980
                                fmx.setUpperBound(upperBound);
3 ✔
1981
                        }
1982
                        if(bnd != null) {
2 ✔
1983
                for (ITypeBinding intbnd : bnd.getInterfaces()) {
17 ✔
1984
                    Type upperBound = ensureFamixType(intbnd);
4 ✔
1985
                    fmx.setUpperBound(upperBound);
3 ✔
1986
                }
1987
            }
1988
                        fmx.setTypeContainer(owner);
3 ✔
1989
                }
1990

1991
                return fmx;
2 ✔
1992
        }
1993

1994
        /**
1995
         * Checks whether the existing unmapped Famix Namespace matches the binding.
1996
         * Checks that the candidate has the same name as the JDT bound package, and checks recursively that owners also match.
1997
         *
1998
         * @param bnd       -- a JDT binding that we are trying to match to the candidate
1999
         * @param name      of the package
2000
         * @param owner     of the package
2001
         * @param candidate -- a Famix Entity
2002
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
2003
         */
2004
        private boolean matchAndMapPackage(IPackageBinding bnd, String name, Package owner, NamedEntity candidate) {
2005
                if (!(candidate instanceof Package)) {
3 !
2006
                        return false;
×
2007
                }
2008

2009
                // check whether bnd and candidate are already bound
2010
                CheckResult res = checkKeyMatch(bnd, candidate);
5 ✔
2011
                if (res == CheckResult.MATCH) {
3 ✔
2012
                        return true;
2 ✔
2013
                } else if (res == CheckResult.FAIL) {
3 ✔
2014
                        return false;
2 ✔
2015
                }
2016

2017
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7 ✔
2018
                        return false;
2 ✔
2019
                }
2020

2021
                // names match, not need to look at owner because names of Namespaces are their fully qualified name
2022
                conditionalMapToKey(bnd, candidate);
4 ✔
2023
                return true;
2 ✔
2024
        }
2025

2026
        /**
2027
         * Checks whether the existing unmapped Famix Type matches the binding.
2028
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
2029
         * We also check that the actual class of the candidate matches (can be a sub-class of FamixType).
2030
         * @param bnd -- a JDT binding that we are trying to match to the candidate
2031
         * @param name of the type
2032
         * @param owner of the type
2033
         * @param candidate -- a Famix NamedEntity (Class, Type, PrimitiveType, Enum, AnnotationType)
2034
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
2035
         */
2036
        private <T extends TWithTypes & TNamedEntity> boolean matchAndMapType(ITypeBinding bnd, String name, TNamedEntity owner, TNamedEntity candidate) {
2037
                if (! (candidate instanceof Type) ) {
3 !
2038
                        return false;
×
2039
                }
2040

2041
                // check whether bnd and candidate are already bound
2042
                CheckResult res = checkKeyMatch(bnd, candidate);
5 ✔
2043
                if (res == CheckResult.MATCH) {
3 ✔
2044
                        return true;
2 ✔
2045
                }
2046
                else if (res == CheckResult.FAIL) {
3 ✔
2047
                        return false;
2 ✔
2048
                }
2049

2050
                if ( (bnd != null) && (bnd.isArray()) ) {
5 ✔
2051
                                bnd = bnd.getElementType();
3 ✔
2052
                }
2053

2054
                // checking names
2055
                if ( (bnd != null) && (bnd.isParameterizedType() || bnd.isRawType()) ) {
8 !
2056
                        name = bnd.getErasure().getName();
×
2057
                }
2058
                else if (bnd != null) {
2 ✔
2059
                        name = bnd.getName();
3 ✔
2060
                }
2061
                // else name = name
2062
                if (checkNameMatch(null, name, candidate) == CheckResult.FAIL) {
7 ✔
2063
                        return false;
2 ✔
2064
                }
2065

2066
                // special case of primitive types
2067
                if (candidate instanceof PrimitiveType) {
3 !
2068
                        if ( (bnd != null) && bnd.isPrimitive() ) {
×
2069
                                // names are equal so it's OK
2070
                                conditionalMapToKey(bnd, candidate);
×
2071
                                return true;
×
2072
                        }
2073
                        else if ( (bnd == null) && (owner == null) ) {
×
2074
                                return true;
×
2075
                        }
2076
                }
2077

2078
                // check owners without bnd
2079
                if (bnd == null) {
2 ✔
2080
                        return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
7 ✔
2081
                }
2082

2083
                // check owners with bnd
2084
                // type is an annotation
2085
                if (bnd.isAnnotation() && (candidate instanceof AnnotationType)) {
6 !
2086
                        if (matchAndMapPackage(bnd.getPackage(), owner.getName(), (Package) Util.getOwner(owner), Util.getOwner(candidate))) {
13 !
2087
                                conditionalMapToKey(bnd, candidate);
4 ✔
2088
                                return true;
2 ✔
2089
                        } else {
2090
                                return false;
×
2091
                        }
2092
                }
2093

2094
                // check owners with bnd
2095
                // type is a Parameterized type
2096
                if ((bnd.isParameterizedType() || bnd.isRawType()) && (candidate instanceof ParametricClass)) {
6 !
2097
                        return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
×
2098
                }
2099

2100
                // check owners with bnd
2101
                // type is an Enum
2102
                if (bnd.isEnum() && (candidate instanceof Enum)) {
3 !
2103
                        return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
×
2104
                }
2105

2106
                // check owners with bnd
2107
                // type is something elae (a class or interface)
2108
                // Annotation are interfaces too, so we should check this one after isAnnotation
2109
                if ( bnd.isClass()) {
3 !
2110
                        return matchAndMapClass(bnd, name, owner, (Type) candidate);
×
2111
                }
2112

2113
                if(bnd.isInterface()) {
3 !
2114
                        return matchAndMapInterface(bnd, name, owner, (Type) candidate);
×
2115
                }
2116

2117
                return false;
2 ✔
2118
        }
2119

2120
        /**
2121
         * Checks whether the existing unmapped Famix Class (or Interface) matches the binding.
2122
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
2123
         * @param bnd -- a JDT binding that we are trying to match to the candidate
2124
         * @param name of the class
2125
         * @param owner of the class
2126
         * @param candidate -- a Famix Entity
2127
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
2128
         */
2129
        private boolean matchAndMapClass(ITypeBinding bnd, String name, TNamedEntity owner, TType candidate) {
2130
                if (!(candidate instanceof Class)) {
3 !
2131
                        return false;
×
2132
                }
2133

2134
                // check whether bnd and candidate are already bound
2135
                CheckResult res = checkKeyMatch(bnd, candidate);
5 ✔
2136
                if (res == CheckResult.MATCH) {
3 ✔
2137
                        return true;
2 ✔
2138
                } else if (res == CheckResult.FAIL) {
3 ✔
2139
                        return false;
2 ✔
2140
                }
2141

2142
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7 !
2143
                        return false;
×
2144
                }
2145

2146
                // checking owner
2147
                return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
7 ✔
2148
        }
2149

2150
        /**
2151
         * Checks whether the existing unmapped Famix Class (or Interface) matches the binding.
2152
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
2153
         * @param bnd -- a JDT binding that we are trying to match to the candidate
2154
         * @param name of the class
2155
         * @param owner of the class
2156
         * @param candidate -- a Famix Entity
2157
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
2158
         */
2159
        private boolean matchAndMapInterface(ITypeBinding bnd, String name, TNamedEntity owner, Type candidate) {
2160
                if (!(candidate instanceof Interface)) {
3 !
2161
                        return false;
×
2162
                }
2163

2164
                // check whether bnd and candidate are already bound
2165
                CheckResult res = checkKeyMatch(bnd, candidate);
5 ✔
2166
                if (res == CheckResult.MATCH) {
3 ✔
2167
                        return true;
2 ✔
2168
                } else if (res == CheckResult.FAIL) {
3 ✔
2169
                        return false;
2 ✔
2170
                }
2171

2172
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7 !
2173
                        return false;
×
2174
                }
2175

2176
                // checking owner
2177
                return matchAndMapTypeOwner(bnd, owner, candidate);
6 ✔
2178
        }
2179

2180
        /**
2181
         * Checks whether the existing unmapped Famix "Method" matches the binding.
2182
         * Checks that the candidate has the same name and same signature as the JDT bound method, and checks recursively that owners also match.
2183
         * Note that AnnotationTypeAttribute are treated as methods by JDT, so they are checked here.
2184
         * @param bnd -- a JDT binding that we are trying to match to the candidate
2185
         * @param sig -- signature of the method
2186
         * @param retTyp -- return type of the method
2187
         * @param owner of the method
2188
         * @param candidate -- a Famix Entity (regular Method or AnnotationTypeAttribute)
2189
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
2190
         */
2191
        private  boolean matchAndMapMethod(IMethodBinding bnd, String sig, TType retTyp, TNamedEntity owner, NamedEntity candidate) {
2192
                if (! (candidate instanceof Method) ) {
3 ✔
2193
                        return false;
2 ✔
2194
                }
2195

2196
                // check whether bnd and candidate are already bound
2197
                CheckResult res = checkKeyMatch(bnd, candidate);
5 ✔
2198
                if (res == CheckResult.MATCH) {
3 ✔
2199
                        return true;
2 ✔
2200
                }
2201
                else if (res == CheckResult.FAIL) {
3 ✔
2202
                        return false;
2 ✔
2203
                }
2204

2205
                // checking names
2206
                String name = (sig != null) ? sig.substring(0, sig.indexOf('(')) : null;
10 !
2207
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7 ✔
2208
                        return false;
2 ✔
2209
                }
2210

2211
                // for methods, the name is not enough, we must test the signature also
2212
                // but not for AnnotationTypeAttribute
2213

2214
                        if (bnd != null) {
2 ✔
2215
                                sig = bnd.getName() + "(" + signatureParamsFromBinding(bnd) + ")";
7 ✔
2216
                        }
2217
                        if (! ((Method) candidate).getSignature().equals(sig)) {
6 ✔
2218
                                return false;
2 ✔
2219
                        }
2220

2221
                        // and still for method, must also check the return type
2222
                        if (bnd != null) {
2 ✔
2223
                                if (isConstructorBinding(bnd)) {
4 !
2224
                                        if ( ((Method) candidate).getDeclaredType() != null ) {
×
2225
                                                return false;
×
2226
                                        }
2227
                                        // else OK for now
2228
                                }
2229
                                else { // not a constructor
2230
                                        if ( ((Method) candidate).getDeclaredType() == null ) {
4 !
2231
                                                return false;
×
2232
                                        }
2233
                                        else if (! matchAndMapType(bnd.getReturnType(), null, null, ((Method) candidate).getDeclaredType()) ) {
10 ✔
2234
                                                return false;
2 ✔
2235
                                        }
2236
                                        // else OK for now
2237
                                }
2238
                        }
2239
                        else {  // bnd == null
2240
                                if (retTyp == null) { // similar to (bnd.isConstructor())
2 !
2241
                                        if ( ((Method) candidate).getDeclaredType() != null ) {
4 ✔
2242
                                                return false;
2 ✔
2243
                                        }
2244
                                        // else OK for now
2245
                                } else { // (ret != null)  i.e. not a constructor
2246
                                        if (((Method) candidate).getDeclaredType() == null) {
×
2247
                                                return false;
×
2248
                                        } else if (!matchAndMapType(null, retTyp.getName(), Util.getOwner(retTyp), (NamedEntity) ((Method) candidate).getDeclaredType())) {
×
2249
                                                return false;
×
2250
                                        }
2251
                                        // else OK for now
2252
                                }
2253
                        }
2254

2255

2256
                // check owner
2257
                if (matchAndMapOwnerAsType(((bnd != null) ? bnd.getDeclaringClass() : null), owner, Util.getOwner(candidate)) == CheckResult.MATCH) {
13 ✔
2258
                        conditionalMapToKey(bnd, candidate);
4 ✔
2259
                        return true;
2 ✔
2260
                } else {
2261
                        return false;
2 ✔
2262
                }
2263
        }
2264

2265
        /** testing that a method binding is for a constructor
2266
         * There is a special case for "diamond constructors" (eg: <code>new HashSet<>()</code>)
2267
         */
2268
        protected boolean isConstructorBinding(IMethodBinding bnd) {
2269
                if (bnd.isConstructor()) {
3 ✔
2270
                        return true;
2 ✔
2271
                }
2272
                if ( bnd.getName().equals("<factory>") ) {
5 ✔
2273
                        return true;
2 ✔
2274
                }
2275
                return false;
2 ✔
2276
        }
2277

2278
        /**
2279
         * Checks whether the candidate (an existing unmapped Famix "Variable" like Attribute, Parameter, ...) matches the binding.
2280
         * Checks that the candidate has the same name as the JDT bound variable, and checks recursively that owners also match.
2281
         * The Famix candidate is a NamedEntity and not a StructuralEntity to allow dealing with Famix EnumValue that JDT treats as variables
2282
         * @param bnd -- a JDT binding that we are trying to match to the candidate
2283
         * @param name of the variable
2284
         * @param owner of the variable
2285
         * @param candidate -- a Famix Entity (a StructuralEntity or an EnumValue)
2286
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
2287
         */
2288
        private boolean matchAndMapVariable(IVariableBinding bnd, String name, TNamedEntity owner, TNamedEntity candidate) {
2289
                if (!(candidate instanceof TStructuralEntity)) {
3 !
2290
                        return false;
×
2291
                }
2292

2293
                // check whether bnd and candidate are already bound
2294
                CheckResult keyMatch = checkKeyMatch(bnd, candidate);
5 ✔
2295
                if (keyMatch == CheckResult.MATCH) {
3 !
2296
                        return true;
×
2297
                } else if (keyMatch == CheckResult.FAIL) {
3 ✔
2298
                        return false;
2 ✔
2299
                }
2300

2301
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7 !
2302
                        return false;
×
2303
                }
2304

2305
                // check owner
2306
                TNamedEntity candidateOwner = Util.getOwner(candidate);
3 ✔
2307

2308
                // local variable or parameter ?
2309
                // owner is a Method? (for example in case of an anonymous class)
2310
                CheckResult res = matchAndMapOwnerAsMethod(((bnd != null) ? bnd.getDeclaringMethod() : null), owner, candidateOwner);
11 ✔
2311
                if (res == CheckResult.FAIL) {
3 ✔
2312
                        return false;
2 ✔
2313
                } else if (res == CheckResult.MATCH) {
3 !
2314
                        conditionalMapToKey(bnd, candidate);
×
2315
                        return true;
×
2316
                }
2317

2318
                // check owner
2319
                // <anArray>.length field?
2320
                if (name.equals("length")) {
4 !
2321
                        boolean isArrayLengthField = ((bnd != null) && (bnd.getDeclaringClass() == null)) ||
×
2322
                                                                                 ((bnd == null) && (owner.getName().equals(EntityDictionary.ARRAYS_NAME)));
×
2323
                        if (isArrayLengthField) {
×
2324
                                if (candidateOwner.getName().equals(EntityDictionary.ARRAYS_NAME)) {
×
2325
                                        conditionalMapToKey(bnd, candidate);
×
2326
                                        return true;
×
2327
                                }
2328
                                else {
2329
                                        return false;
×
2330
                                }
2331
                        }
2332
                }
2333

2334
                // check owner
2335
                // "normal" field?
2336
                res = matchAndMapOwnerAsType( ((bnd != null) ? bnd.getDeclaringClass() : null), owner, candidateOwner);
11 ✔
2337
                if (res == CheckResult.MATCH) {
3 !
2338
                        conditionalMapToKey(bnd, candidate);
4 ✔
2339
                        return true;
2 ✔
2340
                }
2341
                return false;
×
2342
        }
2343

2344
        /**
2345
         * Checks whether the existing unmapped Famix Type's parent (or owner) matches the binding's owner.
2346
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
2347
         * @param bnd -- a JDT binding whose owner we are trying to match to the candidate's owner
2348
         * @param owner -- the owner of the type
2349
         * @param candidate -- a Famix Entity
2350
         * @return whether we found a match (if <b>true</b>, the mapping is recorded)
2351
         */
2352
        private boolean matchAndMapTypeOwner(ITypeBinding bnd, TNamedEntity owner, Type candidate) {
2353
                ContainerEntity candidateOwner = Util.getOwner(candidate);
4 ✔
2354

2355
                // owner is a Method? (for example in case of an anonymous class)
2356
                CheckResult res = matchAndMapOwnerAsMethod(((bnd != null) ? bnd.getDeclaringMethod() : null), owner, candidate);
11 ✔
2357
                if (res == CheckResult.MATCH) {
3 !
2358
                        conditionalMapToKey(bnd, candidate);
×
2359
                        return true;
×
2360
                } else if (res == CheckResult.FAIL) {
3 !
2361
                        return false;
×
2362
                }
2363

2364
                // owner is a class ?
2365
                res = matchAndMapOwnerAsType(((bnd != null) ? bnd.getDeclaringClass() : null), owner, candidateOwner);
11 ✔
2366
                if (res == CheckResult.MATCH) {
3 ✔
2367
                        conditionalMapToKey(bnd, candidate);
4 ✔
2368
                        return true;
2 ✔
2369
                }
2370
                else if (res == CheckResult.FAIL) {
3 ✔
2371
                        return false;
2 ✔
2372
                }
2373

2374
                // owner must be a package
2375
                if (matchAndMapOwnerAsNamespace( ((bnd != null)?bnd.getPackage():null), owner, candidateOwner) == CheckResult.MATCH) {
12 ✔
2376
                        conditionalMapToKey(bnd, candidate);
4 ✔
2377
                        return true;
2 ✔
2378
                }
2379
                return false;
2 ✔
2380
        }
2381

2382
        /**
2383
         * Check whether the owner of candidates is a method macthinf either methBnd or owner
2384
         * @param methBnd
2385
         * @param owner
2386
         * @param candidateOwner
2387
         * @return a {@link CheckResult}
2388
         */
2389
        private  <T extends TNamedEntity> CheckResult matchAndMapOwnerAsMethod(IMethodBinding methBnd, T owner, T candidateOwner) {
2390
                if ((methBnd != null) || (owner instanceof Method)) {
5 !
2391
                        if (!(candidateOwner instanceof Method)) {
3 !
2392
                                return CheckResult.FAIL;
×
2393
                        }
2394

2395
                        ContainerEntity ownerOwner = (owner != null) ? (ContainerEntity) Util.getOwner(owner) : null;
7 !
2396
                        String ownerSig = (owner != null) ? ((Method) owner).getSignature() : null;
7 !
2397
                        Type ownerReturn = (owner != null) ? (Type) ((Method) owner).getDeclaredType() : null;
8 !
2398

2399
                        if (matchAndMapMethod(methBnd, ownerSig, ownerReturn, ownerOwner, (Method) candidateOwner)) {
9 !
2400
                                return CheckResult.MATCH;
×
2401
                        } else {
2402
                                return CheckResult.FAIL;
2 ✔
2403
                        }
2404
                }
2405
                return CheckResult.UNDECIDED;
2 ✔
2406
        }
2407

2408
        /**
2409
         * @param typBnd
2410
         * @param owner
2411
         * @param candidateOwner
2412
         * @return a {@link CheckResult}
2413
         */
2414
        private CheckResult matchAndMapOwnerAsType(ITypeBinding typBnd, TNamedEntity owner, TNamedEntity candidateOwner) {
2415
                if ((typBnd != null) || (owner instanceof Type)) {
5 ✔
2416
                        if (!(candidateOwner instanceof Type)) {
3 ✔
2417
                                return CheckResult.FAIL;
2 ✔
2418
                        }
2419

2420
                        TNamedEntity ownerOwner = (owner != null) ? Util.getOwner(owner) : null;
6 !
2421
                        String ownerName = (owner != null) ? owner.getName() : null;
6 !
2422

2423
                        if (matchAndMapType(typBnd, ownerName, ownerOwner, candidateOwner)) {
7 ✔
2424
                                return CheckResult.MATCH;
2 ✔
2425
                        } else {
2426
                                return CheckResult.FAIL;
2 ✔
2427
                        }
2428
                }
2429
                return CheckResult.UNDECIDED;
2 ✔
2430
        }
2431

2432
        private CheckResult matchAndMapOwnerAsNamespace(IPackageBinding pckgBnd, TNamedEntity owner, ContainerEntity candidateOwner) {
2433
                if ((pckgBnd != null) || (owner instanceof Package)) {
5 !
2434
                        if (!(candidateOwner instanceof Package)) {
3 !
2435
                                return CheckResult.FAIL;
×
2436
                        }
2437

2438
                        Package ownerOwner = (owner != null) ? (Package) Util.getOwner(owner) : null;
7 !
2439
                        String ownerName = (owner != null) ? owner.getName() : null;
6 !
2440

2441
                        if (matchAndMapPackage(pckgBnd, ownerName, ownerOwner, candidateOwner)) {
7 ✔
2442
                                return CheckResult.MATCH;
2 ✔
2443
                        } else {
2444
                                return CheckResult.FAIL;
2 ✔
2445
                        }
2446
                }
2447
                return CheckResult.UNDECIDED;
×
2448
        }
2449

2450
        /**
2451
         * Checks whether the name and the candidate matches the name of the entity (given either by 'bnd' or 'name')<br>
2452
         * 'name' and 'bnd' cannot be null together
2453
         * @param bnd -- binding associated with the entity may be null
2454
         * @param name -- name of the entity may be null
2455
         * @param candidate
2456
         * @return true if names match, false if not
2457
         */
2458
        private CheckResult checkNameMatch(IBinding bnd, String name, TNamedEntity candidate) {
2459
                if ( (bnd != null) && (! bnd.getName().equals(candidate.getName())) ) {
8 ✔
2460
                        return CheckResult.FAIL;
2 ✔
2461
                }
2462
                else if ( (bnd == null) && (name != null) && (! name.equals(candidate.getName())) ) {
9 !
2463
                        return CheckResult.FAIL;
2 ✔
2464
                }
2465
                else {
2466
                        return CheckResult.MATCH;
2 ✔
2467
                }
2468
        }
2469

2470
        /**
2471
         * Check whether key and candidate are already bound together, whether either is bound to something else, or whether none is bound
2472
         * @param key
2473
         * @param candidate
2474
         * @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>
2475
         */
2476
        private CheckResult checkKeyMatch(IBinding key, TNamedEntity candidate) {
2477
                if (key == null) {
2 ✔
2478
                        return CheckResult.UNDECIDED;
2 ✔
2479
                }
2480

2481
                NamedEntity bound = (NamedEntity)getEntityByKey(key);
5 ✔
2482
                if (bound == candidate) {
3 ✔
2483
                        return CheckResult.MATCH;
2 ✔
2484
                }
2485
                else if (bound != null) {
2 ✔
2486
                        return CheckResult.FAIL;
2 ✔
2487
                }
2488
                else if (getEntityKey(candidate) != null) {
4 ✔
2489
                        //we check if the both bindings are linked to the same entity
2490
                        IBinding candidateBinding = getEntityKey(candidate);
4 ✔
2491
                        if(candidateBinding.getKey().equals(key.getKey())) {
6 ✔
2492
                                return CheckResult.MATCH;
2 ✔
2493
                        }
2494
                        
2495
                        //if candidate is a stub, we accept multiple recovery binding
2496
                        //this code will be upgraded with the possibility to add manually code to entities
2497
                        if (candidate instanceof TCanBeStub && ((TCanBeStub) candidate).getIsStub()) {
8 !
2498
                                return CheckResult.UNDECIDED;
2 ✔
2499
                        }
2500
      
2501
                        return CheckResult.FAIL;
2 ✔
2502
                }
2503
                else {
2504
                        return CheckResult.UNDECIDED;
2 ✔
2505
                }
2506
        }
2507

2508
        private void conditionalMapToKey(IBinding bnd, TNamedEntity ent) {
2509
                if (bnd != null) {
2 ✔
2510
                        mapEntityToKey(bnd, ent);
4 ✔
2511
                }
2512
        }
1 ✔
2513

2514
        public Method ensureFamixMethod(IMethodBinding bnd) {
2515
                return ensureFamixMethod(
8 ✔
2516
                                bnd,
2517
                                /*name*/null,
2518
                                /*paramsType*/null,
2519
                                /*returnType*/null,
2520
                                /*owner*/null,
2521
                                (bnd == null) ? UNKNOWN_MODIFIERS : bnd.getModifiers());
6 ✔
2522
        }
2523

2524
        /**
2525
         * Returns a Famix Method associated with the IMethodBinding.
2526
         * The Entity is created if it does not exist.
2527
         * @param name -- the name of the Famix Method (MUST NOT be null, but this is not checked)
2528
         * @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)
2529
         * @param owner -- type defining the method (should not be null, but it will work if it is)
2530
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
2531
         */
2532
        public Method ensureFamixMethod(IMethodBinding bnd, String name, Collection<String> paramTypes, TType ret, TWithMethods owner, int modifiers) {
2533
                Method fmx;
2534
                String signature;
2535
                boolean delayedRetTyp;
2536

2537
                // --------------- to avoid useless computations if we can
2538
                fmx = (Method)getEntityByKey(bnd);
5 ✔
2539
                if (fmx != null) {
2 ✔
2540
                        return fmx;
2 ✔
2541
                }
2542

2543
                // --------------- name
2544
                if (name == null) {
2 ✔
2545
                        if (bnd == null) {
2 ✔
2546
                                return null;
2 ✔
2547
                        }
2548
                        else {
2549
                                name = bnd.getName();
3 ✔
2550
                        }
2551
                }
2552

2553
                // --------------- signature
2554
                signature = name + "(";
3 ✔
2555
                 if (bnd != null) {
2 ✔
2556
                    signature += signatureParamsFromBinding(bnd);
7 ✔
2557
                }
2558
        else if (paramTypes != null) {
2 !
2559
                        signature += signatureParamsFromStringCollection(paramTypes);
7 ✔
2560
                }
2561
                else {
2562
                        signature += "???";
×
2563
                }
2564
                signature += ")";
3 ✔
2565

2566
                // --------------- return type
2567
                delayedRetTyp = false;
2 ✔
2568
                ITypeBinding retTypBnd = null;
2 ✔
2569
                if (ret == null) {
2 !
2570
                        if (bnd != null) {
2 ✔
2571
                // must create the return type
2572
                // but for method like "<T> T mtd()" where T belongs to mtd and mtd returns T,
2573
                // we need T to create the method and the method to create T ...
2574
                // so we need to test the situation and deal with it
2575
                retTypBnd = bnd.getReturnType();
3 ✔
2576
                if ( (retTypBnd != null) && retTypBnd.isTypeVariable() && (retTypBnd.getDeclaringMethod() == bnd) ) {
9 ✔
2577
                    delayedRetTyp = true;
3 ✔
2578
                }
2579
                else {
2580
                        ret = this.referredType(retTypBnd, fmx);
5 ✔
2581
                }
2582
                        }
2583
                }
2584

2585
                // --------------- owner
2586
                if (owner == null) {
2 ✔
2587
                        if (bnd == null) {
2 ✔
2588
                                owner = ensureFamixClassStubOwner();
4 ✔
2589
                        }
2590
                        else {
2591
                                ITypeBinding classBnd = bnd.getDeclaringClass().getErasure();
4 ✔
2592
                                if (classBnd != null) {
2 !
2593
                                        owner = ensureFamixType(classBnd);
5 ✔
2594
                                }
2595
                                else {
2596
                                        owner = ensureFamixClassStubOwner();
×
2597
                                }
2598
                        }
2599
                }
2600

2601
                // --------------- recover from name ?
2602
                for (Method candidate : this.getEntityByName(Method.class, name)) {
13 ✔
2603
                        if (matchAndMapMethod(bnd, signature, ret, (TNamedEntity) owner, candidate)) {
9 ✔
2604
                                fmx = candidate;
2 ✔
2605
                                break;
1 ✔
2606
                        }
2607
                }
1 ✔
2608

2609
                if (fmx == null) {
2 ✔
2610
                        if(bnd != null && bnd.isGenericMethod() && !isConstructorBinding(bnd)) {
9 ✔
2611
                                fmx = ensureFamixEntity(ParametricMethod.class, bnd, name);
7 ✔
2612
                                for(ITypeBinding param: bnd.getTypeParameters()) {
18 ✔
2613
                                        TypeParameter fmxParam = this.ensureFamixTypeParameter(param, null, fmx);
6 ✔
2614
                                        fmxParam.setGenericEntity((ParametricMethod)fmx);
4 ✔
2615
                                }
2616
                        // Parameterized method binding = when the method is the target of an invocation.
2617
                        } else if (bnd != null && bnd.isParameterizedMethod()) {
5 ✔
2618
                                fmx = this.ensureFamixMethod(bnd.getMethodDeclaration());
6 ✔
2619
                        } else {
2620
                if (bnd != null && isConstructorBinding(bnd)) {
6 ✔
2621
                    fmx = ensureFamixEntity(Initializer.class, bnd, name);
8 ✔
2622
                } else {
2623
                    fmx = ensureFamixEntity(Method.class, bnd, name);
7 ✔
2624
                }
2625
            }
2626

2627
                        fmx.setSignature(signature);
3 ✔
2628
                        ITypeBinding returnTypeBnd = (bnd == null) ? null : bnd.getReturnType();
7 ✔
2629
                        ensureFamixEntityTyping(returnTypeBnd, fmx, ret);
6 ✔
2630
                        fmx.setParentType(owner);
3 ✔
2631
                }
2632

2633
                if (fmx != null) {
2 !
2634
                        setMethodModifiers(fmx, modifiers);
4 ✔
2635
                }
2636

2637
        //If it has the #default keywork, we mark it as default implementation
2638
        if (Modifier.isDefault(modifiers)) {
3 ✔
2639
            fmx.setKind(DEFAULT_IMPLEMENTATION_KIND_MARKER);
3 ✔
2640
        }
2641

2642
        if (delayedRetTyp) {
2 ✔
2643
                        int retTypModifiers = retTypBnd.getModifiers();
3 ✔
2644
                        ITypeBinding returnTypeBnd = bnd.getReturnType();
3 ✔
2645
                        ensureFamixEntityTyping(returnTypeBnd, fmx, this.ensureFamixType(retTypBnd, /*name*/null, /*owner*/fmx, /*ctxt*/(ContainerEntity) owner, retTypModifiers));
13 ✔
2646
                }
2647

2648
                return fmx;
2 ✔
2649
        }
2650

2651

2652
        /**
2653
         * Creates or recovers the initializer method containing the attribute initializations of a type.
2654
         * @param owner Type containing the initializer
2655
         * @param isStatic Modifier of the initializer. A type can have 2 initializers for attribute initialization: 1 static and 1 not.
2656
         * @param isInitializationBlock True if the entity is an initialization block. False for the artificial method containing all field initializations.
2657
         * @return the FamixInitializer
2658
         */
2659
        public Initializer ensureFamixInitializer(TWithMethods owner, Boolean isStatic, Boolean isInitializationBlock) {
2660
                Initializer fmx = null;
2 ✔
2661

2662
                if (owner != null) {
2 !
2663
                        Optional<TMethod> existingInitializer = owner.getMethods().stream()
6 ✔
2664
                                        .filter(meth ->
1 ✔
2665
                                                        ((Method) meth).getIsInitializer() &&
8 ✔
2666
                                                        ((Method) meth).getIsConstructor().equals(false) &&
7 ✔
2667
                                                        ((Method) meth).getIsClassSide().equals(isStatic) &&
6 ✔
2668
                                                        ((Initializer) meth).getIsInitializationBlock().equals(isInitializationBlock))
7 ✔
2669
                                        .findFirst();
2 ✔
2670
                        if (existingInitializer.isPresent()) {
3 ✔
2671
                                fmx = (Initializer) existingInitializer.get();
4 ✔
2672
                        }
2673
                }
2674

2675
                if (fmx == null) {
2 ✔
2676
                        fmx = createFamixEntity(Initializer.class, INIT_BLOCK_NAME);
6 ✔
2677
                        fmx.setSignature(INIT_BLOCK_NAME + "()" );
3 ✔
2678
                        fmx.setVisibility(MODIFIER_PRIVATE);
3 ✔
2679
                        fmx.setParentType(owner);
3 ✔
2680
                        fmx.setIsClassSide(isStatic);
3 ✔
2681
                        fmx.setIsInitializationBlock(isInitializationBlock);
3 ✔
2682
                }
2683

2684
                return fmx;
2 ✔
2685
        }
2686

2687

2688
        public Initializer ensureImplicitConstructor(IMethodBinding binding, TWithMethods owner, String name, Collection<String> parameterTypesNames) {
2689
                Initializer fmx = null;
2 ✔
2690

2691
                if (fmx == null) {
2 !
2692
                        int modifiers = (binding != null) ? binding.getModifiers() : EntityDictionary.UNKNOWN_MODIFIERS;
7 ✔
2693
                        if (binding == null) {
2 ✔
2694
                                // OK! Binding is null, this is the default constructor!!
2695
                                // It has no source code :)
2696
                                fmx = ensureFamixEntity(Initializer.class, null, name);
7 ✔
2697
                                fmx.setParentType(owner);
3 ✔
2698
                                fmx.setSignature(name + "()");                                
5 ✔
2699
                        } else {
2700
                                // But, if we have the binding, that means the constructor exists. Let's just go the normal way
2701
                                fmx = (Initializer) this.ensureFamixMethod(binding, name, parameterTypesNames, /*ret type*/null, owner, modifiers);
10 ✔
2702
                        }
2703
                }
2704

2705
                return fmx;
2 ✔
2706
        }
2707

2708
        /**
2709
         * Creates or recovers a stub Famix Method
2710
         * @param name of the method
2711
         * @return the Famix Method
2712
         */
2713
        public Method ensureFamixStubMethod(String name) {
2714
                return ensureFamixMethod(null, name, /*paramType*/null, /*returnType*/null, ensureFamixClassStubOwner(), /*modifiers*/0);
×
2715
        }
2716

2717
        public void setAttributeModifiers(Attribute fmx, int mod) {
2718
                setCommonModifiers(fmx, mod, null);
5 ✔
2719
                fmx.setIsTransient(Modifier.isTransient(mod));
5 ✔
2720
                fmx.setIsVolatile(Modifier.isVolatile(mod));
5 ✔
2721
        }
1 ✔
2722

2723
        public void setMethodModifiers(Method fmx, int mod) {
2724
                setCommonModifiers(fmx, mod, (TWithTypes) fmx.getParentType());
7 ✔
2725
                fmx.setIsAbstract(Modifier.isAbstract(mod));
5 ✔
2726
                fmx.setIsSynchronized(Modifier.isSynchronized(mod));
5 ✔
2727
        }
1 ✔
2728

2729
        public void setClassModifiers(Class fmx, int mod, TWithTypes owner) {
2730
                setCommonModifiers(fmx, mod, null);
5 ✔
2731
                fmx.setIsAbstract(Modifier.isAbstract(mod));
5 ✔
2732
        }
1 ✔
2733

2734
        public void setInterfaceModifiers(Interface fmx, int mod) {
2735
                setCommonModifiers(fmx, mod, null);
5 ✔
2736
        }
1 ✔
2737

2738
        private void setCommonModifiers(Entity fmx, int mod, TWithTypes owner) {
2739
                setVisibility((THasVisibility)fmx, mod, owner);
6 ✔
2740
                ((TCanBeClassSide)fmx).setIsClassSide(Modifier.isStatic(mod));
6 ✔
2741
                ((TCanBeFinal)fmx).setIsFinal(Modifier.isFinal(mod));
6 ✔
2742
        }
1 ✔
2743

2744
        /**
2745
         * Sets the visibility of a FamixNamedEntity
2746
         *
2747
         * @param fmx -- the FamixNamedEntity
2748
         * @param mod -- a description of the modifiers as understood by org.eclipse.jdt.core.dom.Modifier
2749
         */
2750
        public void setVisibility(THasVisibility fmx, int mod, TWithTypes owner) {
2751
                if (Modifier.isPublic(mod)) {
3 ✔
2752
                        fmx.setVisibility(MODIFIER_PUBLIC);
4 ✔
2753
                } else if (Modifier.isPrivate(mod)) {
3 ✔
2754
                        fmx.setVisibility(MODIFIER_PRIVATE);
4 ✔
2755
                } else if (Modifier.isProtected(mod)) {
3 ✔
2756
                        fmx.setVisibility(MODIFIER_PROTECTED);
4 ✔
2757
                } else {
2758
                        //Default visibility!
2759
                        //If we are in an interface, default visibility is public, otherwise package.
2760
                        if (owner instanceof Interface) {
3 ✔
2761
                                fmx.setVisibility(MODIFIER_PUBLIC);
4 ✔
2762
                        } else {
2763
                                fmx.setVisibility(MODIFIER_PACKAGE);
3 ✔
2764
                        }
2765
                }
2766
        }
1 ✔
2767

2768
        /**
2769
         * Returns a Famix Attribute associated with the IVariableBinding.
2770
         * The Entity is created if it does not exist.<br>
2771
         * @param name -- the name of the FAMIX Attribute (MUST NOT be null, but this is not checked)
2772
         * @param type -- Famix Type of the Attribute (should not be null, but it will work if it is)
2773
         * @param owner -- type defining the Attribute (should not be null, but it will work if it is)
2774
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
2775
         */
2776
        public Attribute ensureFamixAttribute(IVariableBinding bnd, String name, Type type, TWithAttributes owner) {
2777
                Attribute fmx;
2778

2779
                // --------------- to avoid useless computations if we can
2780
                fmx = (Attribute)getEntityByKey(bnd);
5 ✔
2781
                if (fmx != null) {
2 ✔
2782
                        return fmx;
2 ✔
2783
                }
2784

2785
                // --------------- name
2786
                if (name == null) {
2 !
2787
                        if (bnd == null) {
×
2788
                                return null;
×
2789
                        }
2790
                        else {
2791
                                name = bnd.getName();
×
2792
                        }
2793
                }
2794

2795
                // --------------- owner
2796
                if (owner == null) {
2 ✔
2797
                        if (bnd == null) {
2 !
2798
                                return null;  // what would be the interest of creating an attribute for which we ignore the declaring class?
×
2799
                        }
2800
                        else {
2801
                                if (bnd.getDeclaringClass() != null && bnd.getDeclaringClass().getErasure() != null) {
7 !
2802
                                        // Declaring class is the generic one if the class is parametric.
2803
                                        owner = (TWithAttributes)ensureFamixType(bnd.getDeclaringClass().getErasure());
8 ✔
2804
                                } else {
2805
                                        return null;  // what would be the interest of creating an attribute for which we ignore the declaring class?
2 ✔
2806
                                }
2807
                        }
2808
                }
2809

2810
                // --------------- recover from name ?
2811
                for (Attribute candidate : getEntityByName(Attribute.class, name)) {
13 ✔
2812
                        if (matchAndMapVariable(bnd, name, (TNamedEntity) owner, candidate)) {
8 ✔
2813
                                fmx = candidate;
2 ✔
2814
                                break;
1 ✔
2815
                        }
2816
                }
1 ✔
2817

2818
                if (fmx == null) {
2 ✔
2819
                        fmx = ensureFamixEntity(Attribute.class, bnd, name);
7 ✔
2820
                        fmx.setParentType( owner);
3 ✔
2821
                }
2822

2823
        fmx.setParentType(owner);
3 ✔
2824
        ITypeBinding declaredTypeBinding = (bnd == null) ? null : bnd.getType();
7 ✔
2825
        ensureFamixEntityTyping(declaredTypeBinding, fmx, type);
6 ✔
2826
        if (bnd != null) {
2 ✔
2827
            int mod = bnd.getModifiers();
3 ✔
2828
            setAttributeModifiers(fmx, mod);
4 ✔
2829
        }
2830

2831
        return fmx;
2 ✔
2832
        }
2833

2834
        public Attribute ensureFamixAttribute(IVariableBinding bnd, String name, TWithAttributes owner) {
2835
                return ensureFamixAttribute(bnd, name, /*declared type*/null, owner);
7 ✔
2836
        }
2837

2838
        /**
2839
         * helper method, we know the var exists, ensureFamixAttribute will recover it
2840
         */
2841
        public Attribute getFamixAttribute(IVariableBinding bnd, String name, TWithAttributes owner) {
2842
                return ensureFamixAttribute(bnd, name, /*declared type*/null, owner);
7 ✔
2843
        }
2844

2845
        /**
2846
         * Returns a Famix Parameter associated with the IVariableBinding.
2847
         * The Entity is created if it does not exist.<br>
2848
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
2849
         */
2850
        public Parameter ensureFamixParameter(IVariableBinding bnd, String name, Type typ, TMethod tMethod) {
2851
                Parameter fmx = null;
2 ✔
2852

2853
                // --------------- to avoid useless computations if we can
2854
                try {
2855
                        fmx = (Parameter)getEntityByKey(bnd);
5 ✔
2856
                }catch(Throwable e) {
×
2857
                        e.printStackTrace();
×
2858
                }
1 ✔
2859
                if (fmx != null) {
2 ✔
2860
                        return fmx;
2 ✔
2861
                }
2862

2863
                // --------------- name
2864
                if (name == null) {
2 !
2865
                        if (bnd == null) {
×
2866
                                return null;
×
2867
                        }
2868
                        else {
2869
                                name = bnd.getName();
×
2870
                        }
2871
                }
2872

2873
                // --------------- owner
2874
                if (tMethod == null) {
2 !
2875
                        if (bnd == null) {
×
2876
                                tMethod = ensureFamixStubMethod("<"+name+"_owner>");
×
2877
                        }
2878
                        else {
2879
                                tMethod = ensureFamixMethod(bnd.getDeclaringMethod());
×
2880
                        }
2881
                }
2882

2883
                // --------------- recover from name ?
2884
                for (Parameter candidate : getEntityByName(Parameter.class, name) ) {
13 ✔
2885
                        if ( matchAndMapVariable(bnd, name, tMethod, candidate) ) {
7 !
2886
                                fmx = candidate;
×
2887
                                break;
×
2888
                        }
2889
                }
1 ✔
2890

2891
                if (fmx == null) {
2 !
2892
                        fmx = ensureFamixEntity(Parameter.class, bnd, name);
7 ✔
2893
                }
2894

2895
                if (fmx != null) {
2 !
2896
                        fmx.setParentBehaviouralEntity(tMethod);
3 ✔
2897
                        ITypeBinding declaredTypeBnd = (bnd == null) ? null : bnd.getType();
5 !
2898
                        ensureFamixEntityTyping(declaredTypeBnd, fmx, typ);
6 ✔
2899
                }
2900

2901
                return fmx;
2 ✔
2902
        }
2903

2904
    /**
2905
         * Returns a Famix LocalVariable associated with the IVariableBinding.
2906
         * The Entity is created if it does not exist.<br>
2907
         * @param name -- the name of the FAMIX LocalVariable
2908
         * @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
2909
         */
2910
        public LocalVariable ensureFamixLocalVariable(IVariableBinding bnd, String name, TWithLocalVariables owner) {
2911
                LocalVariable fmx;
2912

2913
                // --------------- to avoid useless computations if we can
2914
                fmx = (LocalVariable)getEntityByKey(bnd);
5 ✔
2915
                if (fmx != null) {
2 !
2916
                        return fmx;
×
2917
                }
2918

2919
                // --------------- name
2920
                if (name == null) {
2 !
2921
                        if (bnd == null) {
×
2922
                                return null;
×
2923
                        }
2924
                        else {
2925
                                name = bnd.getName();
×
2926
                        }
2927
                }
2928

2929
                // --------------- owner
2930
                if (owner == null) {
2 !
2931
                        if (bnd == null) {
×
2932
                                return null;  // what would be the interest of a local variable for which we ignore the declaring method?
×
2933
                        }
2934
                        else {
2935
                                owner = ensureFamixMethod(bnd.getDeclaringMethod());
×
2936
                        }
2937
                }
2938

2939
                // --------------- recover from name ?
2940
                for (LocalVariable candidate : getEntityByName(LocalVariable.class, name) ) {
13 ✔
2941
                        if ( matchAndMapVariable(bnd, name, (TNamedEntity) owner, candidate) ) {
8 !
2942
                                fmx = candidate;
×
2943
                                break;
×
2944
                        }
2945
                }
1 ✔
2946

2947
                if (fmx == null) {
2 !
2948
                        fmx = ensureFamixEntity(LocalVariable.class, bnd, name);
7 ✔
2949
                        fmx.setParentBehaviouralEntity(owner);
3 ✔
2950
                }
2951

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

2955
        return fmx;
2 ✔
2956
        }
2957

2958
    /**
2959
         * Returns a FAMIX ImplicitVariable with the given <b>name</b> ("self" or "super") and corresponding to the <b>type</b>.
2960
         * If this ImplicitVariable does not exist yet, it is created
2961
         * @param name -- the name of the FAMIX ImplicitVariable (should be Dictionary.SELF_NAME or Dictionary.SUPER_NAME)
2962
         * @param type -- the Famix Type for this ImplicitVariable (should not be null)
2963
         * @param tMethod -- the ContainerEntity where the implicit variable appears (should be a method inside <b>type</b>)
2964
         * @return the FAMIX ImplicitVariable or null in case of a FAMIX error
2965
         */
2966
        public ImplicitVariable ensureFamixImplicitVariable(IBinding key, String name, TType type, TMethod tMethod) {
2967
                ImplicitVariable fmx;
2968
                fmx = ensureFamixEntity(ImplicitVariable.class, key, name);
7 ✔
2969
                fmx.setParentBehaviouralEntity(tMethod);
3 ✔
2970
                fmx.setIsStub(tMethod.getIsStub());
4 ✔
2971
                if (fmx.getTyping() == null) {
3 ✔
2972
                        ensureFamixEntityTyping(null, fmx, type);
7 ✔
2973
                } else {
2974
                        fmx.getTyping().setDeclaredType(type);
4 ✔
2975
                }
2976
                return fmx;
2 ✔
2977
        }
2978

2979
        public ImplicitVariable ensureFamixImplicitVariable(String name, TType tType, TMethod tMethod) {
2980
                IBinding bnd = ImplicitVarBinding.getInstance(tMethod, name);
4 ✔
2981
                return ensureFamixImplicitVariable(bnd, name, tType, tMethod);
7 ✔
2982
        }
2983

2984
        /**
2985
         * Creates and returns a Famix Comment and associates it with an Entity (ex: for Javadocs)
2986
         * @param jCmt -- the content (String) of the comment 
2987
         * @param owner -- the entity that is commented
2988
         * @return the Famix Comment
2989
         */
2990
        public Comment createFamixComment(org.eclipse.jdt.core.dom.Comment jCmt, TWithComments owner) {
2991
                Comment cmt = null;
2 ✔
2992

2993
                if ( (jCmt != null) && (owner != null) ) {
4 !
2994
                        
2995
                        cmt = new Comment();
4 ✔
2996
                        addSourceAnchor(cmt, jCmt);
5 ✔
2997
                        famixRepoAdd(cmt);
3 ✔
2998
                        cmt.setCommentedEntity(owner);
3 ✔
2999
                }
3000

3001
                return cmt;
2 ✔
3002
        }
3003

3004
        /**
3005
         * Creates and returns a Famix Comment and associates it with an Entity
3006
         * @param jCmt -- the content (String) of the comment 
3007
         * @param owner -- the entity that is commented
3008
         * @param content -- the text of the comment
3009
         * @return the Famix Comment
3010
         */
3011
        public Comment createFamixComment(org.eclipse.jdt.core.dom.Comment jCmt, TWithComments owner, String content) {
3012
                Comment cmt = null;
2 ✔
3013

3014
                if ( (jCmt != null) && (owner != null) ) {
4 !
3015
                        cmt = new Comment();
4 ✔
3016
                        cmt.setContent(content );
3 ✔
3017
                        famixRepoAdd(cmt);
3 ✔
3018
                        cmt.setCommentedEntity(owner);
3 ✔
3019
                }
3020

3021
                return cmt;
2 ✔
3022
        }
3023

3024
        /**
3025
         * Adds location information to a Famix Entity.
3026
         * 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.
3027
         * This method also creates some basic links between the entity and others (e.g. declaring container, return type, ...)
3028
         * @param fmx -- Famix Entity to add the anchor to
3029
         * @param node -- JDT ASTNode, where the information is extracted
3030
         * @return the Famix SourceAnchor added to fmx. May be null in case of incorrect parameter ('fmx' or 'ast' == null)
3031
         */
3032
        public SourceAnchor addSourceAnchor(TSourceEntity fmx, ASTNode node) {
3033
                IndexedFileAnchor fa;
3034

3035
                fa = createIndexedFileAnchor(node);
4 ✔
3036
                if ((fmx != null) && (fa != null)) {
4 !
3037
                        fmx.setSourceAnchor(fa);
3 ✔
3038
                        famixRepoAdd(fa);
3 ✔
3039
                }
3040

3041
                return fa;
2 ✔
3042
        }
3043

3044
        /**
3045
         * Special case of  {@linkplain #addSourceAnchor(TSourceEntity, ASTNode)} to add location information to a Famix Method.
3046
         */
3047
        public SourceAnchor addSourceAnchor(Method fmx, MethodDeclaration node) {
3048
                IndexedFileAnchor fa;
3049

3050
                fa = createIndexedFileAnchor(node);
4 ✔
3051
                if ((fmx != null) && (fa != null)) {
4 !
3052

3053
                        // may change the positions
3054
                        List<ASTNode> methodDeclarationModifiers = new ArrayList<>();
4 ✔
3055
                        methodDeclarationModifiers.addAll(node.modifiers());
5 ✔
3056
                        if (node.getName() != null) {
3 !
3057
                                methodDeclarationModifiers.add(node.getName());
5 ✔
3058
                        }
3059
                        if (node.getReturnType2() != null) {
3 ✔
3060
                                methodDeclarationModifiers.add(node.getReturnType2());
5 ✔
3061
                        }
3062
                        int beg = (methodDeclarationModifiers.stream().mapToInt(el -> el.getStartPosition()).min().getAsInt()) + 1;
12 ✔
3063
                        int end = node.getStartPosition() + node.getLength();
6 ✔
3064

3065
                        fa.setStartPos(beg);
4 ✔
3066
                        fa.setEndPos(end);
4 ✔
3067

3068
                        fmx.setSourceAnchor(fa);
3 ✔
3069
                        famixRepoAdd(fa);
3 ✔
3070
                }
3071

3072
                return fa;
2 ✔
3073
        }
3074

3075
        /**
3076
         * Gets the file name holding <code>node</code> and its start and end positions in the file.
3077
         * Information returned in the form of an IndexedFileAnchor
3078
         */
3079
        protected IndexedFileAnchor createIndexedFileAnchor(ASTNode node) {
3080
                IndexedFileAnchor fa;
3081
                
3082
                if (node == null) {
2 !
3083
                        return null;
×
3084
                }
3085

3086
                // position in source file
3087
                int beg = node.getStartPosition() + 1; // Java starts at 0, Moose at 1
5 ✔
3088
                int end = beg + node.getLength() - 1;
7 ✔
3089

3090
                // find source Compilation Unit
3091
                // there is a special case for the JDT Comment Nodes
3092
                if (node instanceof org.eclipse.jdt.core.dom.Comment) {
3 ✔
3093
                        node = ((org.eclipse.jdt.core.dom.Comment) node).getAlternateRoot();
5 ✔
3094
                } else {
3095
                        node = node.getRoot();
3 ✔
3096
                }
3097

3098
                fa = new IndexedFileAnchor();
4 ✔
3099
                fa.setStartPos(beg);
4 ✔
3100
                fa.setEndPos(end);
4 ✔
3101

3102
                fa.setFileName((String) node.getProperty(SOURCE_FILENAME_PROPERTY));
6 ✔
3103

3104
                return fa;
2 ✔
3105
        }
3106

3107
        /**
3108
         * Creates or recovers the Famix Class for "Object".
3109
         * Because "Object" is the root of the inheritance tree, it needs to be treated differently.
3110
         *
3111
         * @return a Famix class for "Object"
3112
         */
3113
        public Class ensureFamixClassObject() {
3114
        // 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...
3115
        Collection<Class> objects = getEntityByName(Class.class, "Object");
5 ✔
3116

3117
        for (Class entity : objects) {
10 ✔
3118
                //We need to cast because the type container is a FamixTWithType but all implementors of this should be named in Java...
3119
                if ("java.lang".equals(((TNamedEntity) entity.getTypeContainer()).getName())) {
7 ✔
3120
                    return entity;
2 ✔
3121
                }
3122
        }
1 ✔
3123

3124
        Class fmx = createFamixEntity(Class.class, "Object");
6 ✔
3125
        fmx.setTypeContainer(ensureFamixPackageJavaLang(null));
5 ✔
3126
                return fmx;
2 ✔
3127
        }
3128

3129
        /***
3130
         * We treat array types as parametrized types Array<T>.
3131
         * This keeps the meta-model simple: a single way to model different concepts.
3132
         * @return
3133
         */
3134
        public ParametricClass ensureParametricArrayClass() {
3135
                
3136
                Collection<ParametricClass> arrayClasses = getEntityByName(ParametricClass.class, "Array");
5 ✔
3137
                
3138
                for (ParametricClass entity : arrayClasses) {
10 ✔
3139
            //We need to cast because the type container is a FamixTWithType but all implementors of this should be named in Java...
3140
            if ("java.lang".equals(((TNamedEntity) entity.getTypeContainer()).getName())) {
7 !
3141
                return entity;
2 ✔
3142
            }
3143
                }
×
3144
                
3145
                //Create the parametric class
3146
                ParametricClass arrayClass = createFamixEntity(ParametricClass.class, "Array"); 
6 ✔
3147

3148
                //And now add the type parameter
3149
                TypeParameter fmxParam = this.ensureFamixTypeParameter(null, "T", arrayClass);
6 ✔
3150
                fmxParam.setGenericEntity(arrayClass);
3 ✔
3151
                fmxParam.setIsStub(true);
4 ✔
3152
                
3153
                //And the class is in java.lang
3154
                arrayClass.setTypeContainer(ensureFamixPackageJavaLang(null));
5 ✔
3155
                
3156
                return arrayClass;
2 ✔
3157
        }
3158

3159
        /**
3160
         * Ensures the Java meta-class: <pre>{@code java.lang.Class<>}</pre>
3161
         */
3162
        public Class ensureFamixMetaClass(ITypeBinding bnd) {
3163
                Package javaLang = ensureFamixPackageJavaLang((bnd == null) ? null : bnd.getPackage());
7 !
3164
                ParametricClass fmx = (ParametricClass) this.ensureFamixClass(null, METACLASS_NAME, javaLang, /*isGeneric*/true, Modifier.PUBLIC & Modifier.FINAL);
9 ✔
3165

3166
                if (fmx != null) {
2 !
3167
                        fmx.addTypeParameters(ensureFamixTypeParameter(null, "T", fmx));
7 ✔
3168
                }
3169

3170
                if ((fmx != null) && (fmx.getSuperInheritances() == null)) {
5 !
3171
                        ensureFamixInheritance(ensureFamixClassObject(), fmx, null, null);
×
3172
                }
3173

3174
                return fmx;
2 ✔
3175
        }
3176

3177
        public Class getFamixMetaClass(ITypeBinding bnd) {
3178
                Package javaLang = ensureFamixPackageJavaLang((bnd == null) ? null : bnd.getPackage());
7 !
3179
                return this.ensureFamixClass(null, METACLASS_NAME, javaLang, /*isGeneric*/true, UNKNOWN_MODIFIERS);
8 ✔
3180
        }
3181

3182
        /**
3183
         * Creates or recovers the Famix Class for all arrays (<pre>{@code <some-type> []}</pre>)
3184
         * In java arrays or objects of special classes (i.e. "I[" for an array of int).
3185
         * JDT does not create a binding for these classes, so we create a stub one here.
3186
         *
3187
         * @return a Famix class
3188
         */
3189
        public Class ensureFamixClassArray() {
3190
                Class fmx = ensureFamixUniqEntity(Class.class, null, ARRAYS_NAME);
×
3191
                if (fmx != null) {
×
3192
                        ensureFamixInheritance(ensureFamixClassObject(), fmx, /*prev*/null, null);
×
3193
                        fmx.setTypeContainer(ensureFamixPackageDefault());
×
3194

3195
                        // may be not needed anymore now that we use modifiers
3196
                        /*fmx.setIsAbstract(Boolean.FALSE);
3197
                        fmx.setIsFinal(Boolean.FALSE);
3198
                        fmx.setIsInterface(Boolean.FALSE); 
3199
                        fmx.setIsPrivate(Boolean.FALSE);
3200
                        fmx.setIsProtected(Boolean.FALSE);*/
3201
                        fmx.setVisibility(MODIFIER_PUBLIC);
×
3202
                }
3203

3204
                return fmx;
×
3205
        }
3206

3207
        public String removeLastPartOfPackageName(String qualifiedName) {
3208
                String ret;
3209
                int last = qualifiedName.lastIndexOf('.');
4 ✔
3210
                if (last > 0) {
2 ✔
3211
                        // recursively creating the parent
3212
                        ret = qualifiedName.substring(0, last);
6 ✔
3213
                }
3214
                else {
3215
                        ret = "";
2 ✔
3216
                }
3217

3218
                return ret;
2 ✔
3219
        }
3220

3221
        /** Generates the list of parameters for a method signature
3222
         * @return a string
3223
         */
3224
        protected String signatureParamsFromBinding(IMethodBinding bnd) {
3225
                boolean first = true;
2 ✔
3226
                String sig = "";
2 ✔
3227

3228
                for (ITypeBinding parBnd : bnd.getParameterTypes()) {
17 ✔
3229
                        if (first) {
2 ✔
3230
                                sig = parBnd.getName();
3 ✔
3231
                                first = false;
3 ✔
3232
                        }
3233
                        else {
3234
                                sig += "," + parBnd.getName();
5 ✔
3235
                        }
3236
                }
3237
                return sig;
2 ✔
3238
        }
3239

3240
        private String signatureParamsFromStringCollection(Collection<String> paramTypes) {
3241
                boolean first = true;
2 ✔
3242
                String sig = "";
2 ✔
3243

3244
                for (String t : paramTypes) {
10 ✔
3245
                        if (first) {
2 ✔
3246
                                sig = t;
2 ✔
3247
                                first = false;
3 ✔
3248
                        }
3249
                        else {
3250
                                sig += "," + t;
4 ✔
3251
                        }
3252
                }
1 ✔
3253
                return sig;
2 ✔
3254
        }
3255

3256
        public String findTypeName(org.eclipse.jdt.core.dom.Type t) {
3257
                if (t == null) {
×
3258
                        return null;
×
3259
                }
3260

3261
                if (t.isPrimitiveType()) {
×
3262
                        return t.toString();
×
3263
                } else if (t.isSimpleType()) {
×
3264
                        String fullName = ((SimpleType) t).getName().getFullyQualifiedName();
×
3265
                        int i = fullName.lastIndexOf('.');
×
3266
                        if (i > 0) {
×
3267
                                return fullName.substring(i+1);
×
3268
                        }
3269
                        else {
3270
                                return fullName;
×
3271
                        }
3272
                } else if (t.isQualifiedType()) {
×
3273
                        return ((QualifiedType) t).getName().getIdentifier();
×
3274
                } else if (t.isArrayType()) {
×
3275
                        return findTypeName(((ArrayType) t).getElementType());
×
3276
                } else if (t.isParameterizedType()) {
×
3277
                        return findTypeName(((org.eclipse.jdt.core.dom.ParameterizedType) t).getType());
×
3278
                } else { // it is a WildCardType
3279
                        if (((org.eclipse.jdt.core.dom.WildcardType) t).isUpperBound()) {
×
3280
                                return findTypeName(((org.eclipse.jdt.core.dom.WildcardType) t).getBound());
×
3281
                        } else {
3282
                                return EntityDictionary.OBJECT_NAME;
×
3283
                        }
3284
                }
3285
        }
3286

3287
        /**
3288
         * Ensures the proper creation of a FamixType for JDT typ in the given context.
3289
         * Useful for parameterizedTypes, or classInstance.
3290
         *
3291
         * @param isClass we are sure that the type is actually a class
3292
         * @return a famix type or null
3293
         */
3294
        public <T extends TWithTypes & TNamedEntity> TType referredType(org.eclipse.jdt.core.dom.Type typ, T ctxt, boolean isClass) {
3295
                return this.referredType(typ, ctxt, isClass, /*isExcep*/false);
7 ✔
3296
        }
3297
        
3298
        /**
3299
         * Ensures the proper creation of a FamixType for JDT type in the given context.
3300
         * Useful for parameterizedTypes, or classInstance.
3301
         *
3302
         * @param isClass we are sure that the type is actually a class
3303
         * @return a famix type or null
3304
         */
3305
        public <T extends TWithTypes & TNamedEntity> TType referredType(org.eclipse.jdt.core.dom.Type typ, T ctxt, boolean isClass, boolean isException) {
3306
                if (typ == null) {
2 !
3307
                        return null;
×
3308
                } else if (typ.resolveBinding() != null) {
3 !
3309
                        return this.referredType(typ.resolveBinding(), ctxt);
7 ✔
3310
                }
3311
                // from here, we assume the owner is the context
3312
                else if (isClass && !isException) {
×
3313
                        return this.ensureFamixClass(null, findTypeName(typ), /*owner*/ctxt, /*isGeneric*/false,
×
3314
                                        EntityDictionary.UNKNOWN_MODIFIERS);
3315
                } else if (isException) {
×
3316
                        // return ensure FamixException
3317
                        return this.ensureFamixException(null, findTypeName(typ), (ContainerEntity) /*owner*/ctxt, /*isGeneric*/false,
×
3318
                                        EntityDictionary.UNKNOWN_MODIFIERS);
3319
                } else {
3320
                        while (typ.isArrayType()) {
×
3321
                                typ = ((ArrayType) typ).getElementType();
×
3322
                        }
3323

3324
                        if (typ.isPrimitiveType()) {
×
3325
                                return this.ensureFamixPrimitiveType(null, findTypeName(typ));
×
3326
                        } else {
3327
                                return this.ensureFamixType(null, findTypeName(typ), /*owner*/ctxt, /*container*/ctxt,
×
3328
                                                EntityDictionary.UNKNOWN_MODIFIERS);
3329
                        }
3330
                }
3331
        }
3332
        
3333
        public TType referredType(ITypeBinding bnd, TNamedEntity ctxt) {
3334
                return this.referredType(bnd, ctxt, 0);
6 ✔
3335
        }
3336
        
3337
        public TType referredType(ITypeBinding bnd, TNamedEntity ctxt, int extraDimensions) {
3338
                TType fmxTyp = null;
2 ✔
3339

3340
                if (bnd == null) {
2 ✔
3341
                        return null;
2 ✔
3342
                }
3343

3344
                String name;
3345
                //Three cases here:
3346
                // - the type binding knows it's an array
3347
                // - the binding is not declared as array, but it has dimensions > 0
3348
                // - the dimension is not in the type but in the variable declaration (thus extra)
3349
                if (bnd.isArray() || bnd.getDimensions() + extraDimensions > 0) {
8 ✔
3350
                        // We treat array types as parametrized types Array<T>.
3351
                        // This keeps the meta-model simple: a single way to model different concepts.
3352
                        return this.ensureParametricArrayClass();
3 ✔
3353
                }
3354
                name = bnd.getName();
3 ✔
3355

3356
                if ( bnd.isParameterizedType() ) {
3 ✔
3357
                        
3358
                        // remove type parameters from the name even for parameterized interfaces
3359
                        int i = name.indexOf('<');
4 ✔
3360
                        if (i > 0) {
2 ✔
3361
                                name = name.substring(0, i);
5 ✔
3362
                        }
3363

3364
                        ITypeBinding parameterizableBnd = bnd.getErasure();
3 ✔
3365
                        int modifiers = (parameterizableBnd != null) ? parameterizableBnd.getModifiers() : EntityDictionary.UNKNOWN_MODIFIERS;
6 !
3366
                        
3367
                        if(parameterizableBnd != null && parameterizableBnd.isInterface()) {
5 !
3368
                                fmxTyp = (ParametricInterface) this.ensureFamixInterface(parameterizableBnd, name, /*owner*/null, /*isGeneric*/true, modifiers);
10 ✔
3369
                        } else {
3370
                                fmxTyp = (ParametricClass) this.ensureFamixClass(parameterizableBnd, name, /*owner*/null, /*isGeneric*/true, modifiers);
9 ✔
3371
                        }
3372
                } else if ( (name != null) && name.equals("var") ) {
7 !
3373
                        fmxTyp = this.ensureFamixUniqEntity(org.moosetechnology.model.famix.famixjavaentities.Type.class, /*binding*/null, EntityDictionary.IMPLICIT_VAR_TYPE_NAME);        
×
3374
                } else {
3375
                        fmxTyp = this.ensureFamixType(bnd, name, /*owner*/null, (TWithTypes) ctxt, bnd.getModifiers());
10 ✔
3376
                }
3377

3378
                return fmxTyp;
2 ✔
3379
        }
3380

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

© 2026 Coveralls, Inc