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

moosetechnology / VerveineJ / 38732768188

15 Oct 2026 12:11PM UTC coverage: 53.987% (+0.08%) from 53.905%
38732768188

Pull #288

github

web-flow
Merge da4573601 into eb7be674b
Pull Request #288: Fix enum constant anonymous dispatch candidates

2142 of 4122 branches covered (51.97%)

Branch coverage included in aggregate %.

27 of 30 new or added lines in 3 files covered. (90.0%)

4662 of 8481 relevant lines covered (54.97%)

2.27 hits per line

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

75.55
/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 !
NEW
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
                }
NEW
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 = (ParametricInheritance)buildFamixParametricAssociation(new ParametricInheritance(), supBnd.getErasure().getTypeParameters(), supBnd.getTypeArguments());
13 ✔
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
                return inh;
2 ✔
525
        }
526
        
527
        /**
528
         * Creates the concretization between the type parameters of the generic entity that is target of an association 
529
         * and the concrete types that concretize them in this association.
530
         * @param association -- the association that must be linked to # or several concretizations
531
         * @param typeParameters -- the collection of type parameters declared in the generic entity
532
         * @param typeArguments -- the collection of concrete types linked to this association
533
         * @return the parametric association
534
         */
535
        public  <T extends TParametricEntity> TParametricAssociation buildFamixParametricAssociation(TParametricAssociation association, ITypeBinding[] typeParameters, ITypeBinding[] typeArguments
536
        ) {
537
                
538
                Iterator<ITypeBinding> genericIterator = Arrays.asList(typeParameters).iterator();
4 ✔
539
                Iterator<ITypeBinding> concreteIterator = Arrays.asList(typeArguments).iterator();
4 ✔
540

541
                while (concreteIterator.hasNext() && genericIterator.hasNext()) {
6 ✔
542
                        TTypeArgument typeArgument = (TTypeArgument)ensureFamixType(concreteIterator.next());
7 ✔
543
                        TypeParameter typeParameter = (TypeParameter)ensureFamixType(genericIterator.next());
7 ✔
544

545
                        Concretization concretization = ensureFamixConcretization(typeArgument, typeParameter);
5 ✔
546
                        association.addConcretization(concretization);
3 ✔
547
                }
1 ✔
548

549
                return association;
2 ✔
550
        }
551

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

563
                Concretization concretization = new Concretization();
4 ✔
564
                concretization.setTypeArgument(typeArgument);
3 ✔
565
                concretization.setTypeParameter(typeParameter);
3 ✔
566

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

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

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

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

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

641
                if (prev == null) {
2 ✔
642
                        for (TReference existingRef : src.getOutgoingReferences()) {
7 !
643
                                if (existingRef.getReferredEntity() == tgt) {
×
644
                                        return (Reference) existingRef;
×
645
                                }
646
                        }
×
647
                }
648

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

658
                                Concretization concretization = ensureFamixConcretization(typeArgument, typeParameter);
5 ✔
659
                                ((ParametricReference)ref).addConcretization(concretization);
4 ✔
660
                        }
661
                }
662

663
                if (ref == null){
2 ✔
664
                        ref = new Reference();
4 ✔
665
                }
666

667
                ref.setReferredEntity(tgt);
3 ✔
668
                ref.setReferencer(src);
3 ✔
669
                chainPrevNext(prev,ref);
4 ✔
670
                famixRepoAdd(ref);
3 ✔
671

672
                return ref;
2 ✔
673
        }
674

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

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

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

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

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

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

779

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

801
                                Concretization concretization = ensureFamixConcretization(typeArgument, typeParameter);
5 ✔
802
                                ((ParametricEntityTyping)typing).addConcretization(concretization);
4 ✔
803
                        }
804
                }
805

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

818

819
        ///// Special Case: ImplicitVariables /////
820

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

854
                return ret;
×
855
        }
856

857
        ///// Special Case: "Uniq" Entities /////
858

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

896
                return fmx;
2 ✔
897
        }
898

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

918
                return fmx;
2 ✔
919
        }
920

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

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

950
                if ((name == null) && (bnd != null)) {
4 !
951
                        name = bnd.getName();
3 ✔
952
                }
953

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

972
                return fmx;
2 ✔
973
        }
974

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

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

1003
        return this.ensureFamixPackage(bnd, OBJECT_PACKAGE_NAME);
5 ✔
1004
        }
1005

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

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

1030
                if (bnd == null) {
2 !
1031
                        if (name == null) {
×
1032
                                return null;
×
1033
                        }
1034
                        fmx = searchTypeInContext(name, ctxt); // WildCard Types don't have binding
×
1035
                        if (fmx != null) {
×
1036
                                return fmx;
×
1037
                        }
1038

1039
                        if ((owner instanceof TParametricEntity)) {
×
1040
                                return this.ensureFamixTypeParameter(null, name, owner);
×
1041
                        }
1042
                        else {
1043
                                fmx = ensureFamixEntity(Type.class, bnd, name);
×
1044
                                fmx.setTypeContainer(owner);
×
1045
                                return fmx;
×
1046
                        }
1047
                }
1048

1049
                // bnd != null
1050

1051
                fmx = (Type) getEntityByKey(bnd);
5 ✔
1052
                if (fmx != null) {
2 ✔
1053
                        return fmx;
2 ✔
1054
                }
1055

1056
                if (bnd.isArray()) {
3 !
1057
                        bnd = bnd.getElementType();
×
1058
                }
1059

1060
                if (bnd.isPrimitive()) {
3 ✔
1061
                        return this.ensureFamixPrimitiveType(bnd, name);
5 ✔
1062
                }
1063

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

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

1081
                if (bnd.isAnnotation()) {
3 ✔
1082
                        return this.ensureFamixAnnotationType(bnd, name, (ContainerEntity) owner);
7 ✔
1083
                }
1084

1085
                if (bnd.isInterface()) {
3 ✔
1086
                        return this.ensureFamixInterface(bnd, name, owner, /*isGeneric*/bnd.isGenericType() || bnd.isParameterizedType() || bnd.isRawType(), modifiers);
19 ✔
1087
                }
1088

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

1099
                //otherwise (none of the above)
1100

1101
                if (name == null) {
2 ✔
1102
                        name = bnd.getName();
3 ✔
1103
                }
1104

1105
                if (owner == null) {
2 !
1106
                        owner = (TWithTypes) this.ensureOwner(bnd);
5 ✔
1107
                }
1108

1109
                if (bnd.isTypeVariable() ) {
3 !
1110
                        fmx = ensureFamixTypeParameter(bnd, name, owner);
6 ✔
1111
                        return fmx;
2 ✔
1112
                }
1113

1114
                fmx = ensureFamixEntity(Type.class, bnd, name);
×
1115
                fmx.setTypeContainer(owner);
×
1116
                return fmx;
×
1117
        }
1118

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

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

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

1153
                // --------------- some special cases
1154
                if (bnd != null) {
2 ✔
1155
                        if (bnd.isArray()) {
3 !
1156
                                bnd = bnd.getElementType();
×
1157
                        }
1158

1159
                        // for inner classes defined in generics !!! For others should not change anything
1160
                        bnd = bnd.getErasure();
3 ✔
1161
                }
1162

1163
                // ---------------- to avoid useless computations if we can
1164
                fmx = getEntityByKey(bnd, Class.class);
6 ✔
1165
                if (fmx != null) {
2 ✔
1166
                        return fmx;
2 ✔
1167
                }
1168

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

1192
        // If we have java.lang.Object we should ensure we create this class
1193
        if (bnd != null && bnd.getQualifiedName().equals("java.lang.Object")) {
7 ✔
1194
                        return ensureFamixClassObject();
3 ✔
1195
                }
1196

1197
                // --------------- owner
1198
                if (owner == null) {
2 ✔
1199
                        if (bnd != null) {
2 ✔
1200
                                owner = ensureOwner(bnd);
4 ✔
1201
                        }
1202
                        /*                                owner = ensureFamixPackageDefault();
1203
                        } else {*/
1204
                }
1205

1206
                // --------------- recover from name ?
1207
                if (owner != null) {
2 ✔
1208
                        for (Class candidate : this.getEntityByName(Class.class, name)) {
13 ✔
1209
                                if (matchAndMapClass(bnd, name, owner, candidate)) {
7 ✔
1210
                                        fmx = candidate;
2 ✔
1211
                                        break;
1 ✔
1212
                                }
1213
                        }
1 ✔
1214
                }
1215

1216
                // ---------------- create
1217
                if (fmx == null) {
2 ✔
1218
                        if (isGeneric) {
2 ✔
1219
                                fmx = ensureFamixParametricClass(bnd, name, (TWithTypes) owner);
8 ✔
1220
                        }
1221
                        else {
1222
                                fmx = ensureFamixEntity(Class.class, bnd, name);
7 ✔
1223
                                fmx.setTypeContainer((TWithTypes)owner);
4 ✔
1224
                        }
1225
                }
1226

1227
                // ---------------- modifiers and super-classes
1228
                if (fmx!=null) {
2 !
1229
                        // we just created it, or it was not bound so we make sure it has the right information in it
1230
                        if (bnd != null) {
2 ✔
1231
                                setClassModifiers(fmx, bnd.getDeclaredModifiers(), (TWithTypes) owner);
7 ✔
1232
                        }
1233

1234
                        TAssociation lastAssoc = null;
2 ✔
1235

1236
                        if (bnd != null) {
2 ✔
1237
                                ITypeBinding supbnd = bnd.getSuperclass();
3 ✔
1238
                                if (supbnd != null) {
2 ✔
1239
                                        lastAssoc = ensureFamixInheritance((TWithInheritances) ensureFamixType(supbnd), fmx, lastAssoc, supbnd);
11 ✔
1240
                                }
1241
                                else {
1242
                                        lastAssoc = ensureFamixInheritance(ensureFamixClassObject(), fmx, lastAssoc, null);
8 ✔
1243
                                }
1244
                                ensureImplementedInterfaces(bnd, fmx, (TWithTypes) owner, lastAssoc);
7 ✔
1245
                        }
1246
                }
1247

1248
                return fmx;
2 ✔
1249
        }
1250

1251
        /**
1252
         * Returns a Famix Exception associated with the ITypeBinding.
1253
         * The Entity is created if it does not exist.
1254
         * @param name -- the name of the Famix Exception
1255
         * @param owner -- type defining the Exception (should not be null, but it will work if it is) 
1256
         *
1257
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
1258
         */
1259
        public <T extends TWithTypes & TNamedEntity> Exception ensureFamixException(ITypeBinding bnd, String name, TWithTypes owner, boolean isGeneric, int modifiers) {
1260
                Exception fmx;
1261

1262
                // --------------- some special cases
1263
                if (bnd != null) {
2 ✔
1264
                        if (bnd.isArray()) {
3 !
1265
                                bnd = bnd.getElementType();
×
1266
                        }
1267

1268
                        // for inner classes defined in generics !!! For others should not change anything
1269
                        bnd = bnd.getErasure();
3 ✔
1270
                }
1271

1272
                // ---------------- to avoid useless computations if we can
1273
                fmx = (Exception) getEntityByKey(bnd);
5 ✔
1274
                if (fmx != null) {
2 ✔
1275
                        return fmx;
2 ✔
1276
                }
1277

1278
                // --------------- name
1279
                if (name == null) {
2 ✔
1280
                        if (bnd == null) {
2 !
1281
                                return null;  // not much we can do
×
1282
                        } else if (!bnd.isAnonymous()) {
3 !
1283
                                name = bnd.getErasure().getName();  // for generics, will give the "core" type name, for normal type, won't change anything
5 ✔
1284
                        } else { // anonymous class
1285
                                if (bnd.getSuperclass() != null) {
×
1286
                                        name = bnd.getSuperclass().getName();
×
1287
                                }
1288
                                if ((name == null) || name.equals(OBJECT_NAME)) {
×
1289
                                        ITypeBinding[] intfcs = bnd.getInterfaces();
×
1290
                                        if ((intfcs != null) && (intfcs.length > 0)) {
×
1291
                                                name = bnd.getInterfaces()[0].getName();
×
1292
                                        }
1293
                                        else {
1294
                                                name = "???";
×
1295
                                        }
1296
                                }
1297
                                name = ANONYMOUS_NAME_PREFIX + "(" + name + ")";
×
1298
                        }
1299
                }
1300

1301
                // --------------- owner
1302
                if (owner == null) {
2 ✔
1303
                        if (bnd == null) {
2 ✔
1304
                                owner = ensureFamixPackageDefault();
4 ✔
1305
                        } else {
1306
                                owner = (TWithTypes) ensureOwner(bnd);
5 ✔
1307
                        }
1308
                }
1309

1310
                // --------------- recover from name ?
1311
                for (Exception candidate : this.getEntityByName(Exception.class, name)) {
13 ✔
1312
                        if (matchAndMapClass(bnd, name, (T) owner, candidate)) {
8 !
1313
                                fmx = candidate;
×
1314
                                break;
×
1315
                        }
1316
                }
1 ✔
1317

1318
                // ---------------- create
1319
                if (fmx == null) {
2 !
1320
                        fmx = ensureFamixEntity(Exception.class, bnd, name);
7 ✔
1321
                        fmx.setTypeContainer(owner);
3 ✔
1322
                }
1323

1324
        // we just created it or it was not bound, so we make sure it has the right information in it
1325
        TAssociation lastAssoc = null;
2 ✔
1326
        if (bnd != null) {
2 ✔
1327
            ITypeBinding supbnd = bnd.getSuperclass();
3 ✔
1328
            if (supbnd != null) {
2 !
1329
                lastAssoc = ensureFamixInheritance((TWithInheritances) ensureFamixType(supbnd), fmx, lastAssoc, supbnd);
11 ✔
1330
            }
1331
            else {
1332
                lastAssoc = ensureFamixInheritance(ensureFamixClassObject(), fmx, lastAssoc, null);
×
1333
            }
1334
            ensureImplementedInterfaces(bnd, fmx, owner, lastAssoc);
6 ✔
1335
        }
1336

1337
        return fmx;
2 ✔
1338
        }
