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

moosetechnology / VerveineJ / 15191557690

22 May 2025 04:10PM UTC coverage: 50.616% (+0.6%) from 49.972%
15191557690

push

github

web-flow
Merge pull request #127 from moosetechnology/new-generics-implementation

New generics implementation

1884 of 3956 branches covered (47.62%)

Branch coverage included in aggregate %.

513 of 1119 new or added lines in 53 files covered. (45.84%)

53 existing lines in 10 files now uncovered.

4277 of 8216 relevant lines covered (52.06%)

2.08 hits per line

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

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

3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.Collection;
6
import java.util.ConcurrentModificationException;
7
import java.util.Hashtable;
8
import java.util.Iterator;
9
import java.util.LinkedList;
10
import java.util.List;
11
import java.util.Map;
12
import java.util.stream.Collectors;
13

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

59
import ch.akuhn.fame.Repository;
60
import fr.inria.verveine.extractor.java.utils.ImplicitVarBinding;
61
import fr.inria.verveine.extractor.java.utils.Util;
62

63
/**
64
 * A dictionary of FamixJava entities to help create them and find them back
65
 * Entities are mapped to keys which are the "binding" provided by the JDT pars
66
 * 
67
 * @author anquetil
68
 */
69
public class EntityDictionary {
70

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

77
        public static final String DEFAULT_PCKG_NAME = "<Default Package>";
78
        public static final String STUB_METHOD_CONTAINER_NAME = "<StubMethodContainer>";
79
        public static final String SELF_NAME = "self";
80
        public static final String SUPER_NAME = "super";
81
        
82
        public static final String OBJECT_NAME = "Object";
83
        public static final String METACLASS_NAME = "Class";
84
        public static final String OBJECT_PACKAGE_NAME = "java.lang";
85
        public static final String ARRAYS_NAME = "default[]";
86
        public static final String INIT_BLOCK_NAME = "<Initializer>";
87
        public static final String ANONYMOUS_NAME_PREFIX = "_Anonymous";
88

89
        public static final int UNKNOWN_MODIFIERS = 0;
90
        public static final String MODIFIER_ABSTRACT = "abstract";
91
        public static final String MODIFIER_PUBLIC   = "public";
92
        public static final String MODIFIER_PRIVATE  = "private";
93
        public static final String MODIFIER_PROTECTED= "protected";
94
        public static final String MODIFIER_PACKAGE = "package";
95
        public static final String MODIFIER_FINAL    = "final";
96
        public static final String MODIFIER_STATIC    = "static";
97
        public static final String MODIFIER_TRANSIENT = "transient";
98
        public static final String MODIFIER_VOLATILE = "volatile";
99
        public static final String MODIFIER_SYNCHRONIZED = "synchronized";
100

101
        /**
102
         * An MSE marker for methods
103
         */
104
        public static final String CONSTRUCTOR_KIND_MARKER = "constructor";
105

106
        /**
107
         * The FAMIX repository where all FAMIX entities are created and stored
108
         */
109
        protected Repository famixRepo;
110

111
        /**
112
         * A dictionary to map a key (provided by the user) to FAMIX Entity
113
         */
114
        protected Map<IBinding,TNamedEntity> keyToEntity;
115
        /**
116
         * A reverse dictionary (see {@link #keyToEntity}) to find the key of an entity.
117
         */
118
        protected Map<TNamedEntity,IBinding> entityToKey;
119

120
        /**
121
         * Another dictionary to map a name to FAMIX Entities with this name
122
         */
123
        protected Map<String,Collection<TNamedEntity>> nameToEntity;
124

125
        /**
126
         * Yet another dictionary for implicit variables ('self' and 'super')
127
         * Because they are implicit, they may not have a binding provided by the parser,
128
         * or may have the same binding than their associated type so they can't be kept easily in {@link #keyToEntity}
129
         */
130
        @Deprecated
131
        protected Map<Type,ImplicitVars> typeToImpVar;
132

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

150

151
        /** Constructor taking a FAMIX repository
152
         * @param famixRepo
153
         */
154
        public EntityDictionary(Repository famixRepo) {
2✔
155
                        this.famixRepo = famixRepo;
3✔
156
                        
157
                        this.keyToEntity = new Hashtable<IBinding,TNamedEntity>();
5✔
158
                        this.entityToKey = new Hashtable<TNamedEntity,IBinding>();
5✔
159
                        this.nameToEntity = new Hashtable<String,Collection<TNamedEntity>>();
5✔
160
                        this.typeToImpVar = new Hashtable<Type,ImplicitVars>();
5✔
161
                        
162
                        if (! this.famixRepo.isEmpty()) {
4✔
163
                                recoverExistingRepository();
2✔
164
                        }
165
                }
1✔
166

167
          public void mapKey(IBinding bnd, NamedEntity fmx) {
168
                mapEntityToKey(bnd, fmx);
×
169
        }
×
170

171
        /**
172
         * Resets the dictionnary in a proper state after loading entities from an existing MSE file:
173
         * <UL>
174
         * <li>map all named entities to their names in <b>mapName</b></li>
175
         * <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>
176
         * </ul>
177
         */
178
        protected void recoverExistingRepository() {
179
                for (NamedEntity ent : famixRepo.all(NamedEntity.class)) {
13✔
180
                        mapEntityToName( ent.getName(), ent);
5✔
181
                        // for the Exception to be raised, the return value must be tested
182
                        try { if (((TCanBeStub) ent).getIsStub()) {} }
5✔
183
                        catch (NullPointerException e) { ((TCanBeStub)ent).setIsStub(Boolean.FALSE); }
6✔
184
                }
1✔
185

186
                for (Access acc : famixRepo.all(Access.class)) {
13✔
187
                        // for the Exception to be raised, the return value must be tested
188
                        try { if (acc.getIsWrite()) {}        }
4✔
189
                        catch (NullPointerException e) { acc.setIsWrite(Boolean.FALSE); }
5✔
190
                }
1✔
191
        }
1✔
192

193
        protected void mapEntityToName(String name, TNamedEntity ent) {
194
                
195
                Collection<TNamedEntity> l_ent = nameToEntity.get(name);
6✔
196
                if (l_ent == null) {
2✔
197
                        l_ent = new LinkedList<TNamedEntity>();
4✔
198
                }
199
                l_ent.add(ent);
4✔
200
                nameToEntity.put(name, l_ent);
6✔
201
        }
1✔
202

203
        public void removeEntity( NamedEntity ent) {
204
                IBinding key;
205
                key = entityToKey.get(ent);
6✔
206
                entityToKey.remove(ent);
5✔
207
                keyToEntity.remove(key);
5✔
208

209
                Collection<TNamedEntity> l_ent = nameToEntity.get(ent.getName());
7✔
210
                l_ent.remove(ent);
4✔
211

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

243
                return ret;
2✔
244
        }
245

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

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

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

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

299
                        mapEntityToName(name, fmx);
4✔
300
                        
301
                        // put new entity in Famix repository
302
                        famixRepoAdd((Entity) fmx);
4✔
303
                }
304

305
                return fmx;
2✔
306
        }
307
        
308
        /**
309
         * Returns a Famix Entity of the type <b>fmxjava.lang.Class</b> and maps it to its binding <b>bnd</b> (if not null).
310
         * The Entity is created if it did not exist.
311
         * @param fmxClass -- the Famix class of the instance to create
312
         * @param bnd -- the binding to map to the new instance
313
         * @param name -- the name of the new instance (used if <pre>{@code bnd == null}</pre>)
314
         * @return the Famix Entity or null if <b>bnd</b> was null or in case of a Famix error
315
         */
316
        @SuppressWarnings("unchecked")
317
        protected <T extends TNamedEntity & TSourceEntity> T ensureFamixEntity(java.lang.Class<T> fmxClass, IBinding bnd, String name) {
318
                T fmx = null;
2✔
319
                
320
                /* 
321
                 * Unfortunately different entities with the same name and same type may exist
322
                 * e.g. 2 parameters of 2 different methods but having the same name
323
                 * so we cannot recover just from the name
324
                 */
325
                
326
                if (bnd != null) {
2✔
327
                        fmx = (T) getEntityByKey(bnd);
4✔
328
                        if (fmx != null) {
2✔
329
                                return fmx;
2✔
330
                        }
331
                }
332
                // else
333
                fmx = createFamixEntity(fmxClass, name);
5✔
334
                if ( (bnd != null) && (fmx != null) ) {
4!
335
                        keyToEntity.put(bnd, fmx);
6✔
336
                        entityToKey.put(fmx, bnd);
6✔
337
                }
338
                
339
                return fmx;
2✔
340
        }
341

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

351

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

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

401
        public AnnotationInstanceAttribute createFamixAnnotationInstanceAttribute(AnnotationTypeAttribute att, String value) {
402
                AnnotationInstanceAttribute fmx = null;
2✔
403
                if ( (att != null) && (value != null) ) {
4!
404
                        fmx = new AnnotationInstanceAttribute();
4✔
405
                        fmx.setAnnotationTypeAttribute(att);
3✔
406
                        fmx.setValue(value);
3✔
407
                        this.famixRepo.add(fmx);
4✔
408
                }
409
                return fmx;
2✔
410
        }
411

412
        public AnnotationInstance addFamixAnnotationInstance(TWithAnnotationInstances fmx, AnnotationType annType, Collection<AnnotationInstanceAttribute> annAtts) {
413
                AnnotationInstance inst = null;
2✔
414
                if ( (fmx != null) && (annType != null) ) {
4!
415
                        inst = new AnnotationInstance();
4✔
416
                        inst.setAnnotatedEntity(fmx);
3✔
417
                        inst.setAnnotationType(annType);
3✔
418
                        inst.addAttributes(annAtts);
3✔
419
                        this.famixRepo.add(inst);
4✔
420
                }
421
                return inst;
2✔
422
        }
423

424
        ///// ensure Famix Relationships /////
425

426
        /**
427
         * Returns a Famix Inheritance relationship between two Famix Classes creating it if needed
428
         * @param sup -- the super class
429
         * @param sub -- the sub class
430
         * @param prev -- previous inheritance relationship in the same context
431
         * @return the Inheritance relationship
432
         */
433
        public Inheritance ensureFamixInheritance(TWithInheritances sup, TWithInheritances sub, TAssociation prev, ITypeBinding supBnd) {
434
                if ( (sup == null) || (sub == null) ) {
4!
435
                        return null;
×
436
                }
437

438
                // Does the inheritance already exist?
439
                for (TInheritance i : (sup).getSubInheritances()) {                        
11✔
440
                        if (i.getSubclass() == sub) {
4✔
441
                                return (Inheritance) i;
3✔
442
                        }
443
                }
1✔
444

445
                Inheritance inh;
446
                if (supBnd != null && supBnd.isParameterizedType()) { // Needs checks and tests.
5✔
447
                        inh = (ParametricInheritance)buildFamixParametricAssociation(new ParametricInheritance(), supBnd.getErasure().getTypeParameters(), supBnd.getTypeArguments());
13✔
448
                } else {
449
                        inh = new Inheritance();
4✔
450
                }
451

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

473
                while (concreteIterator.hasNext() && genericIterator.hasNext()) {
6!
474
                        TTypeArgument typeArgument = (TTypeArgument)ensureFamixType(concreteIterator.next());
7✔
475
                        TypeParameter typeParameter = (TypeParameter)ensureFamixType(genericIterator.next());
7✔
476

477
                        Concretization concretization = ensureFamixConcretization(typeArgument, typeParameter);
5✔
478
                        association.addConcretization(concretization);
3✔
479
                }
1✔
480

481
                return association;
2✔
482
        }
483

484
        /**
485
         * Returns a Famix Concretization relationship between a Concrete Type and a ParameterType
486
         * @param typeArgument -- the concrete type
487
         * @param typeParameter -- the generic type parameter
488
         * @return the Concretization relationship
489
         */
490
        public Concretization ensureFamixConcretization(TTypeArgument typeArgument, TypeParameter typeParameter ) {
491
                if ( (typeArgument == null) || (typeParameter == null) ) {
4!
UNCOV
492
                        return null;
×
493
                }
494

495
                Concretization concretization = new Concretization();
4✔
496
                concretization.setTypeArgument(typeArgument);
3✔
497
                concretization.setTypeParameter(typeParameter);
3✔
498

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

515
                for (TImplementation imp : myInterface.getImplementations()) {
11✔
516
                        if (imp.getImplementingClass() == implementingClass) {
4✔
517
                                return (Implementation) imp;
3✔
518
                        }
519
                }
1✔
520
                
521
                Implementation implementation;
522
                if (supBnd != null && supBnd.isParameterizedType()) { // Needs checks and tests.
5!
523
                        implementation = (ParametricImplementation)buildFamixParametricAssociation(new ParametricImplementation(), supBnd.getErasure().getTypeParameters(), supBnd.getTypeArguments());
13✔
524
                } else {
525
                        implementation = new Implementation();
4✔
526
                }
527

528
                implementation.setImplementingClass(implementingClass);
3✔
529
                implementation.setMyInterface(myInterface);
3✔
530
                chainPrevNext(prev, implementation);
4✔
531
                famixRepoAdd(implementation);
3✔
532
                return implementation;
2✔
533
        }
