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

moosetechnology / VerveineJ / 20965481458

13 Jan 2026 05:03PM UTC coverage: 51.15% (-0.06%) from 51.207%
20965481458

push

github

web-flow
Merge pull request #186 from moosetechnology/fix-parameters-typing

Fix parameters typing

1916 of 3940 branches covered (48.63%)

Branch coverage included in aggregate %.

6 of 7 new or added lines in 1 file covered. (85.71%)

12 existing lines in 1 file now uncovered.

4268 of 8150 relevant lines covered (52.37%)

2.11 hits per line

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

82.65
/app/src/main/java/fr/inria/verveine/extractor/java/visitors/GetVisitedEntityAbstractVisitor.java
1
package fr.inria.verveine.extractor.java.visitors;
2

3
import fr.inria.verveine.extractor.java.EntityDictionary;
4
import fr.inria.verveine.extractor.java.VerveineJOptions;
5
import fr.inria.verveine.extractor.java.utils.EntityStack;
6
import fr.inria.verveine.extractor.java.utils.StubBinding;
7
import fr.inria.verveine.extractor.java.utils.Util;
8
import org.eclipse.jdt.core.dom.*;
9
import org.moosetechnology.model.famix.famixjavaentities.*;
10
import org.moosetechnology.model.famix.famixjavaentities.Exception;
11
import org.moosetechnology.model.famix.famixjavaentities.Package;
12
import org.moosetechnology.model.famix.famixtraits.TMethod;
13
import org.moosetechnology.model.famix.famixtraits.TType;
14
import org.moosetechnology.model.famix.famixtraits.TWithMethods;
15
import org.moosetechnology.model.famix.famixtraits.TWithTypes;
16

17
import java.util.ArrayList;
18
import java.util.Collection;
19
import java.util.List;
20
import java.util.Stack;
21

22
/**
23
 * This visitor offers methods to recover FamixEntities from the main JDT Entities and may put them in the context Stack<br>
24
 * To avoid confusion with the ASTVisitor defaults visit(...) methods, and to simplify the choice of using them or not in each subclass,
25
 * the recovering methods here are named after the visited entities: visit(CompilationUnit node) becomes visitCompilationUnit(CompilationUnit node).
26
 */