1339

1340
        /**
1341
         * Returns a FAMIX Interface with the given <b>name</b>, creating it if it does not exist yet.
1342
         * @param name -- the name of the FAMIX Method
1343
         * @param owner -- type defining the method (should not be null, but it will work if it is) 
1344
         * @return the FAMIX Class or null in case of a FAMIX error
1345
         */
1346
        public <T extends TWithTypes & TNamedEntity> Interface ensureFamixInterface(ITypeBinding bnd, String name, TWithTypes owner, boolean isGeneric, int modifiers) {
1347
                Interface fmx;
1348

1349
                // --------------- some special cases
1350
                if (bnd != null) {
2 !
1351
                        if (bnd.isArray()) {
3 !
1352
                                bnd = bnd.getElementType();
×
1353
                        }
1354

1355
                        // for inner classes defined in generics !!! For others should not change anything
1356
                        bnd = bnd.getErasure();
3 ✔
1357
                }
1358

1359
                // ---------------- to avoid useless computations if we can
1360
                fmx = getEntityByKey(bnd, Interface.class);
6 ✔
1361
                if (fmx != null) {
2 ✔
1362
                        return fmx;
2 ✔
1363
                }
1364

1365
                // --------------- name
1366
                if (name == null) {
2 ✔
1367
                        if (bnd == null) {
2 !
1368
                                return null;  // not much we can do
×
1369
                        } else if (!bnd.isAnonymous()) {
3 !
1370
                                name = bnd.getErasure().getName();  // for generics, will give the "core" type name, for normal type, won't change anything
5 ✔
1371
                        } else { // anonymous class
1372
                                if (bnd.getSuperclass() != null) {
×
1373
                                        name = bnd.getSuperclass().getName();
×
1374
                                }
1375
                                if ((name == null) || name.equals(OBJECT_NAME)) {
×
1376
                                        ITypeBinding[] intfcs = bnd.getInterfaces();
×
1377
                                        if ((intfcs != null) && (intfcs.length > 0)) {
×
1378
                                                name = bnd.getInterfaces()[0].getName();
×
1379
                                        }
1380
                                        else {
1381
                                                name = "???";
×
1382
                                        }
1383
                                }
1384
                                name = ANONYMOUS_NAME_PREFIX + "(" + name + ")";
×
1385
                        }
1386
                }
1387

1388
                // --------------- owner
1389
                if (owner == null) {
2 ✔
1390
                        if (bnd == null) {
2 !
1391
                                owner = ensureFamixPackageDefault();
×
1392
                        } else {
1393
                                owner = (TWithTypes) ensureOwner(bnd);
5 ✔
1394
                        }
1395
                }
1396

1397
                // --------------- recover from name ?
1398
                for (Interface candidate : this.getEntityByName(Interface.class, name)) {
13 ✔
1399
                        if (matchAndMapInterface(bnd, name, (T) owner, candidate)) {
8 ✔
1400
                                fmx = candidate;
2 ✔
1401
                                break;
1 ✔
1402
                        }
1403
                }
1 ✔
1404

1405
                // ---------------- create
1406
                if (fmx == null) {
2 ✔
1407
                        if (isGeneric) {
2 ✔
1408
                                fmx = ensureFamixParametricInterface(bnd, name, owner);
7 ✔
1409
                        }
1410
                        else {
1411
                                fmx = ensureFamixEntity(Interface.class, bnd, name);
7 ✔
1412
                                fmx.setTypeContainer(owner);
3 ✔
1413
                        }
1414
                }
1415

1416
                // ---------------- modifiers and "super interfaces"
1417
                if (fmx!=null) {
2 !
1418
                        // we just created it or it was not bound, so we make sure it has the right information in it
1419
                        if (bnd != null) {
2 !
1420
                                setInterfaceModifiers(fmx, bnd.getModifiers());
5 ✔
1421
                        }
1422
                        TAssociation lastAssociation = null;
2 ✔
1423
                        if (bnd != null) {
2 !
1424
                                ensureImplementedInterfaces(bnd, fmx, owner, lastAssociation);
6 ✔
1425
                        }
1426
                }
1427
                return fmx;
2 ✔
1428
        }
1429

1430
        /**
1431
         * "Converts" (if needed) a TTYpe entity to be a TThrowable. Might involve removing the existing entity, recreating a new one and migrating
1432
         * all the relationship of the former to the later
1433
         */