534

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

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

567
                if (prev == null) {
2✔
568
                        for (TReference ref : src.getOutgoingReferences()) {
7!
569
                                if (ref.getReferredEntity() == tgt) {
×
570
                                        return (Reference) ref;
×
571
                                }
572
                        }
×
573
                }
574

575
                Reference ref;
576
                if (referredTypeBnd != null && referredTypeBnd.isParameterizedType()) { // Needs checks and tests.
5✔
577
                        ref = (ParametricReference)buildFamixParametricAssociation(new ParametricReference(), referredTypeBnd.getErasure().getTypeParameters(), referredTypeBnd.getTypeArguments());
13✔
578
                } else {
579
                        ref = new Reference();
4✔
580
                }
581

582
                ref.setReferredEntity(tgt);
3✔
583
                ref.setReferencer(src);
3✔
584
                chainPrevNext(prev,ref);
4✔
585
                famixRepoAdd(ref);
3✔
586

587
                return ref;
2✔
588
        }
589

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

612
                invocation.setReceiver(receiver);
3✔
613
                invocation.setSender(tMethod);
3✔
614
                invocation.setSignature((signature == null) ? invoked.getSignature() : signature);
5!
615
                invocation.addCandidates(invoked);
3✔
616
                chainPrevNext(prev,invocation);
4✔
617
                famixRepoAdd(invocation);
3✔
618
                
619
                return invocation;
2✔
620
        }
621

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

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

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

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

706

707
        /**
708
         * Returns a Famix EntityTyping between a typed entity and a type.
709
         * @param typedEntity -- the typed entity
710
         * @param declaredType -- the declared type
711
         * @return the FamixEntityTyping
712
         */
713
        public EntityTyping ensureFamixEntityTyping(ITypeBinding declaredTypeBnd, TTypedEntity typedEntity, TType declaredType) {
714
                if ( (typedEntity == null) || (declaredType == null) ) {
4!
715
                        return null;
2✔
716
                }
717
                EntityTyping typing;
718
                if (declaredTypeBnd != null && declaredTypeBnd.isParameterizedType()) {
5✔
719
                        typing = (ParametricEntityTyping)buildFamixParametricAssociation(new ParametricEntityTyping(), declaredTypeBnd.getErasure().getTypeParameters(), declaredTypeBnd.getTypeArguments());
13✔
720
                } else {
721
                        typing = new EntityTyping();
4✔
722
                }
723
                typing.setTypedEntity(typedEntity);
3✔
724
                typing.setDeclaredType(declaredType);
3✔
725
                famixRepoAdd(typing);
3✔
726
                
727
                return typing;
2✔
728
        }
729

730

731
        ///// Special Case: ImplicitVariables /////
732

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

766
                return ret;
×
767
        }
768

769
        ///// Special Case: "Uniq" Entities /////
770

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

808
                return fmx;
2✔
809
        }
810

811
        /**
812
         * Creates or recovers the Famix Class that will own all stub methods (for which the real owner is unknown)
813
         *
814
         * @return a Famix class
815
         */
816
        public Class ensureFamixClassStubOwner() {
817
                Class fmx =  ensureFamixUniqEntity(Class.class, null, STUB_METHOD_CONTAINER_NAME);
7✔
818
                if (fmx != null) {
2!
819
                        fmx.setTypeContainer( ensureFamixPackageDefault());
4✔
820
                }
821
                ensureFamixInheritance(ensureFamixClassObject(null), fmx, /*prev*/null, null);
9✔
822

823
                return fmx;
2✔
824
        }
825

826
        public Type searchTypeInContext(String name, TWithTypes ctxt) {
827
                if (ctxt == null) {
×
828
                        return null;
×
829
                }
830
                
831
                for (TType candidate : ctxt.getTypes()) {
×
832
                        if (((TNamedEntity)candidate).getName().equals(name) ) {
×
833
                                return (Type) candidate;
×
834
                        }
835
                }
×
836
                
837
                return searchTypeInContext(name, Util.getOwner((TNamedEntity)ctxt));
×
838
        }
839

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

855
                if ((name == null) && (bnd != null)) {
4!
856
                        name = bnd.getName();
3✔
857
                }
858

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

877
                return fmx;
2✔
878
        }
879

880
        /**
881
         * Creates or recovers a default Famix Package.
882
         * Because this package does not really exist, it has no binding.
883
         *
884
         * @return a Famix Namespace
885
         */
886
        public Package ensureFamixPackageDefault() {
887
                Package fmx = ensureFamixUniqEntity(Package.class, null, DEFAULT_PCKG_NAME);
7✔
888

889
                return fmx;
2✔
890
        }
891

892
        /**
893
         * Creates or recovers a Famix Package for the package of Java class "Object" (i.e. "java.lang").
894
         * Because "Object" is the root of the inheritance tree, it needs to be treated differently.
895
         *
896
         * @param bnd -- a potential binding for the "java.lang" package
897
         * @return a Famix Namespace for "java.lang"
898
         */
899
        public Package ensureFamixPackageJavaLang(IPackageBinding bnd) {
900
                Package fmx = this.ensureFamixPackage(bnd, OBJECT_PACKAGE_NAME);
5✔
901

902
                return fmx;
2✔
903
        }
904

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

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

929
                if (bnd == null) {
2✔
930
                        if (name == null) {
2!
931
                                return null;
2✔
932
                        }
933
                        fmx = searchTypeInContext(name, ctxt); // WildCard Types don't have binding
×
934
                        if (fmx != null) {
×
935
                                return fmx;
×
936
                        }
937

938
                        if ( (owner != null) && (owner instanceof TParametricEntity) ) {
×
NEW
939
                                return this.ensureFamixTypeParameter(null, name, owner);
×
940
                        }
941
                        else {
942
                                fmx = ensureFamixEntity(Type.class, bnd, name);
×
943
                                fmx.setTypeContainer(owner);
×
944
                                return fmx;
×
945
                        }
946
                }
947

948
                // bnd != null
949

950
                fmx = (TType) getEntityByKey(bnd);
5✔
951
                if (fmx != null) {
2✔
952
                        return fmx;
2✔
953
                }
954

955
                if (bnd.isArray()) {
3!
956
                        bnd = bnd.getElementType();
×
957
                }
958

959
                if (bnd.isPrimitive()) {
3✔
960
                        return this.ensureFamixPrimitiveType(bnd, name);
5✔
961
                }
962

963
                if (bnd.isEnum()) {
3!
964
                        return this.ensureFamixEnum(bnd, name, (ContainerEntity) owner);
×
965
                }
966

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

975
                if (bnd.isInterface()) {
3✔
976
                        return this.ensureFamixInterface(bnd, name, owner, /*isGeneric*/bnd.isGenericType() || bnd.isParameterizedType() || bnd.isRawType(), modifiers);
19✔
977
                }
978

979
                if (isThrowable(bnd)) {
4✔
980
                        return this.ensureFamixException(bnd, name, owner, /*isGeneric*/false, modifiers);
8✔
981
                }
982
                if (bnd.isClass()) {
3✔
983
                        return this.ensureFamixClass(bnd, name, (TNamedEntity) owner, /*isGeneric*/bnd.isGenericType() || bnd.isParameterizedType() || bnd.isRawType(), modifiers);
20!
984
                }
985
                if(bnd.isWildcardType()) {
3✔
986
                        return this.ensureFamixWildcardType(bnd, name, (TParametricEntity)owner, ctxt);
8✔
987
                }
988

989
                //otherwise (none of the above)
990

991
                if (name == null) {
2✔
992
                        name = bnd.getName();
3✔
993
                }
994

995
                if (owner == null) {
2!
996
                        owner = (TWithTypes) this.ensureOwner(bnd);
5✔
997
                }
998

999
                if (bnd.isTypeVariable() ) {
3!
1000
                        fmx = ensureFamixTypeParameter(bnd, name, owner);
6✔
1001
                        return fmx;
2✔
1002
                }
1003

1004
                fmx = ensureFamixEntity(Type.class, bnd, name);
×
1005
                fmx.setTypeContainer(owner);
×
1006
                return fmx;
×
1007
        }
1008

1009
        public TType ensureFamixType(ITypeBinding bnd, TWithTypes context) {
1010
                int modifiers = extractModifierOfTypeFrom(bnd);
4✔
1011
                return ensureFamixType(bnd, /*name*/null, /*owner*/null, context, modifiers);
8✔
1012
        }
1013
        
1014
        public TType ensureFamixType(ITypeBinding bnd) {
1015
                return ensureFamixType(bnd, /*ctxt*/null);
5✔
1016
        }
1017

1018
        private int extractModifierOfTypeFrom(ITypeBinding bnd) {
1019
                int modifiers = (bnd != null) ? bnd.getModifiers() : UNKNOWN_MODIFIERS;
7✔
1020
                return modifiers;
2✔
1021
        }
1022

1023
        public boolean isThrowable(ITypeBinding bnd) {
1024
                if (bnd == null) {
2!
1025
                        return false;
×
1026
                }
1027
                if (bnd.getQualifiedName().equals("java.lang.Throwable")) {
5✔
1028
                        return true;
2✔
1029
                } else if (bnd.getQualifiedName().equals("java.lang.Object")) {
5✔
1030
                        return false;
2✔
1031
                }
1032
                else {
1033
                        return isThrowable(bnd.getSuperclass());
5✔
1034
                }
1035
        }
1036

1037
        /**
1038
         * Returns a Famix Class associated with the ITypeBinding.
1039
         * The Entity is created if it does not exist.
1040
         * @param name -- the name of the FAMIX Method (MUST NOT be null, but this is not checked)
1041
         * @param owner -- type defining the method (should not be null, but it will work if it is) 
1042
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
1043
         */
1044
        @SuppressWarnings("deprecation")
1045
        public Class ensureFamixClass(ITypeBinding bnd, String name, TNamedEntity owner, boolean isGeneric, int modifiers) {
1046
                Class fmx = null;
2✔
1047

1048
                // --------------- some special cases
1049
                if (bnd != null) {
2✔
1050
                        if (bnd.isArray()) {
3!
1051
                                bnd = bnd.getElementType();
×
1052
                        }
1053

1054
                        // for inner classes defined in generics !!! For others should not change anything
1055
                        bnd = bnd.getErasure();
3✔
1056
                }
1057

1058
                // ---------------- to avoid useless computations if we can
1059
                fmx = (Class) getEntityByKey(bnd);
5✔
1060
                if (fmx != null) {
2✔
1061
                        return fmx;
2✔
1062
                }
1063

1064
                // --------------- name
1065
                if (name == null) {
2✔
1066
                        if (bnd == null) {
2!
1067
                                return null;  // not much we can do
×
1068
                        } else if (!bnd.isAnonymous()) {
3!
1069
                                name = bnd.getErasure().getName();  // for generics, will give the "core" type name, for normal type, won't change anything
5✔
1070
                        } else { // anonymous class
1071
                                if (bnd.getSuperclass() != null) {
×
1072
                                        name = bnd.getSuperclass().getName();
×
1073
                                }
1074
                                if ((name == null) || name.equals(OBJECT_NAME)) {
×
1075
                                        ITypeBinding[] intfcs = bnd.getInterfaces();
×
1076
                                        if ((intfcs != null) && (intfcs.length > 0)) {
×
1077
                                                name = bnd.getInterfaces()[0].getName();
×
1078
                                        }
1079
                                        else {
1080
                                                name = "???";
×
1081
                                        }
1082
                                }
1083
                                name = ANONYMOUS_NAME_PREFIX + "(" + name + ")";
×
1084
                        }
1085
                }
1086

1087
                if (name.equals(OBJECT_NAME)) { // TODO && owner == java.lang
4✔
1088
                        return ensureFamixClassObject(bnd);
4✔
1089
                }
1090

1091
                // --------------- owner
1092
                if (owner == null) {
2✔
1093
                        if (bnd != null) {
2✔
1094
                                owner = ensureOwner(bnd);
4✔
1095
                        }
1096
                        /*                                owner = ensureFamixPackageDefault();
1097
                        } else {*/
1098
                }
1099

1100
                // --------------- recover from name ?
1101
                if (owner != null) {
2✔
1102
                        for (Class candidate : this.getEntityByName(Class.class, name)) {
13✔
1103
                                if (matchAndMapClass(bnd, name, (ContainerEntity) owner, candidate)) {
8✔
1104
                                        fmx = candidate;
2✔
1105
                                        break;
1✔
1106
                                }
1107
                        }
1✔
1108
                }
1109

1110
                // ---------------- create
1111
                if (fmx == null) {
2✔
1112
                        if (isGeneric) {
2✔
1113
                                fmx = ensureFamixParametricClass(bnd, name, (TWithTypes) owner);
8✔
1114
                        }
1115
                        else {
1116
                                fmx = ensureFamixEntity(Class.class, bnd, name);
7✔
1117
                                fmx.setTypeContainer((TWithTypes)owner);
4✔
1118
                        }
1119
                }
1120

1121
                if (fmx!=null) {
2!
1122
                        // we just created it or it was not bound, so we make sure it has the right information in it
1123
                        if (bnd != null) {
2✔
1124
                                setClassModifiers(fmx, bnd.getDeclaredModifiers());
5✔
1125
                        }
1126
                        TAssociation lastAssoc = null;
2✔
1127

1128
                        if (bnd != null) {
2✔
1129
                                ITypeBinding supbnd = bnd.getSuperclass();
3✔
1130
                                if (supbnd != null) {
2✔
1131
                                        lastAssoc = ensureFamixInheritance((TWithInheritances) ensureFamixType(supbnd), fmx, lastAssoc, supbnd);
11✔
1132
                                }
1133
                                else {
1134
                                        lastAssoc = ensureFamixInheritance((TWithInheritances) ensureFamixClassObject(null), fmx, lastAssoc, null);
9✔
1135
                                }
1136
                                ensureImplementedInterfaces(bnd, fmx, (TWithTypes) owner, lastAssoc);
7✔
1137
                        }
1138
                }
1139

1140
                return fmx;
2✔
1141
        }
