use of com.eprosima.idl.parser.grammar.IDLLexer in project ihmc-pub-sub by ihmcrobotics.
the class IDLGenerator method execute.
/**
* Generate java classes from an IDL file
*
* @param idlFile IDL file to parse
* @param packageName Target package (IDL Module gets added to this)
* @param targetDirectory Directory to save the generated files in. The whole package structure is generated in this directory
* @throws IOException
*/
public static void execute(File idlFile, String packageName, File targetDirectory, List<File> includePath) throws IOException {
String idlFilename = idlFile.getAbsolutePath();
Field field;
try {
field = TemplateManager.class.getDeclaredField("m_loaderDirectories");
field.setAccessible(true);
field.set(null, "us/ihmc/idl/templates");
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException("API changed, fixme", e);
}
String onlyFileName = Util.getIDLFileNameOnly(idlFilename);
IDLContext context = new IDLContext(onlyFileName, idlFilename, new ArrayList<>());
context.setPackage(packageName);
TypeCode.javapackage = context.isIsPackageEmpty() ? "" : (context.getPackage() + ".");
// Create default @default annotation.
AnnotationDeclaration defaultAnnotation = context.createAnnotationDeclaration("defaultValue", null);
defaultAnnotation.addMember(new AnnotationMember("value", new PrimitiveTypeCode(TypeCode.KIND_STRING), ""));
// Create default @Key annotation.
AnnotationDeclaration keyann = context.createAnnotationDeclaration("Key", null);
keyann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(TypeCode.KIND_BOOLEAN), "true"));
// Create default @Topic annotation.
AnnotationDeclaration topicann = context.createAnnotationDeclaration("Topic", null);
topicann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(TypeCode.KIND_BOOLEAN), "true"));
AnnotationDeclaration abstractann = context.createAnnotationDeclaration("Abstract", null);
abstractann.addMember(new AnnotationMember("type", new PrimitiveTypeCode(TypeCode.KIND_STRING), "java.lang.Object"));
abstractann.addMember(new AnnotationMember("impl", new PrimitiveTypeCode(TypeCode.KIND_STRING), ""));
AnnotationDeclaration typecode = context.createAnnotationDeclaration("TypeCode", null);
typecode.addMember(new AnnotationMember("type", new PrimitiveTypeCode(TypeCode.KIND_BOOLEAN), "INVALID_TYPE_CODE"));
// Create template manager
TemplateManager tmanager = new TemplateManager("FastCdrCommon:Common");
// Create main template
TemplateGroup maintemplates = tmanager.createTemplateGroup("main");
maintemplates.setAttribute("ctx", context);
if (idlFile.exists()) {
Reader reader = createPreProcessedInputStream(idlFile, includePath);
ANTLRInputStream input = new ANTLRInputStream(reader);
IDLLexer lexer = new IDLLexer(input);
lexer.setContext(context);
CommonTokenStream tokens = new CommonTokenStream(lexer);
// printTokenStream(tokens);
IDLParser parser = new IDLParser(tokens);
// Pass the filename without the extension
parser.specification(context, tmanager, maintemplates);
File packageDir = new File(targetDirectory, context.getPackageDir());
if (packageDir.isDirectory() || packageDir.mkdirs()) {
TypesGenerator gen = new TypesGenerator(tmanager, true);
if (!gen.generate(context, packageDir.getPath() + "/", context.getPackage(), null)) {
throw new IOException("Cannot create Java files");
}
} else {
throw new IOException("Cannot create output dir " + packageDir);
}
} else {
throw new IOException("The File " + idlFilename + " was not found.");
}
}
Aggregations