1434
        public TThrowable asException(TType fmxType) {
1435
                if (fmxType instanceof Exception) {
3 ✔
1436
                        return (Exception) fmxType;
3 ✔
1437
                }
1438
                if(fmxType instanceof TypeParameter) {
3 ✔
1439
                        return (TypeParameter) fmxType;
3 ✔
1440
                }
1441

1442
                Exception fmxException = null;
2 ✔
1443
                IBinding key;
1444

1445
                try {
1446
                        key = entityToKey.get(fmxType);
6 ✔
1447

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

1452
                        TWithTypes owner = fmxType.getTypeContainer();
3 ✔
1453
                        fmxType.setTypeContainer(null);
3 ✔
1454
                        fmxException = ensureFamixException((ITypeBinding) key, fmxType.getName(), owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
10 ✔
1455

1456
                        fmxException.addMethods( new ArrayList<>( ((TWithMethods)fmxType).getMethods() ) );
8 ✔
1457
                        if (fmxType instanceof TWithAttributes) {
3 !
1458
                                fmxException.addAttributes( new ArrayList<>( ((TWithAttributes)fmxType).getAttributes() ) );
8 ✔
1459
                        }
1460

1461
                        if (fmxType instanceof TWithInheritances) {
3 !
1462
                                fmxException.addSuperInheritances( new ArrayList<>( ((TWithInheritances) fmxType).getSuperInheritances() ) );
8 ✔
1463
                                fmxException.addSubInheritances( new ArrayList<>( ((TWithInheritances) fmxType).getSubInheritances() ) );
8 ✔
1464
                        }
1465
                        fmxException.setComments(new ArrayList<>( ((TWithComments) fmxType).getComments() ));
8 ✔
1466
                        fmxException.setSourceAnchor(fmxType.getSourceAnchor());
4 ✔
1467
                        fmxException.addIncomingTypings( new ArrayList<>( fmxType.getIncomingTypings() ) );
7 ✔
1468
                        fmxException.addAnnotationInstances( new ArrayList<>( ((NamedEntity)fmxType).getAnnotationInstances() ) );
8 ✔
1469
                        fmxException.addIncomingReferences( new ArrayList<>( fmxType.getIncomingReferences() ) );
7 ✔
1470
                        fmxException.setIsStub(fmxType.getIsStub());
4 ✔
1471
                        fmxException.addTypes( new ArrayList<>( ((ContainerEntity) fmxType).getTypes() ) );
8 ✔
1472
                }
1473
                catch( ConcurrentModificationException e) {
×
1474
                        e.printStackTrace();
×
1475
                }
1 ✔
1476

1477
                return fmxException;
2 ✔
1478
        }
1479

1480
        /**
1481
         * helper method, we know the type exists, ensureFamixClass will recover it
1482
         */
1483
        public Class getFamixClass(ITypeBinding bnd, String name, TNamedEntity owner) {
1484
                return ensureFamixClass(bnd, name, owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
8 ✔
1485
        }
1486

1487
        /**
1488
         * helper method, we know the type exists, ensureFamixInterface will recover it
1489
         */
1490
        public Interface getFamixInterface(ITypeBinding bnd, String name, ContainerEntity owner) {
1491
                return ensureFamixInterface(bnd, name, owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
8 ✔
1492
        }
1493

1494
        /**
1495
         * helper method, we know the type exists, ensureFamixInterface will recover it
1496
         */
1497
        public Exception getFamixException(ITypeBinding bnd, String name, TWithTypes owner) {
1498
                return ensureFamixException(bnd, name, owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
8 ✔
1499
        }
1500

1501
        /**
1502
         * Ensures a famix entity for the owner of a binding.<br>
1503
         * This owner can be a method, a class or a namespace
1504
         * @param bnd -- binding for the owned entity
1505
         * @return a famix entity for the owner
1506
         */
1507
        private TNamedEntity ensureOwner(ITypeBinding bnd) {
1508
                TNamedEntity owner;
1509
                IMethodBinding parentMtd = bnd.getDeclaringMethod();
3 ✔
1510
                if (parentMtd != null) {
2 ✔
1511
                        owner = this.ensureFamixMethod(parentMtd);  // cast needed to desambiguate the call
5 ✔
1512
                }
1513
                else {
1514
                        ITypeBinding parentClass = bnd.getDeclaringClass();
3 ✔
1515
                        if (parentClass != null) {
2 ✔
1516
                owner = this.ensureFamixType(parentClass);
5 ✔
1517
            }
1518
                        else {
1519
                                IPackageBinding parentPckg = bnd.getPackage();
3 ✔
1520
                                if (parentPckg != null) {
2 !
1521
                                        owner = this.ensureFamixPackage(parentPckg, null);
6 ✔
1522
                                } else {
1523
                                        owner = this.ensureFamixPackageDefault();
×
1524
                                }
1525
                        }
1526
                }
1527
                return owner;
2 ✔
1528
        }
1529

1530

1531
        /**
1532
         * Returns a FAMIX PrimitiveType with the given <b>name</b>, creating it if it does not exist yet
1533
         * We assume that PrimitiveType must be uniq for a given name
1534
         * @param name -- the name of the FAMIX PrimitiveType
1535
         * @return the FAMIX PrimitiveType or null in case of a FAMIX error
1536
         */
1537
        public PrimitiveType ensureFamixPrimitiveType(ITypeBinding bnd, String name) {
1538
                if (name == null) {
2 ✔
1539
                        if (bnd == null) {
2 !
1540
                                return null;
×
1541
                        } else {
1542
                                name = bnd.getName();
3 ✔
1543
                        }
1544
                }
1545
                return ensureFamixUniqEntity(PrimitiveType.class, bnd, name);
7 ✔
1546
        }
1547

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

1551
                // --------------- to avoid useless computations if we can
1552
                fmx = (org.moosetechnology.model.famix.famixjavaentities.Enum) getEntityByKey(bnd);
5 ✔
1553
                if (fmx != null) {
2 ✔
1554
                        return fmx;
2 ✔
1555
                }
1556

1557
                // --------------- name
1558
                if (name == null) {
2 ✔
1559
                        if (bnd == null) {
2 !
1560
                                return null;
×
1561
                        }
1562
                        else {
1563
                                name = bnd.getName();
3 ✔
1564
                        }
1565
                }
1566

1567
                // --------------- owner
1568
                if (owner == null) {
2 ✔
1569
                        if (bnd == null) {
2 !
1570
                                owner = ensureFamixPackageDefault();  // not really sure what to do here
×
1571
                        } else {
1572
                                owner = (TWithTypes) ensureOwner(bnd);
5 ✔
1573
                        }
1574
                }
1575

1576
                // --------------- recover from name ?
1577
                for (org.moosetechnology.model.famix.famixjavaentities.Enum candidate : getEntityByName(org.moosetechnology.model.famix.famixjavaentities.Enum.class, name)) {
9 !
1578
                        if (matchAndMapType(bnd, name, (T) owner, candidate)) {
×
1579
                                fmx = candidate;
×
1580
                                break;
×
1581
                        }
1582
                }
×
1583

1584
                if (fmx == null) {
2 !
1585
                        fmx = ensureFamixEntity(Enum.class, bnd, name);
7 ✔
1586
                        fmx.setTypeContainer(owner);
3 ✔
1587
                }
1588

1589
                if (bnd != null) {
2 !
1590
                        setVisibility(fmx, bnd.getModifiers(), owner);
6 ✔
1591
                }
1592

1593
                return fmx;
2 ✔
1594
        }
1595

1596
        /**
1597
         * helper method, we know the type exists, ensureFamixEnum will recover it
1598
         */
1599
        public org.moosetechnology.model.famix.famixjavaentities.Enum getFamixEnum(ITypeBinding bnd, String name, TWithTypes owner) {
1600
                return ensureFamixEnum(bnd, name, owner);
6 ✔
1601
        }
1602

1603
        public EnumValue ensureFamixEnumValue(IVariableBinding bnd,        String name, Enum owner) {
1604
                EnumValue fmx;
1605

1606
                // --------------- to avoid useless computations if we can
1607
                fmx = (EnumValue)getEntityByKey(bnd);
5 ✔
1608
                if (fmx != null) {
2 ✔
1609
                        return fmx;
2 ✔
1610
                }
1611

1612
                // --------------- name
1613
                if (name == null) {
2 !
1614
                        if (bnd == null) {
×
1615
                                return null;
×
1616
                        }
1617
                        else {
1618
                                name = bnd.getName();
×
1619
                        }
1620
                }
1621

1622
                // --------------- owner
1623
                if (owner == null) {
2 ✔
1624
                        if (bnd == null) {
2 !
1625
                                return null;  // what would be the interest of creating an EnumValue without a declaring Enum type?
×
1626
                        }
1627
                        else {
1628
                                owner = ensureFamixEnum(bnd.getDeclaringClass(), null, null);
7 ✔
1629
                        }
1630
                }
1631

1632
                // --------------- recover from name ?
1633
                for (EnumValue candidate : getEntityByName(EnumValue.class, name) ) {
9 !
1634
                        if ( matchAndMapVariable(bnd, name, owner, candidate) ) {
×
1635
                                fmx = candidate;
×
1636
                                break;
×
1637
                        }
1638
                }
×
1639
                if (fmx == null) {
2 !
1640
                        fmx = ensureFamixEntity(EnumValue.class, bnd, name);
7 ✔
1641
                        fmx.setParentEnum(owner);
3 ✔
1642
                }
1643

1644
        fmx.setParentEnum(owner);
3 ✔
1645

1646
        return fmx;
2 ✔
1647
        }
1648

1649
    /**
1650
         * e.g. see {@link EntityDictionary#ensureFamixClass}
1651
         */
1652
        public AnnotationType ensureFamixAnnotationType(ITypeBinding bnd, String name, ContainerEntity owner) {
1653
                AnnotationType fmx;
1654

1655
                // --------------- to avoid useless computations if we can
1656
                fmx = (AnnotationType)getEntityByKey(bnd);
5 ✔
1657
                if (fmx != null) {
2 ✔
1658
                        return fmx;
2 ✔
1659
                }
1660

1661
                // --------------- name
1662
                if (name == null) {
2 ✔
1663
                        if (bnd == null) {
2 !
1664
                                return null;
×
1665
                        }
1666
                        else {
1667
                                name = bnd.getName();
3 ✔
1668
                        }
1669
                }
1670

1671
                // --------------- owner
1672
                if (owner == null) {
2 ✔
1673
                        if (bnd == null) {
2 !
1674
                                owner = ensureFamixPackageDefault();
×
1675
                        }
1676
                        else {
1677
                                IPackageBinding parentPckg = bnd.getPackage();
3 ✔
1678
                                if (parentPckg != null) {
2 !
1679
                                        owner = this.ensureFamixPackage(parentPckg, null);
6 ✔
1680
                                } else {
1681
                                        owner = this.ensureFamixPackageDefault();
×
1682
                                }
1683
                        }
1684
                }
1685

1686
                // --------------- recover from name ?
1687
                for (AnnotationType candidate : getEntityByName(AnnotationType.class, name) ) {
13 ✔
1688
                        if ( matchAndMapType(bnd, name, owner, candidate) ) {
7 !
1689
                                fmx = candidate;
2 ✔
1690
                                break;
1 ✔
1691
                        }
1692
                }
×
1693

1694
                // --------------- create
1695
                if (fmx == null) {
2 ✔
1696
                        fmx = ensureFamixEntity(AnnotationType.class, bnd, name);
7 ✔
1697
                        fmx.setAnnotationTypesContainer(owner);
3 ✔
1698
                }
1699

1700
                if (bnd != null) {
2 !
1701
                        // Not supported in Famix
1702

1703
                        // setVisibility(fmx, bnd.getModifiers());
1704
                }
1705

1706
                return fmx;
2 ✔
1707
        }
1708

1709
        /**
1710
         * helper method, we know the type exists, ensureFamixAnnotationType will recover it
1711
         */
1712
        public AnnotationType getFamixAnnotationType(ITypeBinding bnd, String name, ContainerEntity owner) {
1713
                return ensureFamixAnnotationType(bnd, name, owner);
6 ✔
1714
        }
1715

1716
        public AnnotationTypeAttribute ensureFamixAnnotationTypeAttribute(IMethodBinding bnd, String name, AnnotationType owner) {
1717
                AnnotationTypeAttribute fmx = null;
2 ✔
1718

1719
                // --------------- to avoid useless computations if we can
1720
                fmx = (AnnotationTypeAttribute)getEntityByKey(bnd);
5 ✔
1721
                if (fmx != null) {
2 ✔
1722
                        return fmx;
2 ✔
1723
                }
1724

1725
                // --------------- name
1726
                if (name == null) {
2 !
1727
                        if (bnd == null) {
×
1728
                                return null;
×
1729
                        }
1730
                        else {
1731
                                name = bnd.getName();
×
1732
                        }
1733
                }
1734

1735
                // --------------- owner
1736
                if (owner == null) {
2 !
1737
                        if (bnd == null) {
×
1738
                                return null;  // what would be the use of an AnnotationTypeAttribute without AnnotationType ?
×
1739
                        }
1740
                        else {
1741
                                ITypeBinding parentType = bnd.getDeclaringClass();
×
1742
                                if (parentType != null) {
×
1743
                                        owner = this.ensureFamixAnnotationType(parentType, null, null);
×
1744
                                }
1745
                                else  {
1746
                                        return null;  // what would be the use of an AnnotationTypeAttribute without AnnotationType ?
×
1747
                                }
1748
                        }
1749
                }
1750

1751
                // --------------- recover from name ?
1752
                for (AnnotationTypeAttribute candidate : getEntityByName(AnnotationTypeAttribute.class, name) ) {
13 ✔
1753
                        // JDT treats annotation type attributes as methods ...
1754
                        // checkAndMapMethod wants a signature as 2nd argument so we add empty param list
1755
                        if ( (bnd != null) && matchAndMapMethod(bnd, name+"()", null, owner, candidate) ) {
11 !
1756
                                fmx = candidate;
×
1757
                                break;
×
1758
                        }
1759
                        // if the binding is null, the annotationTypeAttribute migth have been created
1760
                        else if ( (bnd == null) && matchAndMapVariable(null, name, owner, candidate)) {
2 !
1761
                                fmx = candidate;
×
1762
                                break;
×
1763
                        }
1764
                }
1 ✔
1765

1766
                if (fmx == null) {
2 !
1767
                        fmx = ensureFamixEntity(AnnotationTypeAttribute.class, bnd, name);
7 ✔
1768
                        fmx.setParentType(owner);
3 ✔
1769
                }
1770

1771
                if (bnd != null) {
2 !
1772
                        // Not suopp
1773

1774
                        // setVisibility(fmx, bnd.getModifiers());
1775
                }
1776

1777
                return fmx;
2 ✔
1778
        }
1779

1780
        /**
1781
         * helper method, we know the attribute exists, ensureFamixAnnotationTypeAttribute will recover it
1782
         */
1783
        public AnnotationTypeAttribute getFamixAnnotationTypeAttribute(IMethodBinding bnd, String name, AnnotationType owner) {
1784
                return ensureFamixAnnotationTypeAttribute( bnd, name, owner);
6 ✔
1785
        }
1786
        
1787
        
1788
        /**
1789
         * Returns a FAMIX Wildcard with its bounds
1790
         * @param bnd
1791
         * @param name
1792
         * @param owner
1793
         * @return
1794
         */
1795
        public Wildcard ensureFamixWildcardType(ITypeBinding bnd, String name, TParametricEntity owner, TWithTypes ctxt) {
1796
                Wildcard fmx = this.ensureFamixEntity(Wildcard.class, bnd, bnd.getName());
8 ✔
1797
                if(bnd.getBound() != null) {
3 ✔
1798
                        Type bound = this.ensureFamixType(bnd.getBound());
5 ✔
1799
                        if(bnd.isUpperbound()) {
3 ✔
1800
                                fmx.setUpperBound(bound);
3 ✔
1801
                                bound.addUpperBoundedWildcards(fmx);
4 ✔
1802
                        }else{
1803
                                fmx.setLowerBound(bound);
3 ✔
1804
                                bound.addLowerBoundedWildcards(fmx);
3 ✔
1805
                        }
1806
                }
1807
                return fmx;
2 ✔
1808
        }
1809

1810
        /**
1811
         * Returns a Famix TypeParameter (created by a Famix ParametricEntity) with the given <b>name</b>, creating it if it does not exist yet
1812
         * In the second case, sets some default properties: not Abstract, not Final, not Private, not Protected, not Public
1813
         * @param name -- the name of the Famix TypeParameter
1814
         * @return the Famix TypeParameter or null in case of a Famix error
1815
         */
1816
        public TypeParameter ensureFamixTypeParameter(ITypeBinding bnd,        String name, TWithTypes owner) {
1817
                TypeParameter fmx;
1818

1819
                // --------------- to avoid useless computations if we can
1820
                fmx = (TypeParameter)getEntityByKey(bnd);
5 ✔
1821
                if (fmx != null) {
2 ✔
1822
                        return fmx;
2 ✔
1823
                }
1824

1825
                // --------------- name
1826
                if (name == null) {
2 ✔
1827
                        if (bnd == null) {
2 !
1828
                                return null;
×
1829
                        }
1830
                        else {
1831
                                name = bnd.getName();
3 ✔
1832
                        }
1833
                }
1834

1835
                // --------------- owner
1836
                if (owner == null && bnd != null) {
2 !
1837
            if (bnd.getDeclaringClass() != null) {
×
1838
                owner = this.ensureFamixType(bnd.getDeclaringClass());
×
1839
            } else if(bnd.getDeclaringMethod() != null) {
×
1840
                owner = this.ensureFamixMethod(bnd.getDeclaringMethod());
×
1841
            }
1842
                }
1843

1844
                // --------------- recover from name ?
1845
                for (Type candidate : this.getEntityByName(Type.class, name)) {
13 ✔
1846
                        if ( matchAndMapType(bnd, name, (ContainerEntity) owner, candidate) ) {
8 ✔
1847
                                fmx = (TypeParameter) candidate;
3 ✔
1848
                                break;
1 ✔
1849
                        }
1850
                }
1 ✔
1851

1852
                // --------------- create
1853
                if (fmx == null) {
2 ✔
1854
                        fmx = ensureFamixEntity(TypeParameter.class, bnd, name);
7 ✔
1855
                        if(bnd != null && bnd.getSuperclass() != null) {
5 !
1856
                                Type upperBound = ensureFamixType(bnd.getSuperclass());
5 ✔
1857
                                fmx.setUpperBound(upperBound);
3 ✔
1858
                        }
1859
                        if(bnd != null) {
2 ✔
1860
                for (ITypeBinding intbnd : bnd.getInterfaces()) {
17 ✔
1861
                    Type upperBound = ensureFamixType(intbnd);
4 ✔
1862
                    fmx.setUpperBound(upperBound);
3 ✔
1863
                }
1864
            }
1865
                        fmx.setTypeContainer(owner);
3 ✔
1866
                }
1867

1868
                return fmx;
2 ✔
1869
        }
1870

1871
        /**
1872
         * Checks whether the existing unmapped Famix Namespace matches the binding.
1873
         * Checks that the candidate has the same name as the JDT bound package, and checks recursively that owners also match.
1874
         *
1875
         * @param bnd       -- a JDT binding that we are trying to match to the candidate
1876
         * @param name      of the package
1877
         * @param owner     of the package
1878
         * @param candidate -- a Famix Entity
1879
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1880
         */
1881
        private boolean matchAndMapPackage(IPackageBinding bnd, String name, Package owner, NamedEntity candidate) {
1882
                if (!(candidate instanceof Package)) {
3 !
1883
                        return false;
×
1884
                }
1885

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

1894
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7 ✔
1895
                        return false;
2 ✔
1896
                }
1897

1898
                // names match, not need to look at owner because names of Namespaces are their fully qualified name
1899
                conditionalMapToKey(bnd, candidate);
4 ✔
1900
                return true;
2 ✔
1901
        }
1902

1903
        /**
1904
         * Checks whether the existing unmapped Famix Type matches the binding.
1905
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
1906
         * We also check that the actual class of the candidate matches (can be a sub-class of FamixType).
1907
         * @param bnd -- a JDT binding that we are trying to match to the candidate
1908
         * @param name of the type
1909
         * @param owner of the type
1910
         * @param candidate -- a Famix NamedEntity (Class, Type, PrimitiveType, Enum, AnnotationType)
1911
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1912
         */
1913
        private <T extends TWithTypes & TNamedEntity> boolean matchAndMapType(ITypeBinding bnd, String name, TNamedEntity owner, TNamedEntity candidate) {
1914
                if (! (candidate instanceof Type) ) {
3 !
1915
                        return false;
×
1916
                }
1917

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

1927
                if ( (bnd != null) && (bnd.isArray()) ) {
5 ✔
1928
                                bnd = bnd.getElementType();
3 ✔
1929
                }
1930

1931
                // checking names
1932
                if ( (bnd != null) && (bnd.isParameterizedType() || bnd.isRawType()) ) {
8 !
1933
                        name = bnd.getErasure().getName();
×
1934
                }
1935
                else if (bnd != null) {
2 ✔
1936
                        name = bnd.getName();
3 ✔
1937
                }
1938
                // else name = name
1939
                if (checkNameMatch(null, name, candidate) == CheckResult.FAIL) {
7 ✔
1940
                        return false;
2 ✔
1941
                }
1942

1943
                // special case of primitive types
1944
                if (candidate instanceof PrimitiveType) {
3 !
1945
                        if ( (bnd != null) && bnd.isPrimitive() ) {
×
1946
                                // names are equal so it's OK
1947
                                conditionalMapToKey(bnd, candidate);
×
1948
                                return true;
×
1949
                        }
1950
                        else if ( (bnd == null) && (owner == null) ) {
×
1951
                                return true;
×
1952
                        }
1953
                }
1954

1955
                // check owners without bnd
1956
                if (bnd == null) {
2 ✔
1957
                        return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
7 ✔
1958
                }
1959

1960
                // check owners with bnd
1961
                // type is an annotation
1962
                if (bnd.isAnnotation() && (candidate instanceof AnnotationType)) {
6 !
1963
                        if (matchAndMapPackage(bnd.getPackage(), owner.getName(), (Package) Util.getOwner(owner), Util.getOwner(candidate))) {
13 !
1964
                                conditionalMapToKey(bnd, candidate);
4 ✔
1965
                                return true;
2 ✔
1966
                        } else {
1967
                                return false;
×
1968
                        }
1969
                }
1970

1971
                // check owners with bnd
1972
                // type is a Parameterized type
1973
                if ((bnd.isParameterizedType() || bnd.isRawType()) && (candidate instanceof ParametricClass)) {
6 !
1974
                        return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
×
1975
                }
1976

1977
                // check owners with bnd
1978
                // type is an Enum
1979
                if (bnd.isEnum() && (candidate instanceof Enum)) {
3 !
1980
                        return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
×
1981
                }
1982

1983
                // check owners with bnd
1984
                // type is something elae (a class or interface)
1985
                // Annotation are interfaces too, so we should check this one after isAnnotation
1986
                if ( bnd.isClass()) {
3 !
1987
                        return matchAndMapClass(bnd, name, owner, (Type) candidate);
×
1988
                }
1989

1990
                if(bnd.isInterface()) {
3 !
1991
                        return matchAndMapInterface(bnd, name, owner, (Type) candidate);
×
1992
                }
1993

1994
                return false;
2 ✔
1995
        }
1996

1997
        /**
1998
         * Checks whether the existing unmapped Famix Class (or Interface) matches the binding.
1999
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
2000
         * @param bnd -- a JDT binding that we are trying to match to the candidate
2001
         * @param name of the class
2002
         * @param owner of the class
2003
         * @param candidate -- a Famix Entity
2004
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
2005
         */
2006
        private boolean matchAndMapClass(ITypeBinding bnd, String name, TNamedEntity owner, TType candidate) {
2007
                if (!(candidate instanceof Class)) {
3 !
2008
                        return false;
×
2009
                }
2010

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

2019
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7 !
2020
                        return false;
×
2021
                }
2022

2023
                // checking owner
2024
                return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
7 ✔
2025
        }
2026

2027
        /**
2028
         * Checks whether the existing unmapped Famix Class (or Interface) matches the binding.
2029
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
2030
         * @param bnd -- a JDT binding that we are trying to match to the candidate
2031
         * @param name of the class
2032
         * @param owner of the class
2033
         * @param candidate -- a Famix Entity
2034
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
2035
         */
2036
        private boolean matchAndMapInterface(ITypeBinding bnd, String name, TNamedEntity owner, Type candidate) {
2037
                if (!(candidate instanceof Interface)) {
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
                } else if (res == CheckResult.FAIL) {
3 ✔
2046
                        return false;
2 ✔
2047
                }
2048

2049
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7 !
2050
                        return false;
×
2051
                }
2052

2053
                // checking owner
2054
                return matchAndMapTypeOwner(bnd, owner, candidate);
6 ✔
2055
        }