1142

1143
        /**
1144
         * Returns a Famix Exception associated with the ITypeBinding.
1145
         * The Entity is created if it does not exist.
1146
         * @param name -- the name of the Famix Exception (MUST NOT be null, but this is not checked)
1147
         * @param owner -- type defining the Exception (should not be null, but it will work if it is) 
1148
         *
1149
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
1150
         */
1151
        public <T extends TWithTypes & TNamedEntity> Exception ensureFamixException(ITypeBinding bnd, String name, TWithTypes owner, boolean isGeneric, int modifiers) {
1152
                Exception fmx = null;
2✔
1153

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

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

1164
                // ---------------- to avoid useless computations if we can
1165
                fmx = (Exception) getEntityByKey(bnd);
5✔
1166
                if (fmx != null) {
2✔
1167
                        return fmx;
2✔
1168
                }
1169

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

1193
                // --------------- owner
1194
                if (owner == null) {
2✔
1195
                        if (bnd == null) {
2✔
1196
                                owner = ensureFamixPackageDefault();
4✔
1197
                        } else {
1198
                                owner = (TWithTypes) ensureOwner(bnd);
5✔
1199
                        }
1200
                }
1201

1202
                // --------------- recover from name ?
1203
                for (Exception candidate : this.getEntityByName(Exception.class, name)) {
13✔
1204
                        if (matchAndMapClass(bnd, name, (T) owner, candidate)) {
8✔
1205
                                fmx = candidate;
2✔
1206
                                break;
1✔
1207
                        }
1208
                }
1✔
1209

1210
                // ---------------- create
1211
                if (fmx == null) {
2✔
1212
                        fmx = ensureFamixEntity(Exception.class, bnd, name);
7✔
1213
                        fmx.setTypeContainer(owner);
3✔
1214
                }
1215

1216
                if (fmx!=null) {
2!
1217
                        // we just created it or it was not bound, so we make sure it has the right information in it
1218
                        TAssociation lastAssoc = null;
2✔
1219
                        if (bnd != null) {
2✔
1220
                                ITypeBinding supbnd = bnd.getSuperclass();
3✔
1221
                                if (supbnd != null) {
2!
1222
                                        lastAssoc = ensureFamixInheritance((TWithInheritances) ensureFamixType(supbnd), fmx, lastAssoc, supbnd);
11✔
1223
                                }
1224
                                else {
NEW
1225
                                        lastAssoc = ensureFamixInheritance((TWithInheritances) ensureFamixClassObject(null), fmx, lastAssoc, null);
×
1226
                                }
1227
                                ensureImplementedInterfaces(bnd, fmx, owner, lastAssoc);
6✔
1228
                        }
1229
                }
1230

1231
                return fmx;
2✔
1232
        }
1233

1234
        /**
1235
         * Returns a FAMIX Interface with the given <b>name</b>, creating it if it does not exist yet.
1236
         * @param name -- the name of the FAMIX Method (MUST NOT be null, but this is not checked)
1237
         * @param owner -- type defining the method (should not be null, but it will work if it is) 
1238
         * @return the FAMIX Class or null in case of a FAMIX error
1239
         */
1240
        public <T extends TWithTypes & TNamedEntity> Interface ensureFamixInterface(ITypeBinding bnd, String name, TWithTypes owner, boolean isGeneric, int modifiers) {
1241
                Interface fmx = null;
2✔
1242

1243
                // --------------- some special cases
1244
                if (bnd != null) {
2!
1245
                        if (bnd.isArray()) {
3!
1246
                                bnd = bnd.getElementType();
×
1247
                        }
1248

1249
                        // for inner classes defined in generics !!! For others should not change anything
1250
                        bnd = bnd.getErasure();
3✔
1251
                }
1252

1253
                // ---------------- to avoid useless computations if we can
1254
                fmx = (Interface) getEntityByKey(bnd);
5✔
1255
                if (fmx != null) {
2✔
1256
                        return fmx;
2✔
1257
                }
1258

1259
                // --------------- name
1260
                if (name == null) {
2✔
1261
                        if (bnd == null) {
2!
1262
                                return null;  // not much we can do
×
1263
                        } else if (!bnd.isAnonymous()) {
3!
1264
                                name = bnd.getErasure().getName();  // for generics, will give the "core" type name, for normal type, won't change anything
5✔
1265
                        } else { // anonymous class
1266
                                if (bnd.getSuperclass() != null) {
×
1267
                                        name = bnd.getSuperclass().getName();
×
1268
                                }
1269
                                if ((name == null) || name.equals(OBJECT_NAME)) {
×
1270
                                        ITypeBinding[] intfcs = bnd.getInterfaces();
×
1271
                                        if ((intfcs != null) && (intfcs.length > 0)) {
×
1272
                                                name = bnd.getInterfaces()[0].getName();
×
1273
                                        }
1274
                                        else {
1275
                                                name = "???";
×
1276
                                        }
1277
                                }
1278
                                name = ANONYMOUS_NAME_PREFIX + "(" + name + ")";
×
1279
                        }
1280
                }
1281

1282
                // --------------- owner
1283
                if (owner == null) {
2✔
1284
                        if (bnd == null) {
2!
1285
                                owner = ensureFamixPackageDefault();
×
1286
                        } else {
1287
                                owner = (TWithTypes) ensureOwner(bnd);
5✔
1288
                        }
1289
                }
1290

1291
                // --------------- recover from name ?
1292
                for (Interface candidate : this.getEntityByName(Interface.class, name)) {
13✔
1293
                        if (matchAndMapInterface(bnd, name, (T) owner, candidate)) {
8✔
1294
                                fmx = candidate;
2✔
1295
                                break;
1✔
1296
                        }
1297
                }
1✔
1298

1299
                // ---------------- create
1300
                if (fmx == null) {
2✔
1301
                        if (isGeneric) {
2✔
1302
                                fmx = ensureFamixParametricInterface(bnd, name, owner);
7✔
1303
                        }
1304
                        else {
1305
                                fmx = ensureFamixEntity(Interface.class, bnd, name);
7✔
1306
                                fmx.setTypeContainer(owner);
3✔
1307
                        }
1308
                }
1309

1310
                if (fmx!=null) {
2!
1311
                        // we just created it or it was not bound, so we make sure it has the right information in it
1312
                        if (bnd != null) {
2!
1313
                                setInterfaceModifiers(fmx, bnd.getModifiers());
5✔
1314
                        }
1315
                        TAssociation lastAssociation = null;
2✔
1316
                        if (bnd != null) {
2!
1317
                                ensureImplementedInterfaces(bnd, fmx, owner, lastAssociation);
6✔
1318
                        }
1319
                }
1320
                return fmx;
2✔
1321
        }
1322

1323
        public TType asClass(TType excepFmx) {
1324
                Class tmp = null;
×
1325
                IBinding key = null;
×
1326
                try {
1327
                        TWithTypes owner = (TWithTypes) Util.getOwner(excepFmx);
×
1328
                        owner.getTypes().remove(excepFmx);
×
1329
                        removeEntity((NamedEntity) excepFmx);
×
1330

1331
                        key = entityToKey.get((NamedEntity) excepFmx);
×
1332
                        tmp = ensureFamixEntity(Class.class, key, excepFmx.getName());
×
1333
                        tmp.setTypeContainer((ContainerEntity)owner);
×
1334

1335
                        tmp.addMethods(((TWithMethods) excepFmx).getMethods());
×
1336
                        if (excepFmx instanceof TWithAttributes) {
×
1337
                                tmp.addAttributes(((TWithAttributes) excepFmx).getAttributes());
×
1338
                        }
1339

1340
                        if (key != null) {
×
1341
                                setClassModifiers(tmp, key.getModifiers());
×
1342
                        }
1343

1344
                        if (excepFmx instanceof TWithInheritances) {
×
1345
                                tmp.addSuperInheritances(((TWithInheritances) excepFmx).getSuperInheritances());
×
1346
                                tmp.addSubInheritances(((TWithInheritances) excepFmx).getSubInheritances());
×
1347
                        }
1348
                        tmp.setSourceAnchor(excepFmx.getSourceAnchor());
×
1349
                        tmp.addAnnotationInstances(((NamedEntity) excepFmx).getAnnotationInstances());
×
1350
                        // tmp.addComments(excepFmx.getComments());
1351
                        tmp.addIncomingReferences(excepFmx.getIncomingReferences());
×
1352
                        tmp.setIsStub(excepFmx.getIsStub());
×
1353
                        tmp.addTypes(((ContainerEntity) excepFmx).getTypes());
×
1354
                }
1355
                catch( ConcurrentModificationException e) {
×
1356
                        e.printStackTrace();
×
1357
                }
×
1358

1359
                return tmp;
×
1360
        }
1361

