windwoodfirehill opened a new pull request, #16506:
URL: https://github.com/apache/dubbo/pull/16506

   ## What is the purpose of the change?
   
   `ExtensionLoader#loadClassIfActive` is supposed to skip an extension whose 
`@Activate(onClass = ...)` names a class that is not on the classpath. It does 
this for `org.apache.dubbo.common.extension.Activate`, but not for the Dubbo 2 
annotation `com.alibaba.dubbo.common.extension.Activate`, even though the 
method has a branch written for it. That branch can never run.
   
   ### Why the Dubbo 2 branch is dead code
   
   ```java
   Activate activate = clazz.getAnnotation(Activate.class);   // (1) only looks 
up org.apache.dubbo's @Activate
   
   if (activate == null) {
       return true;                                           // (2) a class 
with only com.alibaba's @Activate returns here
   }
   String[] onClass = null;
   
   if (activate instanceof Activate) {                        // (3) always 
true at this point
       onClass = ((Activate) activate).onClass();
   } else if (Dubbo2CompactUtils.isEnabled()
           && Dubbo2ActivateUtils.isActivateLoaded()
           && 
Dubbo2ActivateUtils.getActivateClass().isAssignableFrom(activate.getClass())) {
       onClass = Dubbo2ActivateUtils.getOnClass(activate);    // (4) unreachable
   }
   ```
   
   1. `getAnnotation(Activate.class)` returns either `org.apache.dubbo`'s 
`@Activate` or `null`. It never returns the `com.alibaba` one.
   2. For a class that only carries the `com.alibaba` annotation it returns 
`null`, so the method returns `true` and its `onClass` is never read.
   3. In every other case `activate` is an `org.apache.dubbo` `Activate`, so 
`instanceof` is always true and the `else if` is never reached.
   
   The same `if / else if` shape works in `getActivateExtension`, because there 
the value comes from `cachedActivates`, which is typed `Object` and is filled 
by `cacheActivateClass` with either annotation. `loadClassIfActive` uses the 
same shape, but its variable is typed `Activate` and only the new annotation is 
looked up.
   
   ### Impact
   
   With the `com.alibaba` annotation, a class whose `onClass` dependency is 
missing is still registered as an extension, and `cacheActivateClass` still 
makes it an activate extension. It then fails later, at a point that depends on 
how the class uses the missing type. In a standalone reproduction:
   
   - missing type used inside a method body: `getExtension` succeeds, and the 
call fails with `NoClassDefFoundError` when that method runs;
   - missing type in a public method signature: `injectExtension` fails 
(`Class#getMethods`). Since `getActivateExtension` instantiates every matched 
extension, this one extension makes the whole `getActivateExtension` call fail, 
for example while building a filter chain.
   
   The same class annotated with `org.apache.dubbo`'s `@Activate` is skipped at 
load time and never causes these errors.
   
   ## Brief changelog
   
   - `ExtensionLoader#loadClassIfActive`: when `org.apache.dubbo`'s `@Activate` 
is absent, fall back to `com.alibaba`'s `@Activate` under the same condition 
`cacheActivateClass` uses, and type the variable as `Annotation` so the 
existing `else if` branch reads its `onClass`. Classes with 
`org.apache.dubbo`'s `@Activate`, and classes with neither annotation, go 
through exactly the same path as before.
   - Test (`dubbo-compatible`): `OldActivateOnClassExt1Impl` uses 
`com.alibaba`'s `@Activate` with `onClass` pointing to a class that does not 
exist, registered as `old3`. `ExtensionTest#testOldActivateOnClass` asserts it 
is neither loaded nor returned by `getActivateExtension`.
   
   ## Verifying this change
   
   **What each case does when the `onClass` class is missing**
   
   | Annotation on the extension | Before this PR | After this PR | Covered by |
   |---|---|---|---|
   | `org.apache.dubbo` `@Activate(onClass = missing)` | skipped at load time | 
skipped at load time | `ExtensionLoader_Activate_Test#test_onClass` 
(dubbo-common) |
   | `com.alibaba` `@Activate(onClass = missing)` | **loaded and activated** | 
skipped at load time | `ExtensionTest#testOldActivateOnClass` (new) |
   | `com.alibaba` `@Activate` without `onClass` | loaded and activated | 
loaded and activated | `ExtensionTest#testLoadActivateExtension` |
   
   **Test runs**
   
   | Code | Scope | Tests | Result |
   |---|---|---|---|
   | base `df9c5e13bc` + new test only, no fix | `dubbo-compatible` 
`ExtensionTest` | 3 | 1 failure: `testOldActivateOnClass:75 expected: <false> 
but was: <true>` |
   | this PR, rebased on `3.3` (`ae809de2fc`) | `dubbo-common` 
`org.apache.dubbo.common.extension.**` | 91 | all passed |
   | this PR, rebased on `3.3` (`ae809de2fc`) | `dubbo-compatible` 
`ExtensionTest`, `ActivateComparatorTest` | 4 | all passed |
   | this PR | `spotless:check` on both modules | – | passed |
   
   ## Checklist
   - [ ] Make sure there is a 
[GitHub_issue](https://github.com/apache/dubbo/issues) field for the change. 
(No separate issue was opened; the problem is described above. I can open one 
if preferred.)
   - [x] Write a pull request description that is detailed enough to understand 
what the pull request does, how, and why.
   - [x] Write necessary unit-test to verify your logic correction.
   - [ ] Make sure gitHub actions can pass.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to