2056

2057
        /**
2058
         * Checks whether the existing unmapped Famix "Method" matches the binding.
2059
         * Checks that the candidate has the same name and same signature as the JDT bound method, and checks recursively that owners also match.
2060
         * Note that AnnotationTypeAttribute are treated as methods by JDT, so they are checked here.
2061
         * @param bnd -- a JDT binding that we are trying to match to the candidate
2062
         * @param sig -- signature of the method
2063
         * @param retTyp -- return type of the method
2064
         * @param owner of the method
2065
         * @param candidate -- a Famix Entity (regular Method or AnnotationTypeAttribute)
2066
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
2067
         */
2068
        private  boolean matchAndMapMethod(IMethodBinding bnd, String sig, TType retTyp, TNamedEntity owner, NamedEntity candidate) {
2069
                if (! (candidate instanceof Method) ) {
3 ✔
2070
                        return false;
2 ✔
2071
                }
2072

2073
                // check whether bnd and candidate are already bound
2074
                CheckResult res = checkKeyMatch(bnd, candidate);
5 ✔
2075
                if (res == CheckResult.MATCH) {
3 ✔
2076
                        return true;
2 ✔
2077
                }
2078
                else if (res == CheckResult.FAIL) {
3 ✔
2079
                        return false;
2 ✔
2080
                }
2081

2082
                // checking names
2083
                String name = (sig != null) ? sig.substring(0, sig.indexOf('(')) : null;
10 !
2084
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7 ✔
2085
                        return false;
2 ✔
2086
                }
2087

2088
                // for methods, the name is not enough, we must test the signature also
2089
                // but not for AnnotationTypeAttribute
2090

2091
                        if (bnd != null) {
2 ✔
2092
                                sig = bnd.getName() + "(" + signatureParamsFromBinding(bnd) + ")";
7 ✔
2093
                        }
2094
                        if (! ((Method) candidate).getSignature().equals(sig)) {
6 ✔
2095
                                return false;
2 ✔
2096
                        }
2097

2098
                        // and still for method, must also check the return type
2099
                        if (bnd != null) {
2 ✔
2100
                                if (isConstructorBinding(bnd)) {
4 !
2101
                                        if ( ((Method) candidate).getDeclaredType() != null ) {
×
2102
                                                return false;
×
2103
                                        }
2104
                                        // else OK for now
2105
                                }
2106
                                else { // not a constructor
2107
                                        if ( ((Method) candidate).getDeclaredType() == null ) {
4 !
2108
                                                return false;
×
2109
                                        }
2110
                                        else if (! matchAndMapType(bnd.getReturnType(), null, null, ((Method) candidate).getDeclaredType()) ) {
10 ✔
2111
                                                return false;
2 ✔
2112
                                        }
2113
                                        // else OK for now
2114
                                }
2115
                        }
2116
                        else {  // bnd == null
2117
                                if (retTyp == null) { // similar to (bnd.isConstructor())
2 !
2118
                                        if ( ((Method) candidate).getDeclaredType() != null ) {
4 ✔
2119
                                                return false;
2 ✔
2120
                                        }
2121
                                        // else OK for now
2122
                                } else { // (ret != null)  i.e. not a constructor
2123
                                        if (((Method) candidate).getDeclaredType() == null) {
×
2124
                                                return false;
×
2125
                                        } else if (!matchAndMapType(null, retTyp.getName(), Util.getOwner(retTyp), (NamedEntity) ((Method) candidate).getDeclaredType())) {
×
2126
                                                return false;
×
2127
                                        }
2128
                                        // else OK for now
2129
                                }
2130
                        }
2131

2132

2133
                // check owner
2134
                if (matchAndMapOwnerAsType(((bnd != null) ? bnd.getDeclaringClass() : null), owner, Util.getOwner(candidate)) == CheckResult.MATCH) {
13 ✔
2135
                        conditionalMapToKey(bnd, candidate);
4 ✔
2136
                        return true;
2 ✔
2137
                } else {
2138
                        return false;
2 ✔
2139
                }
2140
        }
2141

2142
        /** testing that a method binding is for a constructor
2143
         * There is a special case for "diamond constructors" (eg: <code>new HashSet<>()</code>)
2144
         */
2145
        protected boolean isConstructorBinding(IMethodBinding bnd) {
2146
                if (bnd.isConstructor()) {
3 ✔
2147
                        return true;
2 ✔
2148
                }
2149
                if ( bnd.getName().equals("<factory>") ) {
5 ✔
2150
                        return true;
2 ✔
2151
                }
2152
                return false;
2 ✔
2153
        }
2154

2155
        /**
2156
         * Checks whether the candidate (an existing unmapped Famix "Variable" like Attribute, Parameter, ...) matches the binding.
2157
         * Checks that the candidate has the same name as the JDT bound variable, and checks recursively that owners also match.
2158
         * The Famix candidate is a NamedEntity and not a StructuralEntity to allow dealing with Famix EnumValue that JDT treats as variables
2159
         * @param bnd -- a JDT binding that we are trying to match to the candidate
2160
         * @param name of the variable
2161
         * @param owner of the variable
2162
         * @param candidate -- a Famix Entity (a StructuralEntity or an EnumValue)
2163
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
2164
         */
2165
        private boolean matchAndMapVariable(IVariableBinding bnd, String name, TNamedEntity owner, TNamedEntity candidate) {
2166
                if (!(candidate instanceof TStructuralEntity)) {
3 !
2167
                        return false;
×
2168
                }
2169

2170
                // check whether bnd and candidate are already bound
2171
                CheckResult keyMatch = checkKeyMatch(bnd, candidate);
5 ✔
2172
                if (keyMatch == CheckResult.MATCH) {
3 !
2173
                        return true;
×
2174
                } else if (keyMatch == CheckResult.FAIL) {
3 ✔
2175
                        return false;
2 ✔
2176
                }
2177

2178
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7 !
2179
                        return false;
×
2180
                }
2181

2182
                // check owner
2183
                TNamedEntity candidateOwner = Util.getOwner(candidate);
3 ✔
2184

2185
                // local variable or parameter ?
2186
                // owner is a Method? (for example in case of an anonymous class)
2187
                CheckResult res = matchAndMapOwnerAsMethod(((bnd != null) ? bnd.getDeclaringMethod() : null), owner, candidateOwner);
11 ✔
2188
                if (res == CheckResult.FAIL) {
3 ✔
2189
                        return false;
2 ✔
2190
                } else if (res == CheckResult.MATCH) {
3 !
2191
                        conditionalMapToKey(bnd, candidate);
×
2192
                        return true;
×
2193
                }
2194

2195
                // check owner
2196
                // <anArray>.length field?
2197
                if (name.equals("length")) {
4 !
2198
                        boolean isArrayLengthField = ((bnd != null) && (bnd.getDeclaringClass() == null)) ||
×
2199
                                                                                 ((bnd == null) && (owner.getName().equals(EntityDictionary.ARRAYS_NAME)));
×
2200
                        if (isArrayLengthField) {
×
2201
                                if (candidateOwner.getName().equals(EntityDictionary.ARRAYS_NAME)) {
×
2202
                                        conditionalMapToKey(bnd, candidate);
×
2203
                                        return true;
×
2204
                                }
2205
                                else {
2206
                                        return false;
×
2207
                                }
2208
                        }
2209
                }