1362
        public TThrowable asException(TType excepFmx) {
1363
                if (excepFmx instanceof Exception) {
3✔
1364
                        return (Exception) excepFmx;
3✔
1365
                }
1366
                if(excepFmx instanceof TypeParameter) {
3✔
1367
                        return (TypeParameter) excepFmx;
3✔
1368
                }
1369
                Exception tmp = null;
2✔
1370
                IBinding key = null;
2✔
1371
                try {
1372
                        TWithTypes owner = (TWithTypes) Util.getOwner(excepFmx);
4✔
1373
                        owner.getTypes().remove(excepFmx);
5✔
1374
                        removeEntity((NamedEntity) excepFmx);
4✔
1375

1376
                        key = entityToKey.get((NamedEntity) excepFmx);
7✔
1377
                        tmp = ensureFamixException((ITypeBinding) key, excepFmx.getName(), owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
10✔
1378

1379
                        tmp.addMethods(((TWithMethods) excepFmx).getMethods());
5✔
1380
                        if (excepFmx instanceof TWithAttributes) {
3!
1381
                                tmp.addAttributes(((TWithAttributes) excepFmx).getAttributes());
5✔
1382
                        }
1383

1384
                        if (excepFmx instanceof TWithInheritances) {
3!
1385
                                tmp.addSuperInheritances(((TWithInheritances) excepFmx).getSuperInheritances());
5✔
1386
                                tmp.addSubInheritances(((TWithInheritances) excepFmx).getSubInheritances());
5✔
1387
                        }
1388
                        tmp.setSourceAnchor(excepFmx.getSourceAnchor());
4✔
1389
                        tmp.addAnnotationInstances(((NamedEntity) excepFmx).getAnnotationInstances());
5✔
1390
                        // tmp.addComments(excepFmx.getComments());
1391
                        List<TReference> newList = excepFmx.getIncomingReferences().stream().collect(Collectors.toList());
7✔
1392
                        tmp.addIncomingReferences(newList);
3✔
1393
                        tmp.setIsStub(excepFmx.getIsStub());
4✔
1394
                        tmp.addTypes(((ContainerEntity) excepFmx).getTypes());
5✔
1395
                }
1396
                catch( ConcurrentModificationException e) {
×
1397
                        e.printStackTrace();
×
1398
                }
1✔
1399

1400
                return (TThrowable)tmp;
2✔
1401
        }
1402

1403
        /**
1404
         * helper method, we know the type exists, ensureFamixClass will recover it
1405
         */
1406
        public Class getFamixClass(ITypeBinding bnd, String name, TNamedEntity owner) {
1407
                return ensureFamixClass(bnd, name, owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
8✔
1408
        }
1409

1410
        /**
1411
         * helper method, we know the type exists, ensureFamixInterface will recover it
1412
         */
1413
        public Interface getFamixInterface(ITypeBinding bnd, String name, ContainerEntity owner) {
1414
                return ensureFamixInterface(bnd, name, owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
8✔
1415
        }
1416

1417
        /**
1418
         * helper method, we know the type exists, ensureFamixInterface will recover it
1419
         */
1420
        public Exception getFamixException(ITypeBinding bnd, String name, TWithTypes owner) {
1421
                return ensureFamixException(bnd, name, owner, /*isGeneric*/false, UNKNOWN_MODIFIERS);
8✔
1422
        }
1423

1424
        /**
1425
         * Ensures a famix entity for the owner of a binding.<br>
1426
         * This owner can be a method, a class or a namespace
1427
         * @param bnd -- binding for the owned entity
1428
         * @return a famix entity for the owner
1429
         */
1430
        private TNamedEntity ensureOwner(ITypeBinding bnd) {
1431
                TNamedEntity owner = null;
2✔
1432
                IMethodBinding parentMtd = bnd.getDeclaringMethod();
3✔
1433
                if (parentMtd != null) {
2✔
1434
                        owner = this.ensureFamixMethod(parentMtd);  // cast needed to desambiguate the call
5✔
1435
                }
1436
                else {
1437
                        ITypeBinding parentClass = bnd.getDeclaringClass();
3✔
1438
                        if (parentClass != null) {
2✔
1439
                                TType tmpOwn = this.ensureFamixType(parentClass);
4✔
1440
                                if (tmpOwn instanceof ParametricClass) {
3✔
1441
                                        owner =  (TNamedEntity) ((ParametricClass) tmpOwn);
4✔
1442
                                }
1443
                                else {
1444
                                        owner = tmpOwn;
2✔
1445
                                }
1446
                        }
1✔
1447
                        else {
1448
                                IPackageBinding parentPckg = bnd.getPackage();
3✔
1449
                                if (parentPckg != null) {
2!
1450
                                        owner = this.ensureFamixPackage(parentPckg, null);
6✔
1451
                                } else {
1452
                                        owner = this.ensureFamixPackageDefault();
×
1453
                                }
1454
                        }
1455
                }
1456
                return owner;
2✔
1457
        }
1458

1459

1460
        /**
1461
         * Returns a FAMIX PrimitiveType with the given <b>name</b>, creating it if it does not exist yet
1462
         * We assume that PrimitiveType must be uniq for a given name
1463
         * @param name -- the name of the FAMIX PrimitiveType
1464
         * @return the FAMIX PrimitiveType or null in case of a FAMIX error
1465
         */
1466
        public PrimitiveType ensureFamixPrimitiveType(ITypeBinding bnd, String name) {
1467
                if (name == null) {
2✔
1468
                        if (bnd == null) {
2!
1469
                                return null;
×
1470
                        } else {
1471
                                name = bnd.getName();
3✔
1472
                        }
1473
                }
1474
                return ensureFamixUniqEntity(PrimitiveType.class, bnd, name);
7✔
1475
        }
1476

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

1480
                // --------------- to avoid useless computations if we can
1481
                fmx = (org.moosetechnology.model.famix.famixjavaentities.Enum) getEntityByKey(bnd);
5✔
1482
                if (fmx != null) {
2✔
1483
                        return fmx;
2✔
1484
                }
1485

1486
                // --------------- name
1487
                if (name == null) {
2✔
1488
                        if (bnd == null) {
2!
1489
                                return null;
×
1490
                        }
1491
                        else {
1492
                                name = bnd.getName();
3✔
1493
                        }
1494
                }
1495

1496
                // --------------- owner
1497
                if (owner == null) {
2✔
1498
                        if (bnd == null) {
2!
1499
                                owner = ensureFamixPackageDefault();  // not really sure what to do here
×
1500
                        } else {
1501
                                owner = (TWithTypes) ensureOwner(bnd);
5✔
1502
                        }
1503
                }
1504

1505
                // --------------- recover from name ?
1506
                for (org.moosetechnology.model.famix.famixjavaentities.Enum candidate : getEntityByName(org.moosetechnology.model.famix.famixjavaentities.Enum.class, name)) {
9!
1507
                        if (matchAndMapType(bnd, name, (T) owner, candidate)) {
×
1508
                                fmx = candidate;
×
1509
                                break;
×
1510
                        }
1511
                }
×
1512

1513
                if (fmx == null) {
2!
1514
                        fmx = ensureFamixEntity(Enum.class, bnd, name);
7✔
1515
                        fmx.setTypeContainer(owner);
3✔
1516
                }
1517

1518
                if ((fmx != null) && (bnd != null) ) {
4!
1519
                        setVisibility(fmx, bnd.getModifiers());
5✔
1520
                }
1521

1522
                return fmx;
2✔
1523
        }
1524

1525
        /**
1526
         * helper method, we know the type exists, ensureFamixEnum will recover it
1527
         */
1528
        public org.moosetechnology.model.famix.famixjavaentities.Enum getFamixEnum(ITypeBinding bnd, String name, TWithTypes owner) {
1529
                return ensureFamixEnum(bnd, name, owner);
6✔
1530
        }
1531

1532
        public EnumValue ensureFamixEnumValue(IVariableBinding bnd,        String name, Enum owner) {
1533
                EnumValue fmx = null;
2✔
1534

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

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

1551
                // --------------- owner
1552
                if (owner == null) {
2✔
1553
                        if (bnd == null) {
2!
1554
                                return null;  // what would be the interest of creating an EnumValue without a declaring Enum type?
×
1555
                        }
1556
                        else {
1557
                                owner = ensureFamixEnum(bnd.getDeclaringClass(), null, null);
7✔
1558
                        }
1559
                }
1560

1561
                // --------------- recover from name ?
1562
                for (EnumValue candidate : getEntityByName(EnumValue.class, name) ) {
9!
1563
                        if ( matchAndMapVariable(bnd, name, owner, candidate) ) {
×
1564
                                fmx = candidate;
×
1565
                                break;
×
1566
                        }
1567
                }
×
1568
                if (fmx == null) {
2!
1569
                        fmx = ensureFamixEntity(EnumValue.class, bnd, name);
7✔
1570
                        fmx.setParentEnum(owner);
3✔
1571
                }
1572

1573
                if (fmx!=null) {
2!
1574
                        fmx.setParentEnum(owner);
3✔
1575
                }
1576

1577
                return fmx;
2✔
1578
        }
1579

1580
        /**
1581
         * helper method, we know the type enumValue, ensureFamixEnumValue will recover it
1582
         */
1583
        public EnumValue getFamixEnumValue(IVariableBinding bnd, String name, Enum owner) {
1584
                return ensureFamixEnumValue(bnd, name, owner);
×
1585
        }
1586

1587
        /**
1588
         * e.g. see {@link EntityDictionary#ensureFamixClass}
1589
         */
1590
        public AnnotationType ensureFamixAnnotationType(ITypeBinding bnd, String name, ContainerEntity owner) {
1591
                AnnotationType fmx = null;
2✔
1592

1593
                // --------------- to avoid useless computations if we can
1594
                fmx = (AnnotationType)getEntityByKey(bnd);
5✔
1595
                if (fmx != null) {
2✔
1596
                        return fmx;
2✔
1597
                }
1598

1599
                // --------------- name
1600
                if (name == null) {
2✔
1601
                        if (bnd == null) {
2!
1602
                                return null;
×
1603
                        }
1604
                        else {
1605
                                name = bnd.getName();
3✔
1606
                        }
1607
                }
1608

1609
                // --------------- owner
1610
                if (owner == null) {
2✔
1611
                        if (bnd == null) {
2!
1612
                                owner = ensureFamixPackageDefault();
×
1613
                        }
1614
                        else {
1615
                                IPackageBinding parentPckg = bnd.getPackage();
3✔
1616
                                if (parentPckg != null) {
2!
1617
                                        owner = this.ensureFamixPackage(parentPckg, null);
6✔
1618
                                } else {
1619
                                        owner = this.ensureFamixPackageDefault();
×
1620
                                }
1621
                        }
1622
                }
1623

1624
                // --------------- recover from name ?
1625
                for (AnnotationType candidate : getEntityByName(AnnotationType.class, name) ) {
13✔
1626
                        if ( matchAndMapType(bnd, name, owner, candidate) ) {
7✔
1627
                                fmx = candidate;
2✔
1628
                                break;
1✔
1629
                        }
1630
                }
1✔
1631

1632
                // --------------- create
1633
                if (fmx == null) {
2✔
1634
                        fmx = ensureFamixEntity(AnnotationType.class, bnd, name);
7✔
1635
                        fmx.setAnnotationTypesContainer(owner);
3✔
1636
                }
1637

1638
                if ( (fmx!=null) && (bnd != null) ) {
4!
1639
                        // Not supported in Famix
1640

1641
                        // setVisibility(fmx, bnd.getModifiers());
1642
                }
1643

1644
                return fmx;
2✔
1645
        }
1646

1647
        /**
1648
         * helper method, we know the type exists, ensureFamixAnnotationType will recover it
1649
         */
1650
        public AnnotationType getFamixAnnotationType(ITypeBinding bnd, String name, ContainerEntity owner) {
1651
                return ensureFamixAnnotationType(bnd, name, owner);
6✔
1652
        }
1653

1654
        public AnnotationTypeAttribute ensureFamixAnnotationTypeAttribute(IMethodBinding bnd, String name, AnnotationType owner) {
1655
                AnnotationTypeAttribute fmx = null;
2✔
1656

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

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

1673
                // --------------- owner
1674
                if (owner == null) {
2!
1675
                        if (bnd == null) {
×
1676
                                return null;  // what would be the use of an AnnotationTypeAttribute without AnnotationType ?
×
1677
                        }
1678
                        else {
1679
                                ITypeBinding parentType = bnd.getDeclaringClass();
×
1680
                                if (parentType != null) {
×
1681
                                        owner = this.ensureFamixAnnotationType(parentType, null, null);
×
1682
                                }
1683
                                else  {
1684
                                        return null;  // what would be the use of an AnnotationTypeAttribute without AnnotationType ?
×
1685
                                }
1686
                        }
1687
                }
1688

1689
                // --------------- recover from name ?
1690
                for (AnnotationTypeAttribute candidate : getEntityByName(AnnotationTypeAttribute.class, name) ) {
13✔
1691
                        // JDT treats annotation type attributes as methods ...
1692
                        // checkAndMapMethod wants a signature as 2nd argument so we add empty param list
1693
                        if ( (bnd != null) && matchAndMapMethod(bnd, name+"()", null, owner, candidate) ) {
11!
1694
                                fmx = candidate;
×
1695
                                break;
×
1696
                        }
1697
                        // if the binding is null, the annotationTypeAttribute migth have been created
1698
                        else if ( (bnd == null) && matchAndMapVariable(null, name, owner, candidate)) {
2!
1699
                                fmx = candidate;
×
1700
                                break;
×
1701
                        }
1702
                }
1✔
1703

1704
                if (fmx == null) {
2!
1705
                        fmx = ensureFamixEntity(AnnotationTypeAttribute.class, bnd, name);
7✔
1706
                        fmx.setParentType(owner);
3✔
1707
                }
1708

1709
                if ( (fmx!=null) && (bnd != null) ) {
4!
1710
                        // Not suopp
1711

1712
                        // setVisibility(fmx, bnd.getModifiers());
1713
                }
1714

1715
                return fmx;
2✔
1716
        }
1717

1718
        /**
1719
         * helper method, we know the attribute exists, ensureFamixAnnotationTypeAttribute will recover it
1720
         */
1721
        public AnnotationTypeAttribute getFamixAnnotationTypeAttribute(IMethodBinding bnd, String name, AnnotationType owner) {
1722
                return ensureFamixAnnotationTypeAttribute( bnd, name, owner);
6✔
1723
        }
1724
        
1725
        
1726
        /**
1727
         * Returns a FAMIX Wildcard with its bounds
1728
         * @param bnd
1729
         * @param name
1730
         * @param owner
1731
         * @return
1732
         */
1733
        public Wildcard ensureFamixWildcardType(ITypeBinding bnd, String name, TParametricEntity owner, TWithTypes ctxt) {
1734
                Wildcard fmx = this.ensureFamixEntity(Wildcard.class, bnd, bnd.getName());
8✔
1735
                if(bnd.getBound() != null) {
3✔
1736
                        Type bound = (Type) this.ensureFamixType(bnd.getBound()); 
6✔
1737
                        if(bnd.isUpperbound()) {
3✔
1738
                                fmx.setUpperBound(bound);
3✔
1739
                                bound.addUpperBoundedWildcards(fmx);
4✔
1740
                        }else{
1741
                                fmx.setLowerBound(bound);
3✔
1742
                                bound.addLowerBoundedWildcards(fmx);
3✔
1743
                        }
1744
                }
1745
                return fmx;
2✔
1746
        }
1747

1748
        /**
1749
         * Returns a Famix TypeParameter (created by a Famix ParametricEntity) with the given <b>name</b>, creating it if it does not exist yet
1750
         * In the second case, sets some default properties: not Abstract, not Final, not Private, not Protected, not Public
1751
         * @param name -- the name of the Famix TypeParameter
1752
         * @return the Famix TypeParameter or null in case of a Famix error
1753
         */
1754
        public TypeParameter ensureFamixTypeParameter(ITypeBinding bnd,        String name, TWithTypes owner) {
1755
                TypeParameter fmx = null;
2✔
1756

1757
                // --------------- to avoid useless computations if we can
1758
                fmx = (TypeParameter)getEntityByKey(bnd);
5✔
1759
                if (fmx != null) {
2✔
1760
                        return fmx;
2✔
1761
                }
1762

1763
                // --------------- name
1764
                if (name == null) {
2✔
1765
                        if (bnd == null) {
2!
1766
                                return null;
×
1767
                        }
1768
                        else {
1769
                                name = bnd.getName();
3✔
1770
                        }
1771
                }
1772

1773
                // --------------- owner
1774
                if (owner == null) {
2!
UNCOV
1775
                        if (bnd == null) {
×
1776
                                owner = null;  // not really sure what to do here
×
1777
                        }
1778
                        else {
UNCOV
1779
                                if (bnd.getDeclaringClass() != null) {
×
NEW
1780
                                        owner = (TWithTypes) this.ensureFamixType(bnd.getDeclaringClass());
×
UNCOV
1781
                                }else if(bnd.getDeclaringMethod() != null) {
×
NEW
1782
                                        owner = (TWithTypes) this.ensureFamixMethod(bnd.getDeclaringMethod());
×
1783
                                }
1784
                                else {
1785
                                        owner = null;  // not really sure what to do here
×
1786
                                }
1787
                        }
1788
                }
1789

1790
                // --------------- recover from name ?
1791
                for (Type candidate : this.getEntityByName(Type.class, name)) {
13✔
1792
                        if ( matchAndMapType(bnd, name, (ContainerEntity) owner, candidate) ) {
8✔
1793
                                fmx = (TypeParameter) candidate;
3✔
1794
                                break;
1✔
1795
                        }
1796
                }
1✔
1797

1798
                // --------------- create
1799
                if (fmx == null) {
2✔
1800
                        fmx = ensureFamixEntity(TypeParameter.class, bnd, name);
7✔
1801
                        if(bnd != null && bnd.getSuperclass() != null) {
5!
1802
                                Type upperBound = (Type)ensureFamixType(bnd.getSuperclass());
6✔
1803
                                fmx.setUpperBound(upperBound);
3✔
1804
                        }
1805
                        if(bnd != null && bnd.getInterfaces().length > 0) {
6✔
1806
                                for(ITypeBinding intbnd: bnd.getInterfaces()) {
17✔
1807
                                        Type upperBound = (Type)ensureFamixType(intbnd);
5✔
1808
                                        fmx.setUpperBound(upperBound);
3✔
1809
                                }
1810
                        }
1811
                        fmx.setTypeContainer((ContainerEntity) owner);
4✔
1812
                }
1813

1814
                return fmx;
2✔
1815
        }
1816

1817

1818
        public IBinding getTypeParameterOwner(ITypeBinding typeParameterBinding, IBinding currentOwner) {
1819
                
1820

1821

NEW
1822
                return currentOwner;
×
1823
        }
1824

1825

1826

1827
        /**
1828
         * Checks whether the existing unmapped Famix Namespace matches the binding.
1829
         * Checks that the candidate has the same name as the JDT bound package, and checks recursively that owners also match.
1830
         *
1831
         * @param bnd       -- a JDT binding that we are trying to match to the candidate
1832
         * @param name      of the package
1833
         * @param owner     of the package
1834
         * @param candidate -- a Famix Entity
1835
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1836
         */
1837
        private boolean matchAndMapPackage(IPackageBinding bnd, String name, Package owner, NamedEntity candidate) {
1838
                if (!(candidate instanceof Package)) {
3!
1839
                        return false;
×
1840
                }
1841

1842
                // check whether bnd and candidate are already bound
1843
                CheckResult res = checkKeyMatch(bnd, candidate);
5✔
1844
                if (res == CheckResult.MATCH) {
3✔
1845
                        return true;
2✔
1846
                } else if (res == CheckResult.FAIL) {
3✔
1847
                        return false;
2✔
1848
                }
1849

1850
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7✔
1851
                        return false;
2✔
1852
                }
1853

1854
                // names match, not need to look at owner because names of Namespaces are their fully qualified name
1855
                conditionalMapToKey(bnd, candidate);
4✔
1856
                return true;
2✔
1857
        }
1858

1859
        /**
1860
         * Checks whether the existing unmapped Famix Type matches the binding.
1861
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
1862
         * We also check that the actual class of the candidate matches (can be a sub-class of FamixType).
1863
         * @param bnd -- a JDT binding that we are trying to match to the candidate
1864
         * @param name of the type
1865
         * @param owner of the type
1866
         * @param candidate -- a Famix NamedEntity (Class, Type, PrimitiveType, Enum, AnnotationType)
1867
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1868
         */
1869
        private <T extends TWithTypes & TNamedEntity> boolean matchAndMapType(ITypeBinding bnd, String name, TNamedEntity owner, TNamedEntity candidate) {
1870
                if (! (candidate instanceof Type) ) {
3!
1871
                        return false;
×
1872
                }
1873

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

1883
                if ( (bnd != null) && (bnd.isArray()) ) {
5!
1884
                                bnd = bnd.getElementType();
×
1885
                }
1886

1887
                // checking names
1888
                if ( (bnd != null) && (bnd.isParameterizedType() || bnd.isRawType()) ) {
8!
1889
                        name = bnd.getErasure().getName();
×
1890
                }
1891
                else if (bnd != null) {
2✔
1892
                        name = bnd.getName();
3✔
1893
                }
1894
                // else name = name
1895
                if (checkNameMatch(null, name, candidate) == CheckResult.FAIL) {
7✔
1896
                        return false;
2✔
1897
                }
1898

1899
                // special case of primitive types
1900
                if (candidate instanceof PrimitiveType) {
3!
1901
                        if ( (bnd != null) && bnd.isPrimitive() ) {
×
1902
                                // names are equal so it's OK
1903
                                conditionalMapToKey(bnd, candidate);
×
1904
                                return true;
×
1905
                        }
1906
                        else if ( (bnd == null) && (owner == null) ) {
×
1907
                                return true;
×
1908
                        }
1909
                }
1910

1911
                // check owners without bnd
1912
                if (bnd == null) {
2✔
1913
                        return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
7✔
1914
                }
1915

1916
                // check owners with bnd
1917
                // type is an annotation
1918
                if (bnd.isAnnotation() && (candidate instanceof AnnotationType)) {
6!
1919
                        if (matchAndMapPackage(bnd.getPackage(), owner.getName(), (Package) Util.getOwner(owner), Util.getOwner(candidate))) {
13!
1920
                                conditionalMapToKey(bnd, candidate);
4✔
1921
                                return true;
2✔
1922
                        } else {
1923
                                return false;
×
1924
                        }
1925
                }
1926

1927
                // check owners with bnd
1928
                // type is a Parameterized type
1929
                if ((bnd.isParameterizedType() || bnd.isRawType()) && (candidate instanceof ParametricClass)) {
6!
1930
                        return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
×
1931
                }
1932

1933
                // check owners with bnd
1934
                // type is an Enum
1935
                if (bnd.isEnum() && (candidate instanceof Enum)) {
3!
1936
                        return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
×
1937
                }
1938

1939
                // check owners with bnd
1940
                // type is something elae (a class or interface)
1941
                // Annotation are interfaces too, so we should check this one after isAnnotation
1942
                if ( bnd.isClass()) {
3!
1943
                        return matchAndMapClass(bnd, name, owner, (Type) candidate);
×
1944
                }
1945

1946
                if(bnd.isInterface()) {
3!
1947
                        return matchAndMapInterface(bnd, name, owner, (Type) candidate);
×
1948
                }
1949

1950
                return false;
2✔
1951
        }
1952

1953
        /**
1954
         * Checks whether the existing unmapped Famix Class (or Interface) matches the binding.
1955
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
1956
         * @param bnd -- a JDT binding that we are trying to match to the candidate
1957
         * @param name of the class
1958
         * @param owner of the class
1959
         * @param candidate -- a Famix Entity
1960
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1961
         */
1962
        private boolean matchAndMapClass(ITypeBinding bnd, String name, TNamedEntity owner, TType candidate) {
1963
                if (!(candidate instanceof Class)) {
3!
1964
                        return false;
×
1965
                }
1966

1967
                // check whether bnd and candidate are already bound
1968
                CheckResult res = checkKeyMatch(bnd, (NamedEntity) candidate);
6✔
1969
                if (res == CheckResult.MATCH) {
3!
1970
                        return true;
×
1971
                } else if (res == CheckResult.FAIL) {
3✔
1972
                        return false;
2✔
1973
                }
1974

1975
                if (checkNameMatch(bnd, name, (NamedEntity) candidate) == CheckResult.FAIL) {
8!
1976
                        return false;
×
1977
                }
1978

1979
                // checking owner
1980
                return matchAndMapTypeOwner(bnd, owner, (Type) candidate);
7✔
1981
        }
1982

1983
        /**
1984
         * Checks whether the existing unmapped Famix Class (or Interface) matches the binding.
1985
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
1986
         * @param bnd -- a JDT binding that we are trying to match to the candidate
1987
         * @param name of the class
1988
         * @param owner of the class
1989
         * @param candidate -- a Famix Entity
1990
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
1991
         */
1992
        private boolean matchAndMapInterface(ITypeBinding bnd, String name, TNamedEntity owner, Type candidate) {
1993
                if (!(candidate instanceof Interface)) {
3!
1994
                        return false;
×
1995
                }
1996

1997
                // check whether bnd and candidate are already bound
1998
                CheckResult res = checkKeyMatch(bnd, candidate);
5✔
1999
                if (res == CheckResult.MATCH) {
3!
2000
                        return true;
×
2001
                } else if (res == CheckResult.FAIL) {
3✔
2002
                        return false;
2✔
2003
                }
2004

2005
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7!
2006
                        return false;
×
2007
                }
2008

2009
                // checking owner
2010
                return matchAndMapTypeOwner(bnd, owner, candidate);
6✔
2011
        }
