diff --git a/.classpath b/.classpath index 72c2ba6..168d8b5 100644 --- a/.classpath +++ b/.classpath @@ -1,7 +1,17 @@ - - - - + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore index 14c1e9a..e42712a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,7 @@ *.lock *.DS_Store *.swp -*.out \ No newline at end of file +*.out +vim* +/protodir/ +/target/ diff --git a/.project b/.project index ce8ffe5..e44bede 100644 --- a/.project +++ b/.project @@ -10,8 +10,14 @@ + + org.eclipse.m2e.core.maven2Builder + + + + org.eclipse.m2e.core.maven2Nature org.eclipse.jdt.core.javanature diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 6fff76a..db24ee7 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,12 +1,15 @@ -#Sat Feb 11 12:17:52 GMT 2012 eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.compliance=1.8 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.source=1.7 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/README.md b/README.md index f41fc8a..aced0a2 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ +# java-proto-generator-example +A sample project to show usage of java-proto-generator +======= ##Java to Proto Copyright - Lloyd Sparkes 2012 @@ -22,7 +25,7 @@ This class simply takes a POJO and creates the associated Proto Buffer Specifica Usage - CLI: - java -jar JavaToProto.jar JavaToProto [] + java -cp :JavaToProro.jar com.lloyd.JavaToProto.JavaToProto [] If output file name is not specified the output will be to the console. @@ -30,7 +33,6 @@ Usage - CLI: Usage - Code: - JavaToProto jpt = new JavaToProto(class name); + JavaToProto jpt = new JavaToProto(Fully qualified class name); String protoFile = jpt.toString(); - diff --git a/README.txt b/README.txt deleted file mode 100644 index 791d73a..0000000 --- a/README.txt +++ /dev/null @@ -1,32 +0,0 @@ -Copyright - Lloyd Sparkes 2012 -LICENSE: Public Domain - do as you wish, just retain this message. - I just ask that if you find bugs or improve the code, you raise a pull request or an issue, so i can update the code for everyone -DISCLAIMER: I am not responsible for your usage of this code, or for any bugs or issues with this code or any resulting side effects - -This class simply takes a POJO and creates the associated Proto Buffer Specification File. - -Supports: - Nested POJO's - Enums - Arrays/Collections/Lists/Sets (BUT only if they have a type specifier!! (so List is supported, List is not supported) - Maps/KeyValuePairs (BUT they need a type specifier!! (so Map is supported, Map is not supported) - Primitives - Boxed Primitives - -Does Not Support: - Nested Collections e.g. Map,String> - Arrays with more than one Dimension - -Usage - CLI: - java -jar JavaToProto.jar JavaToProto [] - - If output file name is not specified the output will be to the console. - - Ensure that the class name is in the class path somewhere. - -Usage - Code: - - JavaToProto jpt = new JavaToProto(class name); - String protoFile = jpt.toString(); - - diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..c4b88d2 --- /dev/null +++ b/pom.xml @@ -0,0 +1,41 @@ + + 4.0.0 + JavaToProto + JavaToProto + 0.0.1-SNAPSHOT + + src + + + maven-compiler-plugin + 3.8.0 + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.1.0 + + + + true + com.lloyd.JavaToProto.JavaToProto + + + + + + + + + + junit + junit + 4.12 + + + diff --git a/src/com/lloyd/JavaToProto/JavaToProto.java b/src/main/java/com/lloyd/JavaToProto/JavaToProto.java similarity index 89% rename from src/com/lloyd/JavaToProto/JavaToProto.java rename to src/main/java/com/lloyd/JavaToProto/JavaToProto.java index d31e727..0324f7a 100644 --- a/src/com/lloyd/JavaToProto/JavaToProto.java +++ b/src/main/java/com/lloyd/JavaToProto/JavaToProto.java @@ -66,7 +66,7 @@ public class JavaToProto { private static String REQUIRED = "required"; private static String REPEATED = "repeated"; private static String LINE_END = ";"; - + private static String SYNTAX = "syntax = \"proto2\";"; private StringBuilder builder; private Stack classStack = new Stack(); private Map typeMap = getPrimitivesMap(); @@ -80,6 +80,7 @@ public static void main(String[] args) { if(args.length == 0){ System.out.println("Usage: \n\tjava -jar JavaToProto.jar JavaToProto []\n"); + System.exit(-1); } Class clazz; @@ -187,16 +188,28 @@ public Map getPrimitivesMap(){ } public void processField(String repeated, String type, String name, int index){ - builder.append(getTabs()).append(repeated).append(SPACE).append(type).append(SPACE).append(name).append(SPACE).append("=").append(SPACE).append(index).append(LINE_END).append(NEWLINE); + builder.append(getTabs()).append(repeated) + .append(SPACE).append((null == type)?"string":type) + .append(SPACE).append(removeSpecialChar(name)).append(SPACE).append("=") + .append(SPACE).append(index).append(LINE_END).append(NEWLINE); + } + private String removeSpecialChar(String inp) + { + if (null != inp && !inp.isEmpty()) + { + return inp.replaceAll("[^a-zA-Z0-9_-]", ""); + } + return inp; } - //end region - private void generateProtoFile(){ builder = new StringBuilder(); //File Header - builder.append(COMMENT).append("Generated by ").append(NAME).append(SPACE).append(VERSION).append(" @ ").append(new Date()).append(NEWLINE).append(NEWLINE); + builder.append(COMMENT).append("Generated by ") + .append(NAME).append(SPACE).append(VERSION).append(" @ ") + .append(new Date()).append(NEWLINE).append(NEWLINE) + .append(SYNTAX).append(NEWLINE); buildMessage(); @@ -204,9 +217,9 @@ private void generateProtoFile(){ private String buildMessage(){ - if(currentClass().isInterface() || currentClass().isEnum() || Modifier.isAbstract(currentClass().getModifiers())){ - throw new RuntimeException("A Message cannot be an Interface, Abstract OR an Enum"); - } + //if(currentClass().isInterface() || currentClass().isEnum() || Modifier.isAbstract(currentClass().getModifiers())){ + // throw new RuntimeException("A Message cannot be an Interface, Abstract OR an Enum"); + //} String messageName = currentClass().getSimpleName(); @@ -233,11 +246,11 @@ private void processFields(){ for(Field f : fields){ i++; - int mod = f.getModifiers(); - if(Modifier.isAbstract(mod) || Modifier.isTransient(mod)){ + //int mod = f.getModifiers(); + //if(Modifier.isAbstract(mod) || Modifier.isTransient(mod)){ //Skip this field - continue; - } + // continue; + //} Class fieldType = f.getType(); @@ -349,7 +362,7 @@ private void processEnum(Class enumType){ int i = 0; for(Object e : enumType.getEnumConstants()){ - builder.append(getTabs()).append(e.toString()).append(" = ").append(i).append(LINE_END).append(NEWLINE); + builder.append(getTabs()).append(e.toString()).append(" = ").append(i++).append(LINE_END).append(NEWLINE); } tabDepth--; diff --git a/src/com/lloyd/JavaToProto/tests/ProtoFileGenerationTests.java b/src/main/java/com/lloyd/JavaToProto/tests/ProtoFileGenerationTests.java similarity index 100% rename from src/com/lloyd/JavaToProto/tests/ProtoFileGenerationTests.java rename to src/main/java/com/lloyd/JavaToProto/tests/ProtoFileGenerationTests.java diff --git a/src/main/java/com/vmware/javatoproto/example/GenerateProto.java b/src/main/java/com/vmware/javatoproto/example/GenerateProto.java new file mode 100644 index 0000000..a46a21b --- /dev/null +++ b/src/main/java/com/vmware/javatoproto/example/GenerateProto.java @@ -0,0 +1,72 @@ +package com.vmware.javatoproto.example; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.util.Scanner; + +import com.lloyd.JavaToProto.JavaToProto; + +public class GenerateProto +{ + + public static void main( String[] args ) + { + if (args.length != 2) { + System.out.println("Kindly provide location of file containing classes and output directory"); + System.exit(-1); + } + String file_path_cwith_class_names = args[0]; + String output_directory = args[1]; + + File file = new File(file_path_cwith_class_names); + try { + Scanner sc = new Scanner(file); + while (sc.hasNextLine()) { + String className = sc.nextLine(); + if ("".equals(className) || className.equals(null)){ + continue; + } + Class clazz; + try { + clazz = Class.forName(className); + } catch (Exception e) { + System.out.println("Could not load class. Make Sure it is in the classpath!! \n" + className); + e.printStackTrace(); + return; + } + //System.out.println(className); + JavaToProto jpt = new JavaToProto(clazz); + String protoFile = null; + try { + protoFile = jpt.toString(); + }catch(Exception ex){ + System.out.println(className); + System.out.println(ex.getMessage()); + //ex.printStackTrace(); + continue; + } + + //Write to File + try + { + File f = new File(output_directory + "/" + className + ".proto"); + FileWriter fw = new FileWriter(f); + BufferedWriter out = new BufferedWriter(fw); + out.write(protoFile); + out.flush(); + out.close(); + } catch (Exception e) { + System.out.println("Got Exception while Writing to File - See Console for File Contents"); + System.out.println(protoFile); + e.printStackTrace(); + } + } + sc.close(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/src/com/lloyd/JavaToProto/tests/samples/ArrayMessage.java b/src/test/java/com/lloyd/JavaToProto/tests/samples/ArrayMessage.java similarity index 100% rename from src/com/lloyd/JavaToProto/tests/samples/ArrayMessage.java rename to src/test/java/com/lloyd/JavaToProto/tests/samples/ArrayMessage.java diff --git a/src/com/lloyd/JavaToProto/tests/samples/EnumMessage.java b/src/test/java/com/lloyd/JavaToProto/tests/samples/EnumMessage.java similarity index 100% rename from src/com/lloyd/JavaToProto/tests/samples/EnumMessage.java rename to src/test/java/com/lloyd/JavaToProto/tests/samples/EnumMessage.java diff --git a/src/com/lloyd/JavaToProto/tests/samples/ListMessage.java b/src/test/java/com/lloyd/JavaToProto/tests/samples/ListMessage.java similarity index 100% rename from src/com/lloyd/JavaToProto/tests/samples/ListMessage.java rename to src/test/java/com/lloyd/JavaToProto/tests/samples/ListMessage.java diff --git a/src/com/lloyd/JavaToProto/tests/samples/MapMessage.java b/src/test/java/com/lloyd/JavaToProto/tests/samples/MapMessage.java similarity index 100% rename from src/com/lloyd/JavaToProto/tests/samples/MapMessage.java rename to src/test/java/com/lloyd/JavaToProto/tests/samples/MapMessage.java diff --git a/src/com/lloyd/JavaToProto/tests/samples/POJOMessage.java b/src/test/java/com/lloyd/JavaToProto/tests/samples/POJOMessage.java similarity index 100% rename from src/com/lloyd/JavaToProto/tests/samples/POJOMessage.java rename to src/test/java/com/lloyd/JavaToProto/tests/samples/POJOMessage.java diff --git a/src/com/lloyd/JavaToProto/tests/samples/SimpleMessage.java b/src/test/java/com/lloyd/JavaToProto/tests/samples/SimpleMessage.java similarity index 100% rename from src/com/lloyd/JavaToProto/tests/samples/SimpleMessage.java rename to src/test/java/com/lloyd/JavaToProto/tests/samples/SimpleMessage.java diff --git a/src/test/java/com/vmware/javatoproto/com/vmware/javatoproto/example/AppTest.java b/src/test/java/com/vmware/javatoproto/com/vmware/javatoproto/example/AppTest.java new file mode 100644 index 0000000..d457ca1 --- /dev/null +++ b/src/test/java/com/vmware/javatoproto/com/vmware/javatoproto/example/AppTest.java @@ -0,0 +1,38 @@ +package com.vmware.javatoproto.com.vmware.javatoproto.example; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +}