Browse Source

fix read main-class exception from contract jar

tags/1.1.4
shaozhuguang 5 years ago
parent
commit
3f064340ac
3 changed files with 75 additions and 17 deletions
  1. +50
    -17
      source/utils/utils-common/src/main/java/com/jd/blockchain/utils/decompiler/utils/DecompilerUtils.java
  2. +25
    -0
      source/utils/utils-common/src/test/java/test/my/utils/DecompilerUtilsTest.java
  3. BIN
      source/utils/utils-common/src/test/resources/contract-JDChain-Contract.jar

+ 50
- 17
source/utils/utils-common/src/main/java/com/jd/blockchain/utils/decompiler/utils/DecompilerUtils.java View File

@@ -15,8 +15,11 @@ import java.io.StringWriter;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

public class DecompilerUtils {

@@ -101,29 +104,59 @@ public class DecompilerUtils {
}

public static String decompileMainClassFromJarFile(final String jarFilePath) {

// 首先获取Main-Class
List<String> manifests = readManifest2Array(jarFilePath, null);
if (manifests == null || manifests.size() == 0) {
throw new IllegalStateException("MANIFEST.MF not Exist or is Empty !!!");
} else {
String mainClass = null;
for (String s : manifests) {
String inner = s.trim().replaceAll(" ", "");
if (inner.startsWith(MAIN_CLASS)) {
mainClass = inner.split(":")[1];
break;
String mainClass = readMainClassFromJarFile(jarFilePath);
if (mainClass == null) {
throw new IllegalStateException("MANIFEST.MF has not Main-Class !!!");
}

// 然后读取MainClass中的内容并进行反编译
String classPath = mainClass.replaceAll("\\.", "/");
return decompileJarFile(jarFilePath, classPath, true, null);
}

public static String readMainClassFromJarFile(final String jarFilePath) {
try {
JarFile jarFile = new JarFile(jarFilePath);
Manifest mf = jarFile.getManifest();
Attributes mainAttributes = mf.getMainAttributes();
for (Map.Entry<Object, Object> entry : mainAttributes.entrySet()) {
String key = entry.getKey().toString();
if (key.equalsIgnoreCase(MAIN_CLASS)) {
return entry.getValue().toString();
}
}
if (mainClass == null || mainClass.length() == 0) {
throw new IllegalStateException("MANIFEST.MF has not Main-Class !!!");
}

// 然后读取MainClass中的内容并进行反编译
String classPath = mainClass.replaceAll("\\.", "/");
return decompileJarFile(jarFilePath, classPath, true, null);
} catch (Exception e) {
throw new IllegalStateException(String.format("Find Main-Class Exception %s", e.getMessage()));
}
return null;
}

// public static String decompileMainClassFromJarFile(final String jarFilePath) {
// // 首先获取Main-Class
// List<String> manifests = readManifest2Array(jarFilePath, null);
// if (manifests == null || manifests.size() == 0) {
// throw new IllegalStateException("MANIFEST.MF not Exist or is Empty !!!");
// } else {
// String mainClass = null;
// for (String s : manifests) {
// String inner = s.trim().replaceAll(" ", "");
// if (inner.startsWith(MAIN_CLASS)) {
// mainClass = inner.split(":")[1];
// break;
// }
// }
// if (mainClass == null || mainClass.length() == 0) {
// throw new IllegalStateException("MANIFEST.MF has not Main-Class !!!");
// }
//
// // 然后读取MainClass中的内容并进行反编译
// String classPath = mainClass.replaceAll("\\.", "/");
// return decompileJarFile(jarFilePath, classPath, true, null);
// }
// }

public static String decompileJarFile(final String jarFilePath, final String source, final boolean isClass, final String charSet) {

// 对于Class文件和非Class文件处理方式不同


+ 25
- 0
source/utils/utils-common/src/test/java/test/my/utils/DecompilerUtilsTest.java View File

@@ -0,0 +1,25 @@
package test.my.utils;

import com.jd.blockchain.utils.decompiler.utils.DecompilerUtils;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class DecompilerUtilsTest {

private String testJarName = "contract-JDChain-Contract.jar";

private String testJarMainClass = "com.jd.jr.contract.service.impl.Test031111621550409227472896ContractImpl";

@Test
public void testMainClassFromJarFile() throws Exception {
// 获取需要测试的jar包
String path = DecompilerUtilsTest.class.getResource("/").toURI().getPath() + testJarName;

String mainClass = DecompilerUtils.readMainClassFromJarFile(path);

System.out.println("find main class = " + mainClass);

assertEquals(mainClass, testJarMainClass);
}
}

BIN
source/utils/utils-common/src/test/resources/contract-JDChain-Contract.jar View File


Loading…
Cancel
Save