Skip to content

Commit

Permalink
优化String.format
Browse files Browse the repository at this point in the history
  • Loading branch information
dongchenxu committed Sep 18, 2019
1 parent aae5882 commit a80d07f
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 71 deletions.
2 changes: 1 addition & 1 deletion bin/sandbox.sh
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ function attach_jvm() {
fi

#fix for windows shell $HOME diff with user.home
test -n "$USERPROFILE" -a -z "$(cat $SANDBOX_TOKEN_FILE)" && SANDBOX_TOKEN_FILE=$USERPROFILE/.sandbox.token
test -n "${USERPROFILE}" -a -z "$(cat ${SANDBOX_TOKEN_FILE})" && SANDBOX_TOKEN_FILE=${USERPROFILE}/.sandbox.token

# get network from attach result
SANDBOX_SERVER_NETWORK=$(grep ${token} ${SANDBOX_TOKEN_FILE}|grep ${TARGET_NAMESPACE}|tail -1|awk -F ";" '{print $3";"$4}');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.alibaba.jvm.sandbox.api.listener.ext;

import com.alibaba.jvm.sandbox.api.event.BeforeEvent;
import com.alibaba.jvm.sandbox.api.event.Event;
import com.alibaba.jvm.sandbox.api.event.InvokeEvent;
import com.alibaba.jvm.sandbox.api.util.LazyGet;

import java.util.ArrayList;
import java.util.HashSet;
Expand All @@ -19,12 +19,11 @@ public class Advice implements Attachment {

private final int processId;
private final int invokeId;
private Behavior behavior;

private final ClassLoader loader;
private final LazyGet<Behavior> behaviorLazyGet;
private final Object[] parameterArray;
private final Object target;
private final AdviceAdapterListener listener;

private Object returnObj;
private Throwable throwable;
Expand All @@ -36,28 +35,28 @@ public class Advice implements Attachment {
private Advice parent = this;
private Event.Type state = Event.Type.BEFORE;

private BeforeEvent beforeEvent;


/**
* 构造通知
*
* @param processId {@link InvokeEvent#processId}
* @param invokeId {@link InvokeEvent#invokeId}
* @param parameterArray 触发事件的行为入参
* @param target 触发事件所归属的对象实例
* @param listener 触发事件的处理监听器
* @param processId {@link InvokeEvent#processId}
* @param invokeId {@link InvokeEvent#invokeId}
* @param behaviorLazyGet 触发事件的行为(懒加载)
* @param loader 触发事件的行为所在ClassLoader
* @param parameterArray 触发事件的行为入参
* @param target 触发事件所归属的对象实例
*/
Advice(final int processId,
final int invokeId,
final LazyGet<Behavior> behaviorLazyGet,
final ClassLoader loader,
final Object[] parameterArray,
final Object target,
final AdviceAdapterListener listener) {
final Object target) {
this.processId = processId;
this.invokeId = invokeId;
this.behaviorLazyGet = behaviorLazyGet;
this.loader = loader;
this.parameterArray = parameterArray;
this.target = target;
this.listener = listener;
}

/**
Expand Down Expand Up @@ -142,32 +141,7 @@ public int getInvokeId() {
* @return 触发事件的行为
*/
public Behavior getBehavior() {

try {
return internalGetBehavior();
} catch (NoSuchMethodException e) {
return null;
} catch (ClassNotFoundException e) {
return null;
}
}

private Behavior internalGetBehavior() throws NoSuchMethodException, ClassNotFoundException {
if (behavior == null) {
synchronized (this) {
if (behavior == null) {
final ClassLoader loader = listener.toClassLoader(beforeEvent.javaClassLoader);
final Class<?> targetClass = listener.toClass(loader, beforeEvent.javaClassName);
behavior = listener.toBehavior(
targetClass,
beforeEvent.javaMethodName,
beforeEvent.javaMethodDesc
);
}
}
}

return behavior;
return behaviorLazyGet.get();
}

/**
Expand Down Expand Up @@ -237,13 +211,13 @@ public int hashCode() {

@Override
public boolean equals(Object obj) {
if (null == obj
|| !(obj instanceof Advice)) {
if (obj instanceof Advice) {
final Advice advice = (Advice) obj;
return processId == advice.processId
&& invokeId == advice.invokeId;
} else {
return false;
}
final Advice advice = (Advice) obj;
return processId == advice.processId
&& invokeId == advice.invokeId;
}

/**
Expand Down Expand Up @@ -322,7 +296,5 @@ public List<Advice> listHasMarkOnChain(final String exceptMark) {
return advices;
}

public void setBeforeEvent(BeforeEvent beforeEvent) {
this.beforeEvent = beforeEvent;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.alibaba.jvm.sandbox.api.util.BehaviorDescriptor;
import com.alibaba.jvm.sandbox.api.util.CacheGet;
import com.alibaba.jvm.sandbox.api.util.GaStringUtils;
import com.alibaba.jvm.sandbox.api.util.LazyGet;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -53,12 +54,23 @@ private void switchEvent(final OpStack opStack,
switch (event.type) {
case BEFORE: {
final BeforeEvent bEvent = (BeforeEvent) event;
final ClassLoader loader = toClassLoader(bEvent.javaClassLoader);
final Advice advice = new Advice(
bEvent.processId,
bEvent.invokeId,
new LazyGet<Behavior>() {
@Override
protected Behavior initialValue() throws Throwable {
return toBehavior(
toClass(loader, bEvent.javaClassName),
bEvent.javaMethodName,
bEvent.javaMethodDesc
);
}
},
loader,
bEvent.argumentArray,
bEvent.target,
this
bEvent.target
);

final Advice top;
Expand All @@ -79,7 +91,6 @@ private void switchEvent(final OpStack opStack,

opStackRef.get().pushForBegin(advice);
adviceListener.before(advice);
advice.setBeforeEvent(bEvent);
break;
}

Expand Down Expand Up @@ -283,20 +294,20 @@ private String toJavaClassName(final String internalClassName) {
if (GaStringUtils.isEmpty(internalClassName)) {
return internalClassName;
} else {
return internalClassName.replace('/', '.');
return internalClassName.replaceAll("/", ".");
}
}

// 提取ClassLoader,从BeforeEvent中获取到的ClassLoader
ClassLoader toClassLoader(ClassLoader loader) {
private ClassLoader toClassLoader(ClassLoader loader) {
return null == loader
// 如果此处为null,则说明遇到了来自Bootstrap的类,
? AdviceAdapterListener.class.getClassLoader()
: loader;
}

// 根据JavaClassName从ClassLoader中提取出Class<?>对象
Class<?> toClass(ClassLoader loader, String javaClassName) throws ClassNotFoundException {
private Class<?> toClass(ClassLoader loader, String javaClassName) throws ClassNotFoundException {
return toClassLoader(loader).loadClass(javaClassName);
}

Expand Down Expand Up @@ -411,7 +422,7 @@ public <T> T attachment() {
* @return 匹配的行为
* @throws NoSuchMethodException 如果匹配不到行为,则抛出该异常
*/
Behavior toBehavior(final Class<?> clazz,
private Behavior toBehavior(final Class<?> clazz,
final String javaMethodName,
final String javaMethodDesc) throws NoSuchMethodException {
final Behavior behavior = toBehaviorCacheGet.getFromCache(new BehaviorCacheKey(clazz, javaMethodName, javaMethodDesc));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.alibaba.jvm.sandbox.core.util;
package com.alibaba.jvm.sandbox.api.util;

/**
* 懒加载
*
* @param <T> 懒加载类型
* @author luanjia@taobao.com
* @since {@code sandbox-api:1.2.2}
*/
public abstract class LazyGet<T> {

Expand All @@ -24,9 +26,15 @@ public T get() {
isInit = true;
return object;
} catch (Throwable throwable) {
throw new UnCaughtException(throwable);
throw new LazyGetUnCaughtException(throwable);
}

}

private static class LazyGetUnCaughtException extends RuntimeException {
LazyGetUnCaughtException(Throwable cause) {
super(cause);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ class CallAsmCodeLock extends AsmCodeLock {
*/
class AsmTryCatchBlock {

protected final Label start;
protected final Label end;
protected final Label handler;
protected final String type;
final Label start;
final Label end;
final Label handler;
final String type;

AsmTryCatchBlock(Label start, Label end, Label handler, String type) {
this.start = start;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.alibaba.jvm.sandbox.core.util.matcher.structure;

import com.alibaba.jvm.sandbox.core.util.LazyGet;
import com.alibaba.jvm.sandbox.api.util.LazyGet;

import java.util.ArrayList;
import java.util.Collection;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.alibaba.jvm.sandbox.core.util.matcher.structure;

import com.alibaba.jvm.sandbox.core.util.BitUtils;
import com.alibaba.jvm.sandbox.core.util.LazyGet;
import com.alibaba.jvm.sandbox.api.util.LazyGet;
import com.alibaba.jvm.sandbox.core.util.collection.Pair;
import com.alibaba.jvm.sandbox.core.util.matcher.structure.PrimitiveClassStructure.Primitive;
import com.google.common.cache.Cache;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.alibaba.jvm.sandbox.core.util.matcher.structure;

import com.alibaba.jvm.sandbox.core.util.LazyGet;
import com.alibaba.jvm.sandbox.api.util.LazyGet;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.alibaba.jvm.sandbox.core.util.matcher.structure;

import com.alibaba.jvm.sandbox.core.util.LazyGet;
import com.alibaba.jvm.sandbox.api.util.LazyGet;

import java.lang.annotation.Inherited;
import java.util.HashSet;
Expand Down
5 changes: 0 additions & 5 deletions sandbox-mgr-module/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@
</build>

<dependencies>
<dependency>
<groupId>com.alibaba.jvm.sandbox</groupId>
<artifactId>sandbox-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down

0 comments on commit a80d07f

Please sign in to comment.