Skip to content

Commit 7d6fed3

Browse files
committed
SONARJAVA-7236 List the implicit default constructor among the members of a class of the file
1 parent a1b85fa commit 7d6fed3

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

‎java-frontend/src/main/java/org/sonar/java/model/JTypeSymbol.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,24 @@ public Collection<Symbol> memberSymbols() {
162162
for (JsonObject member : memberFacts) {
163163
resolved.add(sema.memberFromTypeInfo(this, member));
164164
}
165+
addImplicitConstructor(resolved);
165166
members = resolved;
166167
}
167168
return members;
168169
}
169170

171+
/** A class of this file that declares no constructor still has the default one as a member. */
172+
private void addImplicitConstructor(List<Symbol> resolved) {
173+
if (id == null || isUnknown() || isTypeParameter() || isInterface() || declaration() == null
174+
|| resolved.stream().anyMatch(member -> member.isMethodSymbol() && "<init>".equals(member.name()))) {
175+
return;
176+
}
177+
Symbol constructor = sema.resolve(id + "#<init>()");
178+
if (!constructor.isUnknown()) {
179+
resolved.add(constructor);
180+
}
181+
}
182+
170183
@Override
171184
public Collection<Symbol> lookupSymbols(String name) {
172185
Set<Symbol> found = new HashSet<>();

0 commit comments

Comments
 (0)