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

moosetechnology / VerveineJ / 23737250939

30 Mar 2026 09:17AM UTC coverage: 52.067% (+0.03%) from 52.039%
23737250939

Pull #206

github

web-flow
Merge d49309125 into d564a6404
Pull Request #206: Array support

1973 of 3976 branches covered (49.62%)

Branch coverage included in aggregate %.

86 of 137 new or added lines in 5 files covered. (62.77%)

2 existing lines in 1 file now uncovered.

4388 of 8241 relevant lines covered (53.25%)

2.15 hits per line

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

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

3
import java.util.*;
4

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

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

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

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

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

82
        public static final int UNKNOWN_MODIFIERS = 0;
83
        public static final String MODIFIER_PUBLIC   = "public";
84
        public static final String MODIFIER_PRIVATE  = "private";
85
        public static final String MODIFIER_PROTECTED= "protected";
86
        public static final String MODIFIER_PACKAGE = "package";
87

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

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

98
        /**
99
         * The FAMIX repository where all FAMIX entities are created and stored
100
         */
101
        protected Repository famixRepo;
102

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

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

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

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

142

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

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

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

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

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

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

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

239
                return ret;
2✔
240
        }
241

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

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

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

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

295
                        mapEntityToName(name, fmx);
4✔
296
                        
297
                        // put new entity in Famix repository
298
                        famixRepoAdd((Entity) fmx);
4✔
299
                }
300

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

322
                if (bnd != null) {
2✔
323
                        fmx = (T) getEntityByKey(bnd);
4✔
324
                        if (fmx != null) {
2✔
325
                                return fmx;
2✔
326
                        }
327
                }
328

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

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

348

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

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

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

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

417
        ///// ensure Famix Relationships /////
418

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

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

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

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

466
                while (concreteIterator.hasNext() && genericIterator.hasNext()) {
6!
467
                        TTypeArgument typeArgument = (TTypeArgument)ensureFamixType(concreteIterator.next());
7✔
468
                        TypeParameter typeParameter = (TypeParameter)ensureFamixType(genericIterator.next());
7✔
469

470
                        Concretization concretization = ensureFamixConcretization(typeArgument, typeParameter);
5✔
471
                        association.addConcretization(concretization);
3✔
472
                }
1✔
473

474
                return association;
2✔
475
        }
476

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

488
                Concretization concretization = new Concretization();
4✔
489
                concretization.setTypeArgument(typeArgument);
3✔
490
                concretization.setTypeParameter(typeParameter);
3✔
491

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

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

521
                implementation.setImplementingClass(implementingClass);
3✔
522
                implementation.setMyInterface(myInterface);
3✔
523
                chainPrevNext(prev, implementation);
4✔
524
                famixRepoAdd(implementation);
3✔
525
                return implementation;
2✔
526
        }
527

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

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

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

570
                if (referredTypeBnd != null) {
2✔
571
                        if (referredTypeBnd.isParameterizedType()) {
3✔
572
                                ref = (ParametricReference)buildFamixParametricAssociation(new ParametricReference(), referredTypeBnd.getErasure().getTypeParameters(), referredTypeBnd.getTypeArguments());
13✔
573
                        } else if (referredTypeBnd.isArray()) {
3✔
574
                                ref = new ParametricReference();
4✔
575
                        
576
                                TTypeArgument typeArgument  = (TTypeArgument)ensureFamixType(referredTypeBnd.getElementType());
6✔
577
                                TypeParameter typeParameter = (TypeParameter) ((TParametricEntity)tgt).getTypeParameters().iterator().next();
7✔
578

579
                                Concretization concretization = ensureFamixConcretization(typeArgument, typeParameter);
5✔
580
                                ((ParametricReference)ref).addConcretization(concretization);
4✔
581
                        }
582
                }
583

584
                if (ref == null){
2✔
585
                        ref = new Reference();
4✔
586
                }
587

588
                ref.setReferredEntity(tgt);
3✔
589
                ref.setReferencer(src);
3✔
590
                chainPrevNext(prev,ref);
4✔
591
                famixRepoAdd(ref);
3✔
592

593
                return ref;
2✔
594
        }
595

596
        /**
597
         * Returns a Famix Invocation between two Famix Entities creating it if needed
598
         * @param tMethod of the invocation
599
         * @param invoked -- method invoked
600
         * @param receiver of the invocation
601
         * @param signature -- i.e. actual invocation code
602
         * @param prev -- previous invocation relationship in the same context
603
         * @return the FamixInvocation
604
         */
605
        public Invocation addFamixInvocation(TMethod tMethod, TMethod invoked, TInvocationsReceiver receiver, String signature, TAssociation prev, IMethodBinding invokedBnd) {
606
                if ( (tMethod == null) || (invoked == null) ) {
4!
607
                        return null;
×
608
                }
609
                Invocation invocation;
610
                if (invokedBnd != null && invokedBnd.isParameterizedMethod()) {
5✔
611
                        invocation = (ParametricInvocation)buildFamixParametricAssociation(new ParametricInvocation(), invokedBnd.getMethodDeclaration().getTypeParameters(), invokedBnd.getTypeArguments());
13✔
612
                } else if ( invokedBnd != null && invokedBnd.isConstructor() && invokedBnd.getMethodDeclaration().getDeclaringClass().isGenericType()) {
10✔
613
                        invocation = (ParametricInvocation)buildFamixParametricAssociation(new ParametricInvocation(), invokedBnd.getMethodDeclaration().getDeclaringClass().getTypeParameters(), invokedBnd.getDeclaringClass().getTypeArguments());
15✔
614
                } else {
615
                        invocation = new Invocation();
4✔
616
                }
617

618
                invocation.setReceiver(receiver);
3✔
619
                invocation.setSender(tMethod);
3✔
620
                invocation.setSignature((signature == null) ? invoked.getSignature() : signature);
5!
621
                invocation.addCandidates(invoked);
3✔
622
                chainPrevNext(prev,invocation);
4✔
623
                famixRepoAdd(invocation);
3✔
624
                
625
                return invocation;
2✔
626
        }
627

628
        /**
629
         * Returns a Famix Access between two Famix Entities creating it if needed
630
         * @param accessor -- the entity (presumably a method) accessing the attribute
631
         * @param var -- the variable accessed
632
         * @param isWrite -- whether this is an access for reading or writing in the variable
633
         * @param prev -- previous access relationship in the same context
634
         * @return the FamixAccess
635
         */
636
        public Access addFamixAccess(TWithAccesses accessor, TStructuralEntity var, boolean isWrite, TAssociation prev) {
637
                if ( (accessor == null) || (var == null) ) {
4!
638
                        return null;
×
639
                }
640
                Access acc = new Access();
4✔
641
                acc.setAccessor(accessor);
3✔
642
                acc.addCandidates((TAccessible) var);
3✔
643
                acc.setIsWrite(isWrite);
4✔
644
                chainPrevNext(prev, acc);
4✔
645
                famixRepoAdd(acc);
3✔
646
                
647
                return acc;
2✔
648
        }
649

650
        protected void chainPrevNext(TAssociation prev, TAssociation next) {
651
                if (prev != null) {
2✔
652
                        next.setPrevious(prev);  // not yet implemented in importer
3✔
653
                }
654
        }
1✔
655
        
656
        /**
657
         * Returns a Famix DeclaredException between a method and an Exception that it declares to throw
658
         * @param meth -- the method throwing the exception
659
         * @param excep -- the exception declared to be thrown
660
         * @return the DeclaredException
661
         */
662
        public TThrowable createFamixDeclaredException(Method meth, TThrowable excep) {
663
                if ( (meth == null) || (excep == null) ) {
4!
664
                        return null;
×
665
                }
666
                meth.getDeclaredExceptions().add(excep);
5✔
667
                return excep;
2✔
668
        }
669

670
        /**
671
         * Returns a Famix CaughtException between a method and an Exception that is caught
672
         * @param meth -- the method catching the exception
673
         * @param excep -- the exception caught
674
         * @return the CaughtException
675
         */
676
        public TThrowable createFamixCaughtException(Method meth, TThrowable excep) {
677
                if ( (meth == null) || (excep == null) ) {
4!
678
                        return null;
×
679
                }
680
                meth.getCaughtExceptions().add(excep);
5✔
681
                return excep;
2✔
682
        }
683

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

700

701
        /**
702
         * Returns a Famix EntityTyping between a typed entity and a type.
703
         * @param typedEntity -- the typed entity
704
         * @param declaredType -- the declared type
705
         * @return the FamixEntityTyping
706
         */
707
        public EntityTyping ensureFamixEntityTyping(ITypeBinding declaredTypeBnd, TTypedEntity typedEntity, TType declaredType) {
708
                if ( (typedEntity == null) || (declaredType == null) ) {
4!
709
                        return null;
2✔
710
                }
711
                EntityTyping typing = null;
2✔
712
                
713
                if (declaredTypeBnd != null) {
2✔
714
                        if (declaredTypeBnd.isParameterizedType()) {
3✔
715
                                typing = (ParametricEntityTyping)buildFamixParametricAssociation(new ParametricEntityTyping(), declaredTypeBnd.getErasure().getTypeParameters(), declaredTypeBnd.getTypeArguments());
13✔
716
                        }else if (declaredTypeBnd.isArray()) {
3✔
717
                                typing = new ParametricEntityTyping();
4✔
718
                                
719
                                TTypeArgument typeArgument  = (TTypeArgument)ensureFamixType(declaredTypeBnd.getElementType());
6✔
720
                                TypeParameter typeParameter = (TypeParameter) ((ParametricClass)declaredType).getTypeParameters().iterator().next();
7✔
721

722
                                Concretization concretization = ensureFamixConcretization(typeArgument, typeParameter);
5✔
723
                                ((ParametricEntityTyping)typing).addConcretization(concretization);
4✔
724
                        }
725
                }
726

727
                // If we did not set a typing because not parameterized nor array, set a default one
728
                if (typing == null) {
2✔
729
                        typing = new EntityTyping();
4✔
730
                }
731
                
732
                typing.setTypedEntity(typedEntity);
3✔
733
                typing.setDeclaredType(declaredType);
3✔
734
                famixRepoAdd(typing);
3✔
735
                
736
                return typing;
2✔
737
        }
738

739

740
        ///// Special Case: ImplicitVariables /////
741

742
        /**
743
         * Returns the Famix ImplicitVariable associated to the given binding and name (self or super).
744
         * See also {@link #getEntityByKey(IBinding)}
745
         * @param bnd -- the binding
746
         * @return the Famix Entity associated to the binding or null if not found
747
         */
748
        @Deprecated
749
        public ImplicitVariable getImplicitVariableByBinding(IBinding bnd, String iv_name) {
750
                return getImplicitVariableByType((Class)getEntityByKey(bnd), iv_name);
×
751
        }
752
        
753
        /**
754
         * Returns the Famix ImplicitVariable associated to the given FamixType.
755
         * @param type -- the FamixType
756
         * @param name -- name of the ImplicitVariable (should be Dictionary.SELF_NAME or Dictionary.SUPER_NAME)
757
         * @return the Famix ImplicitVariable associated to the Type or null if not found
758
         */
759
        @Deprecated
760
        public ImplicitVariable getImplicitVariableByType(Type type, String name) {
761
                ImplicitVars iv = typeToImpVar.get(type);
×
762
                ImplicitVariable ret = null;
×
763
                
764
                if (iv == null) {
×
765
                        iv = new ImplicitVars();
×
766
                }
767
                
768
                if (name.equals(THIS_NAME)) {
×
769
                        ret = iv.self_iv;
×
770
                }
771
                else if (name.equals(SUPER_NAME)) {
×
772
                        ret = iv.super_iv;
×
773
                }
774

775
                return ret;
×
776
        }
777

778
        ///// Special Case: "Uniq" Entities /////
779

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

817
                return fmx;
2✔
818
        }
819

820
        /**
821
         * Creates or recovers the Famix Class that will own all stub methods (for which the real owner is unknown)
822
         *
823
         * @return a Famix class
824
         */
825
        public Class ensureFamixClassStubOwner() {
826
                Class fmx =  ensureFamixUniqEntity(Class.class, null, STUB_METHOD_CONTAINER_NAME);
7✔
827
                if (fmx != null) {
2!
828
                        fmx.setTypeContainer( ensureFamixPackageDefault());
4✔
829
                }
830
                ensureFamixInheritance(ensureFamixClassObject(), fmx, /*prev*/null, null);
8✔
831

832
                return fmx;
2✔
833
        }
834

835
        public Type searchTypeInContext(String name, TWithTypes ctxt) {
836
                if (ctxt == null) {
×
837
                        return null;
×
838
                }
839
                
840
                for (TType candidate : ctxt.getTypes()) {
×
841
                        if (candidate.getName().equals(name) ) {
×
842
                                return (Type) candidate;
×
843
                        }
844
                }
×
845
                
846
                return searchTypeInContext(name, Util.getOwner((TNamedEntity)ctxt));
×
847
        }
