Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions .classpath
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/test/java"/>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry exported="true" kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
*.lock
*.DS_Store
*.swp
*.out
*.out
vim*
/protodir/
/target/
6 changes: 6 additions & 0 deletions .project
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 7 additions & 4 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions .settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,15 +25,14 @@ This class simply takes a POJO and creates the associated Proto Buffer Specifica

Usage - CLI:

java -jar JavaToProto.jar JavaToProto <class name> [<output file name>]
java -cp <JAR PATH CONTAINING THE CLAS>:JavaToProro.jar com.lloyd.JavaToProto.JavaToProto <class name> [<output file name>]

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);
JavaToProto jpt = new JavaToProto(Fully qualified class name);
String protoFile = jpt.toString();


32 changes: 0 additions & 32 deletions README.txt

This file was deleted.

41 changes: 41 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>JavaToProto</groupId>
<artifactId>JavaToProto</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.lloyd.JavaToProto.JavaToProto</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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<Class> classStack = new Stack<Class>();
private Map<Class, String> typeMap = getPrimitivesMap();
Expand All @@ -80,6 +80,7 @@ public static void main(String[] args) {

if(args.length == 0){
System.out.println("Usage: \n\tjava -jar JavaToProto.jar JavaToProto <class name> [<output file name>]\n");
System.exit(-1);
}

Class clazz;
Expand Down Expand Up @@ -187,26 +188,38 @@ public Map<Class,String> 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();

}

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();

Expand All @@ -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();

Expand Down Expand Up @@ -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--;
Expand Down
72 changes: 72 additions & 0 deletions src/main/java/com/vmware/javatoproto/example/GenerateProto.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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 );
}
}