Skip to content

Commit

Permalink
升级 precompile插件, 增加compileDependencies参数,默认为true, 同步对依赖的对象进行预编译
Browse files Browse the repository at this point in the history
  • Loading branch information
xiemalin committed Nov 19, 2019
1 parent 4310f25 commit 2a06db7
Show file tree
Hide file tree
Showing 17 changed files with 566 additions and 23 deletions.
4 changes: 2 additions & 2 deletions jprotobuf-precompile-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</parent>
<groupId>com.baidu</groupId>
<artifactId>jprotobuf-precompile-plugin</artifactId>
<version>1.3.0</version>
<version>1.3.1</version>
<packaging>maven-plugin</packaging>
<name>jprotobuf-precompile-plugin</name>
<description>This plugin will do compile jprotobuf pojo class after compile execute</description>
Expand Down Expand Up @@ -60,7 +60,7 @@
<dependency>
<groupId>com.baidu</groupId>
<artifactId>jprotobuf</artifactId>
<version>1.12.0</version>
<version>1.12.1</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import java.io.UnsupportedEncodingException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
Expand All @@ -18,6 +20,7 @@
import com.baidu.bjf.remoting.protobuf.ProtobufProxy;
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;
import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
import com.baidu.bjf.remoting.protobuf.code.TemplateCodeGenerator;
import com.baidu.bjf.remoting.protobuf.utils.FieldUtils;
import com.baidu.bjf.remoting.protobuf.utils.JDKCompilerHelper;
import com.baidu.bjf.remoting.protobuf.utils.StringUtils;
Expand Down Expand Up @@ -47,7 +50,7 @@ public class JprotobufPreCompileMain {
*/
public static void main(String[] args) {

if (args == null || args.length == 0 || args.length != 4) {
if (args == null || args.length == 0 || args.length != 5) {
throw new RuntimeException(printUsage());
}

Expand All @@ -69,7 +72,11 @@ public static void main(String[] args) {

final String[] split = filterClassPackage.split(MULTI_PKG_SPLIT);

final boolean generateProtofile = Boolean .valueOf(args[3]);
final boolean generateProtofile = Boolean.valueOf(args[3]);

final boolean compileDependencies = Boolean.valueOf(args[4]);

final Set<Class> dependenciesClasses = new HashSet<Class>();

ClassScanner scanner = new ClassScanner() {

Expand Down Expand Up @@ -97,6 +104,10 @@ protected void onEntry(EntryData entryData) throws Exception {
if (generateProtofile) {
createProtoFile(c, outputPath.getCanonicalPath());
}
if (compileDependencies) {
TemplateCodeGenerator tcg = new TemplateCodeGenerator(c);
tcg.getAllDependenciesClasses(dependenciesClasses);
}
} catch (Throwable e) {
throw new Exception(e.getMessage(), e);
}
Expand All @@ -111,6 +122,10 @@ protected void onEntry(EntryData entryData) throws Exception {
if (generateProtofile) {
createProtoFile(c, outputPath.getCanonicalPath());
}
if (compileDependencies) {
TemplateCodeGenerator tcg = new TemplateCodeGenerator(c);
tcg.getAllDependenciesClasses(dependenciesClasses);
}
}
} catch (Throwable e) {
throw new Exception(e.getMessage(), e);
Expand All @@ -119,6 +134,21 @@ protected void onEntry(EntryData entryData) throws Exception {
};

scanner.scanDefaultClasspath();

if (compileDependencies) {
// compile dependencies classes
for (Class cls : dependenciesClasses) {
try {
ProtobufProxy.create(cls, false, outputPath);
if (generateProtofile) {
createProtoFile(cls, outputPath.getCanonicalPath());
}
} catch (Exception e) {
// dummy exception
}
}
}


// copy files
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ public class PreCompileMojo
/** The generate proto file. */
@Parameter( required = false, property = "jprotobuf.generateProtoFile" )
private String generateProtoFile = Boolean.FALSE.toString();

/** The compile dependencies. */
@Parameter( required = false, property = "jprotobuf.compileDependencies" )
private String compileDependencies = Boolean.TRUE.toString();

/**
* Execute goal.
Expand All @@ -277,7 +281,7 @@ public void execute()
}

arguments = new String[] {outputParentDirectory.getAbsolutePath(), outputDirectory.getAbsolutePath(),
filterClassPackage, generateProtoFile};
filterClassPackage, generateProtoFile, compileDependencies};

if ( getLog().isDebugEnabled() )
{
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<groupId>com.baidu</groupId>
<artifactId>jprotobuf</artifactId>
<version>1.12.0</version>
<version>1.12.1</version>

<name>Jprotobuf</name>
<description>A very useful utility library for java programmer using google protobuf.</description>
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/baidu/bjf/remoting/protobuf/FieldType.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,14 @@ public boolean isPrimitive() {

return false;
}

/**
* Checks if is enum.
*
* @return true, if is enum
*/
public boolean isEnum() {
return this == ENUM;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -59,7 +62,6 @@ public abstract class AbstractCodeGenerator implements ICodeGenerator {
/**
* Instantiates a new abstract code generator.
*
* @param fields the fields
* @param cls the cls
*/
public AbstractCodeGenerator(Class<?> cls) {
Expand All @@ -70,6 +72,140 @@ public AbstractCodeGenerator(Class<?> cls) {
fields = ProtobufProxyUtils.fetchFieldInfos(cls, true);
}

/**
* Gets the all dependencies classes.
*
* @param list the list
* @return the all dependencies classes
*/
public void getAllDependenciesClasses(Set<Class> list) {
if (list == null) {
throw new RuntimeException("param 'list' is null.");
}

getAllDependenciesClasses(cls, list);

}

/**
* Gets the all dependencies classes.
*
* @param cls the cls
* @param list the list
* @return the all dependencies classes
*/
private void getAllDependenciesClasses(Class cls, Set<Class> list) {
if (list == null) {
throw new RuntimeException("param 'list' is null.");
}

Set<Class> dependenciesClasses = getDependenciesClasses(cls);
if (dependenciesClasses.isEmpty()) {
return;
}

for (Class dependencyClass : dependenciesClasses) {
if (list.contains(dependencyClass)) {
continue;
}
list.add(dependencyClass);

Set<Class> subDependenciesClasses = getDependenciesClasses(dependencyClass);
if (subDependenciesClasses.isEmpty()) {
continue;
}

for (Class subClass : subDependenciesClasses) {
if (list.contains(subClass)) {
continue;
}
list.add(subClass);
getAllDependenciesClasses(subClass, list);
}

}
}


/**
* Gets the dependencies classes.
*
* @param cls the cls
* @return the dependencies classes
*/
public Set<Class> getDependenciesClasses(Class cls) {
List<FieldInfo> fields = null;
try {
fields = ProtobufProxyUtils.fetchFieldInfos(cls, true);
} catch (Exception e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(e.getMessage(), e);
}
}
if (fields == null) {
return Collections.emptySet();
}

Set<Class> dependenciesClasses = getDependenciesClasses(fields);
return dependenciesClasses;
}

/**
* Gets the dependencies classes.
*
* @return the dependencies classes
*/
public Set<Class> getDependenciesClasses() {
return getDependenciesClasses(fields);
}


/**
* Gets the dependencies classes.
*
* @param fields the fields
* @return the dependencies classes
*/
public Set<Class> getDependenciesClasses(List<FieldInfo> fields) {
if (fields == null) {
return Collections.emptySet();
}

Set<Class> ret = new HashSet<Class>();

for (FieldInfo fieldInfo : fields) {
if (fieldInfo.isObjectType()) {
if (fieldInfo.isList()) {
Class<?> genericKeyType = fieldInfo.getGenericKeyType();
if (!FieldInfo.isPrimitiveType(genericKeyType)) {
ret.add(genericKeyType);
}
} else {
ret.add(fieldInfo.getField().getType());
}

} else if (fieldInfo.isMap()) {
Class<?> genericKeyType = fieldInfo.getGenericKeyType();
if (!FieldInfo.isPrimitiveType(genericKeyType)) {
ret.add(genericKeyType);
}
Class<?> genericeValueType = fieldInfo.getGenericeValueType();
if (!FieldInfo.isPrimitiveType(genericeValueType)) {
ret.add(genericeValueType);
}
} else if (fieldInfo.isList()) {
Class<?> genericKeyType = fieldInfo.getGenericKeyType();
if (!FieldInfo.isPrimitiveType(genericKeyType)) {
ret.add(genericKeyType);
}
} else if (fieldInfo.getFieldType().isEnum()) {
ret.add(fieldInfo.getField().getType());
}
}

return ret;
}


/**
* Gets the target proxy classname.
Expand All @@ -80,6 +216,11 @@ protected String getTargetProxyClassname() {
return targetProxyClassname;
}

/**
* Sets the output path.
*
* @param outputPath the new output path
*/
/* (non-Javadoc)
* @see com.baidu.bjf.remoting.protobuf.code.ICodeGenerator#setOutputPath(java.io.File)
*/
Expand All @@ -97,6 +238,11 @@ protected File getOutputPath() {
return outputPath;
}

/**
* Checks if is debug.
*
* @return true, if is debug
*/
/* (non-Javadoc)
* @see com.baidu.bjf.remoting.protobuf.code.ICodeGenerator#isDebug()
*/
Expand All @@ -105,6 +251,11 @@ public boolean isDebug() {
return debug;
}

/**
* Sets the debug.
*
* @param debug the new debug
*/
/* (non-Javadoc)
* @see com.baidu.bjf.remoting.protobuf.code.ICodeGenerator#setDebug(boolean)
*/
Expand All @@ -114,6 +265,11 @@ public void setDebug(boolean debug) {

}

/**
* Gets the class name.
*
* @return the class name
*/
/* (non-Javadoc)
* @see com.baidu.bjf.remoting.protobuf.code.ICodeGenerator#getClassName()
*/
Expand All @@ -122,6 +278,11 @@ public String getClassName() {
return ClassHelper.getClassName(cls);
}

/**
* Gets the package.
*
* @return the package
*/
/* (non-Javadoc)
* @see com.baidu.bjf.remoting.protobuf.code.ICodeGenerator#getPackage()
*/
Expand All @@ -130,6 +291,11 @@ public String getPackage() {
return ClassHelper.getPackage(cls);
}

/**
* Gets the full class name.
*
* @return the full class name
*/
/* (non-Javadoc)
* @see com.baidu.bjf.remoting.protobuf.code.ICodeGenerator#getFullClassName()
*/
Expand Down
Loading

0 comments on commit 2a06db7

Please sign in to comment.