848

849
        /**
850
         * Returns a Famix Package associated with its IPackageBinding and/or fully qualified name.
851
         * The Entity is created if it does not exist.
852
         * We assume that Namespaces must be uniq for a given name
853
         * Also creates or recovers recusively it's parent namespaces.<br>
854
         * At least one of <b>bnd</b> and <b>name</b> must be non null.
855
         *
856
         * @param bnd  -- the JDT Binding that may be used as a uniq key to recover this namespace
857
         * @param name -- fully qualified name of the namespace (e.g. 'java.lang')
858
         * @return the Famix Namespace found or created. May return null in case of a Famix error
859
         */
860
        public Package ensureFamixPackage(IPackageBinding bnd, String name) {
861
                Package fmx;
862
                Package parent;
863

864
                if ((name == null) && (bnd != null)) {
4!
865
                        name = bnd.getName();
3✔
866
                }
867

868
                if ((name == null) || name.equals("")) {
6!
869
                        return ensureFamixPackageDefault();
3✔
870
                } else {
871
                        /* Note: Packages are created with their fully-qualified name to simplify recovering when we don't have a binding
872
                         * (for example when creating parent packages of a package we have a binding for).
873
                         * Because the preferred solution in Moose is to give their simple names to packages, they must be post-processed when
874
                         * all is said and done. */
875
                        fmx = ensureFamixUniqEntity(Package.class, bnd, name);
7✔
876
                        String parentName = removeLastPartOfPackageName(name);
4✔
877
                        if (parentName.length() > 0) {
3✔
878
                                parent = ensureFamixPackage(null, parentName);
5✔
879
                                // set the parentscope relationship
880
                                if ((parent != null) && (fmx != null) && (fmx.getParentPackage() == null)) {
7!
881
                                        parent.addChildEntities(fmx);
3✔
882
                                }
883
                        }
884
                }
885

886
                return fmx;
2✔
887
        }
888

889
        /**
890
         * Creates or recovers a default Famix Package.
891
         * Because this package does not really exist, it has no binding.
892
         *
893
         * @return a Famix Namespace
894
         */
895
        public Package ensureFamixPackageDefault() {
896
        return ensureFamixUniqEntity(Package.class, null, DEFAULT_PCKG_NAME);
7✔
897
        }
898

899
        /**
900
         * Creates or recovers a Famix Package for the package of Java class "Object" (i.e. "java.lang").
901
         * Because "Object" is the root of the inheritance tree, it needs to be treated differently.
902
         *
903
         * @param bnd -- a potential binding for the "java.lang" package
904
         * @return a Famix Namespace for "java.lang"
905
         */
906
        public Package ensureFamixPackageJavaLang(IPackageBinding bnd) {
907

908
        return this.ensureFamixPackage(bnd, OBJECT_PACKAGE_NAME);
5✔
909
        }
910

911
        /**
912
         * Returns the Package with {@link #DEFAULT_PCKG_NAME} or <code>null</code> if not found
913
         */
914
        public Package getFamixPackageDefault() {
915
                Collection<Package> l = getEntityByName(Package.class, DEFAULT_PCKG_NAME);
5✔
916
                if (l.size() > 0) {
3!
917
                        return l.iterator().next();
5✔
918
                } else {
919
                        return null;
×
920
                }
921
        }
922

923
        /**
924
         * Returns a Famix Type with the given <b>name</b>, creating it if it does not exist yet.
925
         * In the second case, sets some default properties: not Abstract, not Final, not Private, not Protected, not Public, not Interface
926
         * @param bnd -- binding for the type to create
927
         * @param name of the type
928
         * @param owner of the type
929
         * @param ctxt -- context of use of the type
930
         */
931
        public Type ensureFamixType(ITypeBinding bnd, String name, TWithTypes owner, TWithTypes ctxt, int modifiers) {
932
                
933
                Type fmx;
934

935
                if (bnd == null) {
2!
UNCOV
936
                        if (name == null) {
×
UNCOV
937
                                return null;
×
938
                        }
939
                        fmx = searchTypeInContext(name, ctxt); // WildCard Types don't have binding
×
940
                        if (fmx != null) {
×
941
                                return fmx;
×
942
                        }
943

944
                        if ((owner instanceof TParametricEntity)) {
×
945
                                return this.ensureFamixTypeParameter(null, name, owner);
×
946
                        }
947
                        else {
948
                                fmx = ensureFamixEntity(Type.class, bnd, name);
×
949
                                fmx.setTypeContainer(owner);
×
950
                                return fmx;
×
951
                        }
952
                }
953

954
                // bnd != null
955

956
                fmx = (Type) getEntityByKey(bnd);
5✔
957
                if (fmx != null) {
2✔
958
                        return fmx;
2✔
959
                }
960

961
                if (bnd.isArray()) {
3!
962
                        bnd = bnd.getElementType();
×
963
                }
964

965
                if (bnd.isPrimitive()) {
3✔
966
                        return this.ensureFamixPrimitiveType(bnd, name);
5✔
967
                }
968

969
                if (bnd.isEnum()) {
3!
970
                        return this.ensureFamixEnum(bnd, name, owner);
×
971
                }
972
 
973
                if ((bnd.isRawType() || bnd.isGenericType()) && !bnd.isInterface() ) {
9✔
974
                        return this.ensureFamixClass(bnd.getErasure(), name, (TNamedEntity) owner, /*isGeneric*/true, modifiers);
10✔
975
                }
976

977
                if (bnd.isCapture()) {
3✔
978
                        if (bnd.getErasure().isInterface()) {
4!
979
                                return this.ensureFamixInterface(bnd.getErasure(), name, owner, /*isGeneric*/true, modifiers);
9✔
980
                        }
981
                        else {
982
                                return this.ensureFamixClass(bnd.getErasure(), name, (TNamedEntity) owner, /*isGeneric*/true, modifiers);
×
983
                        }
984
                }
985

986
                if (bnd.isAnnotation()) {
3✔
987
                        return this.ensureFamixAnnotationType(bnd, name, (ContainerEntity) owner);
7✔
988
                }
989

990
                if (bnd.isInterface()) {
3✔
991
                        return this.ensureFamixInterface(bnd, name, owner, /*isGeneric*/bnd.isGenericType() || bnd.isParameterizedType() || bnd.isRawType(), modifiers);
19✔
992
                }
993

994
                if (isThrowable(bnd)) {
4✔
995
                        return this.ensureFamixException(bnd, name, owner, /*isGeneric*/false, modifiers);
8✔
996
                }
997
                if (bnd.isClass()) {
3✔
998
                        return this.ensureFamixClass(bnd, name, (TNamedEntity) owner, /*isGeneric*/bnd.isGenericType() || bnd.isParameterizedType() || bnd.isRawType(), modifiers);
20!
999
                }
1000
                if(bnd.isWildcardType()) {
3✔
1001
                        return this.ensureFamixWildcardType(bnd, name, (TParametricEntity)owner, ctxt);
8✔
1002
                }
1003

1004
                //otherwise (none of the above)
1005

1006
                if (name == null) {
2✔
1007
                        name = bnd.getName();
3✔
1008
                }
1009

1010
                if (owner == null) {
2!
1011
                        owner = (TWithTypes) this.ensureOwner(bnd);
5✔
1012
                }
1013

1014
                if (bnd.isTypeVariable() ) {
3!
1015
                        fmx = ensureFamixTypeParameter(bnd, name, owner);
6✔
1016
                        return fmx;
2✔
1017
                }
1018

1019
                fmx = ensureFamixEntity(Type.class, bnd, name);
×
1020
                fmx.setTypeContainer(owner);
×
1021
                return fmx;
×
1022
        }
1023

1024
        public Type ensureFamixType(ITypeBinding bnd, TWithTypes context) {
1025
        int modifiers = (bnd != null) ? bnd.getModifiers() : UNKNOWN_MODIFIERS;
6!
1026
                return ensureFamixType(bnd, /*name*/null, /*owner*/null, context, modifiers);
8✔
1027
        }
1028
        
1029
        public Type ensureFamixType(ITypeBinding bnd) {
1030
                return ensureFamixType(bnd, /*ctxt*/null);
5✔
1031
        }
1032

1033
        public boolean isThrowable(ITypeBinding bnd) {
1034
                if (bnd == null) {
2!
1035
                        return false;
×
1036
                }
1037
                if (bnd.getQualifiedName().equals("java.lang.Throwable")) {
5✔
1038
                        return true;
2✔
1039
                } else if (bnd.getQualifiedName().equals("java.lang.Object")) {
5✔
1040
                        return false;
2✔
1041
                }
1042
                else {
1043
                        return isThrowable(bnd.getSuperclass());
5✔
1044
                }
1045
        }
1046

1047
        /**
1048
         * Returns a Famix Class associated with the ITypeBinding.
1049
         * The Entity is created if it does not exist.
1050
         * @param name -- the name of the Famix Class (MUST NOT be null, but this is not checked)
1051
         * @param owner -- package defining the class (should not be null, but it will work if it is)
1052
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
1053
         */
1054
        @SuppressWarnings("deprecation")
1055
        public Class ensureFamixClass(ITypeBinding bnd, String name, TNamedEntity owner, boolean isGeneric, int modifiers) {
1056
                Class fmx;
1057

1058
                // --------------- some special cases
1059
                if (bnd != null) {
2✔
1060
                        if (bnd.isArray()) {
3!
1061
                                bnd = bnd.getElementType();
×
1062
                        }
1063

1064
                        // for inner classes defined in generics !!! For others should not change anything
1065
                        bnd = bnd.getErasure();
3✔
1066
                }
1067

1068
                // ---------------- to avoid useless computations if we can
1069
                fmx = (Class) getEntityByKey(bnd);
5✔
1070
                if (fmx != null) {
2✔
1071
                        return fmx;
2✔
1072
                }
1073

1074
                // --------------- name
1075
                if (name == null) {
2✔
1076
                        if (bnd == null) {
2!
1077
                                return null;  // not much we can do
×
1078
                        } else if (!bnd.isAnonymous()) {
3!
1079
                                name = bnd.getErasure().getName();  // for generics, will give the "core" type name, for normal type, won't change anything
5✔
1080
                        } else { // anonymous class
1081
                                if (bnd.getSuperclass() != null) {
×
1082
                                        name = bnd.getSuperclass().getName();
×
1083
                                }
1084
                                if ((name == null) || name.equals(OBJECT_NAME)) {
×
1085
                                        ITypeBinding[] intfcs = bnd.getInterfaces();
×
1086
                                        if ((intfcs != null) && (intfcs.length > 0)) {
×
1087
                                                name = bnd.getInterfaces()[0].getName();
×
1088
                                        }
1089
                                        else {
1090
                                                name = "???";
×
1091
                                        }
1092
                                }
1093
                                name = ANONYMOUS_NAME_PREFIX + "(" + name + ")";
×
1094
                        }
1095
                }
1096

1097
        // If we have java.lang.Object we should ensure we create this class
1098
        if (bnd != null && bnd.getQualifiedName().equals("java.lang.Object")) {
7✔
1099
                        return ensureFamixClassObject();
3✔
1100
                }
1101

1102
                // --------------- owner
1103
                if (owner == null) {
2✔
1104
                        if (bnd != null) {
2✔
1105
                                owner = ensureOwner(bnd);
4✔
1106
                        }
1107
                        /*                                owner = ensureFamixPackageDefault();
1108
                        } else {*/
1109
                }
1110

1111
                // --------------- recover from name ?
1112
                if (owner != null) {
2✔
1113
                        for (Class candidate : this.getEntityByName(Class.class, name)) {
13✔
1114
                                if (matchAndMapClass(bnd, name, owner, candidate)) {
7✔
1115
                                        fmx = candidate;
2✔
1116
                                        break;
1✔
1117
                                }
1118
                        }
1✔
1119
                }
1120

1121
                // ---------------- create
1122
                if (fmx == null) {
2✔
1123
                        if (isGeneric) {
2✔
1124
                                fmx = ensureFamixParametricClass(bnd, name, (TWithTypes) owner);
8✔
1125
                        }
1126
                        else {
1127
                                fmx = ensureFamixEntity(Class.class, bnd, name);
7✔
1128
                                fmx.setTypeContainer((TWithTypes)owner);
4✔
1129
                        }
1130
                }
1131

1132
                // ---------------- modifiers and super-classes
1133
                if (fmx!=null) {
2!
1134
                        // we just created it, or it was not bound so we make sure it has the right information in it
1135
                        if (bnd != null) {
2✔
1136
                                setClassModifiers(fmx, bnd.getDeclaredModifiers());
5✔
1137
                        }
1138

1139
                        TAssociation lastAssoc = null;
2✔
1140

1141
                        if (bnd != null) {
2✔
1142
                                ITypeBinding supbnd = bnd.getSuperclass();
3✔
1143
                                if (supbnd != null) {
2✔
1144
                                        lastAssoc = ensureFamixInheritance((TWithInheritances) ensureFamixType(supbnd), fmx, lastAssoc, supbnd);
11✔
1145
                                }
1146
                                else {
1147
                                        lastAssoc = ensureFamixInheritance(ensureFamixClassObject(), fmx, lastAssoc, null);
8✔
1148
                                }
1149
                                ensureImplementedInterfaces(bnd, fmx, (TWithTypes) owner, lastAssoc);
7✔
1150
                        }
1151
                }
1152

1153
                return fmx;
2✔
1154
        }
