forked from jhunters/jprotobuf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
xiemalin
committed
Oct 9, 2019
1 parent
128280f
commit 3bf60e4
Showing
11 changed files
with
2,068 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
<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>com.baidu.jprotobuf</groupId> | ||
<artifactId>jprotobuf-precompile-plugin-gradle</artifactId> | ||
<version>1.0.0</version> | ||
<name>jprotobuf-precompile-plugin-gradle</name> | ||
<description>jprotobuf-precompile-plugin-gradle</description> | ||
|
||
<parent> | ||
<groupId>org.sonatype.oss</groupId> | ||
<artifactId>oss-parent</artifactId> | ||
<version>7</version> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
|
||
<java.version>1.6</java.version> | ||
<junit.version>4.10</junit.version> | ||
<mavenVersion>2.2.1</mavenVersion> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.gradle</groupId> | ||
<artifactId>gradle-model-core</artifactId> | ||
<version>4.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.codehaus.groovy</groupId> | ||
<artifactId>groovy</artifactId> | ||
<version>2.5.6</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.gradle</groupId> | ||
<artifactId>gradle-core</artifactId> | ||
<version>4.2.1</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.gradle</groupId> | ||
<artifactId>gradle-core-api</artifactId> | ||
<version>5.6.2</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.gradle</groupId> | ||
<artifactId>gradle-base-services</artifactId> | ||
<version>4.2.1</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.gradle</groupId> | ||
<artifactId>gradle-base-services-groovy</artifactId> | ||
<version>4.1</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.gradle</groupId> | ||
<artifactId>gradle-plugins</artifactId> | ||
<version>5.6.2</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.gradle</groupId> | ||
<artifactId>gradle-logging</artifactId> | ||
<version>4.3</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.maven</groupId> | ||
<artifactId>maven-toolchain</artifactId> | ||
<version>1.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.maven</groupId> | ||
<artifactId>maven-artifact</artifactId> | ||
<version>${mavenVersion}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.maven</groupId> | ||
<artifactId>maven-artifact-manager</artifactId> | ||
<version>${mavenVersion}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.maven</groupId> | ||
<artifactId>maven-plugin-api</artifactId> | ||
<version>${mavenVersion}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.maven.plugin-tools</groupId> | ||
<artifactId>maven-plugin-annotations</artifactId> | ||
<version>3.3</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jodd</groupId> | ||
<artifactId>jodd-core</artifactId> | ||
<version>3.5.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.baidu</groupId> | ||
<artifactId>jprotobuf</artifactId> | ||
<version>1.11.7</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-io</groupId> | ||
<artifactId>commons-io</artifactId> | ||
<version>1.4</version> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-source-plugin</artifactId> | ||
<version>2.1</version> | ||
<configuration> | ||
<attach>true</attach> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<phase>compile</phase> | ||
<goals> | ||
<goal>jar</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-javadoc-plugin</artifactId> | ||
<version>2.9.1</version> | ||
<configuration> | ||
<aggregate>true</aggregate> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<phase>compile</phase> | ||
<goals> | ||
<goal>jar</goal> | ||
</goals> | ||
<configuration> | ||
<additionalparam>-Xdoclint:none</additionalparam> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
|
||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-gpg-plugin</artifactId> | ||
<version>1.0</version> | ||
<executions> | ||
<execution> | ||
<id>sign-artifacts</id> | ||
<phase>verify</phase> | ||
<goals> | ||
<goal>sign</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
64 changes: 64 additions & 0 deletions
64
...f-precompile-plugin-gradle/src/main/java/com/baidu/jprotobuf/plugin/AbstractCompiler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2002-2007 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.baidu.jprotobuf.plugin; | ||
|
||
import java.io.OutputStream; | ||
|
||
/** | ||
* Abstract compiler. (SPI, Prototype, ThreadSafe) | ||
* | ||
* @author xiemalin | ||
* @since 1.0.0 | ||
*/ | ||
public abstract class AbstractCompiler implements Compiler { | ||
|
||
/* | ||
* (non-Javadoc) | ||
* | ||
* @see com.baidu.bjf.remoting.protobuf.utils.compiler.Compiler#compile(java.lang.String, java.lang.String, | ||
* java.lang.ClassLoader, java.io.OutputStream, long) | ||
*/ | ||
public Class<?> compile(String className, String code, ClassLoader classLoader, OutputStream os, long lastModify) { | ||
code = code.trim(); | ||
try { | ||
return Class.forName(className, true, classLoader); | ||
} catch (ClassNotFoundException e) { | ||
if (!code.endsWith("}")) { | ||
throw new IllegalStateException("The java code not endsWith \"}\", code: \n" + code + "\n"); | ||
} | ||
try { | ||
return doCompile(className, code, os); | ||
} catch (RuntimeException t) { | ||
throw t; | ||
} catch (Throwable t) { | ||
throw new IllegalStateException("Failed to compile class, cause: " + t.getMessage() + ", class: " | ||
+ className + ", code: \n" + code + "\n, stack: " + ClassUtils.toString(t)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Do compile action. | ||
* | ||
* @param name the name | ||
* @param source the java source code to compile | ||
* @param os the {@link OutputStream} instance | ||
* @return the class | ||
* @throws Throwable the throwable | ||
*/ | ||
protected abstract Class<?> doCompile(String name, String source, OutputStream os) throws Throwable; | ||
|
||
} |
127 changes: 127 additions & 0 deletions
127
...ecompile-plugin-gradle/src/main/java/com/baidu/jprotobuf/plugin/CacheableJdkCompiler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright 2002-2007 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.baidu.jprotobuf.plugin; | ||
|
||
import java.io.OutputStream; | ||
|
||
import com.baidu.bjf.remoting.protobuf.utils.ClassHelper; | ||
|
||
/** | ||
* Cacheable support compiler. | ||
* | ||
* @author xiemalin | ||
* @since 1.8.6 | ||
*/ | ||
public abstract class CacheableJdkCompiler implements Compiler { | ||
|
||
/** The compiler. */ | ||
protected Compiler compiler; | ||
|
||
/** | ||
* Instantiates a new cacheable jdk compiler. | ||
* | ||
* @param compiler the compiler | ||
*/ | ||
public CacheableJdkCompiler(Compiler compiler) { | ||
this.compiler = compiler; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* | ||
* @see com.baidu.bjf.remoting.protobuf.utils.compiler.Compiler#compile(java.lang.String, java.lang.String, | ||
* java.lang.ClassLoader, java.io.OutputStream, long) | ||
*/ | ||
@Override | ||
public Class<?> compile(String className, String code, ClassLoader classLoader, OutputStream os, long timestamp) { | ||
Class<?> cls = null; | ||
try { | ||
cls = ClassHelper.forName(className, classLoader); | ||
} catch (ClassNotFoundException e) { | ||
// ignore this exception | ||
} catch (LinkageError e) { | ||
// ignore this exception | ||
} | ||
if (cls != null) { | ||
return cls; | ||
} | ||
|
||
// to check cache | ||
byte[] bytes = cached(className, timestamp); | ||
// if has cached and timestamp is not changed will return a not null byte array | ||
if (bytes != null) { | ||
LoadableClassLoader loadableClassLoader = new LoadableClassLoader(classLoader); | ||
loadableClassLoader.defineNewClass(className, bytes, 0, bytes.length); | ||
|
||
try { | ||
return loadableClassLoader.loadClass(className); | ||
} catch (ClassNotFoundException e) { | ||
// ignore this exception | ||
} | ||
} | ||
|
||
cls = compiler.compile(className, code, classLoader, os, timestamp); | ||
|
||
cache(className, compiler.loadBytes(className), timestamp); | ||
return cls; | ||
} | ||
|
||
/** | ||
* Cached. | ||
* | ||
* @param className the class name | ||
* @param timestamp the timestamp | ||
* @return the byte[] | ||
*/ | ||
protected abstract byte[] cached(String className, long timestamp); | ||
|
||
/** | ||
* Cache. | ||
* | ||
* @param className the class name | ||
* @param bytes the bytes | ||
* @param timestamp the timestamp | ||
*/ | ||
protected abstract void cache(String className, byte[] bytes, long timestamp); | ||
|
||
/** | ||
* The Class LoadableClassLoader. | ||
*/ | ||
protected static class LoadableClassLoader extends ClassLoader { | ||
|
||
/** | ||
* Instantiates a new loadable class loader. | ||
* | ||
* @param parent the parent | ||
*/ | ||
protected LoadableClassLoader(ClassLoader parent) { | ||
super(parent); | ||
} | ||
|
||
/** | ||
* Define new class. | ||
* | ||
* @param name the name | ||
* @param b the b | ||
* @param off the off | ||
* @param len the len | ||
*/ | ||
public void defineNewClass(String name, byte[] b, int off, int len) { | ||
defineClass(name, b, off, len); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.