27
public abstract class GetVisitedEntityAbstractVisitor extends ASTVisitor {
28

29
        /**
30
         * The options that control the behavior of the parser
31
         */
32
        protected VerveineJOptions options;
33

34
        /**
35
         * A stack that keeps the current definition context (package/class/method)
36
         */
37
        protected EntityStack context;
38

39
        /** 
40
         * A dictionary allowing to recover created FAMIX Entities
41
         */
42
        protected EntityDictionary dico;
43

44
        /**
45
         * The super type of an anonymous declaration is only available (without resorting to bindings) when 
46
         * we are in its parent node: a ClassInstanceCreation.
47
         * So we must keep this type from the visit(ClassInstanceCreation) to be used in visit(AnonymousClassDeclaration).<br>
48
         * Note that in some special cases one can also have an anonymous class definition without specifying its superclass.
49
         */
50
        protected Stack<String> anonymousSuperTypeName;
51

52
        public GetVisitedEntityAbstractVisitor(EntityDictionary dico, VerveineJOptions options) {
53
                super();
2✔
54
                this.dico = dico;
3✔
55
                this.options = options;
3✔
56
                this.context = new EntityStack();
5✔
57
                this.anonymousSuperTypeName = new Stack<>();
5✔
58
        }
1✔
59

60
        // a generic visit method for node lists
61
    protected void visitNodeList(List<ASTNode> list) {
62
        for (ASTNode child : list) {
10✔
63
            child.accept(this);
3✔
64
        }
1✔
65
    }
1✔
66

67
        // two visit methods never used
68

69
        @Override
70
        public boolean visit(PackageDeclaration node) {
71
                return false; // no need to visit children of the declaration
2✔
72
        }
73

74
        @Override
75
        public boolean visit(ImportDeclaration node) {
76
                return false; // no need to visit children of the declaration        
2✔
77
        }
78

79
        // CompilationUnit --> FamixNamespace
80

81
        protected Package visitCompilationUnit(CompilationUnit node) {
82
                Package fmx;
83
                PackageDeclaration pckg = node.getPackage();
3✔
84
                if (pckg == null) {
2✔
85
                        fmx = dico.getFamixPackageDefault();
5✔
86
                } else {
87
                        fmx = (Package) dico.getEntityByKey(pckg.resolveBinding());
7✔
88
                }
89
                this.context.pushPckg(fmx);
4✔
90

91
                return fmx;
2✔
92
        }
93

94
        protected void endVisitCompilationUnit(CompilationUnit node) {
95
                this.context.popPckg();
4✔
96
                super.endVisit(node);
3✔
97
        }
1✔
98
        
99
        // type (class/interface) --> FamixClass
100

101
        /*
102
         * Can only be a class or interface declaration
103
         * Local type: see comment of visit(ClassInstanceCreation node)
104
         */
105
        protected TType visitTypeDeclaration(TypeDeclaration node) {
106
                ITypeBinding bnd = (ITypeBinding) StubBinding.getDeclarationBinding(node);
4✔
107
                TType fmx;
108
                if(bnd.isInterface()) {
3✔
109
                        fmx = dico.getFamixInterface(bnd, node.getName().getIdentifier(), (ContainerEntity) /*owner*/context.top());
13✔
110
                } else if (dico.isThrowable(bnd)) {
5✔
111
                        fmx = dico.getFamixException(bnd, node.getName().getIdentifier(), (TWithTypes) /*owner*/context.top());
13✔
112
                } else {
113
                        fmx = dico.getFamixClass(bnd, node.getName().getIdentifier(), /*owner*/context.top());
11✔
114
                }
115
                if (fmx != null) {
2!
116
                        this.context.pushType(fmx);
4✔
117
                }
118
                return fmx;
2✔
119
        }
120

121
        protected void endVisitTypeDeclaration(TypeDeclaration node) {
122
                if (context.topType() instanceof org.moosetechnology.model.famix.famixjavaentities.Class || context.topType() instanceof Interface || context.topType() instanceof Exception) {
10!
123
                        context.pop();
4✔
124
                }
125
                super.endVisit(node);
3✔
126
        }
1✔
127

128
        /**
129
         * Creation of an instance of an anonymous class, ie. <code>new AnonymousClassDeclaration</code><br>
130
         * See also field {@link #anonymousSuperTypeName}
131
         * 
132
         * The 'push' possibly done here gets undone in endVisitAnonymousClassDeclaration()
133
         */
134
        protected void possiblyAnonymousClassDeclaration(ClassInstanceCreation node) {
135
                if (node.getAnonymousClassDeclaration() != null) {
3✔
136
                        anonymousSuperTypeName.push(Util.jdtTypeName(node.getType()));
7✔
137
                }
138
        }
1✔
139

140
        /**
141
         * The body of an anonymous class declaration appears within a ClassInstanceCreation<br>
142
         * See also field {@link GetVisitedEntityAbstractVisitor#anonymousSuperTypeName}
143
         */
144
        protected org.moosetechnology.model.famix.famixjavaentities.Class visitAnonymousClassDeclaration(AnonymousClassDeclaration node) {
145
                org.moosetechnology.model.famix.famixjavaentities.Class fmx;
146

147
                ITypeBinding bnd = (ITypeBinding) StubBinding.getDeclarationBinding(node);
4✔
148

149
                fmx = this.dico.getFamixClass(bnd, Util.stringForAnonymousName(getAnonymousSuperTypeName(), context), /*owner*/(ContainerEntity) context.top());
14✔
150
                if (fmx != null) {
2!
151
                        this.context.pushType(fmx);
4✔
152
                }
153
                return fmx;
2✔
154
        }
155

156
        protected void endVisitAnonymousClassDeclaration(AnonymousClassDeclaration node) {
157
                if (context.top() instanceof org.moosetechnology.model.famix.famixjavaentities.Class) {
5✔
158
                        context.pop();
4✔
159
                }
160
                if (!anonymousSuperTypeName.empty()) {
4✔
161
                        anonymousSuperTypeName.pop();
4✔
162
                }
163
        }
1✔
164

165
        protected org.moosetechnology.model.famix.famixjavaentities.Enum visitEnumDeclaration(EnumDeclaration node) {
166
                ITypeBinding bnd = (ITypeBinding) StubBinding.getDeclarationBinding(node);
4✔
167

168
                org.moosetechnology.model.famix.famixjavaentities.Enum fmx = dico.getFamixEnum(bnd, node.getName().getIdentifier(), (TWithTypes) context.top());
12✔
169
                if (fmx != null) {
2!
170
                        this.context.pushType(fmx);
4✔
171
                }
172
                return fmx;
2✔
173
        }
174

175
        protected void endVisitEnumDeclaration(EnumDeclaration node) {
176
                if (context.top() instanceof org.moosetechnology.model.famix.famixjavaentities.Enum) {
5✔
177
                        this.context.popType();
4✔
178
                }
179
                super.endVisit(node);
3✔
180
        }
1✔
181

182
        protected AnnotationType visitAnnotationTypeDeclaration(AnnotationTypeDeclaration node) {
183
        ITypeBinding bnd = (ITypeBinding) StubBinding.getDeclarationBinding(node);
4✔
184

185
                AnnotationType fmx = dico.getFamixAnnotationType(bnd, node.getName().getIdentifier(), (ContainerEntity) context.top());
12✔
186
                if (fmx != null) {
2!
187
                        context.pushType(fmx);
4✔
188
                }
189
                return fmx;
2✔
190
        }
191

192
        protected void endVisitAnnotationTypeDeclaration(AnnotationTypeDeclaration node) {
193
                if (context.topType() instanceof AnnotationType) {
5!
194
                        context.pop();
4✔
195
                }
196
                super.endVisit(node);
3✔
197
        }
1✔
198

199
        @SuppressWarnings("unchecked")
200
        protected Method visitMethodDeclaration(MethodDeclaration node) {
201
                //System.err.println("visitMethodDeclaration(): " + node.getName().getIdentifier());
202
                IMethodBinding bnd = (IMethodBinding) StubBinding.getDeclarationBinding(node);
4✔
203

204
                Collection<String> paramTypes = new ArrayList<>();
4✔
205
                for (SingleVariableDeclaration param : (List<SingleVariableDeclaration>) node.parameters()) {
11✔
206
                        paramTypes.add(Util.jdtTypeName(param.getType()));
6✔
207
                }
1✔
208

209
                Method fmx = dico.ensureFamixMethod(bnd, node.getName().getIdentifier(), paramTypes, /*returnType*/null, (TWithMethods) /*owner*/context.topType(), EntityDictionary.UNKNOWN_MODIFIERS);
15✔
210

211
                context.pushMethod(fmx);  // whether fmx==null or not
4✔
212
                return fmx;
2✔
213
        }
214

215
        protected void endVisitMethodDeclaration(MethodDeclaration node) {
216
                context.popMethod();
4✔
217
                super.endVisit(node);
3✔
218
        }
1✔
219

220
        /**
221
         * BodyDeclaration ::=
222
         *                [ ... ]
223
         *                 FieldDeclaration
224
         *                 Initializer
225
         *                 MethodDeclaration (for methods and constructors)
226
         * Initializer ::=
227
         *      [ static ] Block
228
         */
229
        public Method visitInitializer(Initializer node) {
230
                return ctxtPushInitializerMethod();
3✔
231
        }
232

233
        public void endVisitInitializer(Initializer node) {
234
                if ( (context.top() instanceof Method) && ( context.top().getName().equals(EntityDictionary.INIT_BLOCK_NAME)) ) {
12!
235
                        this.context.pop();
4✔
236
                }
237
                super.endVisit(node);
3✔
238
        }
1✔
239

240
        @SuppressWarnings("unchecked")
241
        public boolean visitEnumConstantDeclaration(EnumConstantDeclaration node) {
UNCOV
242
                boolean hasInitBlock = false;
×
UNCOV
243
                for (Expression expr : (List<Expression>)node.arguments()) {
×
UNCOV
244
                        if (expr != null) {
×
UNCOV
245
                                visitClassMemberInitializer(expr);
×
UNCOV
246
                                hasInitBlock = true;
×
UNCOV
247
                                break;  // we recovered the INIT_BLOCK, no need to look for other declaration
×
248
                        }
249
                }
×
UNCOV
250
                return hasInitBlock;
×
251

252
        }
253

254
        public void endVisitEnumConstantDeclaration(EnumConstantDeclaration node) {
UNCOV
255
                for (Expression expr : (List<Expression>)node.arguments()) {
×
UNCOV
256
                        if (expr != null) {
×
UNCOV
257
                                context.pop();  // pops the EntityDictionary.INIT_BLOCK_NAME method
×
UNCOV
258
                                break;
×
259
                        }
260
                }
×
UNCOV
261
        }
×
262

263
    @SuppressWarnings("unchecked")
264
    public boolean hasInitBlock(FieldDeclaration node) {
265
        boolean hasInitBlock = false;
2✔
266
        for (VariableDeclaration vardecl : (List<VariableDeclaration>)node.fragments() ) {
11✔
267
            if (vardecl.getInitializer() != null) {
3✔
268
                visitClassMemberInitializer(vardecl.getInitializer());
5✔
269
                hasInitBlock = true;
2✔
270
                break;  // we recovered the INIT_BLOCK, no need to look for other declarations
1✔
271
            }
272
        }
1✔
273
        return hasInitBlock;
2✔
274
    }
275

276
        public void endVisitFieldDeclaration(FieldDeclaration node) {
277
                for (VariableDeclaration vardecl : (List<VariableDeclaration>)node.fragments() ) {
11✔
278
                        if (vardecl.getInitializer() != null) {
3✔
279
                                context.pop();  // pops the EntityDictionary.INIT_BLOCK_NAME method
4✔
280
                                break;
1✔
281
                        }
282
                }
1✔
283
        }
1✔
284

285
        /**
286
         * Handles initialization part for FieldDeclaration and EnumConstantDeclaration
287
         *
288
         * VariableDeclarationFragment ::=
289
         *     Identifier { Dimension } [ = Expression ]
290
         */
291
        private Method visitClassMemberInitializer(Expression initializingExpr) {
292
                return ctxtPushInitializerMethod();
3✔
293
        }
294

295
        /**
296
         * Recovers the fake method: {@link EntityDictionary#INIT_BLOCK_NAME}
297
         *
298
         * Used in the case of instance/class initializer and initializing expressions of FieldDeclarations and EnumConstantDeclarations
299
         */
300
        private Method ctxtPushInitializerMethod() {
301
                TType owner = context.topType();
4✔
302
                Method fmx = recoverInitializerMethod((TWithMethods)owner);
5✔
303
                if (fmx == null) {
2✔
304
                        fmx = dico.ensureFamixMethod(null, EntityDictionary.INIT_BLOCK_NAME, /*paramTypes*/new ArrayList<>(), /*returnType*/null, (TWithMethods) owner, EntityDictionary.UNKNOWN_MODIFIERS);
13✔
305
                }
306
                if (fmx != null) {
2!
307
                        context.pushMethod(fmx);
4✔
308
                }
309

310
                return fmx;
2✔
311
        }
312

313
        /**
314
         * Special method to recover the <Initializer> method of a class.
315
         * Cannot do it with ensureFamixMethod because we have no binding, no parameter, no return type
316
         * on which ensureFamixMethod relies
317
         */
318
        private Method recoverInitializerMethod(TWithMethods owner) {
319
                Method ret = null;
2✔
320
                if (owner != null) {
2!
321
                        for (TMethod meth : owner.getMethods()) {
11✔
322
                                if (meth.getName().equals(EntityDictionary.INIT_BLOCK_NAME)) {
5✔
323
                                        ret = (Method) meth;
3✔
324
                                        break;
1✔
325
                                }
326
                        }
1✔
327
                }
328
                return ret;
2✔
329
        }
330

331
        public AnnotationTypeAttribute visitAnnotationTypeMemberDeclaration(AnnotationTypeMemberDeclaration node) {
332
                IMethodBinding bnd = node.resolveBinding();
3✔
333

334
                AnnotationTypeAttribute fmx = dico.getFamixAnnotationTypeAttribute(bnd, node.getName().getIdentifier(), (AnnotationType) context.topType());
12✔
335

336
                context.pushAnnotationMember(fmx);  // whether fmx==null or not
4✔
337
                return fmx;
2✔
338
        }
339

340
        protected String getAnonymousSuperTypeName() {
341
                return anonymousSuperTypeName.empty() ? null : anonymousSuperTypeName.peek();
11✔
342
        }
343

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