1155

1156
        /**
1157
         * Returns a Famix Exception associated with the ITypeBinding.
1158
         * The Entity is created if it does not exist.
1159
         * @param name -- the name of the Famix Exception
1160
         * @param owner -- type defining the Exception (should not be null, but it will work if it is) 
1161
         *
1162
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
1163
         */
1164
        public <T extends TWithTypes & TNamedEntity> Exception ensureFamixException(ITypeBinding bnd, String name, TWithTypes owner, boolean isGeneric, int modifiers) {
1165
                Exception fmx;
1166

1167
                // --------------- some special cases
1168
                if (bnd != null) {
2✔
1169
                        if (bnd.isArray()) {
3!
1170
                                bnd = bnd.getElementType();
×
1171
                        }
1172

1173
                        // for inner classes defined in generics !!! For others should not change anything
1174
                        bnd = bnd.getErasure();
3✔
1175
                }
1176

1177
                // ---------------- to avoid useless computations if we can
1178
                fmx = (Exception) getEntityByKey(bnd);
5✔
1179
                if (fmx != null) {
2✔
1180
                        return fmx;
2✔
1181
                }
1182

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

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

1215
                // --------------- recover from name ?
1216
                for (Exception candidate : this.getEntityByName(Exception.class, name)) {
13✔
1217
                        if (matchAndMapClass(bnd, name, (T) owner, candidate)) {
8!
1218
                                fmx = candidate;
×
1219
                                break;
×
1220
                        }
1221
                }
1✔
1222

1223
                // ---------------- create
1224
                if (fmx == null) {
2!
1225
                        fmx = ensureFamixEntity(Exception.class, bnd, name);
7✔
1226
                        fmx.setTypeContainer(owner);
3✔
1227
                }
1228

1229
        // we just created it or it was not bound, so we make sure it has the right information in it
1230
        TAssociation lastAssoc = null;
2✔
1231
        if (bnd != null) {
2✔
1232
            ITypeBinding supbnd = bnd.getSuperclass();
3✔
1233
            if (supbnd != null) {
2!
1234
                lastAssoc = ensureFamixInheritance((TWithInheritances) ensureFamixType(supbnd), fmx, lastAssoc, supbnd);
11✔
1235
            }
1236
            else {
1237
                lastAssoc = ensureFamixInheritance(ensureFamixClassObject(), fmx, lastAssoc, null);
×
1238
            }
1239
            ensureImplementedInterfaces(bnd, fmx, owner, lastAssoc);
6✔
1240
        }
1241

1242
        return fmx;
2✔
1243
        }
1244

1245
        /**
1246
         * Returns a FAMIX Interface with the given <b>name</b>, creating it if it does not exist yet.
1247
         * @param name -- the name of the FAMIX Method
1248
         * @param owner -- type defining the method (should not be null, but it will work if it is) 
1249
         * @return the FAMIX Class or null in case of a FAMIX error
1250
         */
1251
        public <T extends TWithTypes & TNamedEntity> Interface ensureFamixInterface(ITypeBinding bnd, String name, TWithTypes owner, boolean isGeneric, int modifiers) {
1252
                Interface fmx;
1253

1254
                // --------------- some special cases
1255
                if (bnd != null) {
2!
1256
                        if (bnd.isArray()) {
3!
1257
                                bnd = bnd.getElementType();
×
1258
                        }
1259

1260
                        // for inner classes defined in generics !!! For others should not change anything
1261
                        bnd = bnd.getErasure();
3✔
1262
                }
1263

1264
                // ---------------- to avoid useless computations if we can
1265
                fmx = (Interface) getEntityByKey(bnd);
5✔
1266
                if (fmx != null) {
2✔
1267
                        return fmx;
2✔
1268
                }
1269

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

1293
                // --------------- owner
1294
                if (owner == null) {
2✔
1295
                        if (bnd == null) {
2!
1296
                                owner = ensureFamixPackageDefault();
×
1297
                        } else {
1298
                                owner = (TWithTypes) ensureOwner(bnd);
5✔
1299
                        }
1300
                }
1301

1302
                // --------------- recover from name ?
1303
                for (Interface candidate : this.getEntityByName(Interface.class, name)) {
13✔
1304
                        if (matchAndMapInterface(bnd, name, (T) owner, candidate)) {
8✔
1305
                                fmx = candidate;
2✔
1306
                                break;
1✔
1307
                        }
1308
                }
1✔
1309

1310
                // ---------------- create
1311
                if (fmx == null) {
2✔
1312
                        if (isGeneric) {
2✔
1313
                                fmx = ensureFamixParametricInterface(bnd, name, owner);
7✔
1314
                        }
1315
                        else {
1316
                                fmx = ensureFamixEntity(Interface.class, bnd, name);
7✔
1317
                                fmx.setTypeContainer(owner);
3✔
1318
                        }
1319
                }
1320

1321
                // ---------------- modifiers and "super interfaces"
1322
                if (fmx!=null) {
2!
1323
                        // we just created it or it was not bound, so we make sure it has the right information in it
1324
                        if (bnd != null) {
2!
1325
                                setInterfaceModifiers(fmx, bnd.getModifiers());
5✔
1326
                        }
1327
                        TAssociation lastAssociation = null;
2✔
1328
                        if (bnd != null) {
2!
1329
                                ensureImplementedInterfaces(bnd, fmx, owner, lastAssociation);
6✔
1330
                        }
1331
                }
1332
                return fmx;
2✔
1333
        }
1334

1335
        /**
1336
         * "Converts" (if needed) a TTYpe entity to be a TThrowable. Might involve removing the existing entity, recreating a new one and migrating
1337
         * all the relationship of the former to the later
1338
         */
