Skip to content

Commit eacb816

Browse files
committed
refactoring of directive handler list
1 parent 6e08ad9 commit eacb816

5 files changed

Lines changed: 57 additions & 54 deletions

File tree

jcp/src/main/java/com/igormaznitsa/jcp/InfoHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static List<String> makeTextForHelpInfo() {
8080

8181
result.add("Directives");
8282
result.add(SHORT_DELIMITER);
83-
for (final AbstractDirectiveHandler handler : AbstractDirectiveHandler.getAllDirectives()) {
83+
for (final AbstractDirectiveHandler handler : AbstractDirectiveHandler.findAllDirectives()) {
8484
result.add(makeDirectiveReference(handler));
8585
}
8686
result.add(DELIMITER);

jcp/src/main/java/com/igormaznitsa/jcp/containers/FileInfoContainer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,11 +582,10 @@ private boolean checkDirectiveArgumentRoughly(final AbstractDirectiveHandler dir
582582
protected AfterDirectiveProcessingBehaviour processDirective(final PreprocessingState state,
583583
final String directiveString,
584584
final PreprocessorContext context,
585-
final boolean firstPass)
586-
throws IOException {
585+
final boolean firstPass) {
587586
final boolean executionEnabled = state.isDirectiveCanBeProcessed();
588587

589-
for (final AbstractDirectiveHandler handler : AbstractDirectiveHandler.getAllDirectives()) {
588+
for (final AbstractDirectiveHandler handler : context.getDirectiveHandlers()) {
590589
final String name = handler.getName();
591590
if (directiveString.startsWith(name)) {
592591
if ((firstPass && !handler.isGlobalPhaseAllowed()) ||

jcp/src/main/java/com/igormaznitsa/jcp/context/PreprocessorContext.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import com.igormaznitsa.jcp.containers.FileInfoContainer;
3030
import com.igormaznitsa.jcp.containers.TextFileDataContainer;
31+
import com.igormaznitsa.jcp.directives.AbstractDirectiveHandler;
3132
import com.igormaznitsa.jcp.exceptions.FilePositionInfo;
3233
import com.igormaznitsa.jcp.exceptions.PreprocessorException;
3334
import com.igormaznitsa.jcp.expression.Value;
@@ -117,6 +118,7 @@ public class PreprocessorContext {
117118
@Setter(AccessLevel.NONE)
118119
private PreprocessorLogger preprocessorLogger = new SystemOutLogger();
119120
private List<String> excludeFolders = new ArrayList<>();
121+
private static final List<AbstractDirectiveHandler> directiveHandlers = AbstractDirectiveHandler.findAllDirectives();
120122

121123
/**
122124
* Constructor
@@ -196,6 +198,15 @@ public PreprocessorContext(final PreprocessorContext context) {
196198
this.currentInCloneSource = context.getPreprocessingState().peekFile();
197199
}
198200

201+
/**
202+
* Get all directive handlers allowed for processing.
203+
* @return list of direction handlers for the context
204+
* @since 7.0.6
205+
*/
206+
public List<AbstractDirectiveHandler> getDirectiveHandlers() {
207+
return directiveHandlers;
208+
}
209+
199210
public void addPreprocessedResource(final FileInfoContainer container) {
200211
if (container != null) {
201212
this.preprocessedResources.add(container);

jcp/src/main/java/com/igormaznitsa/jcp/directives/AbstractDirectiveHandler.java

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
package com.igormaznitsa.jcp.directives;
2323

2424
import com.igormaznitsa.jcp.context.PreprocessorContext;
25+
import java.util.ArrayList;
26+
import java.util.List;
2527

2628
/**
2729
* The class is the abstract parent for all classes process preprocessor
@@ -52,55 +54,46 @@ public abstract class AbstractDirectiveHandler {
5254
*/
5355
public static final String ONE_LINE_COMMENT = "//";
5456

55-
/**
56-
* The array contains all directives of the preprocessor
57-
*/
58-
private static volatile AbstractDirectiveHandler[] allDirectives;
59-
60-
61-
public static AbstractDirectiveHandler[] getAllDirectives() {
62-
if (allDirectives == null) {
63-
allDirectives = new AbstractDirectiveHandler[] {
64-
new LocalDirectiveHandler(),
65-
new IfDefinedDirectiveHandler(),
66-
new IfNDefDirectiveHandler(),
67-
new IfDefDirectiveHandler(),
68-
new IfDirectiveHandler(),
69-
new ElseDirectiveHandler(),
70-
new EndIfDirectiveHandler(),
71-
new WhileDirectiveHandler(),
72-
new BreakDirectiveHandler(),
73-
new ContinueDirectiveHandler(),
74-
new EndDirectiveHandler(),
75-
new ExitIfDirectiveHandler(),
76-
new ExitDirectiveHandler(),
77-
new OutdirDirectiveHandler(),
78-
new OutEnabledDirectiveHandler(),
79-
new OutNameDirectiveHandler(),
80-
new OutDisabledDirectiveHandler(),
81-
new CommentNextLineDirectiveHandler(),
82-
new DefinelDirectiveHandler(),
83-
new DefineDirectiveHandler(),
84-
new UndefDirectiveHandler(),
85-
new FlushDirectiveHandler(),
86-
new IncludeDirectiveHandler(),
87-
new ActionDirectiveHandler(),
88-
new PostfixDirectiveHandler(),
89-
new PrefixDirectiveHandler(),
90-
new GlobalDirectiveHandler(),
91-
new GlobalElseDirectiveHandler(),
92-
new GlobalEndIfDirectiveHandler(),
93-
new GlobalIfDirectiveHandler(),
94-
new ExcludeIfDirectiveHandler(),
95-
new ErrorDirectiveHandler(),
96-
new WarningDirectiveHandler(),
97-
new EchoDirectiveHandler(),
98-
new MsgDirectiveHandler(),
99-
new NoAutoFlushHandler(),
100-
new AbortDirectiveHandler()
101-
};
102-
}
103-
return allDirectives;
57+
public static List<AbstractDirectiveHandler> findAllDirectives() {
58+
final List<AbstractDirectiveHandler> result = new ArrayList<>();
59+
result.add(new LocalDirectiveHandler());
60+
result.add(new IfDefinedDirectiveHandler());
61+
result.add(new IfNDefDirectiveHandler());
62+
result.add(new IfDefDirectiveHandler());
63+
result.add(new IfDirectiveHandler());
64+
result.add(new ElseDirectiveHandler());
65+
result.add(new EndIfDirectiveHandler());
66+
result.add(new WhileDirectiveHandler());
67+
result.add(new BreakDirectiveHandler());
68+
result.add(new ContinueDirectiveHandler());
69+
result.add(new EndDirectiveHandler());
70+
result.add(new ExitIfDirectiveHandler());
71+
result.add(new ExitDirectiveHandler());
72+
result.add(new OutdirDirectiveHandler());
73+
result.add(new OutEnabledDirectiveHandler());
74+
result.add(new OutNameDirectiveHandler());
75+
result.add(new OutDisabledDirectiveHandler());
76+
result.add(new CommentNextLineDirectiveHandler());
77+
result.add(new DefinelDirectiveHandler());
78+
result.add(new DefineDirectiveHandler());
79+
result.add(new UndefDirectiveHandler());
80+
result.add(new FlushDirectiveHandler());
81+
result.add(new IncludeDirectiveHandler());
82+
result.add(new ActionDirectiveHandler());
83+
result.add(new PostfixDirectiveHandler());
84+
result.add(new PrefixDirectiveHandler());
85+
result.add(new GlobalDirectiveHandler());
86+
result.add(new GlobalElseDirectiveHandler());
87+
result.add(new GlobalEndIfDirectiveHandler());
88+
result.add(new GlobalIfDirectiveHandler());
89+
result.add(new ExcludeIfDirectiveHandler());
90+
result.add(new ErrorDirectiveHandler());
91+
result.add(new WarningDirectiveHandler());
92+
result.add(new EchoDirectiveHandler());
93+
result.add(new MsgDirectiveHandler());
94+
result.add(new NoAutoFlushHandler());
95+
result.add(new AbortDirectiveHandler());
96+
return result;
10497
}
10598

10699
/**

jcp/src/test/java/com/igormaznitsa/jcp/directives/AbstractDirectiveHandlerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class AbstractDirectiveHandlerTest {
3333
@Test
3434
public void testPotentialConflictsBetweenDirectives() {
3535
final List<String> processed = new ArrayList<>();
36-
for (final AbstractDirectiveHandler h : AbstractDirectiveHandler.getAllDirectives()) {
36+
for (final AbstractDirectiveHandler h : AbstractDirectiveHandler.findAllDirectives()) {
3737
final String name = h.getName();
3838
for (final String p : processed) {
3939
assertFalse(h.getFullName() + " conflicts with " + (AbstractDirectiveHandler.DIRECTIVE_PREFIX + p), name.startsWith(p));

0 commit comments

Comments
 (0)