Browse Source

after compile then check the contract, test OK.

tags/1.0.0
zhaoguangwei 5 years ago
parent
commit
a45da26d54
3 changed files with 69 additions and 6 deletions
  1. +67
    -1
      source/contract/contract-maven-plugin/src/main/java/com/jd/blockchain/CheckImportsMojo.java
  2. +1
    -1
      source/contract/contract-maven-plugin/src/test/java/com/jd/blockchain/ledger/CheckImportsMojoTest.java
  3. +1
    -4
      source/contract/contract-maven-plugin/src/test/java/com/jd/blockchain/ledger/ContractDeployMojoTest.java

+ 67
- 1
source/contract/contract-maven-plugin/src/main/java/com/jd/blockchain/CheckImportsMojo.java View File

@@ -4,6 +4,12 @@ import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration; import com.github.javaparser.ast.ImportDeclaration;
import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.PackageDeclaration;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import com.jd.blockchain.transaction.ContractType;
import com.jd.blockchain.utils.IllegalDataException;
import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Mojo;
@@ -12,15 +18,28 @@ import org.apache.maven.project.MavenProject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;


import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.stream.Collectors; import java.util.stream.Collectors;



/**
* first step, we want to parse the source code by javaParse. But it's repeated and difficult to parse the source.
* This is a try of "from Initail to Abandoned".
* Since we are good at the class, why not?
* Now we change a way of thinking, first we pre-compile the source code, then parse the *.jar.
*
* by zhaogw
* date 2019-06-05 16:17
*/
@Mojo(name = "checkImports") @Mojo(name = "checkImports")
public class CheckImportsMojo extends AbstractMojo { public class CheckImportsMojo extends AbstractMojo {
Logger logger = LoggerFactory.getLogger(CheckImportsMojo.class); Logger logger = LoggerFactory.getLogger(CheckImportsMojo.class);
@@ -28,6 +47,12 @@ public class CheckImportsMojo extends AbstractMojo {
@Parameter(defaultValue = "${project}", required = true, readonly = true) @Parameter(defaultValue = "${project}", required = true, readonly = true)
private MavenProject project; private MavenProject project;


/**
* jar's name;
*/
@Parameter
private String finalName;

@Override @Override
public void execute() throws MojoFailureException { public void execute() throws MojoFailureException {
List<Path> sources; List<Path> sources;
@@ -40,6 +65,9 @@ public class CheckImportsMojo extends AbstractMojo {
sources = Files.find(baseDirPath, Integer.MAX_VALUE, (file, attrs) -> (file.toString().endsWith(".java"))).collect(Collectors.toList()); sources = Files.find(baseDirPath, Integer.MAX_VALUE, (file, attrs) -> (file.toString().endsWith(".java"))).collect(Collectors.toList());
for (Path path : sources) { for (Path path : sources) {
CompilationUnit compilationUnit = JavaParser.parse(path); CompilationUnit compilationUnit = JavaParser.parse(path);

compilationUnit.accept(new MethodVisitor(), null);

NodeList<ImportDeclaration> imports = compilationUnit.getImports(); NodeList<ImportDeclaration> imports = compilationUnit.getImports();
for (ImportDeclaration imp : imports) { for (ImportDeclaration imp : imports) {
String importName = imp.getName().asString(); String importName = imp.getName().asString();
@@ -50,6 +78,19 @@ public class CheckImportsMojo extends AbstractMojo {
} }
} }


//now we parse the jar;
String jarPath = project.getBuild().getDirectory()+ File.separator+finalName+".jar";
File jarFile = new File(jarPath);
URL jarURL = jarFile.toURI().toURL();
ClassLoader classLoader = new URLClassLoader(new URL[]{jarURL},this.getClass().getClassLoader());
Attributes m = new JarFile(jarFile).getManifest().getMainAttributes();
String contractMainClass = m.getValue(Attributes.Name.MAIN_CLASS);
try {
Class mainClass = classLoader.loadClass(contractMainClass);
ContractType.resolve(mainClass);
} catch (ClassNotFoundException e) {
throw new IllegalDataException(e.getMessage());
}
} }
} catch (IOException exception) { } catch (IOException exception) {
logger.error(exception.getMessage()); logger.error(exception.getMessage());
@@ -58,4 +99,29 @@ public class CheckImportsMojo extends AbstractMojo {
logger.error(e.getMessage()); logger.error(e.getMessage());
} }
} }

private class MethodVisitor extends VoidVisitorAdapter<Void> {
@Override
public void visit(MethodDeclaration n, Void arg) {
/* here you can access the attributes of the method.
this method will be called for all methods in this
CompilationUnit, including inner class methods */
logger.info("method:"+n.getName());
super.visit(n, arg);
}

@Override
public void visit(ClassOrInterfaceDeclaration n, Void arg) {
logger.info("class:"+n.getName()+" extends:"+n.getExtendedTypes()+" implements:"+n.getImplementedTypes());

super.visit(n, arg);
}

@Override
public void visit(PackageDeclaration n, Void arg) {
logger.info("package:"+n.getName());
super.visit(n, arg);
}

}
} }

+ 1
- 1
source/contract/contract-maven-plugin/src/test/java/com/jd/blockchain/ledger/CheckImportsMojoTest.java View File

@@ -13,7 +13,7 @@ import java.io.File;
* @Date 2019/3/1 21:27 * @Date 2019/3/1 21:27
*/ */
public class CheckImportsMojoTest extends AbstractMojoTestCase { public class CheckImportsMojoTest extends AbstractMojoTestCase {
Logger logger = LoggerFactory.getLogger(CheckImportsMojo.class);
Logger logger = LoggerFactory.getLogger(CheckImportsMojoTest.class);


@Test @Test
public void test1() throws Exception { public void test1() throws Exception {


+ 1
- 4
source/contract/contract-maven-plugin/src/test/java/com/jd/blockchain/ledger/ContractDeployMojoTest.java View File

@@ -1,10 +1,8 @@
package com.jd.blockchain.ledger; package com.jd.blockchain.ledger;


import com.jd.blockchain.ContractDeployMojo; import com.jd.blockchain.ContractDeployMojo;
import org.junit.Test;


import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;


/** /**
* for contract deploy and exe; * for contract deploy and exe;
@@ -14,8 +12,7 @@ import java.lang.reflect.InvocationTargetException;
public class ContractDeployMojoTest { public class ContractDeployMojoTest {
private ContractDeployMojo contractDeployMojo = new ContractDeployMojo(); private ContractDeployMojo contractDeployMojo = new ContractDeployMojo();


private void fieldHandle(String fieldName,Object objValue) throws NoSuchFieldException,
IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException {
private void fieldHandle(String fieldName,Object objValue) throws NoSuchFieldException, IllegalAccessException {
Field field = contractDeployMojo.getClass().getDeclaredField(fieldName);//name为类Instance中的private属性 Field field = contractDeployMojo.getClass().getDeclaredField(fieldName);//name为类Instance中的private属性
field.setAccessible(true);//=true,可访问私有变量。 field.setAccessible(true);//=true,可访问私有变量。
Class<?> typeClass = field.getType(); Class<?> typeClass = field.getType();


Loading…
Cancel
Save