1339
        public TThrowable asException(TType fmxType) {
1340
                if (fmxType instanceof Exception) {
3✔
1341
                        return (Exception) fmxType;
3✔
1342
                }
1343
                if(fmxType instanceof TypeParameter) {
3✔
1344
                        return (TypeParameter) fmxType;
3✔
1345
                }
1346

1347
                Exception fmxException = null;
2✔
1348
                IBinding key;
1349

1350
                try {
1351
                        key = entityToKey.get(fmxType);
6✔
1352

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

1357
                        TWithTypes owner = fmxType.getTypeContainer();
3✔
1358
                        fmxType.setTypeContainer(null);
3✔
1359
                        fmxException = ensureFamixException((ITypeBinding) key, fmxType.getName(), owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
10✔
1360

1361
                        fmxException.addMethods( new ArrayList<>( ((TWithMethods)fmxType).getMethods() ) );
8✔
1362
                        if (fmxType instanceof TWithAttributes) {
3!
1363
                                fmxException.addAttributes( new ArrayList<>( ((TWithAttributes)fmxType).getAttributes() ) );
8✔
1364
                        }
1365

1366
                        if (fmxType instanceof TWithInheritances) {
3!
1367
                                fmxException.addSuperInheritances( new ArrayList<>( ((TWithInheritances) fmxType).getSuperInheritances() ) );
8✔
1368
                                fmxException.addSubInheritances( new ArrayList<>( ((TWithInheritances) fmxType).getSubInheritances() ) );
8✔
1369
                        }
1370
                        fmxException.setSourceAnchor(fmxType.getSourceAnchor());
4✔
1371
                        fmxException.addIncomingTypings( new ArrayList<>( fmxType.getIncomingTypings() ) );
7✔
1372
                        fmxException.addAnnotationInstances( new ArrayList<>( ((NamedEntity)fmxType).getAnnotationInstances() ) );
8✔
1373
                        fmxException.addIncomingReferences( new ArrayList<>( fmxType.getIncomingReferences() ) );
7✔
1374
                        fmxException.setIsStub(fmxType.getIsStub());
4✔
1375
                        fmxException.addTypes( new ArrayList<>( ((ContainerEntity) fmxType).getTypes() ) );
8✔
1376
                }
1377
                catch( ConcurrentModificationException e) {
×
1378
                        e.printStackTrace();
×
1379
                }
1✔
1380

1381
                return fmxException;
2✔
1382
        }
1383

1384
        /**
1385
         * helper method, we know the type exists, ensureFamixClass will recover it
1386
         */
1387
        public Class getFamixClass(ITypeBinding bnd, String name, TNamedEntity owner) {
1388
                return ensureFamixClass(bnd, name, owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
8✔
1389
        }
1390

1391
        /**
1392
         * helper method, we know the type exists, ensureFamixInterface will recover it
1393
         */
1394
        public Interface getFamixInterface(ITypeBinding bnd, String name, ContainerEntity owner) {
1395
                return ensureFamixInterface(bnd, name, owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
8✔
1396
        }
1397

1398
        /**
1399
         * helper method, we know the type exists, ensureFamixInterface will recover it
1400
         */
1401
        public Exception getFamixException(ITypeBinding bnd, String name, TWithTypes owner) {
1402
                return ensureFamixException(bnd, name, owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
8✔
1403
        }
1404

1405
        /**
1406
         * Ensures a famix entity for the owner of a binding.<br>
1407
         * This owner can be a method, a class or a namespace
1408
         * @param bnd -- binding for the owned entity
1409
         * @return a famix entity for the owner
1410
         */
1411
        private TNamedEntity ensureOwner(ITypeBinding bnd) {
1412
                TNamedEntity owner;
1413
                IMethodBinding parentMtd = bnd.getDeclaringMethod();
3✔
1414
                if (parentMtd != null) {
2✔
1415
                        owner = this.ensureFamixMethod(parentMtd);  // cast needed to desambiguate the call
5✔
1416
                }
1417
                else {
1418
                        ITypeBinding parentClass = bnd.getDeclaringClass();
3✔
1419
                        if (parentClass != null) {
2✔
1420
                owner = this.ensureFamixType(parentClass);
5✔
1421
            }
1422
                        else {
1423
                                IPackageBinding parentPckg = bnd.getPackage();
3✔
1424
                                if (parentPckg != null) {
2!
1425
                                        owner = this.ensureFamixPackage(parentPckg, null);
6✔
1426
                                } else {
1427
                                        owner = this.ensureFamixPackageDefault();
×
1428
                                }
1429
                        }
1430
                }
1431
                return owner;
2✔
1432
        }
1433

1434

1435
        /**
1436
         * Returns a FAMIX PrimitiveType with the given <b>name</b>, creating it if it does not exist yet
1437
         * We assume that PrimitiveType must be uniq for a given name
1438
         * @param name -- the name of the FAMIX PrimitiveType
1439
         * @return the FAMIX PrimitiveType or null in case of a FAMIX error
1440
         */
1441
        public PrimitiveType ensureFamixPrimitiveType(ITypeBinding bnd, String name) {
1442
                if (name == null) {
2✔
1443
                        if (bnd == null) {
2!
1444
                                return null;
×
1445
                        } else {
1446
                                name = bnd.getName();
3✔
1447
                        }
1448
                }
1449
                return ensureFamixUniqEntity(PrimitiveType.class, bnd, name);
7✔
1450
        }
1451

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

1455
                // --------------- to avoid useless computations if we can
1456
                fmx = (org.moosetechnology.model.famix.famixjavaentities.Enum) getEntityByKey(bnd);
5✔
1457
                if (fmx != null) {
2✔
1458
                        return fmx;
2✔
1459
                }
1460

1461
                // --------------- name
1462
                if (name == null) {
2✔
1463
                        if (bnd == null) {
2!
1464
                                return null;
×
1465
                        }
1466
                        else {
1467
                                name = bnd.getName();
3✔
1468
                        }
1469
                }
1470

1471
                // --------------- owner
1472
                if (owner == null) {
2✔
1473
                        if (bnd == null) {
2!
1474
                                owner = ensureFamixPackageDefault();  // not really sure what to do here
×
1475
                        } else {
1476
                                owner = (TWithTypes) ensureOwner(bnd);
5✔
1477
                        }
1478
                }
1479

1480
                // --------------- recover from name ?
1481
                for (org.moosetechnology.model.famix.famixjavaentities.Enum candidate : getEntityByName(org.moosetechnology.model.famix.famixjavaentities.Enum.class, name)) {
9!
1482
                        if (matchAndMapType(bnd, name, (T) owner, candidate)) {
×
1483
                                fmx = candidate;
×
1484
                                break;
×
1485
                        }
1486
                }
×
1487

1488
                if (fmx == null) {
2!
1489
                        fmx = ensureFamixEntity(Enum.class, bnd, name);
7✔
1490
                        fmx.setTypeContainer(owner);
3✔
1491
                }
1492

1493
                if (bnd != null) {
2!
1494
                        setVisibility(fmx, bnd.getModifiers());
5✔
1495
                }
1496

1497
                return fmx;
2✔
1498
        }
1499

1500
        /**
1501
         * helper method, we know the type exists, ensureFamixEnum will recover it
1502
         */
1503
        public org.moosetechnology.model.famix.famixjavaentities.Enum getFamixEnum(ITypeBinding bnd, String name, TWithTypes owner) {
1504
                return ensureFamixEnum(bnd, name, owner);
6✔
1505
        }
1506

1507
        public EnumValue ensureFamixEnumValue(IVariableBinding bnd,        String name, Enum owner) {
1508
                EnumValue fmx;
1509

1510
                // --------------- to avoid useless computations if we can
1511
                fmx = (EnumValue)getEntityByKey(bnd);
5✔
1512
                if (fmx != null) {
2✔
1513
                        return fmx;
2✔
1514
                }
1515

1516
                // --------------- name
1517
                if (name == null) {
2!
1518
                        if (bnd == null) {
×
1519
                                return null;
×
1520
                        }
1521
                        else {
1522
                                name = bnd.getName();
×
1523
                        }
1524
                }
1525

1526
                // --------------- owner
1527
                if (owner == null) {
2✔
1528
                        if (bnd == null) {
2!
1529
                                return null;  // what would be the interest of creating an EnumValue without a declaring Enum type?
×
1530
                        }
1531
                        else {
1532
                                owner = ensureFamixEnum(bnd.getDeclaringClass(), null, null);
7✔
1533
                        }
1534
                }
1535

1536
                // --------------- recover from name ?
1537
                for (EnumValue candidate : getEntityByName(EnumValue.class, name) ) {
9!
1538
                        if ( matchAndMapVariable(bnd, name, owner, candidate) ) {
×
1539
                                fmx = candidate;
×
1540
                                break;
×
1541
                        }
1542
                }
×
1543
                if (fmx == null) {
2!
1544
                        fmx = ensureFamixEntity(EnumValue.class, bnd, name);
7✔
1545
                        fmx.setParentEnum(owner);
3✔
1546
                }
1547

1548
        fmx.setParentEnum(owner);
3✔
1549

1550
        return fmx;
2✔
1551
        }
1552

1553
    /**
1554
         * e.g. see {@link EntityDictionary#ensureFamixClass}
1555
         */
1556
        public AnnotationType ensureFamixAnnotationType(ITypeBinding bnd, String name, ContainerEntity owner) {
1557
                AnnotationType fmx;
1558

1559
                // --------------- to avoid useless computations if we can
1560
                fmx = (AnnotationType)getEntityByKey(bnd);
5✔
1561
                if (fmx != null) {
2✔
1562
                        return fmx;
2✔
1563
                }
1564

1565
                // --------------- name
1566
                if (name == null) {
2✔
1567
                        if (bnd == null) {
2!
1568
                                return null;
×
1569
                        }
1570
                        else {
1571
                                name = bnd.getName();
3✔
1572
                        }
1573
                }
1574

1575
                // --------------- owner
1576
                if (owner == null) {
2✔
1577
                        if (bnd == null) {
2!
1578
                                owner = ensureFamixPackageDefault();
×
1579
                        }
1580
                        else {
1581
                                IPackageBinding parentPckg = bnd.getPackage();
3✔
1582
                                if (parentPckg != null) {
2!
1583
                                        owner = this.ensureFamixPackage(parentPckg, null);
6✔
1584
                                } else {
1585
                                        owner = this.ensureFamixPackageDefault();
×
1586
                                }
1587
                        }
1588
                }
1589

1590
                // --------------- recover from name ?
1591
                for (AnnotationType candidate : getEntityByName(AnnotationType.class, name) ) {
13✔
1592
                        if ( matchAndMapType(bnd, name, owner, candidate) ) {
7✔
1593
                                fmx = candidate;
2✔
1594
                                break;
1✔
1595
                        }
1596
                }
1✔
1597

1598
                // --------------- create
1599
                if (fmx == null) {
2✔
1600
                        fmx = ensureFamixEntity(AnnotationType.class, bnd, name);
7✔
1601
                        fmx.setAnnotationTypesContainer(owner);
3✔
1602
                }
1603

1604
                if (bnd != null) {
2!
1605
                        // Not supported in Famix
1606

1607
                        // setVisibility(fmx, bnd.getModifiers());
1608
                }
1609

1610
                return fmx;
2✔
1611
        }
1612

1613
        /**
1614
         * helper method, we know the type exists, ensureFamixAnnotationType will recover it
1615
         */
1616
        public AnnotationType getFamixAnnotationType(ITypeBinding bnd, String name, ContainerEntity owner) {
1617
                return ensureFamixAnnotationType(bnd, name, owner);
6✔
1618
        }
1619

1620
        public AnnotationTypeAttribute ensureFamixAnnotationTypeAttribute(IMethodBinding bnd, String name, AnnotationType owner) {
1621
                AnnotationTypeAttribute fmx = null;
2✔
1622

1623
                // --------------- to avoid useless computations if we can
1624
                fmx = (AnnotationTypeAttribute)getEntityByKey(bnd);
5✔
1625
                if (fmx != null) {
2✔
1626
                        return fmx;
2✔
1627
                }
1628

1629
                // --------------- name
1630
                if (name == null) {
2!
1631
                        if (bnd == null) {
×
1632
                                return null;
×
1633
                        }
1634
                        else {
1635
                                name = bnd.getName();
×
1636
                        }
1637
                }
1638

1639
                // --------------- owner
1640
                if (owner == null) {
2!
1641
                        if (bnd == null) {
×
1642
                                return null;  // what would be the use of an AnnotationTypeAttribute without AnnotationType ?
×
1643
                        }
1644
                        else {
1645
                                ITypeBinding parentType = bnd.getDeclaringClass();
×
1646
                                if (parentType != null) {
×
1647
                                        owner = this.ensureFamixAnnotationType(parentType, null, null);
×
1648
                                }
1649
                                else  {
1650
                                        return null;  // what would be the use of an AnnotationTypeAttribute without AnnotationType ?
×
1651
                                }
1652
                        }
1653
                }
1654

1655
                // --------------- recover from name ?
1656
                for (AnnotationTypeAttribute candidate : getEntityByName(AnnotationTypeAttribute.class, name) ) {
13✔
1657
                        // JDT treats annotation type attributes as methods ...
1658
                        // checkAndMapMethod wants a signature as 2nd argument so we add empty param list
1659
                        if ( (bnd != null) && matchAndMapMethod(bnd, name+"()", null, owner, candidate) ) {
11!
1660
                                fmx = candidate;
×
1661
                                break;
×
1662
                        }
1663
                        // if the binding is null, the annotationTypeAttribute migth have been created
1664
                        else if ( (bnd == null) && matchAndMapVariable(null, name, owner, candidate)) {
2!
1665
                                fmx = candidate;
×
1666
                                break;
×
1667
                        }
1668
                }
1✔
1669

1670
                if (fmx == null) {
2!
1671
                        fmx = ensureFamixEntity(AnnotationTypeAttribute.class, bnd, name);
7✔
1672
                        fmx.setParentType(owner);
3✔
1673
                }
1674

1675
                if (bnd != null) {
2!
1676
                        // Not suopp
1677

1678
                        // setVisibility(fmx, bnd.getModifiers());
1679
                }
1680

1681
                return fmx;
2✔
1682
        }
1683

1684
        /**
1685
         * helper method, we know the attribute exists, ensureFamixAnnotationTypeAttribute will recover it
1686
         */
1687
        public AnnotationTypeAttribute getFamixAnnotationTypeAttribute(IMethodBinding bnd, String name, AnnotationType owner) {
1688
                return ensureFamixAnnotationTypeAttribute( bnd, name, owner);
6✔
1689
        }
1690
        
1691
        
1692
        /**
1693
         * Returns a FAMIX Wildcard with its bounds
1694
         * @param bnd
1695
         * @param name
1696
         * @param owner
1697
         * @return
1698
         */
1699
        public Wildcard ensureFamixWildcardType(ITypeBinding bnd, String name, TParametricEntity owner, TWithTypes ctxt) {
1700
                Wildcard fmx = this.ensureFamixEntity(Wildcard.class, bnd, bnd.getName());
8✔
1701
                if(bnd.getBound() != null) {
3✔
1702
                        Type bound = this.ensureFamixType(bnd.getBound());
5✔
1703
                        if(bnd.isUpperbound()) {
3✔
1704
                                fmx.setUpperBound(bound);
3✔
1705
                                bound.addUpperBoundedWildcards(fmx);
4✔
1706
                        }else{
1707
                                fmx.setLowerBound(bound);
3✔
1708
                                bound.addLowerBoundedWildcards(fmx);
3✔
1709
                        }
1710
                }
1711
                return fmx;
2✔
1712
        }
1713

1714
        /**
1715
         * Returns a Famix TypeParameter (created by a Famix ParametricEntity) with the given <b>name</b>, creating it if it does not exist yet
1716
         * In the second case, sets some default properties: not Abstract, not Final, not Private, not Protected, not Public
1717
         * @param name -- the name of the Famix TypeParameter
1718
         * @return the Famix TypeParameter or null in case of a Famix error
1719
         */
1720
        public TypeParameter ensureFamixTypeParameter(ITypeBinding bnd,        String name, TWithTypes owner) {
1721
                TypeParameter fmx;
1722

1723
                // --------------- to avoid useless computations if we can
1724
                fmx = (TypeParameter)getEntityByKey(bnd);
5✔
1725
                if (fmx != null) {
2✔
1726
                        return fmx;
2✔
1727
                }
1728

1729
                // --------------- name
1730
                if (name == null) {
2✔
1731
                        if (bnd == null) {
2!
1732
                                return null;
×
1733
                        }
1734
                        else {
1735
                                name = bnd.getName();
3✔
1736
                        }
1737
                }
1738

1739
                // --------------- owner
1740
                if (owner == null && bnd != null) {
2!
1741
            if (bnd.getDeclaringClass() != null) {
×
1742
                owner = this.ensureFamixType(bnd.getDeclaringClass());
×
1743
            } else if(bnd.getDeclaringMethod() != null) {
×
1744
                owner = this.ensureFamixMethod(bnd.getDeclaringMethod());
×
1745
            }
1746
                }
1747

1748
                // --------------- recover from name ?
1749
                for (Type candidate : this.getEntityByName(Type.class, name)) {
13✔
1750
                        if ( matchAndMapType(bnd, name, (ContainerEntity) owner, candidate) ) {
8✔
1751
                                fmx = (TypeParameter) candidate;
3✔
1752
                                break;
1✔
1753
                        }
1754
                }
1✔
1755

1756
                // --------------- create
1757
                if (fmx == null) {
2✔
1758
                        fmx = ensureFamixEntity(TypeParameter.class, bnd, name);
7✔
1759
                        if(bnd != null && bnd.getSuperclass() != null) {
5!
1760
                                Type upperBound = ensureFamixType(bnd.getSuperclass());
5✔
1761
                                fmx.setUpperBound(upperBound);
3✔
1762
                        }
1763
                        if(bnd != null) {
2✔
1764
                for (ITypeBinding intbnd : bnd.getInterfaces()) {
17✔
1765
                    Type upperBound = ensureFamixType(intbnd);
4✔
1766
                    fmx.setUpperBound(upperBound);
3✔
1767
                }
1768
            }
1769
                        fmx.setTypeContainer(owner);
3✔
1770
                }
1771

1772
                return fmx;
2✔
1773
        }
1774

1775
        /**
1776
         * Checks whether the existing unmapped Famix Namespace matches the binding.
1777
         * Checks that the candidate has the same name as the JDT bound package, and checks recursively that owners also match.
1778
         *
1779
         * @param bnd       -- a JDT binding that we are trying to match to the candidate
1780
         * @param name      of the package
1781
         * @param owner     of the package
1782
         * @param candidate -- a Famix Entity
1783
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1784
         */
1785
        private boolean matchAndMapPackage(IPackageBinding bnd, String name, Package owner, NamedEntity candidate) {
1786
                if (!(candidate instanceof Package)) {
3!
1787
                        return false;
×
1788
                }
1789

1790
                // check whether bnd and candidate are already bound
1791
                CheckResult res = checkKeyMatch(bnd, candidate);
5✔
1792
                if (res == CheckResult.MATCH) {
3✔
1793
                        return true;
2✔
1794
                } else if (res == CheckResult.FAIL) {
3!
1795
                        return false;
×
1796
                }
1797

1798
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7✔
1799
                        return false;
2✔
1800
                }
1801

1802
                // names match, not need to look at owner because names of Namespaces are their fully qualified name
1803
                conditionalMapToKey(bnd, candidate);
4✔
1804
                return true;
2✔
1805
        }
1806

1807
        /**
1808
         * Checks whether the existing unmapped Famix Type matches the binding.
1809
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
1810
         * We also check that the actual class of the candidate matches (can be a sub-class of FamixType).
1811
         * @param bnd -- a JDT binding that we are trying to match to the candidate
1812
         * @param name of the type
1813
         * @param owner of the type
1814
         * @param candidate -- a Famix NamedEntity (Class, Type, PrimitiveType, Enum, AnnotationType)
1815
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1816
         */
1817
        private <T extends TWithTypes & TNamedEntity> boolean matchAndMapType(ITypeBinding bnd, String name, TNamedEntity owner, TNamedEntity candidate) {
1818
                if (! (candidate instanceof Type) ) {
3!
1819
                        return false;
×
1820
                }
1821

1822
                // check whether bnd and candidate are already bound
1823
                CheckResult res = checkKeyMatch(bnd, candidate);
5✔
1824
                if (res == CheckResult.MATCH) {
3✔
1825
                        return true;
2✔
1826
                }
1827
                else if (res == CheckResult.FAIL) {
3✔
1828
                        return false;
2✔
1829
                }
1830

1831
                if ( (bnd != null) && (bnd.isArray()) ) {
5!
1832
                                bnd = bnd.getElementType();
×
1833
                }
1834

1835
                // checking names
1836
                if ( (bnd != null) && (bnd.isParameterizedType() || bnd.isRawType()) ) {
8!
1837
                        name = bnd.getErasure().getName();
×
1838
                }
1839
                else if (bnd != null) {
2✔
1840
                        name = bnd.getName();
3✔
1841
                }
1842
                // else name = name
1843
                if (checkNameMatch(null, name, candidate) == CheckResult.FAIL) {
7✔
1844
                        return false;
2✔
1845
                }
1846

1847
                // special case of primitive types
1848
                if (candidate instanceof PrimitiveType) {
3!
1849
                        if ( (bnd != null) && bnd.isPrimitive() ) {
×
1850
                                // names are equal so it's OK
1851
                                conditionalMapToKey(bnd, candidate);
×
1852
                                return true;
×
1853
                        }
1854
                        else if ( (bnd == null) && (owner == null) ) {
×
1855
                                return true;
×
1856
                        }
1857
                }
1858

1859
                // check owners without bnd
1860
                if (bnd == null) {
2✔
1861
                        return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
7✔
1862
                }
1863

1864
                // check owners with bnd
1865
                // type is an annotation
1866
                if (bnd.isAnnotation() && (candidate instanceof AnnotationType)) {
6!
1867
                        if (matchAndMapPackage(bnd.getPackage(), owner.getName(), (Package) Util.getOwner(owner), Util.getOwner(candidate))) {
13!
1868
                                conditionalMapToKey(bnd, candidate);
4✔
1869
                                return true;
2✔
1870
                        } else {
1871
                                return false;
×
1872
                        }
1873
                }
1874

1875
                // check owners with bnd
1876
                // type is a Parameterized type
1877
                if ((bnd.isParameterizedType() || bnd.isRawType()) && (candidate instanceof ParametricClass)) {
6!
1878
                        return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
×
1879
                }
1880

1881
                // check owners with bnd
1882
                // type is an Enum
1883
                if (bnd.isEnum() && (candidate instanceof Enum)) {
3!
1884
                        return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
×
1885
                }
1886

1887
                // check owners with bnd
1888
                // type is something elae (a class or interface)
1889
                // Annotation are interfaces too, so we should check this one after isAnnotation
1890
                if ( bnd.isClass()) {
3!
1891
                        return matchAndMapClass(bnd, name, owner, (Type) candidate);
×
1892
                }
1893

1894
                if(bnd.isInterface()) {
3!
1895
                        return matchAndMapInterface(bnd, name, owner, (Type) candidate);
×
1896
                }
1897

1898
                return false;
2✔
1899
        }
1900

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

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

1923
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7!
1924
                        return false;
×
1925
                }
1926

1927
                // checking owner
1928
                return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
7✔
1929
        }