2210

2211
                // check owner
2212
                // "normal" field?
2213
                res = matchAndMapOwnerAsType( ((bnd != null) ? bnd.getDeclaringClass() : null), owner, candidateOwner);
11 ✔
2214
                if (res == CheckResult.MATCH) {
3 !
2215
                        conditionalMapToKey(bnd, candidate);
4 ✔
2216
                        return true;
2 ✔
2217
                }
2218
                return false;
×
2219
        }
2220

2221
        /**
2222
         * Checks whether the existing unmapped Famix Type's parent (or owner) matches the binding's owner.
2223
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
2224
         * @param bnd -- a JDT binding whose owner we are trying to match to the candidate's owner
2225
         * @param owner -- the owner of the type
2226
         * @param candidate -- a Famix Entity
2227
         * @return whether we found a match (if <b>true</b>, the mapping is recorded)
2228
         */
2229
        private boolean matchAndMapTypeOwner(ITypeBinding bnd, TNamedEntity owner, Type candidate) {
2230
                ContainerEntity candidateOwner = Util.getOwner(candidate);
4 ✔
2231

2232
                // owner is a Method? (for example in case of an anonymous class)
2233
                CheckResult res = matchAndMapOwnerAsMethod(((bnd != null) ? bnd.getDeclaringMethod() : null), owner, candidate);
11 ✔
2234
                if (res == CheckResult.MATCH) {
3 !
2235
                        conditionalMapToKey(bnd, candidate);
×
2236
                        return true;
×
2237
                } else if (res == CheckResult.FAIL) {
3 !
2238
                        return false;
×
2239
                }
2240

2241
                // owner is a class ?
2242
                res = matchAndMapOwnerAsType(((bnd != null) ? bnd.getDeclaringClass() : null), owner, candidateOwner);
11 ✔
2243
                if (res == CheckResult.MATCH) {
3 ✔
2244
                        conditionalMapToKey(bnd, candidate);
4 ✔
2245
                        return true;
2 ✔
2246
                }
2247
                else if (res == CheckResult.FAIL) {
3 ✔
2248
                        return false;
2 ✔
2249
                }
2250

2251
                // owner must be a package
2252
                if (matchAndMapOwnerAsNamespace( ((bnd != null)?bnd.getPackage():null), owner, candidateOwner) == CheckResult.MATCH) {
12 ✔
2253
                        conditionalMapToKey(bnd, candidate);
4 ✔
2254
                        return true;
2 ✔
2255
                }
2256
                return false;
2 ✔
2257
        }
2258

2259
        /**
2260
         * Check whether the owner of candidates is a method macthinf either methBnd or owner
2261
         * @param methBnd
2262
         * @param owner
2263
         * @param candidateOwner
2264
         * @return a {@link CheckResult}
2265
         */
2266
        private  <T extends TNamedEntity> CheckResult matchAndMapOwnerAsMethod(IMethodBinding methBnd, T owner, T candidateOwner) {
2267
                if ((methBnd != null) || (owner instanceof Method)) {
5 !
2268
                        if (!(candidateOwner instanceof Method)) {
3 !
2269
                                return CheckResult.FAIL;
×
2270
                        }
2271

2272
                        ContainerEntity ownerOwner = (owner != null) ? (ContainerEntity) Util.getOwner(owner) : null;
7 !
2273
                        String ownerSig = (owner != null) ? ((Method) owner).getSignature() : null;
7 !
2274
                        Type ownerReturn = (owner != null) ? (Type) ((Method) owner).getDeclaredType() : null;
8 !
2275

2276
                        if (matchAndMapMethod(methBnd, ownerSig, ownerReturn, ownerOwner, (Method) candidateOwner)) {
9 !
2277
                                return CheckResult.MATCH;
×
2278
                        } else {
2279
                                return CheckResult.FAIL;
2 ✔
2280
                        }
2281
                }
2282
                return CheckResult.UNDECIDED;
2 ✔
2283
        }
2284

2285
        /**
2286
         * @param typBnd
2287
         * @param owner
2288
         * @param candidateOwner
2289
         * @return a {@link CheckResult}
2290
         */
2291
        private CheckResult matchAndMapOwnerAsType(ITypeBinding typBnd, TNamedEntity owner, TNamedEntity candidateOwner) {
2292
                if ((typBnd != null) || (owner instanceof Type)) {
5 ✔
2293
                        if (!(candidateOwner instanceof Type)) {
3 ✔
2294
                                return CheckResult.FAIL;
2 ✔
2295
                        }
2296

2297
                        TNamedEntity ownerOwner = (owner != null) ? Util.getOwner(owner) : null;
6 !
2298
                        String ownerName = (owner != null) ? owner.getName() : null;
6 !
2299

2300
                        if (matchAndMapType(typBnd, ownerName, ownerOwner, candidateOwner)) {
7 ✔
2301
                                return CheckResult.MATCH;
2 ✔
2302
                        } else {
2303
                                return CheckResult.FAIL;
2 ✔
2304
                        }
2305
                }
2306
                return CheckResult.UNDECIDED;
2 ✔
2307
        }
2308

2309
        private CheckResult matchAndMapOwnerAsNamespace(IPackageBinding pckgBnd, TNamedEntity owner, ContainerEntity candidateOwner) {
2310
                if ((pckgBnd != null) || (owner instanceof Package)) {
5 !
2311
                        if (!(candidateOwner instanceof Package)) {
3 !
2312
                                return CheckResult.FAIL;
×
2313
                        }
2314

2315
                        Package ownerOwner = (owner != null) ? (Package) Util.getOwner(owner) : null;
7 !
2316
                        String ownerName = (owner != null) ? owner.getName() : null;
6 !
2317

2318
                        if (matchAndMapPackage(pckgBnd, ownerName, ownerOwner, candidateOwner)) {
7 ✔
2319
                                return CheckResult.MATCH;
2 ✔
2320
                        } else {
2321
                                return CheckResult.FAIL;
2 ✔
2322
                        }
2323
                }
2324
                return CheckResult.UNDECIDED;
×
2325
        }
2326

2327
        /**
2328
         * Checks whether the name and the candidate matches the name of the entity (given either by 'bnd' or 'name')<br>
2329
         * 'name' and 'bnd' cannot be null together
2330
         * @param bnd -- binding associated with the entity may be null
2331
         * @param name -- name of the entity may be null
2332
         * @param candidate
2333
         * @return true if names match, false if not
2334
         */
2335
        private CheckResult checkNameMatch(IBinding bnd, String name, TNamedEntity candidate) {
2336
                if ( (bnd != null) && (! bnd.getName().equals(candidate.getName())) ) {
8 ✔
2337
                        return CheckResult.FAIL;
2 ✔
2338
                }
2339
                else if ( (bnd == null) && (name != null) && (! name.equals(candidate.getName())) ) {
9 !
2340
                        return CheckResult.FAIL;
2 ✔
2341
                }
2342
                else {
2343
                        return CheckResult.MATCH;
2 ✔
2344
                }
2345
        }
2346

2347
        /**
2348
         * Check whether key and candidate are already bound together, whether either is bound to something else, or whether none is bound
2349
         * @param key
2350
         * @param candidate
2351
         * @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>
2352
         */
2353
        private CheckResult checkKeyMatch(IBinding key, TNamedEntity candidate) {
2354
                if (key == null) {
2 ✔
2355
                        return CheckResult.UNDECIDED;
2 ✔
2356
                }
2357

2358
                NamedEntity bound = (NamedEntity)getEntityByKey(key);
5 ✔
2359
                if (bound == candidate) {
3 ✔
2360
                        return CheckResult.MATCH;
2 ✔
2361
                }
2362
                else if (bound != null) {
2 ✔
2363
                        return CheckResult.FAIL;
2 ✔
2364
                }
2365
                else if (getEntityKey(candidate) != null) {
4 ✔
2366
                        //we check if the both bindings are linked to the same entity
2367
                        IBinding candidateBinding = getEntityKey(candidate);
4 ✔
2368
                        if(candidateBinding.getKey().equals(key.getKey())) {
6 ✔
2369
                                return CheckResult.MATCH;
2 ✔
2370
                        }
2371
                        
2372
                        //if candidate is a stub, we accept multiple recovery binding
2373
                        //this code will be upgraded with the possibility to add manually code to entities
2374
                        if (candidate instanceof TCanBeStub && ((TCanBeStub) candidate).getIsStub()) {
8 !
2375
                                return CheckResult.UNDECIDED;
2 ✔
2376
                        }
2377
      
2378
                        return CheckResult.FAIL;
2 ✔
2379
                }
2380
                else {
2381
                        return CheckResult.UNDECIDED;
2 ✔
2382
                }
2383
        }
2384

2385
        private void conditionalMapToKey(IBinding bnd, TNamedEntity ent) {
2386
                if (bnd != null) {
2 ✔
2387
                        mapEntityToKey(bnd, ent);
4 ✔
2388
                }
2389
        }
1 ✔
2390

2391
        public Method ensureFamixMethod(IMethodBinding bnd) {
2392
                return ensureFamixMethod(
8 ✔
2393
                                bnd,
2394
                                /*name*/null,
2395
                                /*paramsType*/null,
2396
                                /*returnType*/null,
2397
                                /*owner*/null,
2398
                                (bnd == null) ? UNKNOWN_MODIFIERS : bnd.getModifiers());
6 ✔
2399
        }
2400

2401
        /**
2402
         * Returns a Famix Method associated with the IMethodBinding.
2403
         * The Entity is created if it does not exist.
2404
         * @param name -- the name of the Famix Method (MUST NOT be null, but this is not checked)
2405
         * @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)
2406
         * @param owner -- type defining the method (should not be null, but it will work if it is)
2407
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
2408
         */
2409
        public Method ensureFamixMethod(IMethodBinding bnd, String name, Collection<String> paramTypes, TType ret, TWithMethods owner, int modifiers) {
2410
                Method fmx;
2411
                String signature;
2412
                boolean delayedRetTyp;
2413

2414
                // --------------- to avoid useless computations if we can
2415
                fmx = (Method)getEntityByKey(bnd);
5 ✔
2416
                if (fmx != null) {
2 ✔
2417
                        return fmx;
2 ✔
2418
                }
2419

2420
                // --------------- name
2421
                if (name == null) {
2 ✔
2422
                        if (bnd == null) {
2 ✔
2423
                                return null;
2 ✔
2424
                        }
2425
                        else {
2426
                                name = bnd.getName();
3 ✔
2427
                        }
2428
                }
2429

2430
                // --------------- signature
2431
                signature = name + "(";
3 ✔
2432
                 if (bnd != null) {
2 ✔
2433
                    signature += signatureParamsFromBinding(bnd);
7 ✔
2434
                }
2435
        else if (paramTypes != null) {
2 !
2436
                        signature += signatureParamsFromStringCollection(paramTypes);
7 ✔
2437
                }
2438
                else {
2439
                        signature += "???";
×
2440
                }
2441
                signature += ")";
3 ✔
2442

2443
                // --------------- return type
2444
                delayedRetTyp = false;
2 ✔
2445
                ITypeBinding retTypBnd = null;
2 ✔
2446
                if (ret == null) {
2 !
2447
                        if (bnd != null) {
2 ✔
2448
                // must create the return type
2449
                // but for method like "<T> T mtd()" where T belongs to mtd and mtd returns T,
2450
                // we need T to create the method and the method to create T ...
2451
                // so we need to test the situation and deal with it
2452
                retTypBnd = bnd.getReturnType();
3 ✔
2453
                if ( (retTypBnd != null) && retTypBnd.isTypeVariable() && (retTypBnd.getDeclaringMethod() == bnd) ) {
9 ✔
2454
                    delayedRetTyp = true;
3 ✔
2455
                }
2456
                else {
2457
                        ret = this.referredType(retTypBnd, fmx);
5 ✔
2458
                }
2459
                        }
2460
                }
2461

2462
                // --------------- owner
2463
                if (owner == null) {
2 ✔
2464
                        if (bnd == null) {
2 ✔
2465
                                owner = ensureFamixClassStubOwner();
4 ✔
2466
                        }
2467
                        else {
2468
                                ITypeBinding classBnd = bnd.getDeclaringClass().getErasure();
4 ✔
2469
                                if (classBnd != null) {
2 !
2470
                                        owner = ensureFamixType(classBnd);
5 ✔
2471
                                }
2472
                                else {
2473
                                        owner = ensureFamixClassStubOwner();
×
2474
                                }
2475
                        }
2476
                }
2477

2478
                // --------------- recover from name ?
2479
                for (Method candidate : this.getEntityByName(Method.class, name)) {
13 ✔
2480
                        if (matchAndMapMethod(bnd, signature, ret, (TNamedEntity) owner, candidate)) {
9 ✔
2481
                                fmx = candidate;
2 ✔
2482
                                break;
1 ✔
2483
                        }
2484
                }
1 ✔
2485

2486
                if (fmx == null) {
2 ✔
2487
                        if(bnd != null && bnd.isGenericMethod() && !isConstructorBinding(bnd)) {
9 ✔
2488
                                fmx = ensureFamixEntity(ParametricMethod.class, bnd, name);
7 ✔
2489
                                for(ITypeBinding param: bnd.getTypeParameters()) {
18 ✔
2490
                                        TypeParameter fmxParam = this.ensureFamixTypeParameter(param, null, fmx);
6 ✔
2491
                                        fmxParam.setGenericEntity((ParametricMethod)fmx);
4 ✔
2492
                                }
2493
                        // Parameterized method binding = when the method is the target of an invocation.
2494
                        } else if (bnd != null && bnd.isParameterizedMethod()) {
5 ✔
2495
                                fmx = this.ensureFamixMethod(bnd.getMethodDeclaration());
6 ✔
2496
                        } else {
2497
                if (bnd != null && isConstructorBinding(bnd)) {
6 ✔
2498
                    fmx = ensureFamixEntity(Initializer.class, bnd, name);
8 ✔
2499
                } else {
2500
                    fmx = ensureFamixEntity(Method.class, bnd, name);
7 ✔
2501
                }
2502
            }
2503

2504
                        fmx.setSignature(signature);
3 ✔
2505
                        ITypeBinding returnTypeBnd = (bnd == null) ? null : bnd.getReturnType();
7 ✔
2506
                        ensureFamixEntityTyping(returnTypeBnd, fmx, ret);
6 ✔
2507
                        fmx.setParentType(owner);
3 ✔
2508
                }
2509

2510
                if (fmx != null) {
2 !
2511
                        setMethodModifiers(fmx, modifiers);
4 ✔
2512
                }
2513

2514
        //If it has the #default keywork, we mark it as default implementation
2515
        if (Modifier.isDefault(modifiers)) {
3 ✔
2516
            fmx.setKind(DEFAULT_IMPLEMENTATION_KIND_MARKER);
3 ✔
2517
        }
2518

2519
        if (delayedRetTyp) {
2 ✔
2520
                        int retTypModifiers = retTypBnd.getModifiers();
3 ✔
2521
                        ITypeBinding returnTypeBnd = bnd.getReturnType();
3 ✔
2522
                        ensureFamixEntityTyping(returnTypeBnd, fmx, this.ensureFamixType(retTypBnd, /*name*/null, /*owner*/fmx, /*ctxt*/(ContainerEntity) owner, retTypModifiers));
13 ✔
2523
                }
2524

2525
                return fmx;
2 ✔
2526
        }
