From a45da26d54cc6bcf90e6e351289c8de4049c2e6c Mon Sep 17 00:00:00 2001 From: zhaoguangwei Date: Thu, 6 Jun 2019 09:52:34 +0800 Subject: [PATCH] after compile then check the contract, test OK. --- .../com/jd/blockchain/CheckImportsMojo.java | 68 ++++++++++++++++++- .../ledger/CheckImportsMojoTest.java | 2 +- .../ledger/ContractDeployMojoTest.java | 5 +- 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/source/contract/contract-maven-plugin/src/main/java/com/jd/blockchain/CheckImportsMojo.java b/source/contract/contract-maven-plugin/src/main/java/com/jd/blockchain/CheckImportsMojo.java index 4f9279a7..64a12e20 100644 --- a/source/contract/contract-maven-plugin/src/main/java/com/jd/blockchain/CheckImportsMojo.java +++ b/source/contract/contract-maven-plugin/src/main/java/com/jd/blockchain/CheckImportsMojo.java @@ -4,6 +4,12 @@ import com.github.javaparser.JavaParser; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.ImportDeclaration; 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.MojoFailureException; import org.apache.maven.plugins.annotations.Mojo; @@ -12,15 +18,28 @@ import org.apache.maven.project.MavenProject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.net.URL; +import java.net.URLClassLoader; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; import java.util.Properties; +import java.util.jar.Attributes; +import java.util.jar.JarFile; 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") public class CheckImportsMojo extends AbstractMojo { Logger logger = LoggerFactory.getLogger(CheckImportsMojo.class); @@ -28,6 +47,12 @@ public class CheckImportsMojo extends AbstractMojo { @Parameter(defaultValue = "${project}", required = true, readonly = true) private MavenProject project; + /** + * jar's name; + */ + @Parameter + private String finalName; + @Override public void execute() throws MojoFailureException { List 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()); for (Path path : sources) { CompilationUnit compilationUnit = JavaParser.parse(path); + + compilationUnit.accept(new MethodVisitor(), null); + NodeList imports = compilationUnit.getImports(); for (ImportDeclaration imp : imports) { 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) { logger.error(exception.getMessage()); @@ -58,4 +99,29 @@ public class CheckImportsMojo extends AbstractMojo { logger.error(e.getMessage()); } } + + private class MethodVisitor extends VoidVisitorAdapter { + @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); + } + + } } diff --git a/source/contract/contract-maven-plugin/src/test/java/com/jd/blockchain/ledger/CheckImportsMojoTest.java b/source/contract/contract-maven-plugin/src/test/java/com/jd/blockchain/ledger/CheckImportsMojoTest.java index 8cdce97a..66b194c0 100644 --- a/source/contract/contract-maven-plugin/src/test/java/com/jd/blockchain/ledger/CheckImportsMojoTest.java +++ b/source/contract/contract-maven-plugin/src/test/java/com/jd/blockchain/ledger/CheckImportsMojoTest.java @@ -13,7 +13,7 @@ import java.io.File; * @Date 2019/3/1 21:27 */ public class CheckImportsMojoTest extends AbstractMojoTestCase { - Logger logger = LoggerFactory.getLogger(CheckImportsMojo.class); + Logger logger = LoggerFactory.getLogger(CheckImportsMojoTest.class); @Test public void test1() throws Exception { diff --git a/source/contract/contract-maven-plugin/src/test/java/com/jd/blockchain/ledger/ContractDeployMojoTest.java b/source/contract/contract-maven-plugin/src/test/java/com/jd/blockchain/ledger/ContractDeployMojoTest.java index 85c02021..0d205758 100644 --- a/source/contract/contract-maven-plugin/src/test/java/com/jd/blockchain/ledger/ContractDeployMojoTest.java +++ b/source/contract/contract-maven-plugin/src/test/java/com/jd/blockchain/ledger/ContractDeployMojoTest.java @@ -1,10 +1,8 @@ package com.jd.blockchain.ledger; import com.jd.blockchain.ContractDeployMojo; -import org.junit.Test; import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; /** * for contract deploy and exe; @@ -14,8 +12,7 @@ import java.lang.reflect.InvocationTargetException; public class ContractDeployMojoTest { 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.setAccessible(true);//=true,可访问私有变量。 Class typeClass = field.getType();