1930

1931
        /**
1932
         * Checks whether the existing unmapped Famix Class (or Interface) matches the binding.
1933
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
1934
         * @param bnd -- a JDT binding that we are trying to match to the candidate
1935
         * @param name of the class
1936
         * @param owner of the class
1937
         * @param candidate -- a Famix Entity
1938
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1939
         */
1940
        private boolean matchAndMapInterface(ITypeBinding bnd, String name, TNamedEntity owner, Type candidate) {
1941
                if (!(candidate instanceof Interface)) {
3!
1942
                        return false;
×
1943
                }
1944

1945
                // check whether bnd and candidate are already bound
1946
                CheckResult res = checkKeyMatch(bnd, candidate);
5✔
1947
                if (res == CheckResult.MATCH) {
3!
1948
                        return true;
×
1949
                } else if (res == CheckResult.FAIL) {
3✔
1950
                        return false;
2✔
1951
                }
1952

1953
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7!
1954
                        return false;
×
1955
                }
1956

1957
                // checking owner
1958
                return matchAndMapTypeOwner(bnd, owner, candidate);
6✔
1959
        }
1960

1961
        /**
1962
         * Checks whether the existing unmapped Famix "Method" matches the binding.
1963
         * Checks that the candidate has the same name and same signature as the JDT bound method, and checks recursively that owners also match.
1964
         * Note that AnnotationTypeAttribute are treated as methods by JDT, so they are checked here.
1965
         * @param bnd -- a JDT binding that we are trying to match to the candidate
1966
         * @param sig -- signature of the method
1967
         * @param retTyp -- return type of the method
1968
         * @param owner of the method
1969
         * @param candidate -- a Famix Entity (regular Method or AnnotationTypeAttribute)
1970
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1971
         */
1972
        private  boolean matchAndMapMethod(IMethodBinding bnd, String sig, TType retTyp, TNamedEntity owner, NamedEntity candidate) {
1973
                if (! (candidate instanceof Method) ) {
3✔
1974
                        return false;
2✔
1975
                }
1976

1977
                // check whether bnd and candidate are already bound
1978
                CheckResult res = checkKeyMatch(bnd, candidate);
5✔
1979
                if (res == CheckResult.MATCH) {
3!
1980
                        return true;
×
1981
                }
1982
                else if (res == CheckResult.FAIL) {
3✔
1983
                        return false;
2✔
1984
                }
1985

1986
                // checking names
1987
                String name = (sig != null) ? sig.substring(0, sig.indexOf('(')) : null;
10!
1988
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7!
1989
                        return false;
×
1990
                }
1991

1992
                // for methods, the name is not enough, we must test the signature also
1993
                // but not for AnnotationTypeAttribute
1994

1995
                        if (bnd != null) {
2✔
1996
                                sig = bnd.getName() + "(" + signatureParamsFromBinding(bnd) + ")";
7✔
1997
                        }
1998
                        if (! ((Method) candidate).getSignature().equals(sig)) {
6✔
1999
                                return false;
2✔
2000
                        }
2001

2002
                        // and still for method, must also check the return type
2003
                        if (bnd != null) {
2✔
2004
                                if (bnd.isConstructor()) {
3✔
2005
                                        if ( ((Method) candidate).getDeclaredType() != null ) {
4!
2006
                                                return false;
×
2007
                                        }
2008
                                        // else OK for now
2009
                                }
2010
                                else { // not a constructor
2011
                                        if ( ((Method) candidate).getDeclaredType() == null ) {
4!
2012
                                                return false;
×
2013
                                        }
2014
                                        else if (! matchAndMapType(bnd.getReturnType(), null, null, ((Method) candidate).getDeclaredType()) ) {
10!
2015
                                                return false;
×
2016
                                        }
2017
                                        // else OK for now
2018
                                }
2019
                        }
2020
                        else {  // bnd == null
2021
                                if (retTyp == null) { // similar to (bnd.isConstructor())
2!
2022
                                        if ( ((Method) candidate).getDeclaredType() != null ) {
4✔
2023
                                                return false;
2✔
2024
                                        }
2025
                                        // else OK for now
2026
                                } else { // (ret != null)  i.e. not a constructor
2027
                                        if (((Method) candidate).getDeclaredType() == null) {
×
2028
                                                return false;
×
2029
                                        } else if (!matchAndMapType(null, retTyp.getName(), Util.getOwner(retTyp), (NamedEntity) ((Method) candidate).getDeclaredType())) {
×
2030
                                                return false;
×
2031
                                        }
2032
                                        // else OK for now
2033
                                }
2034
                        }
2035

2036

2037
                // check owner
2038
                if (matchAndMapOwnerAsType(((bnd != null) ? bnd.getDeclaringClass() : null), owner, Util.getOwner(candidate)) == CheckResult.MATCH) {
13✔
2039
                        conditionalMapToKey(bnd, candidate);
4✔
2040
                        return true;
2✔
2041
                } else {
2042
                        return false;
2✔
2043
                }
2044
        }
2045

2046
        /**
2047
         * Checks whether the candidate (an existing unmapped Famix "Variable" like Attribute, Parameter, ...) matches the binding.
2048
         * Checks that the candidate has the same name as the JDT bound variable, and checks recursively that owners also match.
2049
         * The Famix candidate is a NamedEntity and not a StructuralEntity to allow dealing with Famix EnumValue that JDT treats as variables
2050
         * @param bnd -- a JDT binding that we are trying to match to the candidate
2051
         * @param name of the variable
2052
         * @param owner of the variable
2053
         * @param candidate -- a Famix Entity (a StructuralEntity or an EnumValue)
2054
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
2055
         */
2056
        private boolean matchAndMapVariable(IVariableBinding bnd, String name, TNamedEntity owner, TNamedEntity candidate) {
2057
                if (!(candidate instanceof TStructuralEntity)) {
3!
2058
                        return false;
×
2059
                }
2060

2061
                // check whether bnd and candidate are already bound
2062
                CheckResult keyMatch = checkKeyMatch(bnd, candidate);
5✔
2063
                if (keyMatch == CheckResult.MATCH) {
3!
2064
                        return true;
×
2065
                } else if (keyMatch == CheckResult.FAIL) {
3✔
2066
                        return false;
2✔
2067
                }
2068

2069
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7!
2070
                        return false;
×
2071
                }
2072

2073
                // check owner
2074
                TNamedEntity candidateOwner = Util.getOwner(candidate);
3✔
2075

2076
                // local variable or parameter ?
2077
                // owner is a Method? (for example in case of an anonymous class)
2078
                CheckResult res = matchAndMapOwnerAsMethod(((bnd != null) ? bnd.getDeclaringMethod() : null), owner, candidateOwner);
11✔
2079
                if (res == CheckResult.FAIL) {
3✔
2080
                        return false;
2✔
2081
                } else if (res == CheckResult.MATCH) {
3!
2082
                        conditionalMapToKey(bnd, candidate);
×
2083
                        return true;
×
2084
                }
2085

2086
                // check owner
2087
                // <anArray>.length field?
2088
                if (name.equals("length")) {
4!
2089
                        boolean isArrayLengthField = ((bnd != null) && (bnd.getDeclaringClass() == null)) ||
×
2090
                                                                                 ((bnd == null) && (owner.getName().equals(EntityDictionary.ARRAYS_NAME)));
×
2091
                        if (isArrayLengthField) {
×
2092
                                if (candidateOwner.getName().equals(EntityDictionary.ARRAYS_NAME)) {
×
2093
                                        conditionalMapToKey(bnd, candidate);
×
2094
                                        return true;
×
2095
                                }
2096
                                else {
2097
                                        return false;
×
2098
                                }
2099
                        }
2100
                }
2101

2102
                // check owner
2103
                // "normal" field?
2104
                res = matchAndMapOwnerAsType( ((bnd != null) ? bnd.getDeclaringClass() : null), owner, candidateOwner);
11✔
2105
                if (res == CheckResult.MATCH) {
3!
2106
                        conditionalMapToKey(bnd, candidate);
4✔
2107
                        return true;
2✔
2108
                }
2109
                return false;
×
2110
        }
2111