2527

2528

2529
        /**
2530
         * Creates or recovers the initializer method containing the attribute initializations of a type.
2531
         * @param owner Type containing the initializer
2532
         * @param isStatic Modifier of the initializer. A type can have 2 initializers for attribute initialization: 1 static and 1 not.
2533
         * @param isInitializationBlock True if the entity is an initialization block. False for the artificial method containing all field initializations.
2534
         * @return the FamixInitializer
2535
         */
2536
        public Initializer ensureFamixInitializer(TWithMethods owner, Boolean isStatic, Boolean isInitializationBlock) {
2537
                Initializer fmx = null;
2 ✔
2538

2539
                if (owner != null) {
2 !
2540
                        Optional<TMethod> existingInitializer = owner.getMethods().stream()
6 ✔
2541
                                        .filter(meth ->
1 ✔
2542
                                                        ((Method) meth).getIsInitializer() &&
8 ✔
2543
                                                        ((Method) meth).getIsConstructor().equals(false) &&
7 ✔
2544
                                                        ((Method) meth).getIsClassSide().equals(isStatic) &&
6 ✔
2545
                                                        ((Initializer) meth).getIsInitializationBlock().equals(isInitializationBlock))
7 ✔
2546
                                        .findFirst();
2 ✔
2547
                        if (existingInitializer.isPresent()) {
3 ✔
2548
                                fmx = (Initializer) existingInitializer.get();
4 ✔
2549
                        }
2550
                }
2551

2552
                if (fmx == null) {
2 ✔
2553
                        fmx = createFamixEntity(Initializer.class, INIT_BLOCK_NAME);
6 ✔
2554
                        fmx.setSignature(INIT_BLOCK_NAME + "()" );
3 ✔
2555
                        fmx.setVisibility(MODIFIER_PRIVATE);
3 ✔
2556
                        fmx.setParentType(owner);
3 ✔
2557
                        fmx.setIsClassSide(isStatic);
3 ✔
2558
                        fmx.setIsInitializationBlock(isInitializationBlock);
3 ✔
2559
                }
2560

2561
                return fmx;
2 ✔
2562
        }
2563

2564

2565
        public Initializer ensureImplicitConstructor(IMethodBinding binding, TWithMethods owner, String name, Collection<String> parameterTypesNames) {
2566
                Initializer fmx = null;
2 ✔
2567

2568
                if (fmx == null) {
2 !
2569
                        int modifiers = (binding != null) ? binding.getModifiers() : EntityDictionary.UNKNOWN_MODIFIERS;
7 ✔
2570
                        if (binding == null) {
2 ✔
2571
                                // OK! Binding is null, this is the default constructor!!
2572
                                // It has no source code :)
2573
                                fmx = ensureFamixEntity(Initializer.class, null, name);
7 ✔
2574
                                fmx.setParentType(owner);
3 ✔
2575
                                fmx.setSignature(name + "()");                                
5 ✔
2576
                        } else {
2577
                                // But, if we have the binding, that means the constructor exists. Let's just go the normal way
2578
                                fmx = (Initializer) this.ensureFamixMethod(binding, name, parameterTypesNames, /*ret type*/null, owner, modifiers);
10 ✔
2579
                        }
2580
                }
2581

2582
                return fmx;
2 ✔
2583
        }
2584

2585
        /**
2586
         * Creates or recovers a stub Famix Method
2587
         * @param name of the method
2588
         * @return the Famix Method
2589
         */
2590
        public Method ensureFamixStubMethod(String name) {
2591
                return ensureFamixMethod(null, name, /*paramType*/null, /*returnType*/null, ensureFamixClassStubOwner(), /*modifiers*/0);
×
2592
        }
2593

2594
        public void setAttributeModifiers(Attribute fmx, int mod) {
2595
                setCommonModifiers(fmx, mod, null);
5 ✔
2596
                fmx.setIsTransient(Modifier.isTransient(mod));
5 ✔
2597
                fmx.setIsVolatile(Modifier.isVolatile(mod));
5 ✔
2598
        }
1 ✔
2599

2600
        public void setMethodModifiers(Method fmx, int mod) {
2601
                setCommonModifiers(fmx, mod, (TWithTypes) fmx.getParentType());
7 ✔
2602
                fmx.setIsAbstract(Modifier.isAbstract(mod));
5 ✔
2603
                fmx.setIsSynchronized(Modifier.isSynchronized(mod));
5 ✔
2604
        }
1 ✔
2605

2606
        public void setClassModifiers(Class fmx, int mod, TWithTypes owner) {
2607
                setCommonModifiers(fmx, mod, null);
5 ✔
2608
                fmx.setIsAbstract(Modifier.isAbstract(mod));
5 ✔
2609
        }
1 ✔
2610

2611
        public void setInterfaceModifiers(Interface fmx, int mod) {
2612
                setCommonModifiers(fmx, mod, null);
5 ✔
2613
        }
1 ✔
2614

2615
        private void setCommonModifiers(Entity fmx, int mod, TWithTypes owner) {
2616
                setVisibility((THasVisibility)fmx, mod, owner);
6 ✔
2617
                ((TCanBeClassSide)fmx).setIsClassSide(Modifier.isStatic(mod));
6 ✔
2618
                ((TCanBeFinal)fmx).setIsFinal(Modifier.isFinal(mod));
6 ✔
2619
        }
1 ✔
2620

2621
        /**
2622
         * Sets the visibility of a FamixNamedEntity
2623
         *
2624
         * @param fmx -- the FamixNamedEntity
2625
         * @param mod -- a description of the modifiers as understood by org.eclipse.jdt.core.dom.Modifier
2626
         */
2627
        public void setVisibility(THasVisibility fmx, int mod, TWithTypes owner) {
2628
                if (Modifier.isPublic(mod)) {
3 ✔
2629
                        fmx.setVisibility(MODIFIER_PUBLIC);
4 ✔
2630
                } else if (Modifier.isPrivate(mod)) {
3 ✔
2631
                        fmx.setVisibility(MODIFIER_PRIVATE);
4 ✔
2632
                } else if (Modifier.isProtected(mod)) {
3 ✔
2633
                        fmx.setVisibility(MODIFIER_PROTECTED);
4 ✔
2634
                } else {
2635
                        //Default visibility!
2636
                        //If we are in an interface, default visibility is public, otherwise package.
2637
                        if (owner instanceof Interface) {
3 ✔
2638
                                fmx.setVisibility(MODIFIER_PUBLIC);
4 ✔
2639
                        } else {
2640
                                fmx.setVisibility(MODIFIER_PACKAGE);
3 ✔
2641
                        }
2642
                }
2643
        }
1 ✔
2644

2645
        /**
2646
         * Returns a Famix Attribute associated with the IVariableBinding.
2647
         * The Entity is created if it does not exist.<br>
2648
         * @param name -- the name of the FAMIX Attribute (MUST NOT be null, but this is not checked)
2649
         * @param type -- Famix Type of the Attribute (should not be null, but it will work if it is)
2650
         * @param owner -- type defining the Attribute (should not be null, but it will work if it is)
2651
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
2652
         */
2653
        public Attribute ensureFamixAttribute(IVariableBinding bnd, String name, Type type, TWithAttributes owner) {
2654
                Attribute fmx;
2655

2656
                // --------------- to avoid useless computations if we can
2657
                fmx = (Attribute)getEntityByKey(bnd);
5 ✔
2658
                if (fmx != null) {
2 ✔
2659
                        return fmx;
2 ✔
2660
                }
2661

2662
                // --------------- name
2663
                if (name == null) {
2 !
2664
                        if (bnd == null) {
×
2665
                                return null;
×
2666
                        }
2667
                        else {
2668
                                name = bnd.getName();
×
2669
                        }
2670
                }
2671

2672
                // --------------- owner
2673
                if (owner == null) {
2 ✔
2674
                        if (bnd == null) {
2 !
2675
                                return null;  // what would be the interest of creating an attribute for which we ignore the declaring class?
×
2676
                        }
2677
                        else {
2678
                                if (bnd.getDeclaringClass() != null && bnd.getDeclaringClass().getErasure() != null) {
7 !
2679
                                        // Declaring class is the generic one if the class is parametric.
2680
                                        owner = (TWithAttributes)ensureFamixType(bnd.getDeclaringClass().getErasure());
8 ✔
2681
                                } else {
2682
                                        return null;  // what would be the interest of creating an attribute for which we ignore the declaring class?
2 ✔
2683
                                }
2684
                        }
2685
                }
2686

2687
                // --------------- recover from name ?
2688
                for (Attribute candidate : getEntityByName(Attribute.class, name)) {
13 ✔
2689
                        if (matchAndMapVariable(bnd, name, (TNamedEntity) owner, candidate)) {
8 ✔
2690
                                fmx = candidate;
2 ✔
2691
                                break;
1 ✔
2692
                        }
2693
                }
1 ✔
2694

2695
                if (fmx == null) {
2 ✔
2696
                        fmx = ensureFamixEntity(Attribute.class, bnd, name);
7 ✔
2697
                        fmx.setParentType( owner);
3 ✔
2698
                }
2699

2700
        fmx.setParentType(owner);
3 ✔
2701
        ITypeBinding declaredTypeBinding = (bnd == null) ? null : bnd.getType();
7 ✔
2702
        ensureFamixEntityTyping(declaredTypeBinding, fmx, type);
6 ✔
2703
        if (bnd != null) {
2 ✔
2704
            int mod = bnd.getModifiers();
3 ✔
2705
            setAttributeModifiers(fmx, mod);
4 ✔
2706
        }
2707

2708
        return fmx;
2 ✔
2709
        }
2710

2711
        public Attribute ensureFamixAttribute(IVariableBinding bnd, String name, TWithAttributes owner) {
2712
                return ensureFamixAttribute(bnd, name, /*declared type*/null, owner);
7 ✔
2713
        }
2714

2715
        /**
2716
         * helper method, we know the var exists, ensureFamixAttribute will recover it
2717
         */
