Skip to content

Commit

Permalink
refact code
Browse files Browse the repository at this point in the history
  • Loading branch information
xiemalin committed Mar 2, 2021
1 parent 9988007 commit 91fec0f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
*/
public class ProtobufIDLProxy {

/** The Constant JAVA_SUFFIX. */
private static final String JAVA_SUFFIX = ".java";

/** The Constant formatJavaField. */
private static boolean formatJavaField = false;

Expand Down Expand Up @@ -1780,7 +1783,7 @@ private static void writeSourceCode(CodeDependent cd, File sourceOutputDir) {

FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(f, cd.name + ".java"));
fos = new FileOutputStream(new File(f, cd.name + JAVA_SUFFIX));
fos.write(cd.code.getBytes(UTF_8));
fos.flush();
} catch (Exception e) {
Expand Down
26 changes: 14 additions & 12 deletions src/main/java/com/baidu/bjf/remoting/protobuf/ProtobufProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -59,7 +60,7 @@
* @author xiemalin
* @since 1.0.0
*/
public final class ProtobufProxy {
public final class ProtobufProxy {

/** The Constant DEBUG_CONTROLLER. */
public static final ThreadLocal<Boolean> DEBUG_CONTROLLER = new ThreadLocal<Boolean>();
Expand All @@ -77,9 +78,9 @@ public final class ProtobufProxy {

/** The Constant OUTPUT_PATH for target directory to create generated source code out. */
public static final ThreadLocal<Boolean> CACHE_ENABLED = new ThreadLocal<Boolean>();

/** The Constant for debug control from environment. */
private static final String DEBUG_CONTROL = "X_DEBUG_ENABLE";
private static final String DEBUG_CONTROL = "X_DEBUG_ENABLE";

/**
* Clear cache.
Expand All @@ -103,7 +104,7 @@ public static void compile(Class<?> cls, File outputPath) {
}

}

/**
* To create a protobuf proxy class for target class.
*
Expand Down Expand Up @@ -150,7 +151,7 @@ public static <T> Codec<T> create(Class<T> cls, boolean debug, File path) {
if (codec != null) {
return codec;
}

return create(cls, debug, path, null, getCodeGenerator(cls));
}

Expand Down Expand Up @@ -282,7 +283,7 @@ protected static <T> Codec<T> doCreate(Class<T> cls, boolean debug, Compiler com
}

try {
Codec<T> newInstance = (Codec<T>) newClass.newInstance();
Codec<T> newInstance = (Codec<T>)newClass.getDeclaredConstructor(new Class[0]).newInstance(new Object[0]);
if (!CACHED.containsKey(uniClsName)) {
CACHED.put(uniClsName, newInstance);
}
Expand All @@ -300,14 +301,15 @@ protected static <T> Codec<T> doCreate(Class<T> cls, boolean debug, Compiler com
}

return newInstance;
} catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (InstantiationException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e.getMessage(), e);
}
}


/**
* To generate a protobuf proxy java source code for target class.
*
Expand Down Expand Up @@ -353,7 +355,7 @@ public static void dynamicCodeGenerate(OutputStream os, Class cls, Charset chars
public static void enableCache(boolean enabled) {
CACHE_ENABLED.set(enabled);
}

/**
* Gets the class loader.
*
Expand All @@ -363,7 +365,7 @@ private static ClassLoader getClassLoader() {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
return contextClassLoader;
}

/**
* Gets the code generator.
*
Expand Down Expand Up @@ -429,12 +431,12 @@ public static boolean isDebugEnabled() {
if (debugEnv != null && Boolean.parseBoolean(debugEnv)) {
return true;
}

Boolean debug = DEBUG_CONTROLLER.get();
if (debug == null) {
debug = false; // set default to close debug info
}

return debug;
}

Expand Down Expand Up @@ -472,7 +474,7 @@ private static <T> Codec<T> loadCompiledClass(String uniClsName, String classNam
throw new RuntimeException(e.getMessage(), e);
}
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void testGetDescriptor() throws IOException {
Object field = parseFrom.getField(stringMapFD);
Assert.assertTrue(field instanceof List);

Codec<AddressBookProtosPOJO> codec = ProtobufProxy.create(AddressBookProtosPOJO.class, true);
Codec<AddressBookProtosPOJO> codec = ProtobufProxy.create(AddressBookProtosPOJO.class, false);
Descriptor descriptor = codec.getDescriptor();

stringMapFD = descriptor.findFieldByName("list");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public void testDefaultValue() {
"optional string vName = 3 [default = \"hello world\"];" +
"}";

IDLProxyObject idlProxyObject = ProtobufIDLProxy.createSingle(idl, true);
IDLProxyObject idlProxyObject = ProtobufIDLProxy.createSingle(idl, false);

Object type = idlProxyObject.get("type");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void testPressureWithCached() throws IOException {
@Test
public void testPressureWithoutCached() throws IOException {
String code = ProtobufIDLGenerator.getIDL(AddressBookProtosPOJO.class);
Map<String, IDLProxyObject> idlProxyObjects = ProtobufIDLProxy.create(code, true, null, false);
Map<String, IDLProxyObject> idlProxyObjects = ProtobufIDLProxy.create(code, false, null, false);

IDLProxyObject idlProxyObject = idlProxyObjects.get(AddressBookProtosPOJO.class.getSimpleName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public void testPOJODescriptorWorksWell() throws IOException {
Descriptor descriptor2 = AddressBookProtos.Person.getDescriptor();
DescriptorProto proto = descriptor2.toProto();
byte[] byteArray = proto.toByteArray();
Codec<DescriptorProtoPOJO> codec = ProtobufProxy.create(DescriptorProtoPOJO.class, true);
Codec<DescriptorProtoPOJO> codec = ProtobufProxy.create(DescriptorProtoPOJO.class, false);

DescriptorProtoPOJO decode = codec.decode(byteArray);

Expand Down

0 comments on commit 91fec0f

Please sign in to comment.