-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #20: added ServiceLoader-API oriented main class with load
checking
- Loading branch information
flofreud
committed
Apr 28, 2013
1 parent
a1cdd7c
commit 47654ad
Showing
7 changed files
with
192 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
javabite-compiler/src/main/java/swp_compiler_ss13/javabite/compiler/JavabiteCompiler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package swp_compiler_ss13.javabite.compiler; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import swp_compiler_ss13.common.backend.Backend; | ||
import swp_compiler_ss13.common.ir.IntermediateCodeGenerator; | ||
import swp_compiler_ss13.common.lexer.Lexer; | ||
import swp_compiler_ss13.common.parser.Parser; | ||
import swp_compiler_ss13.javabite.compiler.util.ModuleProvider; | ||
|
||
/** | ||
* main class for the JavaBite-compiler | ||
* | ||
* @author flofreud | ||
* | ||
*/ | ||
public class JavabiteCompiler { | ||
final static Logger log = LoggerFactory.getLogger(JavabiteCompiler.class); | ||
|
||
Lexer lexer = null; | ||
Parser parser = null; | ||
IntermediateCodeGenerator codegen = null; | ||
Backend backend = null; | ||
|
||
public JavabiteCompiler() { | ||
lexer = ModuleProvider.getLexerInstance(); | ||
parser = ModuleProvider.getParserInstance(); | ||
codegen = ModuleProvider.getCodeGeneratorInstance(); | ||
backend = ModuleProvider.getBackendInstance(); | ||
} | ||
|
||
/** | ||
* @return check if all modules could be loaded | ||
*/ | ||
public boolean checkSetup() { | ||
boolean setupOk = true; | ||
|
||
if (lexer == null) { | ||
setupOk = false; | ||
log.error("No lexer module loaded..."); | ||
} | ||
if (parser == null) { | ||
setupOk = false; | ||
log.error("No parser module loaded..."); | ||
} | ||
if (codegen == null) { | ||
setupOk = false; | ||
log.error("No code generation module loaded..."); | ||
} | ||
if (backend == null) { | ||
setupOk = false; | ||
log.error("No backend module loaded..."); | ||
} | ||
|
||
return setupOk; | ||
} | ||
|
||
|
||
public static void main(String[] args) { | ||
System.out.println("Javabite-Compiler Basic Console"); | ||
JavabiteCompiler compiler = new JavabiteCompiler(); | ||
if (compiler.checkSetup()) { | ||
System.out.println("Compiler is ready to start"); | ||
} else { | ||
System.out.println("Compiler could not load all need modules"); | ||
} | ||
|
||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
javabite-compiler/src/main/java/swp_compiler_ss13/javabite/compiler/util/ModuleProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package swp_compiler_ss13.javabite.compiler.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.ServiceConfigurationError; | ||
import java.util.ServiceLoader; | ||
|
||
import swp_compiler_ss13.common.backend.Backend; | ||
import swp_compiler_ss13.common.ir.IntermediateCodeGenerator; | ||
import swp_compiler_ss13.common.lexer.Lexer; | ||
import swp_compiler_ss13.common.parser.Parser; | ||
|
||
/** | ||
* The module provider gives access to the module implementation which is | ||
* configured by the ServiceLoader-API. | ||
* | ||
* If the configuration of the ServiceLoader-API is not correct, a | ||
* {@link ServiceConfigurationError} may be thrown. | ||
* | ||
* @author flofreud | ||
* | ||
*/ | ||
public class ModuleProvider { | ||
|
||
/** | ||
* @return a available Lexer implementation instance | ||
* @throws ServiceConfigurationError | ||
*/ | ||
public static Lexer getLexerInstance() { | ||
return getModuleInstance(Lexer.class); | ||
} | ||
|
||
/** | ||
* @return a collection of all available Lexer implementation instance | ||
* @throws ServiceConfigurationError | ||
*/ | ||
public static Collection<? extends Lexer> getAllLexerInstance() { | ||
return getAllModuleInstances(Lexer.class); | ||
} | ||
|
||
/** | ||
* @return a available Parser implementation instance | ||
* @throws ServiceConfigurationError | ||
*/ | ||
public static Parser getParserInstance() { | ||
return getModuleInstance(Parser.class); | ||
} | ||
|
||
/** | ||
* @return a collection of all available Parser implementation instance | ||
* @throws ServiceConfigurationError | ||
*/ | ||
public static Collection<? extends Parser> getAllParserInstance() { | ||
return getAllModuleInstances(Parser.class); | ||
} | ||
|
||
/** | ||
* @return a available IntermediateCodeGenerator implementation instance | ||
* @throws ServiceConfigurationError | ||
*/ | ||
public static IntermediateCodeGenerator getCodeGeneratorInstance() { | ||
return getModuleInstance(IntermediateCodeGenerator.class); | ||
} | ||
|
||
/** | ||
* @return a collection of all available IntermediateCodeGenerator implementation instance | ||
* @throws ServiceConfigurationError | ||
*/ | ||
public static Collection<? extends IntermediateCodeGenerator> getCodeAllGeneratorInstance() { | ||
return getAllModuleInstances(IntermediateCodeGenerator.class); | ||
} | ||
|
||
/** | ||
* @return a available Backend implementation instance | ||
* @throws ServiceConfigurationError | ||
*/ | ||
public static Backend getBackendInstance() { | ||
return getModuleInstance(Backend.class); | ||
} | ||
|
||
/** | ||
* @return a collection of all available Backend implementation instance | ||
* @throws ServiceConfigurationError | ||
*/ | ||
public static Collection<? extends Backend> getAllBackendInstance() { | ||
return getAllModuleInstances(Backend.class); | ||
} | ||
|
||
private static <T> T getModuleInstance(Class<T> clazz) { | ||
Iterator<T> iterator = ServiceLoader.load(clazz).iterator(); | ||
return iterator.hasNext() ? iterator.next() : null; | ||
} | ||
|
||
private static <T> Collection<? extends T> getAllModuleInstances( | ||
Class<T> clazz) { | ||
Collection<T> result = new ArrayList<T>(); | ||
for (T e : ServiceLoader.load(clazz)) | ||
result.add(e); | ||
return result; | ||
} | ||
} |
Empty file.
1 change: 1 addition & 0 deletions
1
...rc/main/resources/META-INF/services/swp_compiler_ss13.common.ir.IntermediateCodeGenerator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
swp_compiler_ss13.javabite.codegen.JavaBiteCodeGenerator |
Empty file.
1 change: 1 addition & 0 deletions
1
...bite-compiler/src/main/resources/META-INF/services/swp_compiler_ss13.common.parser.Parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
swp_compiler_ss13.javabite.parser.JavabiteParser |