2718
        public Attribute getFamixAttribute(IVariableBinding bnd, String name, TWithAttributes owner) {
2719
                return ensureFamixAttribute(bnd, name, /*declared type*/null, owner);
7 ✔
2720
        }
2721

2722
        /**
2723
         * Returns a Famix Parameter associated with the IVariableBinding.
2724
         * The Entity is created if it does not exist.<br>
2725
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
2726
         */
2727
        public Parameter ensureFamixParameter(IVariableBinding bnd, String name, Type typ, TMethod tMethod) {
2728
                Parameter fmx = null;
2 ✔
2729

2730
                // --------------- to avoid useless computations if we can
2731
                try {
2732
                        fmx = (Parameter)getEntityByKey(bnd);
5 ✔
2733
                }catch(Throwable e) {
×
2734
                        e.printStackTrace();
×
2735
                }
1 ✔
2736
                if (fmx != null) {
2 ✔
2737
                        return fmx;
2 ✔
2738
                }
2739

2740
                // --------------- name
2741
                if (name == null) {
2 !
2742
                        if (bnd == null) {
×
2743
                                return null;
×
2744
                        }
2745
                        else {
2746
                                name = bnd.getName();
×
2747
                        }
2748
                }
2749

2750
                // --------------- owner
2751
                if (tMethod == null) {
2 !
2752
                        if (bnd == null) {
×
2753
                                tMethod = ensureFamixStubMethod("<"+name+"_owner>");
×
2754
                        }
2755
                        else {
2756
                                tMethod = ensureFamixMethod(bnd.getDeclaringMethod());
×
2757
                        }
2758
                }
2759

2760
                // --------------- recover from name ?
2761
                for (Parameter candidate : getEntityByName(Parameter.class, name) ) {
13 ✔
2762
                        if ( matchAndMapVariable(bnd, name, tMethod, candidate) ) {
7 !
2763
                                fmx = candidate;
×
2764
                                break;
×
2765
                        }
2766
                }
1 ✔
2767

2768
                if (fmx == null) {
2 !
2769
                        fmx = ensureFamixEntity(Parameter.class, bnd, name);
7 ✔
2770
                }
2771

2772
                if (fmx != null) {
2 !
2773
                        fmx.setParentBehaviouralEntity(tMethod);
3 ✔
2774
                        ITypeBinding declaredTypeBnd = (bnd == null) ? null : bnd.getType();
5 !
2775
                        ensureFamixEntityTyping(declaredTypeBnd, fmx, typ);
6 ✔
2776
                }
2777

2778
                return fmx;
2 ✔
2779
        }
2780

2781
    /**
2782
         * Returns a Famix LocalVariable associated with the IVariableBinding.
2783
         * The Entity is created if it does not exist.<br>
2784
         * @param name -- the name of the FAMIX LocalVariable
2785
         * @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
2786
         */
2787
        public LocalVariable ensureFamixLocalVariable(IVariableBinding bnd, String name, TWithLocalVariables owner) {
2788
                LocalVariable fmx;
2789

2790
                // --------------- to avoid useless computations if we can
2791
                fmx = (LocalVariable)getEntityByKey(bnd);
5 ✔
2792
                if (fmx != null) {
2 !
2793
                        return fmx;
×
2794
                }
2795

2796
                // --------------- name
2797
                if (name == null) {
2 !
2798
                        if (bnd == null) {
×
2799
                                return null;
×
2800
                        }
2801
                        else {
2802
                                name = bnd.getName();
×
2803
                        }
2804
                }
2805

2806
                // --------------- owner
2807
                if (owner == null) {
2 !
2808
                        if (bnd == null) {
×
2809
                                return null;  // what would be the interest of a local variable for which we ignore the declaring method?
×
2810
                        }
2811
                        else {
2812
                                owner = ensureFamixMethod(bnd.getDeclaringMethod());
×
2813
                        }
2814
                }
2815

2816
                // --------------- recover from name ?
2817
                for (LocalVariable candidate : getEntityByName(LocalVariable.class, name) ) {
13 ✔
2818
                        if ( matchAndMapVariable(bnd, name, (TNamedEntity) owner, candidate) ) {
8 !
2819
                                fmx = candidate;
×
2820
                                break;
×
2821
                        }
2822
                }
1 ✔
2823

2824
                if (fmx == null) {
2 !
2825
                        fmx = ensureFamixEntity(LocalVariable.class, bnd, name);
7 ✔
2826
                        fmx.setParentBehaviouralEntity(owner);
3 ✔
2827
                }
2828

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

2832
        return fmx;
2 ✔
2833
        }
2834

2835
    /**
2836
         * Returns a FAMIX ImplicitVariable with the given <b>name</b> ("self" or "super") and corresponding to the <b>type</b>.
2837
         * If this ImplicitVariable does not exist yet, it is created
2838
         * @param name -- the name of the FAMIX ImplicitVariable (should be Dictionary.SELF_NAME or Dictionary.SUPER_NAME)
2839
         * @param type -- the Famix Type for this ImplicitVariable (should not be null)
2840
         * @param tMethod -- the ContainerEntity where the implicit variable appears (should be a method inside <b>type</b>)
2841
         * @return the FAMIX ImplicitVariable or null in case of a FAMIX error
2842
         */
2843
        public ImplicitVariable ensureFamixImplicitVariable(IBinding key, String name, TType type, TMethod tMethod) {
2844
                ImplicitVariable fmx;
2845
                fmx = ensureFamixEntity(ImplicitVariable.class, key, name);
7 ✔
2846
                fmx.setParentBehaviouralEntity(tMethod);
3 ✔
2847
                fmx.setIsStub(tMethod.getIsStub());
4 ✔
2848
                if (fmx.getTyping() == null) {
3 ✔
2849
                        ensureFamixEntityTyping(null, fmx, type);
7 ✔
2850
                } else {
2851
                        fmx.getTyping().setDeclaredType(type);
4 ✔
2852
                }
2853
                return fmx;
2 ✔
2854
        }
2855

2856
        public ImplicitVariable ensureFamixImplicitVariable(String name, TType tType, TMethod tMethod) {
2857
                IBinding bnd = ImplicitVarBinding.getInstance(tMethod, name);
4 ✔
2858
                return ensureFamixImplicitVariable(bnd, name, tType, tMethod);
7 ✔
2859
        }
2860

2861
        /**
2862
         * Creates and returns a Famix Comment and associates it with an Entity (ex: for Javadocs)
2863
         * @param jCmt -- the content (String) of the comment 
2864
         * @param owner -- the entity that is commented
2865
         * @return the Famix Comment
2866
         */
2867
        public Comment createFamixComment(org.eclipse.jdt.core.dom.Comment jCmt, TWithComments owner) {
2868
                Comment cmt = null;
2 ✔
2869

2870
                if ( (jCmt != null) && (owner != null) ) {
4 !
2871
                        
2872
                        cmt = new Comment();
4 ✔
2873
                        addSourceAnchor(cmt, jCmt);
5 ✔
2874
                        famixRepoAdd(cmt);
3 ✔
2875
                        cmt.setCommentedEntity(owner);
3 ✔
2876
                }
2877

2878
                return cmt;
2 ✔
2879
        }
2880

2881
        /**
2882
         * Creates and returns a Famix Comment and associates it with an Entity
2883
         * @param jCmt -- the content (String) of the comment 
2884
         * @param owner -- the entity that is commented
2885
         * @param content -- the text of the comment
2886
         * @return the Famix Comment
2887
         */
2888
        public Comment createFamixComment(org.eclipse.jdt.core.dom.Comment jCmt, TWithComments owner, String content) {
2889
                Comment cmt = null;
2 ✔
2890

2891
                if ( (jCmt != null) && (owner != null) ) {
4 !
2892
                        cmt = new Comment();
4 ✔
2893
                        cmt.setContent(content );
3 ✔
2894
                        famixRepoAdd(cmt);
3 ✔
2895
                        cmt.setCommentedEntity(owner);
3 ✔
2896
                }
2897

2898
                return cmt;
2 ✔
2899
        }
2900

2901
        /**
2902
         * Adds location information to a Famix Entity.
2903
         * 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.
2904
         * This method also creates some basic links between the entity and others (e.g. declaring container, return type, ...)
2905
         * @param fmx -- Famix Entity to add the anchor to
2906
         * @param node -- JDT ASTNode, where the information is extracted
2907
         * @return the Famix SourceAnchor added to fmx. May be null in case of incorrect parameter ('fmx' or 'ast' == null)
2908
         */
2909
        public SourceAnchor addSourceAnchor(TSourceEntity fmx, ASTNode node) {
2910
                IndexedFileAnchor fa;
2911

2912
                fa = createIndexedFileAnchor(node);
4 ✔
2913
                if ((fmx != null) && (fa != null)) {
4 !
2914
                        fmx.setSourceAnchor(fa);
3 ✔
2915
                        famixRepoAdd(fa);
3 ✔
2916
                }
2917

2918
                return fa;
2 ✔
2919
        }
2920

2921
        /**
2922
         * Special case of  {@linkplain #addSourceAnchor(TSourceEntity, ASTNode)} to add location information to a Famix Method.
2923
         */
2924
        public SourceAnchor addSourceAnchor(Method fmx, MethodDeclaration node) {
2925
                IndexedFileAnchor fa;
2926

2927
                fa = createIndexedFileAnchor(node);
4 ✔
2928
                if ((fmx != null) && (fa != null)) {
4 !
2929

2930
                        // may change the positions
2931
                        List<ASTNode> methodDeclarationModifiers = new ArrayList<>();
4 ✔
2932
                        methodDeclarationModifiers.addAll(node.modifiers());
5 ✔
2933
                        if (node.getName() != null) {
3 !
2934
                                methodDeclarationModifiers.add(node.getName());
5 ✔
2935
                        }
2936
                        if (node.getReturnType2() != null) {
3 ✔
2937
                                methodDeclarationModifiers.add(node.getReturnType2());
5 ✔
2938
                        }
2939
                        int beg = (methodDeclarationModifiers.stream().mapToInt(el -> el.getStartPosition()).min().getAsInt()) + 1;
12 ✔
2940
                        int end = node.getStartPosition() + node.getLength();
6 ✔
2941

2942
                        fa.setStartPos(beg);
4 ✔
2943
                        fa.setEndPos(end);
4 ✔
2944

2945
                        fmx.setSourceAnchor(fa);
3 ✔
2946
                        famixRepoAdd(fa);
3 ✔
2947
                }
2948

2949
                return fa;
2 ✔
2950
        }
2951

2952
        /**
2953
         * Gets the file name holding <code>node</code> and its start and end positions in the file.
2954
         * Information returned in the form of an IndexedFileAnchor
2955
         */
2956
        protected IndexedFileAnchor createIndexedFileAnchor(ASTNode node) {
2957
                IndexedFileAnchor fa;
2958
                
2959
                if (node == null) {
2 !
2960
                        return null;
×
2961
                }
2962

2963
                // position in source file
2964
                int beg = node.getStartPosition() + 1; // Java starts at 0, Moose at 1
5 ✔
2965
                int end = beg + node.getLength() - 1;
7 ✔
2966

2967
                // find source Compilation Unit
2968
                // there is a special case for the JDT Comment Nodes
2969
                if (node instanceof org.eclipse.jdt.core.dom.Comment) {
3 ✔
2970
                        node = ((org.eclipse.jdt.core.dom.Comment) node).getAlternateRoot();
5 ✔
2971
                } else {
2972
                        node = node.getRoot();
3 ✔
2973
                }
2974

2975
                fa = new IndexedFileAnchor();
4 ✔
2976
                fa.setStartPos(beg);
4 ✔
2977
                fa.setEndPos(end);
4 ✔
2978

2979
                fa.setFileName((String) node.getProperty(SOURCE_FILENAME_PROPERTY));
6 ✔
2980

2981
                return fa;
2 ✔
2982
        }
2983

2984
        /**
2985
         * Creates or recovers the Famix Class for "Object".
2986
         * Because "Object" is the root of the inheritance tree, it needs to be treated differently.
2987
         *
2988
         * @return a Famix class for "Object"
2989
         */
2990
        public Class ensureFamixClassObject() {
2991
        // 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...
2992
        Collection<Class> objects = getEntityByName(Class.class, "Object");
5 ✔
2993

2994
        for (Class entity : objects) {
10 ✔
2995
                //We need to cast because the type container is a FamixTWithType but all implementors of this should be named in Java...
2996
                if ("java.lang".equals(((TNamedEntity) entity.getTypeContainer()).getName())) {
7 ✔
2997
                    return entity;
2 ✔
2998
                }
2999
        }
1 ✔
3000

3001
        Class fmx = createFamixEntity(Class.class, "Object");
6 ✔
3002
        fmx.setTypeContainer(ensureFamixPackageJavaLang(null));
5 ✔
3003
                return fmx;
2 ✔
3004
        }