2012

2013
        /**
2014
         * Checks whether the existing unmapped Famix "Method" matches the binding.
2015
         * Checks that the candidate has the same name and same signature as the JDT bound method, and checks recursively that owners also match.
2016
         * Note that AnnotationTypeAttribute are treated as methods by JDT, so they are checked here.
2017
         * @param bnd -- a JDT binding that we are trying to match to the candidate
2018
         * @param sig -- signature of the method
2019
         * @param retTyp -- return type of the method
2020
         * @param owner of the method
2021
         * @param candidate -- a Famix Entity (regular Method or AnnotationTypeAttribute)
2022
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
2023
         */
2024
        private  boolean matchAndMapMethod(IMethodBinding bnd, String sig, TType retTyp, TNamedEntity owner, NamedEntity candidate) {
2025
                if (! (candidate instanceof Method) ) {
3✔
2026
                        return false;
2✔
2027
                }
2028

2029
                // check whether bnd and candidate are already bound
2030
                CheckResult res = checkKeyMatch(bnd, candidate);
5✔
2031
                if (res == CheckResult.MATCH) {
3!
UNCOV
2032
                        return true;
×
2033
                }
2034
                else if (res == CheckResult.FAIL) {
3✔
2035
                        return false;
2✔
2036
                }
2037

2038
                // checking names
2039
                String name = (sig != null) ? sig.substring(0, sig.indexOf('(')) : null;
10!
2040
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7!
2041
                        return false;
×
2042
                }
2043

2044
                // for methods, the name is not enough, we must test the signature also
