Skip to content

Commit

Permalink
generate simple class
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan committed Jul 3, 2023
1 parent 7930a38 commit 0c29a6c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public void initAttribute(String line) {
type = type.replace("long", "int");
}

if(type.contains("unsigned")) {
type = type.replace("unsigned", "").trim();
}

if(type.equals("any")) {
isAny = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void test_NoDeleteClassTest_attributes() {
Assert.assertEquals("ReadOnlyInt", idlAttribute.name);
Assert.assertTrue(idlClass.attributes.get(0).isStatic);
Assert.assertTrue(idlClass.attributes.get(0).isReadOnly);
Assert.assertEquals("unsigned int", idlClass.attributes.get(0).type);
Assert.assertEquals("int", idlClass.attributes.get(0).type);
Assert.assertTrue(idlClass.attributes.get(1).isConst);
Assert.assertEquals("constNormalClass", idlClass.attributes.get(1).name);
Assert.assertEquals("NormalClassTest", idlClass.attributes.get(1).type);
Expand All @@ -51,7 +51,7 @@ public void test_NoDeleteClassTest_attributes() {
Assert.assertEquals("arrayBoolean", idlClass.attributes.get(6).name);
Assert.assertEquals("boolean[]", idlClass.attributes.get(6).type);
Assert.assertEquals("arrayInt", idlClass.attributes.get(7).name);
Assert.assertEquals("unsigned int[]", idlClass.attributes.get(7).type);
Assert.assertEquals("int[]", idlClass.attributes.get(7).type);
Assert.assertEquals("anyObject", idlClass.attributes.get(8).name);
Assert.assertEquals("any", idlClass.attributes.get(8).type);
Assert.assertTrue(idlClass.attributes.get(8).isAny);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ public static void main(String[] args) throws Exception {

String genDir = "../example-core/src";

String basePackage = "com.github.xpenatan.jparser.example";

IDLReader idlReader = IDLReader.readIDL(path);
IDLDefaultCodeParser idlParser = new IDLDefaultCodeParser("IDL-Test", idlReader);
IDLDefaultCodeParser idlParser = new IDLDefaultCodeParser(basePackage, "IDL-Test", idlReader);
JParser.generate(idlParser, basePath, genDir);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.github.xpenatan.jparser.core.codeparser;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.xpenatan.jparser.core.JParser;
import com.github.xpenatan.jparser.core.JParserItem;
import com.github.xpenatan.jparser.core.util.CustomFileDescriptor;
import com.github.xpenatan.jparser.idl.IDLClass;
import com.github.xpenatan.jparser.idl.IDLFile;
import com.github.xpenatan.jparser.idl.IDLReader;
import java.io.File;

/**
* @author xpenatan
Expand All @@ -22,17 +27,40 @@ public IDLClassGeneratorParser(String basePackage, String headerCMD, IDLReader i

@Override
public void onParseStart(JParser jParser) {
// Generate class if it does not exist

String packagePath = File.separator + basePackage.replace(".", File.separator);
String basePath = new File(jParser.genDir + packagePath).getAbsolutePath();

for(IDLFile idlFile : idlReader.fileArray) {
for(IDLClass idlClass : idlFile.classArray) {
JParserItem parserItem = jParser.getParserUnitItem(idlClass.name);
String className = idlClass.name;
JParserItem parserItem = jParser.getParserUnitItem(className);
if(parserItem == null) {
// Generate class if it does not exist

String classPath = basePath + File.separator + className + ".java";
CustomFileDescriptor fileDescriptor = new CustomFileDescriptor(classPath);
CompilationUnit compilationUnit = setupClass(idlClass);
String code = compilationUnit.toString();
fileDescriptor.writeString(code, false);

// System.out.println();
JParserItem item = new JParserItem(compilationUnit, classPath, classPath);
jParser.unitArray.add(item);
}
}
}
}

private CompilationUnit setupClass(IDLClass idlClass) {
String className = idlClass.name;
CompilationUnit compilationUnit = new CompilationUnit();
compilationUnit.setPackageDeclaration(basePackage);
ClassOrInterfaceDeclaration classDeclaration = compilationUnit.addClass(className);
classDeclaration.setPublic(true);

if(idlClass.classHeader.isNoDelete) {
classDeclaration.addConstructor(Modifier.Keyword.PROTECTED);
}

return compilationUnit;
}
}

0 comments on commit 0c29a6c

Please sign in to comment.