Skip to content

Commit

Permalink
clean up the code a little bit by following Uncle Bob's clean code gu…
Browse files Browse the repository at this point in the history
…ide (alibaba#785)
  • Loading branch information
jamesxuhaozhe authored and hengyunabc committed Jul 23, 2019
1 parent 24979e3 commit 4b63aa5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 36 deletions.
27 changes: 16 additions & 11 deletions boot/src/main/java/com/taobao/arthas/boot/DownloadUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,8 @@ public class DownloadUtils {
*/
public static String readMavenReleaseVersion(String mavenMetaData) {
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(mavenMetaData.getBytes("UTF-8"));
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document document = dBuilder.parse(inputStream);

Document document = transformMavenMetaData(mavenMetaData);
NodeList nodeList = document.getDocumentElement().getElementsByTagName("release");

return nodeList.item(0).getTextContent();
} catch (Exception e) {
// ignore
Expand All @@ -67,11 +62,7 @@ public static String readMavenReleaseVersion(String mavenMetaData) {
public static List<String> readAllMavenVersion(String mavenMetaData) {
List<String> result = new ArrayList<String>();
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(mavenMetaData.getBytes("UTF-8"));
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document document = dBuilder.parse(inputStream);

Document document = transformMavenMetaData(mavenMetaData);
NodeList nodeList = document.getDocumentElement().getElementsByTagName("version");
int length = nodeList.getLength();
for (int i = 0; i < length; ++i) {
Expand Down Expand Up @@ -187,6 +178,20 @@ public static void saveUrl(final String filename, final String urlString, boolea
}
}

/**
* transform the maven meta data which is in the format of String into document object.
*
* @param mavenMetaData
* @return
* @throws Exception
*/
private static Document transformMavenMetaData(String mavenMetaData) throws Exception {
ByteArrayInputStream inputStream = new ByteArrayInputStream(mavenMetaData.getBytes("UTF-8"));
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
return dBuilder.parse(inputStream);
}

/**
* support redirect
*
Expand Down
24 changes: 12 additions & 12 deletions common/src/main/java/com/taobao/arthas/common/JavaVersionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,43 @@
*
*/
public class JavaVersionUtils {
private static final String versionPropName = "java.specification.version";
private static final String javaVersionStr = System.getProperty(versionPropName);
private static final float javaVersion = Float.parseFloat(javaVersionStr);
private static final String VERSION_PROP_NAME = "java.specification.version";
private static final String JAVA_VERSION_STR = System.getProperty(VERSION_PROP_NAME);
private static final float JAVA_VERSION = Float.parseFloat(JAVA_VERSION_STR);

public static String javaVersionStr() {
return javaVersionStr;
return JAVA_VERSION_STR;
}

public static String javaVersionStr(Properties props) {
return (null != props) ? props.getProperty(versionPropName): null;
return (null != props) ? props.getProperty(VERSION_PROP_NAME): null;
}

public static float javaVersion() {
return javaVersion;
return JAVA_VERSION;
}

public static boolean isJava6() {
return javaVersionStr.equals("1.6");
return JAVA_VERSION_STR.equals("1.6");
}

public static boolean isJava7() {
return javaVersionStr.equals("1.7");
return JAVA_VERSION_STR.equals("1.7");
}

public static boolean isJava8() {
return javaVersionStr.equals("1.8");
return JAVA_VERSION_STR.equals("1.8");
}

public static boolean isJava9() {
return javaVersionStr.equals("9");
return JAVA_VERSION_STR.equals("9");
}

public static boolean isLessThanJava9() {
return javaVersion < 9.0f;
return JAVA_VERSION < 9.0f;
}

public static boolean isGreaterThanJava8() {
return javaVersion > 1.8f;
return JAVA_VERSION > 1.8f;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ public void process(CommandProcess process) {
} else if (hashCode != null && this.loadClass != null) {
processLoadClass(process, inst);
} else if (hashCode != null) {
processClassloader(process, inst);
processClassLoader(process, inst);
} else if (listClassLoader || isTree){
processClassloaders(process, inst);
processClassLoaders(process, inst);
} else {
processClassLoaderStats(process, inst);
}
Expand Down Expand Up @@ -152,7 +152,7 @@ private void processClassLoaderStats(CommandProcess process, Instrumentation ins
process.end();
}

private void processClassloaders(CommandProcess process, Instrumentation inst) {
private void processClassLoaders(CommandProcess process, Instrumentation inst) {
RowAffect affect = new RowAffect();
List<ClassLoaderInfo> classLoaderInfos = includeReflectionClassLoader ? getAllClassLoaderInfo(inst) :
getAllClassLoaderInfo(inst, new SunReflectionClassLoaderFilter());
Expand All @@ -165,10 +165,10 @@ private void processClassloaders(CommandProcess process, Instrumentation inst) {
}

// 根据 hashCode 来打印URLClassLoader的urls
private void processClassloader(CommandProcess process, Instrumentation inst) {
private void processClassLoader(CommandProcess process, Instrumentation inst) {
RowAffect affect = new RowAffect();

Set<ClassLoader> allClassLoader = getAllClassLoader(inst);
Set<ClassLoader> allClassLoader = getAllClassLoaders(inst);
for (ClassLoader cl : allClassLoader) {
if (Integer.toHexString(cl.hashCode()).equals(hashCode)) {
process.write(RenderUtil.render(renderClassLoaderUrls(cl), process.width()));
Expand All @@ -184,7 +184,7 @@ private void processClassloader(CommandProcess process, Instrumentation inst) {
private void processResources(CommandProcess process, Instrumentation inst) {
RowAffect affect = new RowAffect();
int rowCount = 0;
Set<ClassLoader> allClassLoader = getAllClassLoader(inst);
Set<ClassLoader> allClassLoader = getAllClassLoaders(inst);
for (ClassLoader cl : allClassLoader) {
if (Integer.toHexString(cl.hashCode()).equals(hashCode)) {
TableElement table = new TableElement().leftCellPadding(1).rightCellPadding(1);
Expand All @@ -209,7 +209,7 @@ private void processResources(CommandProcess process, Instrumentation inst) {

// Use ClassLoader to loadClass
private void processLoadClass(CommandProcess process, Instrumentation inst) {
Set<ClassLoader> allClassLoader = getAllClassLoader(inst);
Set<ClassLoader> allClassLoader = getAllClassLoaders(inst);
for (ClassLoader cl : allClassLoader) {
if (Integer.toHexString(cl.hashCode()).equals(hashCode)) {
try {
Expand Down Expand Up @@ -382,7 +382,7 @@ private static void renderParent(TreeElement node, ClassLoaderInfo parent, List<
}
}

private static Set<ClassLoader> getAllClassLoader(Instrumentation inst, Filter... filters) {
private static Set<ClassLoader> getAllClassLoaders(Instrumentation inst, Filter... filters) {
Set<ClassLoader> classLoaderSet = new HashSet<ClassLoader>();

for (Class<?> clazz : inst.getAllLoadedClasses()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.taobao.arthas.core.command.klass100;

import com.taobao.arthas.core.advisor.Enhancer;
import com.taobao.arthas.core.command.Constants;
import com.taobao.arthas.core.shell.cli.Completion;
import com.taobao.arthas.core.shell.cli.CompletionUtils;
Expand All @@ -26,13 +25,12 @@
import com.taobao.text.ui.TableElement;
import com.taobao.text.util.RenderUtil;

import static java.lang.System.arraycopy;

import java.io.File;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;
import java.util.*;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import static com.taobao.text.ui.Element.label;
Expand Down

0 comments on commit 4b63aa5

Please sign in to comment.