2045
                // but not for AnnotationTypeAttribute
2046

2047
                        if (bnd != null) {
2✔
2048
                                sig = bnd.getName() + "(" + signatureParamsFromBinding(bnd) + ")";
7✔
2049
                        }
2050
                        if (! ((Method) candidate).getSignature().equals(sig)) {
6✔
2051
                                return false;
2✔
2052
                        }
2053

2054
                        // and still for method, must also check the return type
2055
                        if (bnd != null) {
2✔
2056
                                if (bnd.isConstructor()) {
3!
2057
                                        if ( ((Method) candidate).getDeclaredType() != null ) {
×
2058
                                                return false;
×
2059
                                        }
2060
                                        // else OK for now
2061
                                }
2062
                                else { // not a constructor
2063
                                        if ( ((Method) candidate).getDeclaredType() == null ) {
4!
2064
                                                return false;
×
2065
                                        }
2066
                                        else if (! matchAndMapType(bnd.getReturnType(), null, null, ((Method) candidate).getDeclaredType()) ) {
10!
2067
                                                return false;
×
2068
                                        }
2069
                                        // else OK for now
2070
                                }
2071
                        }
2072
                        else {  // bnd == null
2073
                                if (retTyp == null) { // similar to (bnd.isConstructor())
2!
2074
                                        if ( ((Method) candidate).getDeclaredType() != null ) {
4!
2075
                                                return false;
×
2076
                                        }
2077
                                        // else OK for now
2078
                                } else { // (ret != null)  i.e. not a constructor
2079
                                        if (((Method) candidate).getDeclaredType() == null) {
×
2080
                                                return false;
×
2081
                                        } else if (!matchAndMapType(null, retTyp.getName(), Util.getOwner(retTyp), (NamedEntity) ((Method) candidate).getDeclaredType())) {
×
2082
                                                return false;
×
2083
                                        }
2084
                                        // else OK for now
2085
                                }
2086
                        }
2087

2088

2089
                // check owner
2090
                if (matchAndMapOwnerAsType(((bnd != null) ? bnd.getDeclaringClass() : null), owner, Util.getOwner(candidate)) == CheckResult.MATCH) {
13✔
2091
                        conditionalMapToKey(bnd, candidate);
4✔
2092
                        return true;
2✔
2093
                } else {
2094
                        return false;
2✔
2095
                }
2096
        }
2097

2098
        /**
2099
         * Checks whether the candidate (an existing unmapped Famix "Variable" like Attribute, Parameter, ...) matches the binding.
2100
         * Checks that the candidate has the same name as the JDT bound variable, and checks recursively that owners also match.
2101
         * The Famix candidate is a NamedEntity and not a StructuralEntity to allow dealing with Famix EnumValue that JDT treats as variables
2102
         * @param bnd -- a JDT binding that we are trying to match to the candidate
2103
         * @param name of the variable
2104
         * @param owner of the variable
2105
         * @param candidate -- a Famix Entity (a StructuralEntity or an EnumValue)
2106
         * @return whether the binding matches the candidate (if <b>true</b>, the mapping is recorded)
2107
         */
2108
        private boolean matchAndMapVariable(IVariableBinding bnd, String name, TNamedEntity owner, TNamedEntity candidate) {
2109
                if (!(candidate instanceof TStructuralEntity)) {
3!
2110
                        return false;
×
2111
                }
2112

2113
                // check whether bnd and candidate are already bound
2114
                CheckResult keyMatch = checkKeyMatch(bnd, candidate);
5✔
2115
                if (keyMatch == CheckResult.MATCH) {
3!
2116
                        return true;
×
2117
                } else if (keyMatch == CheckResult.FAIL) {
3✔
2118
                        return false;
2✔
2119
                }
2120

2121
                if (checkNameMatch(bnd, name, candidate) == CheckResult.FAIL) {
7!
2122
                        return false;
×
2123
                }
2124

2125
                // check owner
2126
                TNamedEntity candidateOwner = Util.getOwner(candidate);
3✔
2127

2128
                // local variable or parameter ?
2129
                // owner is a Method? (for example in case of an anonymous class)
2130
                CheckResult res = matchAndMapOwnerAsMethod(((bnd != null) ? bnd.getDeclaringMethod() : null), owner, candidateOwner);
11✔
2131
                if (res == CheckResult.FAIL) {
3✔
2132
                        return false;
2✔
2133
                } else if (res == CheckResult.MATCH) {
3!
2134
                        conditionalMapToKey(bnd, candidate);
×
2135
                        return true;
×
2136
                }
2137

2138
                // check owner
2139
                // <anArray>.length field?
2140
                if (name.equals("length")) {
4!
2141
                        boolean isArrayLengthField = ((bnd != null) && (bnd.getDeclaringClass() == null)) ||
×
2142
                                                                                 ((bnd == null) && (owner.getName().equals(EntityDictionary.ARRAYS_NAME)));
×
2143
                        if (isArrayLengthField) {
×
2144
                                if (candidateOwner.getName().equals(EntityDictionary.ARRAYS_NAME)) {
×
2145
                                        conditionalMapToKey(bnd, candidate);
×
2146
                                        return true;
×
2147
                                }
2148
                                else {
2149
                                        return false;
×
2150
                                }
2151
                        }
2152
                }
2153

2154
                // check owner
2155
                // "normal" field?
2156
                res = matchAndMapOwnerAsType( ((bnd != null) ? bnd.getDeclaringClass() : null), owner, candidateOwner);
11✔
2157
                if (res == CheckResult.MATCH) {
3!
2158
                        conditionalMapToKey(bnd, candidate);
4✔
2159
                        return true;
2✔
2160
                }
2161
                return false;
×
2162
        }
2163

2164
        /**
2165
         * Checks whether the existing unmapped Famix Type's parent (or owner) matches the binding's owner.
2166
         * Checks that the candidate has the same name as the JDT bound type, and checks recursively that owners also match.
2167
         * @param bnd -- a JDT binding whose owner we are trying to match to the candidate's owner
2168
         * @param owner -- the owner of the type
2169
         * @param candidate -- a Famix Entity
2170
         * @return whether we found a match (if <b>true</b>, the mapping is recorded)
2171
         */
2172
        private boolean matchAndMapTypeOwner(ITypeBinding bnd, TNamedEntity owner, Type candidate) {
2173
                ContainerEntity candidateOwner = Util.getOwner(candidate);
4✔
2174

2175
                // owner is a Method? (for example in case of an anonymous class)
2176
                CheckResult res = matchAndMapOwnerAsMethod(((bnd != null) ? bnd.getDeclaringMethod() : null), owner, candidate);
11✔
2177
                if (res == CheckResult.MATCH) {
3!
2178
                        conditionalMapToKey(bnd, candidate);
×
2179
                        return true;
×
2180
                } else if (res == CheckResult.FAIL) {
3!
2181
                        return false;
×
2182
                }
2183

2184
                // owner is a class ?
2185
                res = matchAndMapOwnerAsType(((bnd != null) ? bnd.getDeclaringClass() : null), owner, candidateOwner);
11✔
2186
                if (res == CheckResult.MATCH) {
3✔
2187
                        conditionalMapToKey(bnd, candidate);
4✔
2188
                        return true;
2✔
2189
                }
2190
                else if (res == CheckResult.FAIL) {
3✔
2191
                        return false;
2✔
2192
                }
2193

2194
                // owner must be a package
2195
                if (matchAndMapOwnerAsNamespace( ((bnd != null)?bnd.getPackage():null), owner, candidateOwner) == CheckResult.MATCH) {
12✔
2196
                        conditionalMapToKey(bnd, candidate);
4✔
2197
                        return true;
2✔
2198
                }
2199
                return false;
2✔
2200
        }
2201

2202
        /**
2203
         * Check whether the owner of candidates is a method macthinf either methBnd or owner
2204
         * @param methBnd
2205
         * @param owner
2206
         * @param candidateOwner
2207
         * @return a {@link CheckResult}
2208
         */
2209
        private  <T extends TNamedEntity> CheckResult matchAndMapOwnerAsMethod(IMethodBinding methBnd, T owner, T candidateOwner) {
2210
                if ((methBnd != null) || ((owner != null) && (owner instanceof Method))) {
7!
2211
                        if (!(candidateOwner instanceof Method)) {
3!
2212
                                return CheckResult.FAIL;
×
2213
                        }
2214

2215
                        ContainerEntity ownerOwner = (owner != null) ? (ContainerEntity) Util.getOwner(owner) : null;
7!
2216
                        String ownerSig = (owner != null) ? ((Method) owner).getSignature() : null;
7!
2217
                        Type ownerReturn = (owner != null) ? (Type) ((Method) owner).getDeclaredType() : null;
8!
2218

2219
                        if (matchAndMapMethod(methBnd, ownerSig, ownerReturn, ownerOwner, (Method) candidateOwner)) {
9!
2220
                                return CheckResult.MATCH;
×
2221
                        } else {
2222
                                return CheckResult.FAIL;
2✔
2223
                        }
2224
                }
2225
                return CheckResult.UNDECIDED;
2✔
2226
        }
2227

2228
        /**
2229
         * @param typBnd
2230
         * @param owner
2231
         * @param candidateOwner
2232
         * @return a {@link CheckResult}
2233
         */
2234
        private CheckResult matchAndMapOwnerAsType(ITypeBinding typBnd, TNamedEntity owner, TNamedEntity candidateOwner) {
2235
                if ((typBnd != null) || ((owner != null) && (owner instanceof Type))) {
7!
2236
                        if (!(candidateOwner instanceof Type)) {
3✔
2237
                                return CheckResult.FAIL;
2✔
2238
                        }
2239

2240
                        TNamedEntity ownerOwner = (owner != null) ? Util.getOwner(owner) : null;
6!
2241
                        String ownerName = (owner != null) ? ((Type) owner).getName() : null;
7!
2242

2243
                        if (matchAndMapType(typBnd, ownerName, ownerOwner, candidateOwner)) {
7✔
2244
                                return CheckResult.MATCH;
2✔
2245
                        } else {
2246
                                return CheckResult.FAIL;
2✔
2247
                        }
2248
                }
2249
                return CheckResult.UNDECIDED;
2✔
2250
        }
2251

2252
        private CheckResult matchAndMapOwnerAsNamespace(IPackageBinding pckgBnd, TNamedEntity owner, ContainerEntity candidateOwner) {
2253
                if ((pckgBnd != null) || ((owner != null) && (owner instanceof Package))) {
7!
2254
                        if (!(candidateOwner instanceof Package)) {
3!
2255
                                return CheckResult.FAIL;
×
2256
                        }
2257

2258
                        Package ownerOwner = (owner != null) ? (Package) Util.getOwner(owner) : null;
7!
2259
                        String ownerName = (owner != null) ? ((Package) owner).getName() : null;
7!
2260

2261
                        if (matchAndMapPackage(pckgBnd, ownerName, ownerOwner, candidateOwner)) {
7✔
2262
                                return CheckResult.MATCH;
2✔
2263
                        } else {
2264
                                return CheckResult.FAIL;
2✔
2265
                        }
2266
                }
2267
                return CheckResult.UNDECIDED;
×
2268
        }
2269

2270
        /**
2271
         * Checks whether the name and the candidate matches the name of the entity (given either by 'bnd' or 'name')<br>
2272
         * 'name' and 'bnd' cannot be null together
2273
         * @param bnd -- binding associated with the entity may be null
2274
         * @param name -- name of the entity may be null
2275
         * @param candidate
2276
         * @return true if names match, false if not
2277
         */
2278
        private CheckResult checkNameMatch(IBinding bnd, String name, TNamedEntity candidate) {
2279
                if ( (bnd != null) && (! bnd.getName().equals(candidate.getName())) ) {
8!
2280
                        return CheckResult.FAIL;
×
2281
                }
2282
                else if ( (bnd == null) && (name != null) && (! name.equals(candidate.getName())) ) {
9!
2283
                        return CheckResult.FAIL;
2✔
2284
                }
2285
                else {
2286
                        return CheckResult.MATCH;
2✔
2287
                }
2288
        }
2289

2290
        /**
2291
         * Check whether key and candidate are already bound together, whether either is bound to something else, or whether none is bound
2292
         * @param key
2293
         * @param candidate
2294
         * @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>
2295
         */
2296
        private CheckResult checkKeyMatch(IBinding key, TNamedEntity candidate) {
2297
                if (key == null) {
2✔
2298
                        return CheckResult.UNDECIDED;
2✔
2299
                }
2300

2301
                NamedEntity bound = (NamedEntity)getEntityByKey(key);
5✔
2302
                if (bound == candidate) {
3✔
2303
                        return CheckResult.MATCH;
2✔
2304
                }
2305
                else if (bound != null) {
2✔
2306
                        return CheckResult.FAIL;
2✔
2307
                }
2308
                else if (getEntityKey(candidate) != null) {
4✔
2309
                        // candidate already bound, and not to this binding
2310
                        return CheckResult.FAIL;
2✔
2311
                }
2312
                else {
2313
                        return CheckResult.UNDECIDED;
2✔
2314
                }
2315
        }
