Skip to content

Commit 958ec5d

Browse files
committed
iss-907: clean up
1 parent 3579733 commit 958ec5d

File tree

14 files changed

+190
-126
lines changed

14 files changed

+190
-126
lines changed

hystrix-contrib/hystrix-javanica/build.gradle

+2-3
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,16 @@ ext {
6161

6262

6363
dependencies {
64-
//compile project(':hystrix-core')
64+
compile project(':hystrix-core')
6565
ajtools "org.aspectj:aspectjtools:$aspectjVersion"
6666
testRuntime "org.aspectj:aspectjrt:$aspectjVersion"
67-
compile 'com.netflix.hystrix:hystrix-core:1.4.15'
6867
compile "org.aspectj:aspectjweaver:$aspectjVersion"
6968
compile "org.aspectj:aspectjrt:$aspectjVersion"
70-
7169
compile 'com.google.guava:guava:15.0'
7270
compile 'commons-collections:commons-collections:3.2.1'
7371
compile 'org.apache.commons:commons-lang3:3.1'
7472
compile 'com.google.code.findbugs:jsr305:2.0.0'
73+
7574
testCompile 'junit:junit-dep:4.10'
7675
testCompile 'org.springframework:spring-core:4.0.0.RELEASE'
7776
testCompile 'org.springframework:spring-context:4.0.0.RELEASE'

hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/aop/aspectj/HystrixCacheAspect.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@
3131
import java.lang.reflect.Method;
3232

3333
import static com.netflix.hystrix.contrib.javanica.utils.AopUtils.getMethodFromTarget;
34-
import static com.netflix.hystrix.contrib.javanica.utils.EnvUtils.getWeavingMode;
3534
import static com.netflix.hystrix.contrib.javanica.utils.EnvUtils.isCompileWeaving;
36-
import static com.netflix.hystrix.contrib.javanica.utils.ajc.AjcUtils.getOriginalMethod;
35+
import static com.netflix.hystrix.contrib.javanica.utils.ajc.AjcUtils.getAjcMethodAroundAdvice;
3736

3837
/**
3938
* AspectJ aspect to process methods which annotated with annotations from
@@ -57,8 +56,7 @@ public Object methodsAnnotatedWithCacheRemove(final ProceedingJoinPoint joinPoin
5756
MetaHolder metaHolder = MetaHolder.builder()
5857
.args(args).method(method).obj(obj)
5958
.executionType(ExecutionType.SYNCHRONOUS)
60-
.weavingMode(getWeavingMode())
61-
.ajcMethod(isCompileWeaving() ? getOriginalMethod(obj.getClass(), method) : null)
59+
.ajcMethod(isCompileWeaving() ? getAjcMethodAroundAdvice(obj.getClass(), method) : null)
6260
.build();
6361
CacheInvocationContext<CacheRemove> context = CacheInvocationContextFactory
6462
.createCacheRemoveInvocationContext(metaHolder);

hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/aop/aspectj/HystrixCommandAspect.java

+63-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* Copyright 2012 Netflix, Inc.
3-
*
3+
* <p/>
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
* <p/>
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
* <p/>
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -15,6 +15,7 @@
1515
*/
1616
package com.netflix.hystrix.contrib.javanica.aop.aspectj;
1717

18+
import com.google.common.base.Throwables;
1819
import com.google.common.collect.ImmutableMap;
1920
import com.netflix.hystrix.HystrixExecutable;
2021
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
@@ -26,21 +27,25 @@
2627
import com.netflix.hystrix.contrib.javanica.command.MetaHolder;
2728
import com.netflix.hystrix.exception.HystrixBadRequestException;
2829
import org.apache.commons.lang3.Validate;
30+
import org.aspectj.lang.JoinPoint;
2931
import org.aspectj.lang.ProceedingJoinPoint;
3032
import org.aspectj.lang.annotation.Around;
3133
import org.aspectj.lang.annotation.Aspect;
3234
import org.aspectj.lang.annotation.Pointcut;
35+
import org.aspectj.lang.reflect.MethodSignature;
3336

3437
import java.lang.reflect.Method;
38+
import java.lang.reflect.ParameterizedType;
39+
import java.lang.reflect.Type;
3540
import java.util.List;
3641
import java.util.Map;
42+
import java.util.concurrent.Future;
43+
3744

38-
import static com.netflix.hystrix.contrib.javanica.utils.AopUtils.getAjcMethodFromTarget;
3945
import static com.netflix.hystrix.contrib.javanica.utils.AopUtils.getDeclaredMethod;
4046
import static com.netflix.hystrix.contrib.javanica.utils.AopUtils.getMethodFromTarget;
41-
import static com.netflix.hystrix.contrib.javanica.utils.EnvUtils.getWeavingMode;
4247
import static com.netflix.hystrix.contrib.javanica.utils.EnvUtils.isCompileWeaving;
43-
import static com.netflix.hystrix.contrib.javanica.utils.ajc.AjcUtils.getOriginalBatchMethod;
48+
import static com.netflix.hystrix.contrib.javanica.utils.ajc.AjcUtils.getAjcMethodAroundAdvice;
4449

4550
/**
4651
* AspectJ aspect to process methods which annotated with {@link HystrixCommand} annotation.
@@ -108,11 +113,14 @@ public MetaHolder create(final ProceedingJoinPoint joinPoint) {
108113
public abstract MetaHolder create(Object proxy, Method method, Object obj, Object[] args, final ProceedingJoinPoint joinPoint);
109114

110115
MetaHolder.Builder metaHolderBuilder(Object proxy, Method method, Object obj, Object[] args, final ProceedingJoinPoint joinPoint) {
111-
return MetaHolder.builder()
116+
MetaHolder.Builder builder = MetaHolder.builder()
112117
.args(args).method(method).obj(obj).proxyObj(proxy)
113118
.defaultGroupKey(obj.getClass().getSimpleName())
114-
.joinPoint(joinPoint).weavingMode(getWeavingMode())
115-
.ajcMethod(getAjcMethodFromTarget(joinPoint)); // todo: we don't need to call it everytime, only if weaving mode = compile
119+
.joinPoint(joinPoint);
120+
if (isCompileWeaving()) {
121+
builder.ajcMethod(getAjcMethodFromTarget(joinPoint));
122+
}
123+
return builder;
116124
}
117125
}
118126

@@ -121,12 +129,40 @@ private static class CollapserMetaHolderFactory extends MetaHolderFactory {
121129
@Override
122130
public MetaHolder create(Object proxy, Method collapserMethod, Object obj, Object[] args, final ProceedingJoinPoint joinPoint) {
123131
HystrixCollapser hystrixCollapser = collapserMethod.getAnnotation(HystrixCollapser.class);
132+
if (collapserMethod.getParameterTypes().length > 1 || collapserMethod.getParameterTypes().length == 0) {
133+
throw new IllegalStateException("Collapser method must have one argument: " + collapserMethod);
134+
}
135+
124136
Method batchCommandMethod = getDeclaredMethod(obj.getClass(), hystrixCollapser.batchMethod(), List.class);
125137
if (batchCommandMethod == null || !batchCommandMethod.getReturnType().equals(List.class)) {
126138
throw new IllegalStateException("required batch method for collapser is absent: "
127139
+ "(java.util.List) " + obj.getClass().getCanonicalName() + "." +
128140
hystrixCollapser.batchMethod() + "(java.util.List)");
129141
}
142+
143+
if (!collapserMethod.getParameterTypes()[0]
144+
.equals(getGenericParameter(batchCommandMethod.getGenericParameterTypes()[0]))) {
145+
throw new IllegalStateException("required batch method for collapser is absent, wrong generic type: expected"
146+
+ obj.getClass().getCanonicalName() + "." +
147+
hystrixCollapser.batchMethod() + "(java.util.List<" + collapserMethod.getParameterTypes()[0] + ">), but it's " +
148+
getGenericParameter(batchCommandMethod.getGenericParameterTypes()[0]));
149+
}
150+
151+
Class<?> collapserMethodReturnType;
152+
if (Future.class.isAssignableFrom(collapserMethod.getReturnType())) {
153+
collapserMethodReturnType = getGenericParameter(collapserMethod.getGenericReturnType());
154+
} else {
155+
collapserMethodReturnType = collapserMethod.getReturnType();
156+
}
157+
158+
if (!collapserMethodReturnType
159+
.equals(getGenericParameter(batchCommandMethod.getGenericReturnType()))) {
160+
throw new IllegalStateException("Return type of batch method must be java.util.List parametrized with corresponding type: expected " +
161+
"(java.util.List<" + collapserMethodReturnType + ">)" + obj.getClass().getCanonicalName() + "." +
162+
hystrixCollapser.batchMethod() + "(java.util.List<" + collapserMethod.getParameterTypes()[0] + ">), but it's " +
163+
getGenericParameter(batchCommandMethod.getGenericReturnType()));
164+
}
165+
130166
HystrixCommand hystrixCommand = batchCommandMethod.getAnnotation(HystrixCommand.class);
131167
if (hystrixCommand == null) {
132168
throw new IllegalStateException("batch method must be annotated with HystrixCommand annotation");
@@ -136,11 +172,10 @@ public MetaHolder create(Object proxy, Method collapserMethod, Object obj, Objec
136172
MetaHolder.Builder builder = MetaHolder.builder()
137173
.args(args).method(batchCommandMethod).obj(obj).proxyObj(proxy)
138174
.defaultGroupKey(obj.getClass().getSimpleName())
139-
.joinPoint(joinPoint).weavingMode(getWeavingMode());
140-
175+
.joinPoint(joinPoint);
141176

142-
if(isCompileWeaving()){
143-
builder.ajcMethod(getOriginalBatchMethod(obj.getClass(), batchCommandMethod.getName()));
177+
if (isCompileWeaving()) {
178+
builder.ajcMethod(getAjcMethodAroundAdvice(obj.getClass(), batchCommandMethod.getName(), List.class));
144179
}
145180

146181
builder.hystrixCollapser(hystrixCollapser);
@@ -175,4 +210,19 @@ static HystrixPointcutType of(Method method) {
175210
}
176211
}
177212

213+
private static Method getAjcMethodFromTarget(JoinPoint joinPoint) {
214+
return getAjcMethodAroundAdvice(joinPoint.getTarget().getClass(), (MethodSignature) joinPoint.getSignature());
215+
}
216+
217+
218+
private static Class<?> getGenericParameter(Type type) {
219+
Type tType = ((ParameterizedType) type).getActualTypeArguments()[0];
220+
String className = tType.toString().split(" ")[1];
221+
try {
222+
return Class.forName(className);
223+
} catch (ClassNotFoundException e) {
224+
throw Throwables.propagate(e);
225+
}
226+
}
227+
178228
}

hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/aop/aspectj/WeavingMode.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2012 Netflix, Inc.
2+
* Copyright 2015 Netflix, Inc.
33
* <p/>
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/command/AbstractHystrixCommandFactory.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
2323
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
2424
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
25-
import com.netflix.hystrix.contrib.javanica.utils.EnvUtils;
2625
import org.apache.commons.lang3.StringUtils;
2726
import org.apache.commons.lang3.Validate;
2827

@@ -34,7 +33,7 @@
3433
import static com.netflix.hystrix.contrib.javanica.cache.CacheInvocationContextFactory.createCacheRemoveInvocationContext;
3534
import static com.netflix.hystrix.contrib.javanica.cache.CacheInvocationContextFactory.createCacheResultInvocationContext;
3635
import static com.netflix.hystrix.contrib.javanica.utils.EnvUtils.isCompileWeaving;
37-
import static com.netflix.hystrix.contrib.javanica.utils.ajc.AjcUtils.getOriginalMethod;
36+
import static com.netflix.hystrix.contrib.javanica.utils.ajc.AjcUtils.getAjcMethodAroundAdvice;
3837

3938
/**
4039
* Base implementation of {@link HystrixCommandFactory} interface.
@@ -95,7 +94,6 @@ CommandAction createFallbackAction(MetaHolder metaHolder,
9594
.obj(metaHolder.getObj())
9695
.method(fallbackMethod)
9796
.ajcMethod(getAjcMethod(metaHolder.getObj(), fallbackMethod))
98-
.weavingMode(EnvUtils.getWeavingMode())
9997
.args(metaHolder.getArgs())
10098
.defaultCollapserKey(metaHolder.getDefaultCollapserKey())
10199
.defaultCommandKey(fallbackMethod.getName())
@@ -104,12 +102,11 @@ CommandAction createFallbackAction(MetaHolder metaHolder,
104102
.hystrixCommand(fallbackMethod.getAnnotation(HystrixCommand.class)).build();
105103
fallbackAction = new LazyCommandExecutionAction(GenericHystrixCommandFactory.getInstance(), fmMetaHolder, collapsedRequests);
106104
} else {
107-
// if falback methid isn't annotated with command annotation then we don't need to get ajc method for this
105+
108106
MetaHolder fmMetaHolder = MetaHolder.builder()
109107
.obj(metaHolder.getObj())
110108
.method(fallbackMethod)
111-
.ajcMethod(null)
112-
.weavingMode(EnvUtils.getWeavingMode())
109+
.ajcMethod(null) // if fallback method isn't annotated with command annotation then we don't need to get ajc method for this
113110
.args(metaHolder.getArgs()).build();
114111

115112
fallbackAction = new MethodExecutionAction(fmMetaHolder.getObj(), fallbackMethod, fmMetaHolder.getArgs(), fmMetaHolder);
@@ -126,7 +123,7 @@ CommandAction createFallbackAction(MetaHolder metaHolder,
126123

127124
private Method getAjcMethod(Object target, Method fallback) {
128125
if (isCompileWeaving()) {
129-
return getOriginalMethod(target.getClass(), fallback);
126+
return getAjcMethodAroundAdvice(target.getClass(), fallback);
130127
}
131128
return null;
132129
}

hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/command/LazyCommandExecutionAction.java

-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ private MetaHolder createHolder(ExecutionType executionType) {
7474
.obj(metaHolder.getObj())
7575
.method(metaHolder.getMethod())
7676
.ajcMethod(metaHolder.getAjcMethod())
77-
.weavingMode(metaHolder.getWeavingMode())
7877
.executionType(executionType)
7978
.args(metaHolder.getArgs())
8079
.defaultCollapserKey(metaHolder.getDefaultCollapserKey())
@@ -90,7 +89,6 @@ private MetaHolder createHolder(ExecutionType executionType, Object[] args) {
9089
.method(metaHolder.getMethod())
9190
.executionType(executionType)
9291
.ajcMethod(metaHolder.getAjcMethod())
93-
.weavingMode(metaHolder.getWeavingMode())
9492
.args(args)
9593
.defaultCollapserKey(metaHolder.getDefaultCollapserKey())
9694
.defaultCommandKey(metaHolder.getDefaultCommandKey())

hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/command/MetaHolder.java

-12
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public class MetaHolder {
4646
private final String defaultCollapserKey;
4747
private final ExecutionType executionType;
4848
private final ExecutionType collapserExecutionType;
49-
private final WeavingMode weavingMode;
5049
private final JoinPoint joinPoint;
5150

5251
private MetaHolder(Builder builder) {
@@ -64,7 +63,6 @@ private MetaHolder(Builder builder) {
6463
this.hystrixCollapser = builder.hystrixCollapser;
6564
this.executionType = builder.executionType;
6665
this.collapserExecutionType = builder.collapserExecutionType;
67-
this.weavingMode = builder.weavingMode;
6866
this.joinPoint = builder.joinPoint;
6967
}
7068

@@ -136,10 +134,6 @@ public boolean isCollapser(){
136134
return hystrixCollapser!=null;
137135
}
138136

139-
public WeavingMode getWeavingMode() {
140-
return weavingMode;
141-
}
142-
143137
public JoinPoint getJoinPoint() {
144138
return joinPoint;
145139
}
@@ -160,7 +154,6 @@ public static final class Builder {
160154
private String defaultCollapserKey;
161155
private ExecutionType executionType;
162156
private ExecutionType collapserExecutionType;
163-
private WeavingMode weavingMode;
164157
private JoinPoint joinPoint;
165158

166159
public Builder hystrixCollapser(HystrixCollapser hystrixCollapser) {
@@ -233,11 +226,6 @@ public Builder defaultCollapserKey(String defCollapserKey) {
233226
return this;
234227
}
235228

236-
public Builder weavingMode(WeavingMode weavingMode) {
237-
this.weavingMode = weavingMode;
238-
return this;
239-
}
240-
241229
public Builder joinPoint(JoinPoint joinPoint) {
242230
this.joinPoint = joinPoint;
243231
return this;

hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/command/MethodExecutionAction.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@
1616
package com.netflix.hystrix.contrib.javanica.command;
1717

1818

19-
import com.netflix.hystrix.contrib.javanica.aop.aspectj.WeavingMode;
2019
import com.netflix.hystrix.contrib.javanica.command.closure.Closure;
2120
import com.netflix.hystrix.contrib.javanica.command.closure.ClosureFactoryRegistry;
2221
import com.netflix.hystrix.contrib.javanica.exception.CommandActionExecutionException;
2322
import com.netflix.hystrix.contrib.javanica.exception.ExceptionUtils;
24-
import org.aspectj.lang.JoinPoint;
2523

2624
import java.lang.reflect.InvocationTargetException;
2725
import java.lang.reflect.Method;
2826

27+
import static com.netflix.hystrix.contrib.javanica.utils.EnvUtils.isCompileWeaving;
2928
import static com.netflix.hystrix.contrib.javanica.utils.ajc.AjcUtils.invokeAjcMethod;
3029

3130
/**
@@ -57,8 +56,6 @@ public MethodExecutionAction(Object object, Method method, Object[] args, MetaHo
5756
this.metaHolder = metaHolder;
5857
}
5958

60-
61-
6259
public Object getObject() {
6360
return object;
6461
}
@@ -108,7 +105,7 @@ private Object execute(Object o, Method m, Object... args) throws CommandActionE
108105
Object result = null;
109106
try {
110107
m.setAccessible(true); // suppress Java language access
111-
if (WeavingMode.COMPILE == metaHolder.getWeavingMode() && metaHolder.getAjcMethod() != null) { // metaHolder.getAjcMethod() != null todo this is hack, refactor
108+
if (isCompileWeaving() && metaHolder.getAjcMethod() != null) {
112109
result = invokeAjcMethod(metaHolder.getAjcMethod(), o, metaHolder, args);
113110
} else {
114111
result = m.invoke(o, args);

hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/command/closure/AbstractClosureFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
package com.netflix.hystrix.contrib.javanica.command.closure;
1717

1818
import com.google.common.base.Throwables;
19-
import com.netflix.hystrix.contrib.javanica.aop.aspectj.WeavingMode;
2019
import com.netflix.hystrix.contrib.javanica.command.ClosureCommand;
2120
import com.netflix.hystrix.contrib.javanica.command.MetaHolder;
2221

2322
import java.lang.reflect.InvocationTargetException;
2423
import java.lang.reflect.Method;
2524

25+
import static com.netflix.hystrix.contrib.javanica.utils.EnvUtils.isCompileWeaving;
2626
import static com.netflix.hystrix.contrib.javanica.utils.ajc.AjcUtils.invokeAjcMethod;
2727
import static org.slf4j.helpers.MessageFormatter.format;
2828

@@ -54,7 +54,7 @@ public Closure createClosure(MetaHolder metaHolder, Method method, Object o, Obj
5454
try {
5555
Object closureObj;
5656
method.setAccessible(true);
57-
if (WeavingMode.COMPILE == metaHolder.getWeavingMode()) {
57+
if (isCompileWeaving()) {
5858
closureObj = invokeAjcMethod(metaHolder.getAjcMethod(), o, metaHolder, args);
5959
} else {
6060
closureObj = method.invoke(o, args); // creates instance of an anonymous class

0 commit comments

Comments
 (0)