Skip to content

Commit 847e4b2

Browse files
committed
Pull getDirectives out into a util to reduce duplication
1 parent e7555ca commit 847e4b2

File tree

3 files changed

+47
-24
lines changed

3 files changed

+47
-24
lines changed

delphi-frontend/src/main/java/au/com/integradev/delphi/antlr/ast/node/AnonymousMethodHeadingNodeImpl.java

+2-12
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@
1818
*/
1919
package au.com.integradev.delphi.antlr.ast.node;
2020

21+
import au.com.integradev.delphi.antlr.ast.node.utils.RoutineDirectiveUtils;
2122
import au.com.integradev.delphi.antlr.ast.visitors.DelphiParserVisitor;
22-
import com.google.common.collect.ImmutableSet;
2323
import java.util.Set;
2424
import org.antlr.runtime.Token;
2525
import org.sonar.plugins.communitydelphi.api.ast.AnonymousMethodHeadingNode;
26-
import org.sonar.plugins.communitydelphi.api.ast.DelphiNode;
2726
import org.sonar.plugins.communitydelphi.api.ast.RoutineParametersNode;
2827
import org.sonar.plugins.communitydelphi.api.ast.RoutineReturnTypeNode;
2928
import org.sonar.plugins.communitydelphi.api.symbol.declaration.RoutineDirective;
3029
import org.sonar.plugins.communitydelphi.api.symbol.declaration.RoutineKind;
31-
import org.sonar.plugins.communitydelphi.api.token.DelphiToken;
3230

3331
public final class AnonymousMethodHeadingNodeImpl extends DelphiNodeImpl
3432
implements AnonymousMethodHeadingNode {
@@ -69,15 +67,7 @@ public RoutineKind getRoutineKind() {
6967
@Override
7068
public Set<RoutineDirective> getDirectives() {
7169
if (directives == null) {
72-
var builder = new ImmutableSet.Builder<RoutineDirective>();
73-
for (DelphiNode child : getChildren()) {
74-
DelphiToken token = child.getToken();
75-
RoutineDirective directive = RoutineDirective.fromToken(token);
76-
if (directive != null) {
77-
builder.add(directive);
78-
}
79-
}
80-
directives = builder.build();
70+
directives = RoutineDirectiveUtils.getDirectives(this);
8171
}
8272
return directives;
8373
}

delphi-frontend/src/main/java/au/com/integradev/delphi/antlr/ast/node/RoutineHeadingNodeImpl.java

+2-12
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818
*/
1919
package au.com.integradev.delphi.antlr.ast.node;
2020

21+
import au.com.integradev.delphi.antlr.ast.node.utils.RoutineDirectiveUtils;
2122
import au.com.integradev.delphi.antlr.ast.visitors.DelphiParserVisitor;
22-
import com.google.common.collect.ImmutableSet;
2323
import java.util.Collections;
2424
import java.util.List;
2525
import java.util.Set;
2626
import org.antlr.runtime.Token;
2727
import org.sonar.plugins.communitydelphi.api.ast.AttributeListNode;
28-
import org.sonar.plugins.communitydelphi.api.ast.DelphiNode;
2928
import org.sonar.plugins.communitydelphi.api.ast.FormalParameterNode.FormalParameterData;
3029
import org.sonar.plugins.communitydelphi.api.ast.RoutineDeclarationNode;
3130
import org.sonar.plugins.communitydelphi.api.ast.RoutineHeadingNode;
@@ -36,7 +35,6 @@
3635
import org.sonar.plugins.communitydelphi.api.ast.TypeDeclarationNode;
3736
import org.sonar.plugins.communitydelphi.api.symbol.declaration.RoutineDirective;
3837
import org.sonar.plugins.communitydelphi.api.symbol.declaration.RoutineKind;
39-
import org.sonar.plugins.communitydelphi.api.token.DelphiToken;
4038
import org.sonar.plugins.communitydelphi.api.token.DelphiTokenType;
4139
import org.sonar.plugins.communitydelphi.api.type.Type;
4240

@@ -194,15 +192,7 @@ public RoutineKind getRoutineKind() {
194192
@Override
195193
public Set<RoutineDirective> getDirectives() {
196194
if (directives == null) {
197-
var builder = new ImmutableSet.Builder<RoutineDirective>();
198-
for (DelphiNode child : getChildren()) {
199-
DelphiToken token = child.getToken();
200-
RoutineDirective directive = RoutineDirective.fromToken(token);
201-
if (directive != null) {
202-
builder.add(directive);
203-
}
204-
}
205-
directives = builder.build();
195+
directives = RoutineDirectiveUtils.getDirectives(this);
206196
}
207197
return directives;
208198
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Sonar Delphi Plugin
3+
* Copyright (C) 2024 Integrated Application Development
4+
*
5+
* This program is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 3 of the License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
18+
*/
19+
package au.com.integradev.delphi.antlr.ast.node.utils;
20+
21+
import com.google.common.collect.ImmutableSet;
22+
import java.util.Set;
23+
import org.sonar.plugins.communitydelphi.api.ast.DelphiNode;
24+
import org.sonar.plugins.communitydelphi.api.symbol.declaration.RoutineDirective;
25+
import org.sonar.plugins.communitydelphi.api.token.DelphiToken;
26+
27+
public final class RoutineDirectiveUtils {
28+
private RoutineDirectiveUtils() {
29+
// Utility class
30+
}
31+
32+
public static Set<RoutineDirective> getDirectives(DelphiNode node) {
33+
var builder = new ImmutableSet.Builder<RoutineDirective>();
34+
for (DelphiNode child : node.getChildren()) {
35+
DelphiToken token = child.getToken();
36+
RoutineDirective directive = RoutineDirective.fromToken(token);
37+
if (directive != null) {
38+
builder.add(directive);
39+
}
40+
}
41+
return builder.build();
42+
}
43+
}

0 commit comments

Comments
 (0)