2316

2317
        private void conditionalMapToKey(IBinding bnd, TNamedEntity ent) {
2318
                if (bnd != null) {
2✔
2319
                        mapEntityToKey(bnd, ent);
4✔
2320
                }
2321
        }
1✔
2322

2323
        public Method ensureFamixMethod(IMethodBinding bnd) {
2324
                return ensureFamixMethod(
9✔
2325
                                bnd,
2326
                                /*name*/null,
2327
                                /*paramsType*/(Collection<String>)null,
2328
                                /*returnType*/null,
2329
                                /*owner*/null,
2330
                                (bnd == null) ? UNKNOWN_MODIFIERS : bnd.getModifiers());
6✔
2331
        }
2332

2333
        /**
2334
         * Returns a Famix Method associated with the IMethodBinding. The Entity is created if it does not exist.
2335
         * The Entity is created if it does not exist.
2336
         * @param name -- the name of the FAMIX Method (MUST NOT be null, but this is not checked)
2337
         * @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)
2338
         * @param owner -- type defining the method (should not be null, but it will work if it is)
2339
         * @return the Famix Entity found or created. May return null if "bnd" is null or in case of a Famix error
2340
         */
2341
        public Method ensureFamixMethod(IMethodBinding bnd, String name, Collection<String> paramTypes, TType ret, TWithMethods owner, int modifiers) {
2342
                Method fmx = null;
2✔
2343
                String sig;
2344
                boolean delayedRetTyp;
2345

2346
                // --------------- to avoid useless computations if we can
2347
                fmx = (Method)getEntityByKey(bnd);
5✔
2348
                if (fmx != null) {
2✔
2349
                        return fmx;
2✔
2350
                }
2351

2352
                // --------------- name
2353
                if (name == null) {
2✔
2354
                        if (bnd == null) {
2✔
2355
                                return null;
2✔
2356
                        }
2357
                        else {
2358
                                name = bnd.getName();
3✔
2359
                        }
2360
                }
2361

2362
                // --------------- signature
2363
                sig = name + "(";
3✔
2364
                 if (bnd != null) {
2✔
2365
                    sig += signatureParamsFromBinding(bnd);
7✔
2366
                }
2367
        else if (paramTypes != null) {
2!
2368
                        sig += signatureParamsFromStringCollection(paramTypes);
7✔
2369
                }
2370
                else {
2371
                        sig += "???";
×
2372
                }
2373
                sig += ")";
3✔
2374

2375
                // --------------- return type
2376
                delayedRetTyp = false;
2✔
2377
                ITypeBinding retTypBnd = null;
2✔
2378
                if (ret == null) {
2!
2379
                        if (bnd == null) {
2✔
2380
                                ret = null;  // what else ?
3✔
2381
                        }
2382
                        else {
2383
                                if (bnd.isConstructor()) {
3✔
2384
                                        ret = null;
3✔
2385
                                }
2386
                                else {
2387

2388
                                        // must create the return type
2389
                                        // but for method like "<T> T mtd()" where T belongs to mtd and mtd returns T,
2390
                                        // we need T to create the method and the method to create T ...
2391
                                        // so we need to test the situation and deal with it
2392
                                        retTypBnd = bnd.getReturnType();
3✔
2393
                                        if (retTypBnd == null) {
2✔
2394
                                                ret = null;
3✔
2395
                                        }
2396
                                        else if (retTypBnd.isArray()) {
3✔
2397
                                                retTypBnd = retTypBnd.getElementType();
3✔
2398
                                        }
2399

2400
                                        if ( (retTypBnd != null) && retTypBnd.isTypeVariable() && (retTypBnd.getDeclaringMethod() == bnd) ) {
9✔
2401
                                                ret = null;
2✔
2402
                                                delayedRetTyp = true;
3✔
2403
                                        }
2404
                                        else {
2405
                                                ret = this.ensureFamixType(retTypBnd,(TWithTypes) /*ctxt*/owner);
6✔
2406
                                        }
2407
                                }
2408
                        }
2409
                }
2410

2411
                // --------------- owner
2412
                if (owner == null) {
2✔
2413
                        if (bnd == null) {
2✔
2414
                                owner = ensureFamixClassStubOwner();
4✔
2415
                        }
2416
                        else {
2417
                                ITypeBinding classBnd = bnd.getDeclaringClass().getErasure();
4✔
2418
                                if (classBnd != null) {
2!
2419
                                        TType tmpOwn = ensureFamixType(classBnd);
4✔
2420

2421
                                        owner = (TWithMethods) tmpOwn;
3✔
2422
                                        
2423
                                }
1✔
2424
                                else {
2425
                                        owner = ensureFamixClassStubOwner();
×
2426
                                }
2427
                        }
2428
                }
2429

2430
                // --------------- recover from name ?
2431
                for (Method candidate : this.getEntityByName(Method.class, name)) {
13✔
2432
                        if (matchAndMapMethod(bnd, sig, ret, (TNamedEntity) owner, candidate)) {
9✔
2433
                                fmx = candidate;
2✔
2434
                                break;
1✔
2435
                        }
2436
                }
1✔
2437

2438
                if (fmx == null) {
2✔
2439
                        if(bnd != null && bnd.isGenericMethod()) {
5✔
2440
                                fmx = ensureFamixEntity(ParametricMethod.class, bnd, name);
7✔
2441
                                for(ITypeBinding param: bnd.getTypeParameters()) {
18✔
2442
                                        TypeParameter fmxParam = this.ensureFamixTypeParameter(param, null, (TWithTypes)fmx);
6✔
2443
                                        fmxParam.setGenericEntity((ParametricMethod)fmx);
4✔
2444
                                }
2445
                        // parameterized method binding = when the method is the target of an invocation.
2446
                        } else if (bnd != null && bnd.isParameterizedMethod()) {
5!
NEW
2447
                                fmx = (ParametricMethod)this.ensureFamixMethod(bnd.getMethodDeclaration());
×
2448
                        }else{
2449
                                fmx = ensureFamixEntity(Method.class, bnd, name);
7✔
2450
                        }
2451
                        
2452
                        fmx.setSignature(sig);
3✔
2453
                        ITypeBinding returnTypeBnd = (bnd == null) ? null : bnd.getReturnType();
7✔
2454
                        ensureFamixEntityTyping(returnTypeBnd, fmx, ret);
6✔
2455
                        fmx.setParentType(owner);
3✔
2456
                }
2457

2458
                if (fmx != null) {
2!
2459
                        setMethodModifiers(fmx, modifiers);
4✔
2460
                        // if it's a constructor
2461
                        if (fmx.getName().equals(Util.getOwner(fmx).getName())) {
7✔
2462
                                fmx.setKind(CONSTRUCTOR_KIND_MARKER);
3✔
2463
                        }
2464
                }
2465

2466
                if ((fmx != null) && delayedRetTyp) {
4!
2467
                        int retTypModifiers = (retTypBnd != null) ? retTypBnd.getModifiers() : UNKNOWN_MODIFIERS;
6!
2468
                        ITypeBinding returnTypeBnd = (bnd == null) ? null : bnd.getReturnType();
5!
2469
                        ensureFamixEntityTyping(returnTypeBnd, fmx, this.ensureFamixType(retTypBnd, /*name*/null, /*owner*/fmx, /*ctxt*/(ContainerEntity) owner, retTypModifiers));
13✔
2470
                }
2471

2472
                return fmx;
2✔
2473
        }
2474

2475
        /**
2476
         * Creates or recovers a stub Famix Method
2477
         * @param name of the method
2478
         * @return the Famix Method
2479
         */
2480
        public Method ensureFamixStubMethod(String name) {
2481
                return ensureFamixMethod(null, name, /*paramType*/(Collection<String>)null, /*returnType*/null, ensureFamixClassStubOwner(), /*modifiers*/0);  // cast needed to desambiguate the call
×
2482
        }
2483

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

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

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

2501
        public void setInterfaceModifiers(Interface fmx, int mod) {
2502
                setCommonModifiers(fmx, mod);
4✔
2503
        }
1✔
2504

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

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

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

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

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

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

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

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

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

2594
                return fmx;
2✔
2595
        }
2596

2597
        public Attribute ensureFamixAttribute(IVariableBinding bnd, String name, TWithAttributes owner) {
2598
                return ensureFamixAttribute(bnd, name, /*declared type*/null, owner);
7✔
2599
        }
2600

2601
        /**
2602
         * helper method, we know the var exists, ensureFamixAttribute will recover it
2603
         */
2604
        public Attribute getFamixAttribute(IVariableBinding bnd, String name, TWithAttributes owner) {
2605
                return ensureFamixAttribute(bnd, name, /*declared type*/null, owner);
7✔
2606
        }
2607

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

2616
                // --------------- to avoid useless computations if we can
2617
                try {
2618
                        fmx = (Parameter)getEntityByKey(bnd);
5✔
2619

2620
                }catch(Throwable e) {
×
2621
                        e.printStackTrace();
×
2622
                }
1✔
2623
                if (fmx != null) {
2✔
2624
                        return fmx;
2✔
2625
                }
2626

2627
                // --------------- name
2628
                if (name == null) {
2!
2629
                        if (bnd == null) {
×
2630
                                return null;
×
2631
                        }
2632
                        else {
2633
                                name = bnd.getName();
×
2634
                        }
2635
                }
2636

2637
                // --------------- owner
2638
                if (tMethod == null) {
2!
2639
                        if (bnd == null) {
×
2640
                                tMethod = ensureFamixStubMethod("<"+name+"_owner>");
×
2641
                        }
2642
                        else {
2643
                                tMethod = ensureFamixMethod(bnd.getDeclaringMethod());
×
2644
                        }
2645
                }
2646

2647
                // --------------- recover from name ?
2648
                for (Parameter candidate : getEntityByName(Parameter.class, name) ) {
13✔
2649
                        if ( matchAndMapVariable(bnd, name, tMethod, candidate) ) {
7!
2650
                                fmx = candidate;
×
2651
                                break;
×
2652
                        }
2653
                }
1✔
2654

2655
                if (fmx == null) {
2!
2656
                        fmx = ensureFamixEntity(Parameter.class, bnd, name);
7✔
2657
                        fmx.setParentBehaviouralEntity(tMethod);
3✔
2658
                }
2659

2660
                if (fmx != null) {
2!
2661
                        fmx.setParentBehaviouralEntity(tMethod);
3✔
2662
                        ITypeBinding declaredTypeBnd = (bnd == null) ? null : bnd.getType();
5!
2663
                        ensureFamixEntityTyping(declaredTypeBnd, fmx, typ);
6✔
2664
                }
2665

2666
                return fmx;
2✔
2667
        }
2668

2669
        /**
2670
         * helper method, we know the var exists, ensureFamixParameter will recover it
2671
         */
2672
        public Parameter getFamixParameter(IVariableBinding bnd, String name, TMethod tMethod) {
2673
                return ensureFamixParameter(bnd, name, /*declared type*/null, tMethod);
×
2674
        }
2675

2676
        /**
2677
         * Returns a Famix LocalVariable associated with the IVariableBinding.
2678
         * The Entity is created if it does not exist.<br>
2679
         * @param name -- the name of the FAMIX LocalVariable
2680
         * @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
2681
         */
2682
        public LocalVariable ensureFamixLocalVariable(IVariableBinding bnd, String name, TWithLocalVariables owner) {
2683
                LocalVariable fmx = null;
2✔
2684

2685
                // --------------- to avoid useless computations if we can
2686
                fmx = (LocalVariable)getEntityByKey(bnd);
5✔
2687
                if (fmx != null) {
2!
2688
                        return fmx;
×
2689
                }
2690

2691
                // --------------- name
2692
                if (name == null) {
2!
2693
                        if (bnd == null) {
×
2694
                                return null;
×
2695
                        }
2696
                        else {
2697
                                name = bnd.getName();
×
2698
                        }
2699
                }
2700

2701
                // --------------- owner
2702
                if (owner == null) {
2!
2703
                        if (bnd == null) {
×
2704
                                return null;  // what would be the interest of a local variable for which we ignore the declaring method?
×
2705
                        }
2706
                        else {
2707
                                owner = ensureFamixMethod(bnd.getDeclaringMethod());
×
2708
                        }
2709
                }
2710

2711
                // --------------- recover from name ?
2712
                for (LocalVariable candidate : getEntityByName(LocalVariable.class, name) ) {
13✔
2713
                        if ( matchAndMapVariable(bnd, name, (TNamedEntity) owner, candidate) ) {
8!
2714
                                fmx = candidate;
×
2715
                                break;
×
2716
                        }
2717
                }
1✔
2718

2719
                if (fmx == null) {
2!
2720
                        fmx = ensureFamixEntity(LocalVariable.class, bnd, name);
7✔
2721
                        fmx.setParentBehaviouralEntity(owner);
3✔
2722
                }
2723

2724
                if (fmx != null) {
2!
2725
                        // we just created it or it was not bound, so we make sure it has the right information in it
2726
                        fmx.setParentBehaviouralEntity(owner);
3✔
2727
                }
2728

2729
                return fmx;
2✔
2730
        }
