Skip to content

Commit

Permalink
Issue #20: added ServiceLoader-API oriented main class with load
Browse files Browse the repository at this point in the history
checking
  • Loading branch information
flofreud committed Apr 28, 2013
1 parent a1cdd7c commit 47654ad
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 24 deletions.
42 changes: 18 additions & 24 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,49 @@ allprojects {
}
}

repositories {
mavenCentral()
}
repositories { mavenCentral() }

dependencies {
compile 'org.slf4j:slf4j-simple:1.7.5'
testCompile 'junit:junit:4.11'
}

test {
systemProperties 'org.slf4j.simpleLogger.defaultLogLevel': 'debug'
}
test { systemProperties 'org.slf4j.simpleLogger.defaultLogLevel': 'debug' }
}
project(':common'){
sourceSets {
main {
java {
srcDir 'interfaces/src'
}
java { srcDir 'interfaces/src' }
}
}
}
project(':javabite-compiler') {
apply plugin:'application'

jar {
manifest {
attributes 'Main-Class': 'swp_compiler_ss13.javabite.compiler.JavabiteCompiler'
}
}

mainClassName = 'swp_compiler_ss13.javabite.compiler.JavabiteCompiler'

dependencies {
compile project(':javabite-common'), project(':javabite-lexer'), project(':javabite-parser'), project(':javabite-code-gen'), project(':javabite-backend')
}
}
project(':javabite-lexer') {
dependencies {
compile project(':javabite-common')
}
dependencies { compile project(':javabite-common') }
}
project(':javabite-parser') {
dependencies {
compile project(':javabite-common')
}
dependencies { compile project(':javabite-common') }
}
project(':javabite-code-gen') {
dependencies {
compile project(':javabite-common')
}
dependencies { compile project(':javabite-common') }
}
project(':javabite-backend') {
dependencies {
compile project(':javabite-common')
}
dependencies { compile project(':javabite-common') }
}
project(':javabite-common') {
dependencies {
compile project(':common')
}
dependencies { compile project(':common') }
}
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");
}

}
}
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
swp_compiler_ss13.javabite.codegen.JavaBiteCodeGenerator
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
swp_compiler_ss13.javabite.parser.JavabiteParser

0 comments on commit 47654ad

Please sign in to comment.