3005

3006
        /***
3007
         * We treat array types as parametrized types Array<T>.
3008
         * This keeps the meta-model simple: a single way to model different concepts.
3009
         * @return
3010
         */
3011
        public ParametricClass ensureParametricArrayClass() {
3012
                
3013
                Collection<ParametricClass> arrayClasses = getEntityByName(ParametricClass.class, "Array");
5 ✔
3014
                
3015
                for (ParametricClass entity : arrayClasses) {
10 ✔
3016
            //We need to cast because the type container is a FamixTWithType but all implementors of this should be named in Java...
3017
            if ("java.lang".equals(((TNamedEntity) entity.getTypeContainer()).getName())) {
7 !
3018
                return entity;
2 ✔
3019
            }
3020
                }
×
3021
                
3022
                //Create the parametric class
3023
                ParametricClass arrayClass = createFamixEntity(ParametricClass.class, "Array"); 
6 ✔
3024

3025
                //And now add the type parameter
3026
                TypeParameter fmxParam = this.ensureFamixTypeParameter(null, "T", arrayClass);
6 ✔
3027
                fmxParam.setGenericEntity(arrayClass);
3 ✔
3028
                fmxParam.setIsStub(true);
4 ✔
3029
                
3030
                //And the class is in java.lang
3031
                arrayClass.setTypeContainer(ensureFamixPackageJavaLang(null));
5 ✔
3032
                
3033
                return arrayClass;
2 ✔
3034
        }
3035

3036
        /**
3037
         * Ensures the Java meta-class: <pre>{@code java.lang.Class<>}</pre>
3038
         */
3039
        public Class ensureFamixMetaClass(ITypeBinding bnd) {
3040
                Package javaLang = ensureFamixPackageJavaLang((bnd == null) ? null : bnd.getPackage());
7 !
3041
                ParametricClass fmx = (ParametricClass) this.ensureFamixClass(null, METACLASS_NAME, javaLang, /*isGeneric*/true, Modifier.PUBLIC & Modifier.FINAL);
9 ✔
3042

3043
                if (fmx != null) {
2 !
3044
                        fmx.addTypeParameters(ensureFamixTypeParameter(null, "T", fmx));
7 ✔
3045
                }
3046

3047
                if ((fmx != null) && (fmx.getSuperInheritances() == null)) {
5 !
3048
                        ensureFamixInheritance(ensureFamixClassObject(), fmx, null, null);
×
3049
                }
3050

3051
                return fmx;
2 ✔
3052
        }
3053

3054
        public Class getFamixMetaClass(ITypeBinding bnd) {
3055
                Package javaLang = ensureFamixPackageJavaLang((bnd == null) ? null : bnd.getPackage());
7 !
3056
                return this.ensureFamixClass(null, METACLASS_NAME, javaLang, /*isGeneric*/true, UNKNOWN_MODIFIERS);
8 ✔
3057
        }
3058

3059
        /**
3060
         * Creates or recovers the Famix Class for all arrays (<pre>{@code <some-type> []}</pre>)
3061
         * In java arrays or objects of special classes (i.e. "I[" for an array of int).
3062
         * JDT does not create a binding for these classes, so we create a stub one here.
3063
         *
3064
         * @return a Famix class
3065
         */
3066
        public Class ensureFamixClassArray() {
3067
                Class fmx = ensureFamixUniqEntity(Class.class, null, ARRAYS_NAME);
×
3068
                if (fmx != null) {
×
3069
                        ensureFamixInheritance(ensureFamixClassObject(), fmx, /*prev*/null, null);
×
3070
                        fmx.setTypeContainer(ensureFamixPackageDefault());
×
3071

3072
                        // may be not needed anymore now that we use modifiers
3073
                        /*fmx.setIsAbstract(Boolean.FALSE);
3074
                        fmx.setIsFinal(Boolean.FALSE);
3075
                        fmx.setIsInterface(Boolean.FALSE); 
3076
                        fmx.setIsPrivate(Boolean.FALSE);
3077
                        fmx.setIsProtected(Boolean.FALSE);*/
3078
                        fmx.setVisibility(MODIFIER_PUBLIC);
×
3079
                }
3080

3081
                return fmx;
×
3082
        }
3083

3084
        public String removeLastPartOfPackageName(String qualifiedName) {
3085
                String ret;
3086
                int last = qualifiedName.lastIndexOf('.');
4 ✔
3087
                if (last > 0) {
2 ✔
3088
                        // recursively creating the parent
3089
                        ret = qualifiedName.substring(0, last);
6 ✔
3090
                }
3091
                else {
3092
                        ret = "";
2 ✔
3093
                }
3094

3095
                return ret;
2 ✔
3096
        }
3097

3098
        /** Generates the list of parameters for a method signature
3099
         * @return a string
3100
         */
3101
        protected String signatureParamsFromBinding(IMethodBinding bnd) {
3102
                boolean first = true;
2 ✔
3103
                String sig = "";
2 ✔
3104

3105
                for (ITypeBinding parBnd : bnd.getParameterTypes()) {
17 ✔
3106
                        if (first) {
2 ✔
3107
                                sig = parBnd.getName();
3 ✔
3108
                                first = false;
3 ✔
3109
                        }
3110
                        else {
3111
                                sig += "," + parBnd.getName();
5 ✔
3112
                        }
3113
                }
3114
                return sig;
2 ✔
3115
        }
3116

3117
        private String signatureParamsFromStringCollection(Collection<String> paramTypes) {
3118
                boolean first = true;
2 ✔
3119
                String sig = "";
2 ✔
3120

3121
                for (String t : paramTypes) {
10 ✔
3122
                        if (first) {
2 ✔
3123
                                sig = t;
2 ✔
3124
                                first = false;
3 ✔
3125
                        }
3126
                        else {
3127
                                sig += "," + t;
4 ✔
3128
                        }
3129
                }
1 ✔
3130
                return sig;
2 ✔
3131
        }
3132

3133
        public String findTypeName(org.eclipse.jdt.core.dom.Type t) {
3134
                if (t == null) {
×
3135
                        return null;
×
3136
                }
3137

3138
                if (t.isPrimitiveType()) {
×
3139
                        return t.toString();
×
3140
                } else if (t.isSimpleType()) {
×
3141
                        String fullName = ((SimpleType) t).getName().getFullyQualifiedName();
×
3142
                        int i = fullName.lastIndexOf('.');
×
3143
                        if (i > 0) {
×
3144
                                return fullName.substring(i+1);
×
3145
                        }
3146
                        else {
3147
                                return fullName;
×
3148
                        }
3149
                } else if (t.isQualifiedType()) {
×
3150
                        return ((QualifiedType) t).getName().getIdentifier();
×
3151
                } else if (t.isArrayType()) {
×
3152
                        return findTypeName(((ArrayType) t).getElementType());
×
3153
                } else if (t.isParameterizedType()) {
×
3154
                        return findTypeName(((org.eclipse.jdt.core.dom.ParameterizedType) t).getType());
×
3155
                } else { // it is a WildCardType
3156
                        if (((org.eclipse.jdt.core.dom.WildcardType) t).isUpperBound()) {
×
3157
                                return findTypeName(((org.eclipse.jdt.core.dom.WildcardType) t).getBound());
×
3158
                        } else {
3159
                                return EntityDictionary.OBJECT_NAME;
×
3160
                        }
3161
                }
3162
        }
3163

3164
        /**
3165
         * Ensures the proper creation of a FamixType for JDT typ in the given context.
3166
         * Useful for parameterizedTypes, or classInstance.
3167
         *
3168
         * @param isClass we are sure that the type is actually a class
3169
         * @return a famix type or null
3170
         */
3171
        public <T extends TWithTypes & TNamedEntity> TType referredType(org.eclipse.jdt.core.dom.Type typ, T ctxt, boolean isClass) {
3172
                return this.referredType(typ, ctxt, isClass, /*isExcep*/false);
7 ✔
3173
        }
3174
        
3175
        /**
3176
         * Ensures the proper creation of a FamixType for JDT type in the given context.
3177
         * Useful for parameterizedTypes, or classInstance.
3178
         *
3179
         * @param isClass we are sure that the type is actually a class
3180
         * @return a famix type or null
3181
         */
3182
        public <T extends TWithTypes & TNamedEntity> TType referredType(org.eclipse.jdt.core.dom.Type typ, T ctxt, boolean isClass, boolean isException) {
3183
                if (typ == null) {
2 !
3184
                        return null;
×
3185
                } else if (typ.resolveBinding() != null) {
3 !
3186
                        return this.referredType(typ.resolveBinding(), ctxt);
7 ✔
3187
                }
3188
                // from here, we assume the owner is the context
3189
                else if (isClass && !isException) {
×
3190
                        return this.ensureFamixClass(null, findTypeName(typ), /*owner*/ctxt, /*isGeneric*/false,
×
3191
                                        EntityDictionary.UNKNOWN_MODIFIERS);
3192
                } else if (isException) {
×
3193
                        // return ensure FamixException
3194
                        return this.ensureFamixException(null, findTypeName(typ), (ContainerEntity) /*owner*/ctxt, /*isGeneric*/false,
×
3195
                                        EntityDictionary.UNKNOWN_MODIFIERS);
3196
                } else {
3197
                        while (typ.isArrayType()) {
×
3198
                                typ = ((ArrayType) typ).getElementType();
×
3199
                        }
3200

3201
                        if (typ.isPrimitiveType()) {
×
3202
                                return this.ensureFamixPrimitiveType(null, findTypeName(typ));
×
3203
                        } else {
3204
                                return this.ensureFamixType(null, findTypeName(typ), /*owner*/ctxt, /*container*/ctxt,
×
3205
                                                EntityDictionary.UNKNOWN_MODIFIERS);
3206
                        }
3207
                }
3208
        }
3209
        
3210
        public TType referredType(ITypeBinding bnd, TNamedEntity ctxt) {
3211
                return this.referredType(bnd, ctxt, 0);
6 ✔
3212
        }
3213
        
3214
        public TType referredType(ITypeBinding bnd, TNamedEntity ctxt, int extraDimensions) {
3215
                TType fmxTyp = null;
2 ✔
3216

3217
                if (bnd == null) {
2 ✔
3218
                        return null;
2 ✔
3219
                }
3220

3221
                String name;
3222
                //Three cases here:
3223
                // - the type binding knows it's an array
3224
                // - the binding is not declared as array, but it has dimensions > 0
3225
                // - the dimension is not in the type but in the variable declaration (thus extra)
3226
                if (bnd.isArray() || bnd.getDimensions() + extraDimensions > 0) {
8 ✔
3227
                        // We treat array types as parametrized types Array<T>.
3228
                        // This keeps the meta-model simple: a single way to model different concepts.
3229
                        return this.ensureParametricArrayClass();
3 ✔
3230
                }
3231
                name = bnd.getName();
3 ✔
3232

3233
                if ( bnd.isParameterizedType() ) {
3 ✔
3234
                        
3235
                        // remove type parameters from the name even for parameterized interfaces
3236
                        int i = name.indexOf('<');
4 ✔
3237
                        if (i > 0) {
2 ✔
3238
                                name = name.substring(0, i);
5 ✔
3239
                        }
3240

3241
                        ITypeBinding parameterizableBnd = bnd.getErasure();
3 ✔
3242
                        int modifiers = (parameterizableBnd != null) ? parameterizableBnd.getModifiers() : EntityDictionary.UNKNOWN_MODIFIERS;
6 !
3243
                        
3244
                        if(parameterizableBnd != null && parameterizableBnd.isInterface()) {
5 !
3245
                                fmxTyp = (ParametricInterface) this.ensureFamixInterface(parameterizableBnd, name, /*owner*/null, /*isGeneric*/true, modifiers);
10 ✔
3246
                        } else {
3247
                                fmxTyp = (ParametricClass) this.ensureFamixClass(parameterizableBnd, name, /*owner*/null, /*isGeneric*/true, modifiers);
9 ✔
3248
                        }
3249
                } else if ( (name != null) && name.equals("var") ) {
7 !
3250
                        fmxTyp = this.ensureFamixUniqEntity(org.moosetechnology.model.famix.famixjavaentities.Type.class, /*binding*/null, EntityDictionary.IMPLICIT_VAR_TYPE_NAME);        
×
3251
                } else {
3252
                        fmxTyp = this.ensureFamixType(bnd, name, /*owner*/null, (TWithTypes) ctxt, bnd.getModifiers());
10 ✔
3253
                }
3254

3255
                return fmxTyp;
2 ✔
3256
        }
3257

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