2731

2732
        /**
2733
         * helper method, we know the var exists, ensureFamixLocalVariable will recover it
2734
         */
2735
        public LocalVariable getFamixLocalVariable(IVariableBinding bnd, String name, TWithLocalVariables owner) {
2736
                return ensureFamixLocalVariable(bnd, name, owner);
×
2737
        }
2738

2739
        /**
2740
         * Returns a FAMIX ImplicitVariable with the given <b>name</b> ("self" or "super") and corresponding to the <b>type</b>.
2741
         * If this ImplicitVariable does not exist yet, it is created
2742
         * @param name -- the name of the FAMIX ImplicitVariable (should be Dictionary.SELF_NAME or Dictionary.SUPER_NAME)
2743
         * @param type -- the Famix Type for this ImplicitVariable (should not be null)
2744
         * @param tMethod -- the ContainerEntity where the implicit variable appears (should be a method inside <b>type</b>)
2745
         * @return the FAMIX ImplicitVariable or null in case of a FAMIX error
2746
         */
2747
        public ImplicitVariable ensureFamixImplicitVariable(IBinding key, String name, TType type, TMethod tMethod) {
2748
                ImplicitVariable fmx;
2749
                fmx = ensureFamixEntity(ImplicitVariable.class, key, name);
7✔
2750
                fmx.setParentBehaviouralEntity(tMethod);
3✔
2751
                return fmx;
2✔
2752
        }
2753

2754
        public ImplicitVariable ensureFamixImplicitVariable(String name, TType tType, TMethod tMethod) {
2755
                IBinding bnd = ImplicitVarBinding.getInstance(tMethod, name);
4✔
2756
                return ensureFamixImplicitVariable(bnd, name, tType, tMethod);
7✔
2757
        }
2758

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

2768
                if ( (jCmt != null) && (owner != null) ) {
4!
2769
                        
2770
                        cmt = new Comment();
4✔
2771
                        addSourceAnchor(cmt, jCmt);
5✔
2772
                        famixRepoAdd(cmt);
3✔
2773
                        cmt.setCommentedEntity(owner);
3✔
2774
                }
2775

2776
                return cmt;
2✔
2777
        }
2778

2779
        /**
2780
         * Creates and returns a Famix Comment and associates it with an Entity
2781
         * @param jCmt -- the content (String) of the comment 
2782
         * @param owner -- the entity that is commented
2783
         * @param content -- the text of the comment
2784
         * @return the Famix Comment
2785
         */
2786
        public Comment createFamixComment(org.eclipse.jdt.core.dom.Comment jCmt, TWithComments owner, String content) {
2787
                Comment cmt = null;
2✔
2788

2789
                if ( (jCmt != null) && (owner != null) ) {
4!
2790
                        cmt = new Comment();
4✔
2791
                        cmt.setContent(content );
3✔
2792
                        famixRepoAdd(cmt);
3✔
2793
                        cmt.setCommentedEntity(owner);
3✔
2794
                }
2795

2796
                return cmt;
2✔
2797
        }
2798

2799
        /**
2800
         * Adds location information to a Famix Entity.
2801
         * 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.
2802
         * This method also creates some basic links between the entity and others (e.g. declaring container, return type, ...)
2803
         * @param fmx -- Famix Entity to add the anchor to
2804
         * @param node -- JDT ASTNode, where the information is extracted
2805
         * @return the Famix SourceAnchor added to fmx. May be null in case of incorrect parameter ('fmx' or 'ast' == null)
2806
         */
2807
        public SourceAnchor addSourceAnchor(TSourceEntity fmx, ASTNode node) {
2808
                IndexedFileAnchor fa = null;
2✔
2809

2810
                fa = createIndexedFileAnchor(node);
4✔
2811
                if ((fmx != null) && (fa != null)) {
4!
2812
                        fmx.setSourceAnchor(fa);
3✔
2813
                        famixRepoAdd(fa);
3✔
2814
                }
2815

2816
                return fa;
2✔
2817
        }
2818

2819
        /**
2820
         * Special case of  {@linkplain #addSourceAnchor(TSourceEntity, ASTNode)} to add location information to a Famix Method.
2821
         */
2822
        public SourceAnchor addSourceAnchor(Method fmx, MethodDeclaration node) {
2823
                IndexedFileAnchor fa = null;
2✔
2824

2825
                fa = createIndexedFileAnchor(node);
4✔
2826
                if ((fmx != null) && (fa != null)) {
4!
2827

2828
                        // may change the positions
2829
                        List<ASTNode> methodDeclarationModifiers = new ArrayList<>();
4✔
2830
                        methodDeclarationModifiers.addAll(node.modifiers());
5✔
2831
                        if (node.getName() != null) {
3!
2832
                                methodDeclarationModifiers.add(node.getName());
5✔
2833
                        }
2834
                        if (node.getReturnType2() != null) {
3✔
2835
                                methodDeclarationModifiers.add(node.getReturnType2());
5✔
2836
                        }
2837
                        int beg = (methodDeclarationModifiers.stream().mapToInt(el -> el.getStartPosition()).min().getAsInt()) + 1;
12✔
2838
                        int end = node.getStartPosition() + node.getLength();
6✔
2839

2840
                        ((IndexedFileAnchor)fa).setStartPos(beg);
4✔
2841
                        ((IndexedFileAnchor)fa).setEndPos(end);
4✔
2842

2843
                        fmx.setSourceAnchor(fa);
3✔
2844
                        famixRepoAdd(fa);
3✔
2845
                }
2846

2847
                return fa;
2✔
2848
        }
2849

2850
        /**
2851
         * Gets the file name holding <code>node</code> and its start and end positions in the file.
2852
         * Information returned in the form of an IndexedFileAnchor
2853
         */
2854
        protected IndexedFileAnchor createIndexedFileAnchor(ASTNode node) {
2855
                IndexedFileAnchor fa;
2856
                
2857
                if (node == null) {
2!
2858
                        return null;
×
2859
                }
2860

2861
                // position in source file
2862
                int beg = node.getStartPosition() + 1; // Java starts at 0, Moose at 1
5✔
2863
                int end = beg + node.getLength() - 1;
7✔
2864

2865
                // find source Compilation Unit
2866
                // there is a special case for the JDT Comment Nodes
2867
                if (node instanceof org.eclipse.jdt.core.dom.Comment) {
3✔
2868
                        node = ((org.eclipse.jdt.core.dom.Comment) node).getAlternateRoot();
5✔
2869
                } else {
2870
                        node = node.getRoot();
3✔
2871
                }
2872

2873
                fa = new IndexedFileAnchor();
4✔
2874
                ((IndexedFileAnchor)fa).setStartPos(beg);
4✔
2875
                ((IndexedFileAnchor)fa).setEndPos(end);
4✔
2876

2877
                fa.setFileName((String) ((CompilationUnit)node).getProperty(SOURCE_FILENAME_PROPERTY));
7✔
2878

2879
                return fa;
2✔
2880
        }
2881

2882
        /**
2883
         * Creates or recovers the Famix Class for "Object".
2884
         * Because "Object" is the root of the inheritance tree, it needs to be treated differently.
2885
         *
2886
         * @param bnd -- a potential binding for the java "Object" class
2887
         * @return a Famix class for "Object"
2888
         */
2889
        public Class ensureFamixClassObject(ITypeBinding bnd) {
2890
                Class fmx = ensureFamixUniqEntity(Class.class, bnd, OBJECT_NAME);
7✔
2891

2892
                if (fmx != null) {
2!
2893
                        fmx.setTypeContainer(ensureFamixPackageJavaLang(null));
5✔
2894
                }
2895
                // Note: "Object" has no superclass
2896

2897
                return fmx;
2✔
2898
        }
2899

2900
        /**
2901
         * Ensures the Java meta-class: <pre>{@code java.lang.Class<>}</pre>
2902
         */
2903
        public Class ensureFamixMetaClass(ITypeBinding bnd) {
2904
                Package javaLang = ensureFamixPackageJavaLang((bnd == null) ? null : bnd.getPackage());
7!
2905
                ParametricClass fmx = (ParametricClass) this.ensureFamixClass(null, METACLASS_NAME, javaLang, /*isGeneric*/true, Modifier.PUBLIC & Modifier.FINAL);
9✔
2906

2907
                if (fmx != null) {
2!
2908
                        fmx.addTypeParameters(ensureFamixTypeParameter(null, "T", fmx));
7✔
2909
                }
2910

2911
                if ((fmx != null) && (fmx.getSuperInheritances() == null)) {
5!
NEW
2912
                        ensureFamixInheritance(ensureFamixClassObject(null), fmx, null, null);
×
2913
                }
2914

2915
                return fmx;
2✔
2916
        }
2917

2918
        public Class getFamixMetaClass(ITypeBinding bnd) {
2919
                Package javaLang = ensureFamixPackageJavaLang((bnd == null) ? null : bnd.getPackage());
7!
2920
                return this.ensureFamixClass(null, METACLASS_NAME, javaLang, /*isGeneric*/true, UNKNOWN_MODIFIERS);
8✔
2921
        }
2922

2923
        /**
2924
         * Creates or recovers the Famix Class for all arrays (<pre>{@code <some-type> []}</pre>)
2925
         * In java arrays or objects of special classes (i.e. "I[" for an array of int).
2926
         * JDT does not create a binding for these classes, so we create a stub one here.
2927
         *
2928
         * @return a Famix class
2929
         */
2930
        public Class ensureFamixClassArray() {
2931
                Class fmx = ensureFamixUniqEntity(Class.class, null, ARRAYS_NAME);
×
2932
                if (fmx != null) {
×
NEW
2933
                        ensureFamixInheritance(ensureFamixClassObject(null), fmx, /*prev*/null, null);
×
2934
                        fmx.setTypeContainer(ensureFamixPackageDefault());
×
2935

2936
                        // may be not needed anymore now that we use modifiers
2937
                        /*fmx.setIsAbstract(Boolean.FALSE);
2938
                        fmx.setIsFinal(Boolean.FALSE);
2939
                        fmx.setIsInterface(Boolean.FALSE); 
2940
                        fmx.setIsPrivate(Boolean.FALSE);
2941
                        fmx.setIsProtected(Boolean.FALSE);*/
2942
                        fmx.setVisibility(MODIFIER_PUBLIC);
×
2943
                }
2944

2945
                return fmx;
×
2946
        }
2947

2948
        public String removeLastPartOfPackageName(String qualifiedName) {
2949
                String ret = null;
2✔
2950
                int last = qualifiedName.lastIndexOf('.');
4✔
2951
                if (last > 0) {
2✔
2952
                        // recursively creating the parent
2953
                        ret = qualifiedName.substring(0, last);
6✔
2954
                }
2955
                else {
2956
                        ret = "";
2✔
2957
                }
2958

2959
                return ret;
2✔
2960
        }
2961

2962
        /** Generates the list of parameters for a method signature
2963
         * @return a string
2964
         */
2965
        protected String signatureParamsFromBinding(IMethodBinding bnd) {
2966
                boolean first = true;
2✔
2967
                String sig = new String();
4✔
2968

2969
                for (ITypeBinding parBnd : bnd.getParameterTypes()) {
17✔
2970
                        if (first) {
2✔
2971
                                sig = parBnd.getName();
3✔
2972
                                first = false;
3✔
2973
                        }
2974
                        else {
2975
                                sig += "," + parBnd.getName();
5✔
2976
                        }
2977
                }
2978
                return sig;
2✔
2979
        }
2980

2981
        private String signatureParamsFromStringCollection(Collection<String> paramTypes) {
2982
                boolean first = true;
2✔
2983
                String sig = new String();
4✔
2984

2985
                for (String t : paramTypes) {
10✔
2986
                        if (first) {
2✔
2987
                                sig = t;
2✔
2988
                                first = false;
3✔
2989
                        }
2990
                        else {
2991
                                sig += "," + t;
4✔
2992
                        }
2993
                }
1✔
2994
                return sig;
2✔
2995
        }
2996

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