2112
        /**
2113
         * Checks whether the existing unmapped Famix Type's parent (or owner) matches the binding's owner.
2114
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
2115
         * @param bnd -- a JDT binding whose owner we are trying to match to the candidate's owner
2116
         * @param owner -- the owner of the type
2117
         * @param candidate -- a Famix Entity
2118
         * @return whether we found a match (if <b>true</b>, the mapping is recorded)
2119
         */
2120
        private boolean matchAndMapTypeOwner(ITypeBinding bnd, TNamedEntity owner, Type candidate) {
2121
                ContainerEntity candidateOwner = Util.getOwner(candidate);
4✔
2122

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

2132
                // owner is a class ?
2133
                res = matchAndMapOwnerAsType(((bnd != null) ? bnd.getDeclaringClass() : null), owner, candidateOwner);
11✔
2134
                if (res == CheckResult.MATCH) {
3✔
2135
                        conditionalMapToKey(bnd, candidate);
4✔
2136
                        return true;
2✔
2137
                }
2138
                else if (res == CheckResult.FAIL) {
3✔
2139
                        return false;
2✔
2140
                }
2141

2142
                // owner must be a package
2143
                if (matchAndMapOwnerAsNamespace( ((bnd != null)?bnd.getPackage():null), owner, candidateOwner) == CheckResult.MATCH) {
12✔
2144
                        conditionalMapToKey(bnd, candidate);
4✔
2145
                        return true;
2✔
2146
                }
2147
                return false;
2✔
2148
        }
2149

2150
        /**
2151
         * Check whether the owner of candidates is a method macthinf either methBnd or owner
2152
         * @param methBnd
2153
         * @param owner
2154
         * @param candidateOwner
2155
         * @return a {@link CheckResult}
2156
         */
2157
        private  <T extends TNamedEntity> CheckResult matchAndMapOwnerAsMethod(IMethodBinding methBnd, T owner, T candidateOwner) {
2158
                if ((methBnd != null) || (owner instanceof Method)) {
5!
2159
                        if (!(candidateOwner instanceof Method)) {
3!
2160
                                return CheckResult.FAIL;
×
2161
                        }
2162

2163
                        ContainerEntity ownerOwner = (owner != null) ? (ContainerEntity) Util.getOwner(owner) : null;
7!
2164
                        String ownerSig = (owner != null) ? ((Method) owner).getSignature() : null;
7!
2165
                        Type ownerReturn = (owner != null) ? (Type) ((Method) owner).getDeclaredType() : null;
8!
2166

2167
                        if (matchAndMapMethod(methBnd, ownerSig, ownerReturn, ownerOwner, (Method) candidateOwner)) {
9!
2168
                                return CheckResult.MATCH;
×
2169
                        } else {
2170
                                return CheckResult.FAIL;
2✔
2171
                        }
2172
                }
2173
                return CheckResult.UNDECIDED;
2✔
2174
        }
2175

2176
        /**
2177
         * @param typBnd
2178
         * @param owner
2179
         * @param candidateOwner
2180
         * @return a {@link CheckResult}
2181
         */
2182
        private CheckResult matchAndMapOwnerAsType(ITypeBinding typBnd, TNamedEntity owner, TNamedEntity candidateOwner) {
2183
                if ((typBnd != null) || (owner instanceof Type)) {
5✔
2184
                        if (!(candidateOwner instanceof Type)) {
3✔
2185
                                return CheckResult.FAIL;
2✔
2186
                        }
2187

2188
                        TNamedEntity ownerOwner = (owner != null) ? Util.getOwner(owner) : null;
6!
2189
                        String ownerName = (owner != null) ? owner.getName() : null;
6!
2190

2191
                        if (matchAndMapType(typBnd, ownerName, ownerOwner, candidateOwner)) {
7✔
2192
                                return CheckResult.MATCH;
2✔
2193
                        } else {
2194
                                return CheckResult.FAIL;
2✔
2195
                        }
2196
                }
2197
                return CheckResult.UNDECIDED;
2✔
2198
        }
2199

2200
        private CheckResult matchAndMapOwnerAsNamespace(IPackageBinding pckgBnd, TNamedEntity owner, ContainerEntity candidateOwner) {
2201
                if ((pckgBnd != null) || (owner instanceof Package)) {
5!
2202
                        if (!(candidateOwner instanceof Package)) {
3!
2203
                                return CheckResult.FAIL;
×
2204
                        }
2205

2206
                        Package ownerOwner = (owner != null) ? (Package) Util.getOwner(owner) : null;
7!
2207
                        String ownerName = (owner != null) ? owner.getName() : null;
6!
2208

2209
                        if (matchAndMapPackage(pckgBnd, ownerName, ownerOwner, candidateOwner)) {
7✔
2210
                                return CheckResult.MATCH;
2✔
2211
                        } else {
2212
                                return CheckResult.FAIL;
2✔
2213
                        }
2214
                }
2215
                return CheckResult.UNDECIDED;
×
2216
        }
2217

2218
        /**
2219
         * Checks whether the name and the candidate matches the name of the entity (given either by 'bnd' or 'name')<br>
2220
         * 'name' and 'bnd' cannot be null together
2221
         * @param bnd -- binding associated with the entity may be null
2222
         * @param name -- name of the entity may be null
2223
         * @param candidate
2224
         * @return true if names match, false if not
2225
         */
2226
        private CheckResult checkNameMatch(IBinding bnd, String name, TNamedEntity candidate) {
2227
                if ( (bnd != null) && (! bnd.getName().equals(candidate.getName())) ) {
8!
2228
                        return CheckResult.FAIL;
×
2229
                }
2230
                else if ( (bnd == null) && (name != null) && (! name.equals(candidate.getName())) ) {
9!
2231
                        return CheckResult.FAIL;
2✔
2232
                }
2233
                else {
2234
                        return CheckResult.MATCH;
2✔
2235
                }
2236
        }
2237

2238
        /**
2239
         * Check whether key and candidate are already bound together, whether either is bound to something else, or whether none is bound
2240
         * @param key
2241
         * @param candidate
2242
         * @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>
2243
         */
2244
        private CheckResult checkKeyMatch(IBinding key, TNamedEntity candidate) {
2245
                if (key == null) {
2✔
2246
                        return CheckResult.UNDECIDED;
2✔
2247
                }
2248

2249
                NamedEntity bound = (NamedEntity)getEntityByKey(key);
5✔
2250
                if (bound == candidate) {
3✔
2251
                        return CheckResult.MATCH;
2✔
2252
                }
2253
                else if (bound != null) {
2✔
2254
                        return CheckResult.FAIL;
2✔
2255
                }
2256
                else if (getEntityKey(candidate) != null) {
4✔
2257
                        // candidate already bound, and not to this binding
2258
                        return CheckResult.FAIL;
2✔
2259
                }
2260
                else {
2261
                        return CheckResult.UNDECIDED;
2✔
2262
                }
2263
        }
2264

2265
        private void conditionalMapToKey(IBinding bnd, TNamedEntity ent) {
2266
                if (bnd != null) {
2✔
2267
                        mapEntityToKey(bnd, ent);
4✔
2268
                }
2269
        }
1✔
2270

2271
        public Method ensureFamixMethod(IMethodBinding bnd) {
2272
                return ensureFamixMethod(
8✔
2273
                                bnd,
2274
                                /*name*/null,
2275
                                /*paramsType*/null,
2276
                                /*returnType*/null,
2277
                                /*owner*/null,
2278
                                (bnd == null) ? UNKNOWN_MODIFIERS : bnd.getModifiers());
6✔
2279
        }
2280

2281
        /**
2282
         * Returns a Famix Method associated with the IMethodBinding. The Entity is created if it does not exist.
2283
         * The Entity is created if it does not exist.
2284
         * @param name -- the name of the Famix Method (MUST NOT be null, but this is not checked)
2285
         * @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)
2286
         * @param owner -- type defining the method (should not be null, but it will work if it is)
2287
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
2288
         */
2289
        public Method ensureFamixMethod(IMethodBinding bnd, String name, Collection<String> paramTypes, TType ret, TWithMethods owner, int modifiers) {
2290
                Method fmx;
2291
                String signature;
2292
                boolean delayedRetTyp;
2293

2294
                // --------------- to avoid useless computations if we can
2295
                fmx = (Method)getEntityByKey(bnd);
5✔
2296
                if (fmx != null) {
2✔
2297
                        return fmx;
2✔
2298
                }
2299

2300
                // --------------- name
2301
                if (name == null) {
2✔
2302
                        if (bnd == null) {
2✔
2303
                                return null;
2✔
2304
                        }
2305
                        else {
2306
                                name = bnd.getName();
3✔
2307
                        }
2308
                }
2309

2310
                // --------------- signature
2311
                signature = name + "(";
3✔
2312
                 if (bnd != null) {
2✔
2313
                    signature += signatureParamsFromBinding(bnd);
7✔
2314
                }
2315
        else if (paramTypes != null) {
2!
2316
                        signature += signatureParamsFromStringCollection(paramTypes);
7✔
2317
                }
2318
                else {
2319
                        signature += "???";
×
2320
                }
2321
                signature += ")";
3✔
2322

2323
                // --------------- return type
2324
                delayedRetTyp = false;
2✔
2325
                ITypeBinding retTypBnd = null;
2✔
2326
                if (ret == null) {
2!
2327
                        if (bnd != null) {
2✔
2328
                // must create the return type
2329
                // but for method like "<T> T mtd()" where T belongs to mtd and mtd returns T,
2330
                // we need T to create the method and the method to create T ...
2331
                // so we need to test the situation and deal with it
2332
                retTypBnd = bnd.getReturnType();
3✔
2333
                if ( (retTypBnd != null) && retTypBnd.isTypeVariable() && (retTypBnd.getDeclaringMethod() == bnd) ) {
9✔
2334
                    delayedRetTyp = true;
3✔
2335
                }
2336
                else {
2337
                        ret = this.referredType(retTypBnd, fmx);
5✔
2338
                }
2339
                        }
2340
                }
2341

2342
                // --------------- owner
2343
                if (owner == null) {
2✔
2344
                        if (bnd == null) {
2✔
2345
                                owner = ensureFamixClassStubOwner();
4✔
2346
                        }
2347
                        else {
2348
                                ITypeBinding classBnd = bnd.getDeclaringClass().getErasure();
4✔
2349
                                if (classBnd != null) {
2!
2350
                                        owner = ensureFamixType(classBnd);
5✔
2351
                                }
2352
                                else {
2353
                                        owner = ensureFamixClassStubOwner();
×
2354
                                }
2355
                        }
2356
                }
2357

2358
                // --------------- recover from name ?
2359
                for (Method candidate : this.getEntityByName(Method.class, name)) {
13✔
2360
                        if (matchAndMapMethod(bnd, signature, ret, (TNamedEntity) owner, candidate)) {
9✔
2361
                                fmx = candidate;
2✔
2362
                                break;
1✔
2363
                        }
2364
                }
1✔
2365

2366
                if (fmx == null) {
2✔
2367
                        if(bnd != null && bnd.isGenericMethod()) {
5✔
2368
                                fmx = ensureFamixEntity(ParametricMethod.class, bnd, name);
7✔
2369
                                for(ITypeBinding param: bnd.getTypeParameters()) {
18✔
2370
                                        TypeParameter fmxParam = this.ensureFamixTypeParameter(param, null, fmx);
6✔
2371
                                        fmxParam.setGenericEntity((ParametricMethod)fmx);
4✔
2372
                                }
2373
                        // Parameterized method binding = when the method is the target of an invocation.
2374
                        } else if (bnd != null && bnd.isParameterizedMethod()) {
5!
2375
                                fmx = this.ensureFamixMethod(bnd.getMethodDeclaration());
×
2376
                        } else {
2377
                if (bnd != null && bnd.isConstructor()) {
5✔
2378
                    fmx = ensureFamixEntity(Initializer.class, bnd, name);
8✔
2379
                } else {
2380
                    fmx = ensureFamixEntity(Method.class, bnd, name);
7✔
2381
                }
2382
            }
2383

2384
                        fmx.setSignature(signature);
3✔
2385
                        ITypeBinding returnTypeBnd = (bnd == null) ? null : bnd.getReturnType();
7✔
2386
                        ensureFamixEntityTyping(returnTypeBnd, fmx, ret);
6✔
2387
                        fmx.setParentType(owner);
3✔
2388
                }
2389

2390
                if (fmx != null) {
2!
2391
                        setMethodModifiers(fmx, modifiers);
4✔
2392
                }
2393

2394
        //If it has the #default keywork, we mark it as default implementation
2395
        if (Modifier.isDefault(modifiers)) {
3✔
2396
            fmx.setKind(DEFAULT_IMPLEMENTATION_KIND_MARKER);
3✔
2397
        }
2398

2399
        if (delayedRetTyp) {
2✔
2400
                        int retTypModifiers = retTypBnd.getModifiers();
3✔
2401
                        ITypeBinding returnTypeBnd = bnd.getReturnType();
3✔
2402
                        ensureFamixEntityTyping(returnTypeBnd, fmx, this.ensureFamixType(retTypBnd, /*name*/null, /*owner*/fmx, /*ctxt*/(ContainerEntity) owner, retTypModifiers));
13✔
2403
                }
2404

2405
                return fmx;
2✔
2406
        }
