• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aspectran / aspectran / #3073

16 Nov 2023 11:58AM CUT coverage: 37.858% (-0.01%) from 37.871%
#3073

push

github

topframe
[maven-release-plugin] prepare release v7.4.0

12304 of 32500 relevant lines covered (37.86%)

0.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

23.53
/core/src/main/java/com/aspectran/core/component/bean/proxy/JavassistDynamicProxyBean.java
1
/*
2
 * Copyright (c) 2008-2023 The Aspectran Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.aspectran.core.component.bean.proxy;
17

18
import com.aspectran.core.activity.Activity;
19
import com.aspectran.core.activity.InstantActivity;
20
import com.aspectran.core.activity.InstantActivityException;
21
import com.aspectran.core.component.aspect.AspectAdviceRuleRegistry;
22
import com.aspectran.core.context.ActivityContext;
23
import com.aspectran.core.context.rule.BeanRule;
24
import javassist.util.proxy.MethodHandler;
25
import javassist.util.proxy.ProxyFactory;
26

27
import java.lang.reflect.Method;
28

29
/**
30
 * The Class JavassistDynamicBeanProxy.
31
 *
32
 * @since 1.1.0
33
 */
34
public class JavassistDynamicProxyBean extends AbstractDynamicProxyBean implements MethodHandler  {
35

36
    private final ActivityContext context;
37

38
    private final BeanRule beanRule;
39

40
    private JavassistDynamicProxyBean(ActivityContext context, BeanRule beanRule) {
41
        super(context.getAspectRuleRegistry());
1✔
42
        this.context = context;
1✔
43
        this.beanRule = beanRule;
1✔
44
    }
1✔
45

46
    @Override
47
    public Object invoke(Object self, Method overridden, Method proceed, Object[] args) throws Throwable {
48
        if (isAvoidAdvice(overridden)) {
×
49
            return proceed.invoke(self, args);
×
50
        }
51
        if (context.hasCurrentActivity()) {
×
52
            Activity activity = context.getCurrentActivity();
×
53
            return invoke(self, overridden, proceed, args, activity);
×
54
        } else {
55
            try {
56
                Activity activity = new InstantActivity(context);
×
57
                return activity.perform(() -> invoke(self, overridden, proceed, args, activity));
×
58
            } catch (Exception e) {
×
59
                throw new InstantActivityException(e);
×
60
            }
61
        }
62
    }
63

64
    private Object invoke(Object self, Method overridden, Method proceed, Object[] args, Activity activity)
65
            throws Exception {
66
        String beanId = beanRule.getId();
×
67
        String className = beanRule.getClassName();
×
68
        String methodName = overridden.getName();
×
69
        AspectAdviceRuleRegistry aarr = getAspectAdviceRuleRegistry(activity, beanId, className, methodName);
×
70
        if (aarr == null) {
×
71
            return proceed.invoke(self, args);
×
72
        }
73
        try {
74
            try {
75
                beforeAdvice(aarr.getBeforeAdviceRuleList(), beanRule, activity);
×
76
                Object result = proceed.invoke(self, args);
×
77
                afterAdvice(aarr.getAfterAdviceRuleList(), beanRule, activity);
×
78
                return result;
×
79
            } finally {
80
                finallyAdvice(aarr.getFinallyAdviceRuleList(), beanRule, activity);
×
81
            }
82
        } catch (Exception e) {
×
83
            if (exceptionally(aarr.getExceptionRuleList(), e, activity)) {
×
84
                return null;
×
85
            }
86
            throw e;
×
87
        }
88
    }
89

90
    /**
91
     * Creates a proxy class of bean and returns an instance of that class.
92
     * @param context the activity context
93
     * @param beanRule the bean rule
94
     * @param args the arguments passed to a constructor
95
     * @param argTypes the parameter types for a constructor
96
     * @return a new proxy bean object
97
     */
98
    public static Object newInstance(ActivityContext context, BeanRule beanRule, Object[] args, Class<?>[] argTypes) {
99
        try {
100
            ProxyFactory proxyFactory = new ProxyFactory();
1✔
101
            proxyFactory.setSuperclass(beanRule.getBeanClass());
1✔
102
            MethodHandler methodHandler = new JavassistDynamicProxyBean(context, beanRule);
1✔
103
            return proxyFactory.create(argTypes, args, methodHandler);
1✔
104
        } catch (Exception e) {
×
105
            throw new ProxyBeanInstantiationException(beanRule, e);
×
106
        }
107
    }
108

109
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc