Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.common.utils;

import org.apache.dubbo.rpc.model.FrameworkModel;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public final class DefaultParameterNameReader implements ParameterNameReader {

private final Map<Object, Optional<String[]>> cache = CollectionUtils.newConcurrentHashMap();
private final List<ParameterNameReader> readers;

public DefaultParameterNameReader(FrameworkModel frameworkModel) {
readers = frameworkModel.getActivateExtensions(ParameterNameReader.class);
}

@Override
public String[] readParameterNames(Method method) {
return cache.computeIfAbsent(method, k -> {
String[] names = readByReflection(method.getParameters());
if (names == null) {
for (ParameterNameReader reader : readers) {
names = reader.readParameterNames(method);
if (names != null) {
break;
}
}
}
return Optional.ofNullable(names);
})
.orElse(null);
}

@Override
public String[] readParameterNames(Constructor<?> ctor) {
return cache.computeIfAbsent(ctor, k -> {
String[] names = readByReflection(ctor.getParameters());
if (names == null) {
for (ParameterNameReader reader : readers) {
names = reader.readParameterNames(ctor);
if (names != null) {
break;
}
}
}
return Optional.ofNullable(names);
})
.orElse(null);
}

private static String[] readByReflection(Parameter[] parameters) {
int len = parameters.length;
if (len == 0) {
return StringUtils.EMPTY_STRING_ARRAY;
}
String[] names = new String[len];
for (int i = 0; i < len; i++) {
Parameter param = parameters[i];
if (!param.isNamePresent()) {
return null;
}
names[i] = param.getName();
}
return names;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.common.utils;

import org.apache.dubbo.common.constants.LoggerCodeConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Map;
import javassist.ClassPool;
import javassist.CtBehavior;
import javassist.CtConstructor;
import javassist.CtMethod;
import javassist.LoaderClassPath;
import javassist.bytecode.CodeAttribute;
import javassist.bytecode.Descriptor;
import javassist.bytecode.LocalVariableAttribute;

import static org.apache.dubbo.common.logger.LoggerFactory.getErrorTypeAwareLogger;

@Activate(order = 100, onClass = "javassist.ClassPool")
public class JavassistParameterNameReader implements ParameterNameReader {

private static final ErrorTypeAwareLogger LOG = getErrorTypeAwareLogger(JavassistParameterNameReader.class);

private final Map<Integer, ClassPool> classPoolMap = CollectionUtils.newConcurrentHashMap();

@Override
public String[] readParameterNames(Method method) {
try {
Class<?>[] paramTypes = method.getParameterTypes();
if (paramTypes.length == 0) {
return StringUtils.EMPTY_STRING_ARRAY;
}
String descriptor = getDescriptor(paramTypes, method.getReturnType());
Class<?> clazz = method.getDeclaringClass();
CtMethod ctMethod = getClassPool(clazz).get(clazz.getName()).getMethod(method.getName(), descriptor);
return read(ctMethod, Modifier.isStatic(method.getModifiers()) ? 0 : 1, paramTypes.length);
} catch (Throwable t) {
LOG.warn(LoggerCodeConstants.INTERNAL_ERROR, "", "", "Read parameter names error", t);
return null;
}
}

@Override
public String[] readParameterNames(Constructor<?> ctor) {
try {
Class<?>[] paramTypes = ctor.getParameterTypes();
if (paramTypes.length == 0) {
return StringUtils.EMPTY_STRING_ARRAY;
}
String descriptor = getDescriptor(paramTypes, void.class);
Class<?> clazz = ctor.getDeclaringClass();
CtConstructor ctCtor = getClassPool(clazz).get(clazz.getName()).getConstructor(descriptor);
return read(ctCtor, 1, paramTypes.length);
} catch (Throwable t) {
LOG.warn(LoggerCodeConstants.INTERNAL_ERROR, "", "", "Read parameter names error", t);
return null;
}
}

private static String getDescriptor(Class<?>[] parameterTypes, Class<?> returnType) {
StringBuilder descriptor = new StringBuilder(32);
descriptor.append('(');
for (Class<?> type : parameterTypes) {
descriptor.append(toJvmName(type));
}
descriptor.append(')');
descriptor.append(toJvmName(returnType));
return descriptor.toString();
}

private static String toJvmName(Class<?> clazz) {
return clazz.isArray() ? Descriptor.toJvmName(clazz.getName()) : Descriptor.of(clazz.getName());
}

private ClassPool getClassPool(Class<?> clazz) {
ClassLoader classLoader = ClassUtils.getClassLoader(clazz);
return classPoolMap.computeIfAbsent(System.identityHashCode(classLoader), k -> {
ClassPool pool = new ClassPool();
pool.appendClassPath(new LoaderClassPath(classLoader));
return pool;
});
}

private static String[] read(CtBehavior behavior, int start, int len) {
if (behavior == null) {
return null;
}
CodeAttribute codeAttr = behavior.getMethodInfo().getCodeAttribute();
if (codeAttr == null) {
return null;
}
LocalVariableAttribute attr = (LocalVariableAttribute) codeAttr.getAttribute(LocalVariableAttribute.tag);
if (attr == null) {
return null;
}
String[] names = new String[len];
for (int i = 0, tLen = attr.tableLength(); i < tLen; i++) {
int j = attr.index(i) - start;
if (j >= 0 && j < len) {
names[j] = attr.variableName(i);
}
}

return names;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.common.utils;

import org.apache.dubbo.common.extension.ExtensionScope;
import org.apache.dubbo.common.extension.SPI;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

@SPI(scope = ExtensionScope.FRAMEWORK)
public interface ParameterNameReader {

String[] readParameterNames(Method method);

String[] readParameterNames(Constructor<?> ctor);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
javassist=org.apache.dubbo.common.utils.JavassistParameterNameReader
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.common.utils;

import org.apache.dubbo.common.URL;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.emptyArray;
import static org.hamcrest.Matchers.equalTo;

class JavassistParameterNameReaderTest {

private final ParameterNameReader reader = new JavassistParameterNameReader();

@Test
void readFromConstructor() {
Class<?> clazz = URL.class;
for (Constructor<?> ctor : clazz.getConstructors()) {
String[] names = reader.readParameterNames(ctor);
// System.out.println(ctor + " -> " + Arrays.toString(names));
if (names.length == 7) {
assertThat(names[0], equalTo("protocol"));
}
}
}

@Test
void readFromMethod() {
Class<?> clazz = URL.class;
for (Method method : clazz.getMethods()) {
String[] names = reader.readParameterNames(method);
// System.out.println(method + " -> " + Arrays.toString(names));
switch (method.getName()) {
case "getAddress":
assertThat(names, emptyArray());
break;
case "setAddress":
assertThat(names[0], equalTo("address"));
break;
case "buildKey":
assertThat(names[0], equalTo("path"));
break;
default:
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.config.spring.util;

import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.utils.ParameterNameReader;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;

@Activate(onClass = "org.springframework.core.DefaultParameterNameDiscoverer")
public class SpringParameterNameReader implements ParameterNameReader {

private final ParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer();

@Override
public String[] readParameterNames(Method method) {
return discoverer.getParameterNames(method);
}

@Override
public String[] readParameterNames(Constructor<?> ctor) {
return discoverer.getParameterNames(ctor);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring=org.apache.dubbo.config.spring.util.SpringParameterNameReader
3 changes: 3 additions & 0 deletions dubbo-distribution/dubbo-all-shaded/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,9 @@
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/dubbo/internal/org.apache.dubbo.common.threadpool.manager.ExecutorRepository</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/dubbo/internal/org.apache.dubbo.common.utils.ParameterNameReader</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/dubbo/internal/org.apache.dubbo.common.url.component.param.DynamicParamSource</resource>
</transformer>
Expand Down
3 changes: 3 additions & 0 deletions dubbo-distribution/dubbo-all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,9 @@
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/dubbo/internal/org.apache.dubbo.common.url.component.param.DynamicParamSource</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/dubbo/internal/org.apache.dubbo.common.utils.ParameterNameReader</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/dubbo/internal/org.apache.dubbo.config.ConfigInitializer</resource>
</transformer>
Expand Down
3 changes: 3 additions & 0 deletions dubbo-distribution/dubbo-core-spi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/dubbo/internal/org.apache.dubbo.config.ConfigInitializer</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/dubbo/internal/org.apache.dubbo.common.utils.ParameterNameReader</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/dubbo/internal/org.apache.dubbo.config.ConfigPostProcessor</resource>
</transformer>
Expand Down
Loading
Loading