2407

2408

2409
        /**
2410
         * Creates or recovers the initializer method containing the attribute initializations of a type.
2411
         * @param owner Type containing the initializer
2412
         * @param isStatic Modifier of the initializer. A type can have 2 initializers for attribute initialization: 1 static and 1 not.
2413
         * @param isInitializationBlock True if the entity is an initialization block. False for the artificial method containing all field initializations.
2414
         * @return the FamixInitializer
2415
         */
2416
        public Initializer ensureFamixInitializer(TWithMethods owner, Boolean isStatic, Boolean isInitializationBlock) {
2417
                Initializer fmx = null;
2✔
2418

2419
                if (owner != null) {
2!
2420
                        Optional<TMethod> existingInitializer = owner.getMethods().stream()
6✔
2421
                                        .filter(meth ->
1✔
2422
                                                        ((Method) meth).getIsInitializer() &&
8✔
2423
                                                        ((Method) meth).getIsConstructor().equals(false) &&
7✔
2424
                                                        ((Method) meth).getIsClassSide().equals(isStatic) &&
6✔
2425
                                                        ((Initializer) meth).getIsInitializationBlock().equals(isInitializationBlock))
7✔
2426
                                        .findFirst();
2✔
2427
                        if (existingInitializer.isPresent()) {
3✔
2428
                                fmx = (Initializer) existingInitializer.get();
4✔
2429
                        }
2430
                }
2431

2432
                if (fmx == null) {
2✔
2433
                        fmx = new Initializer();
4✔
2434
                        fmx.setName(INIT_BLOCK_NAME);
3✔
2435
                        fmx.setSignature(INIT_BLOCK_NAME + "()" );
3✔
2436
                        fmx.setVisibility(MODIFIER_PRIVATE);
3✔
2437
                        fmx.setParentType(owner);
3✔
2438
                        fmx.setIsClassSide(isStatic);
3✔
2439
                        fmx.setIsInitializationBlock(isInitializationBlock);
3✔
2440
                }
2441

2442
                return fmx;
2✔
2443
        }
2444

2445

2446
        public Initializer ensureImplicitConstructor(TWithMethods owner, String name, Collection<String> parameterTypesNames) {
2447
                Initializer fmx = null;
2✔
2448

2449
                if (owner != null) {
2!
2450
                        Optional<TMethod> existingInitializer = owner.getMethods().stream()
5✔
2451
                                        .filter(meth ->
1✔
2452
                                                        ((Method) meth).getIsInitializer() &&
8✔
2453
                                                                        ((Method) meth).getIsConstructor()
4✔
2454
                                                                        && meth.getParameters().size() == parameterTypesNames.size()
6✔
2455
                                                                        && (parameterTypesNames.stream().allMatch(parameterName -> meth.getSignature().contains(parameterName))) )
13!
2456
                                        .findFirst();
2✔
2457
                        if (existingInitializer.isPresent()) {
3✔
2458
                                fmx = (Initializer) existingInitializer.get();
4✔
2459
                        }
2460
                }
2461

2462
                if (fmx == null) {
2✔
2463
                        fmx = ensureFamixEntity(Initializer.class, null, name);
7✔
2464
                        fmx.setParentType(owner);
3✔
2465
                        fmx.setSignature(name + "()");
4✔
2466

2467
                }
2468

2469
                return fmx;
2✔
2470
        }
2471

2472
        /**
2473
         * Creates or recovers a stub Famix Method
2474
         * @param name of the method
2475
         * @return the Famix Method
2476
         */
2477
        public Method ensureFamixStubMethod(String name) {
2478
                return ensureFamixMethod(null, name, /*paramType*/null, /*returnType*/null, ensureFamixClassStubOwner(), /*modifiers*/0);
×
2479
        }
2480

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

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

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

2498
        public void setInterfaceModifiers(Interface fmx, int mod) {
2499
                setCommonModifiers(fmx, mod);
4✔
2500
        }
1✔
2501

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

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

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

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

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

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

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

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

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

2589
        return fmx;
2✔
2590
        }
2591

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

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

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

2611
                // --------------- to avoid useless computations if we can
2612
                try {
2613
                        fmx = (Parameter)getEntityByKey(bnd);
5✔
2614
                }catch(Throwable e) {
×
2615
                        e.printStackTrace();
×
2616
                }
1✔
2617
                if (fmx != null) {
2✔
2618
                        return fmx;
2✔
2619
                }
2620

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

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

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

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

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

2659
                return fmx;
2✔
2660
        }
2661

2662
    /**
2663
         * Returns a Famix LocalVariable associated with the IVariableBinding.
2664
         * The Entity is created if it does not exist.<br>
2665
         * @param name -- the name of the FAMIX LocalVariable
2666
         * @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
2667
         */
2668
        public LocalVariable ensureFamixLocalVariable(IVariableBinding bnd, String name, TWithLocalVariables owner) {
2669
                LocalVariable fmx;
2670

2671
                // --------------- to avoid useless computations if we can
2672
                fmx = (LocalVariable)getEntityByKey(bnd);
5✔
2673
                if (fmx != null) {
2!
2674
                        return fmx;
×
2675
                }
2676

2677
                // --------------- name
2678
                if (name == null) {
2!
2679
                        if (bnd == null) {
×
2680
                                return null;
×
2681
                        }
2682
                        else {
2683
                                name = bnd.getName();
×
2684
                        }
2685
                }
2686

2687
                // --------------- owner
2688
                if (owner == null) {
2!
2689
                        if (bnd == null) {
×
2690
                                return null;  // what would be the interest of a local variable for which we ignore the declaring method?
×
2691
                        }
2692
                        else {
2693
                                owner = ensureFamixMethod(bnd.getDeclaringMethod());
×
2694
                        }
2695
                }
2696

2697
                // --------------- recover from name ?
2698
                for (LocalVariable candidate : getEntityByName(LocalVariable.class, name) ) {
13✔
2699
                        if ( matchAndMapVariable(bnd, name, (TNamedEntity) owner, candidate) ) {
8!
2700
                                fmx = candidate;
×
2701
                                break;
×
2702
                        }
2703
                }
1✔
2704

2705
                if (fmx == null) {
2!
2706
                        fmx = ensureFamixEntity(LocalVariable.class, bnd, name);
7✔
2707
                        fmx.setParentBehaviouralEntity(owner);
3✔
2708
                }
2709

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

2713
        return fmx;
2✔
2714
        }
2715

2716
    /**
2717
         * Returns a FAMIX ImplicitVariable with the given <b>name</b> ("self" or "super") and corresponding to the <b>type</b>.
2718
         * If this ImplicitVariable does not exist yet, it is created
2719
         * @param name -- the name of the FAMIX ImplicitVariable (should be Dictionary.SELF_NAME or Dictionary.SUPER_NAME)
2720
         * @param type -- the Famix Type for this ImplicitVariable (should not be null)
2721
         * @param tMethod -- the ContainerEntity where the implicit variable appears (should be a method inside <b>type</b>)
2722
         * @return the FAMIX ImplicitVariable or null in case of a FAMIX error
2723
         */
2724
        public ImplicitVariable ensureFamixImplicitVariable(IBinding key, String name, TType type, TMethod tMethod) {
2725
                ImplicitVariable fmx;
2726
                fmx = ensureFamixEntity(ImplicitVariable.class, key, name);
7✔
2727
                fmx.setParentBehaviouralEntity(tMethod);
3✔
2728
                return fmx;
2✔
2729
        }
2730

2731
        public ImplicitVariable ensureFamixImplicitVariable(String name, TType tType, TMethod tMethod) {
2732
                IBinding bnd = ImplicitVarBinding.getInstance(tMethod, name);
4✔
2733
                return ensureFamixImplicitVariable(bnd, name, tType, tMethod);
7✔
2734
        }
2735

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

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

2753
                return cmt;
2✔
2754
        }
2755

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

2766
                if ( (jCmt != null) && (owner != null) ) {
4!
2767
                        cmt = new Comment();
4✔
2768
                        cmt.setContent(content );
3✔
2769
                        famixRepoAdd(cmt);
3✔
2770
                        cmt.setCommentedEntity(owner);
3✔
2771
                }
2772

2773
                return cmt;
2✔
2774
        }
2775

2776
        /**
2777
         * Adds location information to a Famix Entity.
2778
         * 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.
2779
         * This method also creates some basic links between the entity and others (e.g. declaring container, return type, ...)
2780
         * @param fmx -- Famix Entity to add the anchor to
2781
         * @param node -- JDT ASTNode, where the information is extracted
2782
         * @return the Famix SourceAnchor added to fmx. May be null in case of incorrect parameter ('fmx' or 'ast' == null)
2783
         */
2784
        public SourceAnchor addSourceAnchor(TSourceEntity fmx, ASTNode node) {
2785
                IndexedFileAnchor fa;
2786

2787
                fa = createIndexedFileAnchor(node);
4✔
2788
                if ((fmx != null) && (fa != null)) {
4!
2789
                        fmx.setSourceAnchor(fa);
3✔
2790
                        famixRepoAdd(fa);
3✔
2791
                }
2792

2793
                return fa;
2✔
2794
        }
2795

2796
        /**
2797
         * Special case of  {@linkplain #addSourceAnchor(TSourceEntity, ASTNode)} to add location information to a Famix Method.
2798
         */
2799
        public SourceAnchor addSourceAnchor(Method fmx, MethodDeclaration node) {
2800
                IndexedFileAnchor fa;
2801

2802
                fa = createIndexedFileAnchor(node);
4✔
2803
                if ((fmx != null) && (fa != null)) {
4!
2804

2805
                        // may change the positions
2806
                        List<ASTNode> methodDeclarationModifiers = new ArrayList<>();
4✔
2807
                        methodDeclarationModifiers.addAll(node.modifiers());
5✔
2808
                        if (node.getName() != null) {
3!
2809
                                methodDeclarationModifiers.add(node.getName());
5✔
2810
                        }
2811
                        if (node.getReturnType2() != null) {
3✔
2812
                                methodDeclarationModifiers.add(node.getReturnType2());
5✔
2813
                        }
2814
                        int beg = (methodDeclarationModifiers.stream().mapToInt(el -> el.getStartPosition()).min().getAsInt()) + 1;
12✔
2815
                        int end = node.getStartPosition() + node.getLength();
6✔
2816

2817
                        fa.setStartPos(beg);
4✔
2818
                        fa.setEndPos(end);
4✔
2819

2820
                        fmx.setSourceAnchor(fa);
3✔
2821
                        famixRepoAdd(fa);
3✔
2822
                }
2823

2824
                return fa;
2✔
2825
        }
2826

2827
        /**
2828
         * Gets the file name holding <code>node</code> and its start and end positions in the file.
2829
         * Information returned in the form of an IndexedFileAnchor
2830
         */
2831
        protected IndexedFileAnchor createIndexedFileAnchor(ASTNode node) {
2832
                IndexedFileAnchor fa;
2833
                
2834
                if (node == null) {
2!
2835
                        return null;
×
2836
                }
2837

2838
                // position in source file
2839
                int beg = node.getStartPosition() + 1; // Java starts at 0, Moose at 1
5✔
2840
                int end = beg + node.getLength() - 1;
7✔
2841

2842
                // find source Compilation Unit
2843
                // there is a special case for the JDT Comment Nodes
2844
                if (node instanceof org.eclipse.jdt.core.dom.Comment) {
3✔
2845
                        node = ((org.eclipse.jdt.core.dom.Comment) node).getAlternateRoot();
5✔
2846
                } else {
2847
                        node = node.getRoot();
3✔
2848
                }
2849

2850
                fa = new IndexedFileAnchor();
4✔
2851
                fa.setStartPos(beg);
4✔
2852
                fa.setEndPos(end);
4✔
2853

2854
                fa.setFileName((String) node.getProperty(SOURCE_FILENAME_PROPERTY));
6✔
2855

2856
                return fa;
2✔
2857
        }
2858

2859
        /**
2860
         * Creates or recovers the Famix Class for "Object".
2861
         * Because "Object" is the root of the inheritance tree, it needs to be treated differently.
2862
         *
2863
         * @return a Famix class for "Object"
2864
         */
2865
        public Class ensureFamixClassObject() {
2866
        // 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...
2867
        Collection<Class> objects = getEntityByName(Class.class, "Object");
5✔
2868

2869
        for (Class entity : objects) {
10✔
2870
                //We need to cast because the type container is a FamixTWithType but all implementors of this should be named in Java...
2871
                if ("java.lang".equals(((TNamedEntity) entity.getTypeContainer()).getName())) {
7✔
2872
                    return entity;
2✔
2873
                }
2874
        }
1✔
2875

2876
        Class fmx = createFamixEntity(Class.class, "Object");
6✔
2877
        fmx.setTypeContainer(ensureFamixPackageJavaLang(null));
5✔
2878
                return fmx;
2✔
2879
        }
2880

2881
        /***
2882
         * We treat array types as parametrized types Array<T>.
2883
         * This keeps the meta-model simple: a single way to model different concepts.
2884
         * @return
2885
         */
2886
        public ParametricClass ensureParametricArrayClass() {
2887
                
2888
                Collection<ParametricClass> arrayClasses = getEntityByName(ParametricClass.class, "Array");
5✔
2889
                
2890
                for (ParametricClass entity : arrayClasses) {
10✔
2891
            //We need to cast because the type container is a FamixTWithType but all implementors of this should be named in Java...
2892
            if ("java.lang".equals(((TNamedEntity) entity.getTypeContainer()).getName())) {
7!
2893
                return entity;
2✔
2894
            }
NEW
2895
                }
×
2896
                
2897
                //Create the parametric class
2898
                ParametricClass arrayClass = createFamixEntity(ParametricClass.class, "Array"); 
6✔
2899

2900
                //And now add the type parameter
2901
                TypeParameter fmxParam = this.ensureFamixTypeParameter(null, "T", arrayClass);
6✔
2902
                fmxParam.setGenericEntity(arrayClass);
3✔
2903
                fmxParam.setIsStub(true);
4✔
2904
                
2905
                //And the class is in java.lang
2906
                arrayClass.setTypeContainer(ensureFamixPackageJavaLang(null));
5✔
2907
                
2908
                return arrayClass;
2✔
2909
        }
2910

2911
        /**
2912
         * Ensures the Java meta-class: <pre>{@code java.lang.Class<>}</pre>
2913
         */
2914
        public Class ensureFamixMetaClass(ITypeBinding bnd) {
2915
                Package javaLang = ensureFamixPackageJavaLang((bnd == null) ? null : bnd.getPackage());
7!
2916
                ParametricClass fmx = (ParametricClass) this.ensureFamixClass(null, METACLASS_NAME, javaLang, /*isGeneric*/true, Modifier.PUBLIC & Modifier.FINAL);
9✔
2917

2918
                if (fmx != null) {
2!
2919
                        fmx.addTypeParameters(ensureFamixTypeParameter(null, "T", fmx));
7✔
2920
                }
2921

2922
                if ((fmx != null) && (fmx.getSuperInheritances() == null)) {
5!
2923
                        ensureFamixInheritance(ensureFamixClassObject(), fmx, null, null);
×
2924
                }
2925

2926
                return fmx;
2✔
2927
        }
2928

2929
        public Class getFamixMetaClass(ITypeBinding bnd) {
2930
                Package javaLang = ensureFamixPackageJavaLang((bnd == null) ? null : bnd.getPackage());
7!
2931
                return this.ensureFamixClass(null, METACLASS_NAME, javaLang, /*isGeneric*/true, UNKNOWN_MODIFIERS);
8✔
2932
        }
2933

2934
        /**
2935
         * Creates or recovers the Famix Class for all arrays (<pre>{@code <some-type> []}</pre>)
2936
         * In java arrays or objects of special classes (i.e. "I[" for an array of int).
2937
         * JDT does not create a binding for these classes, so we create a stub one here.
2938
         *
2939
         * @return a Famix class
2940
         */
2941
        public Class ensureFamixClassArray() {
2942
                Class fmx = ensureFamixUniqEntity(Class.class, null, ARRAYS_NAME);
×
2943
                if (fmx != null) {
×
2944
                        ensureFamixInheritance(ensureFamixClassObject(), fmx, /*prev*/null, null);
×
2945
                        fmx.setTypeContainer(ensureFamixPackageDefault());
×
2946

2947
                        // may be not needed anymore now that we use modifiers
2948
                        /*fmx.setIsAbstract(Boolean.FALSE);
2949
                        fmx.setIsFinal(Boolean.FALSE);
2950
                        fmx.setIsInterface(Boolean.FALSE); 
2951
                        fmx.setIsPrivate(Boolean.FALSE);
2952
                        fmx.setIsProtected(Boolean.FALSE);*/
2953
                        fmx.setVisibility(MODIFIER_PUBLIC);
×
2954
                }
2955

2956
                return fmx;
×
2957
        }
2958

2959
        public String removeLastPartOfPackageName(String qualifiedName) {
2960
                String ret;
2961
                int last = qualifiedName.lastIndexOf('.');
4✔
2962
                if (last > 0) {
2✔
2963
                        // recursively creating the parent
2964
                        ret = qualifiedName.substring(0, last);
6✔
2965
                }
2966
                else {
2967
                        ret = "";
2✔
2968
                }
2969

2970
                return ret;
2✔
2971
        }
2972

2973
        /** Generates the list of parameters for a method signature
2974
         * @return a string
2975
         */
2976
        protected String signatureParamsFromBinding(IMethodBinding bnd) {
2977
                boolean first = true;
2✔
2978
                String sig = "";
2✔
2979

2980
                for (ITypeBinding parBnd : bnd.getParameterTypes()) {
17✔
2981
                        if (first) {
2✔
2982
                                sig = parBnd.getName();
3✔
2983
                                first = false;
3✔
2984
                        }
2985
                        else {
2986
                                sig += "," + parBnd.getName();
5✔
2987
                        }
2988
                }
2989
                return sig;
2✔
2990
        }
2991

2992
        private String signatureParamsFromStringCollection(Collection<String> paramTypes) {
2993
                boolean first = true;
2✔
2994
                String sig = "";
2✔
2995

2996
                for (String t : paramTypes) {
10✔
2997
                        if (first) {
2✔
2998
                                sig = t;
2✔
2999
                                first = false;
3✔
3000
                        }
3001
                        else {
3002
                                sig += "," + t;
4✔
3003
                        }
3004
                }
1✔
3005
                return sig;
2✔
3006
        }
3007

3008
        public String findTypeName(org.eclipse.jdt.core.dom.Type t) {
NEW
3009
                if (t == null) {
×
NEW
3010
                        return null;
×
3011
                }
3012

NEW
3013
                if (t.isPrimitiveType()) {
×
NEW
3014
                        return t.toString();
×
NEW
3015
                } else if (t.isSimpleType()) {
×
NEW
3016
                        String fullName = ((SimpleType) t).getName().getFullyQualifiedName();
×
NEW
3017
                        int i = fullName.lastIndexOf('.');
×
NEW
3018
                        if (i > 0) {
×
NEW
3019
                                return fullName.substring(i+1);
×
3020
                        }
3021
                        else {
NEW
3022
                                return fullName;
×
3023
                        }
NEW
3024
                } else if (t.isQualifiedType()) {
×
NEW
3025
                        return ((QualifiedType) t).getName().getIdentifier();
×
NEW
3026
                } else if (t.isArrayType()) {
×
NEW
3027
                        return findTypeName(((ArrayType) t).getElementType());
×
NEW
3028
                } else if (t.isParameterizedType()) {
×
NEW
3029
                        return findTypeName(((org.eclipse.jdt.core.dom.ParameterizedType) t).getType());
×
3030
                } else { // it is a WildCardType
NEW
3031
                        if (((org.eclipse.jdt.core.dom.WildcardType) t).isUpperBound()) {
×
NEW
3032
                                return findTypeName(((org.eclipse.jdt.core.dom.WildcardType) t).getBound());
×
3033
                        } else {
NEW
3034
                                return EntityDictionary.OBJECT_NAME;
×
3035
                        }
3036
                }
3037
        }
3038

3039
        /**
3040
         * Ensures the proper creation of a FamixType for JDT typ in the given context.
3041
         * Useful for parameterizedTypes, or classInstance.
3042
         *
3043
         * @param isClass we are sure that the type is actually a class
3044
         * @return a famix type or null
3045
         */
3046
        public <T extends TWithTypes & TNamedEntity> TType referredType(org.eclipse.jdt.core.dom.Type typ, T ctxt, boolean isClass) {
3047
                return this.referredType(typ, ctxt, isClass, /*isExcep*/false);
7✔
3048
        }
3049
        
3050
        /**
3051
         * Ensures the proper creation of a FamixType for JDT type in the given context.
3052
         * Useful for parameterizedTypes, or classInstance.
3053
         *
3054
         * @param isClass we are sure that the type is actually a class
3055
         * @return a famix type or null
3056
         */
3057
        public <T extends TWithTypes & TNamedEntity> TType referredType(org.eclipse.jdt.core.dom.Type typ, T ctxt, boolean isClass, boolean isException) {
3058
                if (typ == null) {
2✔
3059
                        return null;
2✔
3060
                } else if (typ.resolveBinding() != null) {
3!
3061
                        return this.referredType(typ.resolveBinding(), ctxt);
7✔
3062
                }
3063
                // from here, we assume the owner is the context
NEW
3064
                else if (isClass && !isException) {
×
NEW
3065
                        return this.ensureFamixClass(null, findTypeName(typ), /*owner*/ctxt, /*isGeneric*/false,
×
3066
                                        EntityDictionary.UNKNOWN_MODIFIERS);
NEW
3067
                } else if (isException) {
×
3068
                        // return ensure FamixException
NEW
3069
                        return this.ensureFamixException(null, findTypeName(typ), (ContainerEntity) /*owner*/ctxt, /*isGeneric*/false,
×
3070
                                        EntityDictionary.UNKNOWN_MODIFIERS);
3071
                } else {
NEW
3072
                        while (typ.isArrayType()) {
×
NEW
3073
                                typ = ((ArrayType) typ).getElementType();
×
3074
                        }
3075

NEW
3076
                        if (typ.isPrimitiveType()) {
×
NEW
3077
                                return this.ensureFamixPrimitiveType(null, findTypeName(typ));
×
3078
                        } else {
NEW
3079
                                return this.ensureFamixType(null, findTypeName(typ), /*owner*/ctxt, /*container*/ctxt,
×
3080
                                                EntityDictionary.UNKNOWN_MODIFIERS);
3081
                        }
3082
                }
3083
        }
3084
        
3085
        public TType referredType(ITypeBinding bnd, TNamedEntity ctxt) {
3086
                TType fmxTyp = null;
2✔
3087

3088
                if (bnd == null) {
2✔
3089
                        return null;
2✔
3090
                }
3091

3092
                String name;
3093
                if (bnd.isArray()) {
3✔
3094
                        // We treat array types as parametrized types Array<T>.
3095
                        // This keeps the meta-model simple: a single way to model different concepts.
3096
                        return this.ensureParametricArrayClass();
3✔
3097
                }
3098
                name = bnd.getName();
3✔
3099

3100
                if ( bnd.isParameterizedType() ) {
3✔
3101
                        
3102
                        // remove type parameters from the name even for parameterized interfaces
3103
                        int i = name.indexOf('<');
4✔
3104
                        if (i > 0) {
2!
3105
                                name = name.substring(0, i);
5✔
3106
                        }
3107

3108
                        ITypeBinding parameterizableBnd = bnd.getErasure();
3✔
3109
                        int modifiers = (parameterizableBnd != null) ? parameterizableBnd.getModifiers() : EntityDictionary.UNKNOWN_MODIFIERS;
6!
3110
                        
3111
                        if(parameterizableBnd != null && parameterizableBnd.isInterface()) {
5!
3112
                                fmxTyp = (ParametricInterface) this.ensureFamixInterface(parameterizableBnd, name, /*owner*/null, /*isGeneric*/true, modifiers);
10✔
3113
                        } else {
3114
                                fmxTyp = (ParametricClass) this.ensureFamixClass(parameterizableBnd, name, /*owner*/null, /*isGeneric*/true, modifiers);
9✔
3115
                        }
3116
                } else if ( (name != null) && name.equals("var") ) {
7!
3117
                        fmxTyp = this.ensureFamixUniqEntity(org.moosetechnology.model.famix.famixjavaentities.Type.class, /*binding*/null, EntityDictionary.IMPLICIT_VAR_TYPE_NAME);        
8✔
3118
                } else {
3119
                        fmxTyp = this.ensureFamixType(bnd, name, /*owner*/null, (TWithTypes) ctxt, bnd.getModifiers());
10✔
3120
                }
3121

3122
                return fmxTyp;
2✔
3123
        }
3124

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

© 2026 Coveralls, Inc