Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SCB-1071] Protobuf codec support array and primitive and packed #1056

Merged
merged 16 commits into from Jan 14, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion LICENSE
Expand Up @@ -211,7 +211,7 @@ following licenses.

================================================================
For foundations/foundation-vertx/src/main/java/io/vertx/ext/web/impl/MimeTypesUtils.java
transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/RestBodyHandler.java
transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/RestBodyHandler.java
================================================================
This product bundles files from vertx which is licensed under the Apache License v2.
For details, see https://github.com/vert-x3/vertx-web
Expand All @@ -233,3 +233,21 @@ For handlers/handler-bizkeeper/src/main/java/org/apache/servicecomb/bizkeeper/Hy
================================================================
This product bundles files from Hystrix which is licensed under the Apache License v2.
For details, see https://github.com/Netflix/Hystrix

================================================================
For foundations/foundation-protobuf/src/main/java/io/protostuff/runtime/ArrayFieldMapEx.java
foundations/foundation-protobuf/src/main/java/io/protostuff/runtime/FieldMapEx.java
foundations/foundation-protobuf/src/main/java/io/protostuff/runtime/FieldSchema.java
foundations/foundation-protobuf/src/main/java/io/protostuff/runtime/FieldTypeUtils.java
foundations/foundation-protobuf/src/main/java/io/protostuff/runtime/HashFieldMapEx.java
foundations/foundation-protobuf/src/main/java/io/protostuff/ByteBufferInputEx.java
foundations/foundation-protobuf/src/main/java/io/protostuff/InputEx.java
foundations/foundation-protobuf/src/main/java/io/protostuff/OutputEx.java
foundations/foundation-protobuf/src/main/java/io/protostuff/package-info.java
foundations/foundation-protobuf/src/main/java/io/protostuff/ProtobufOutputEx.java
foundations/foundation-protobuf/src/main/java/io/protostuff/SchemaEx.java
foundations/foundation-protobuf/src/main/java/io/protostuff/SchemaReader.java
foundations/foundation-protobuf/src/main/java/io/protostuff/SchemaWriter.java
================================================================
This product bundles files from protostuff which is licensed under the Apache License v2.
For details, see https://github.com/protostuff/protostuff
Expand Up @@ -45,6 +45,9 @@ public SimpleSubscriber(Object instance, Method method) {
try {
lambda = LambdaMetafactoryUtils.createLambda(instance, method, Consumer.class);
} catch (Throwable throwable) {
// because enhance LambdaMetafactoryUtils to support ALL_MODES by reflect
// never run into this branch.
// otherwise create a listener instance of anonymous class will run into this branch
LOGGER.warn("Failed to create lambda for method: {}, fallback to reflect.", method);
lambda = event -> {
try {
Expand Down
Expand Up @@ -25,12 +25,74 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import java.util.HashMap;
import java.util.Map;

import org.apache.servicecomb.foundation.common.utils.bean.BoolGetter;
import org.apache.servicecomb.foundation.common.utils.bean.BoolSetter;
import org.apache.servicecomb.foundation.common.utils.bean.ByteGetter;
import org.apache.servicecomb.foundation.common.utils.bean.ByteSetter;
import org.apache.servicecomb.foundation.common.utils.bean.DoubleGetter;
import org.apache.servicecomb.foundation.common.utils.bean.DoubleSetter;
import org.apache.servicecomb.foundation.common.utils.bean.FloatGetter;
import org.apache.servicecomb.foundation.common.utils.bean.FloatSetter;
import org.apache.servicecomb.foundation.common.utils.bean.Getter;
import org.apache.servicecomb.foundation.common.utils.bean.IntGetter;
import org.apache.servicecomb.foundation.common.utils.bean.IntSetter;
import org.apache.servicecomb.foundation.common.utils.bean.LongGetter;
import org.apache.servicecomb.foundation.common.utils.bean.LongSetter;
import org.apache.servicecomb.foundation.common.utils.bean.Setter;
import org.apache.servicecomb.foundation.common.utils.bean.ShortGetter;
import org.apache.servicecomb.foundation.common.utils.bean.ShortSetter;

public final class LambdaMetafactoryUtils {
private static Lookup lookup = MethodHandles.lookup();
private static Field allowedModesField;

private static final int ALL_MODES = (MethodHandles.Lookup.PRIVATE | MethodHandles.Lookup.PROTECTED
| MethodHandles.Lookup.PACKAGE | MethodHandles.Lookup.PUBLIC);

private static final Lookup LOOKUP = MethodHandles.lookup();

private static final Map<Class<?>, Class<?>> GETTER_MAP = new HashMap<>();

private static final Map<Class<?>, Class<?>> SETTER_MAP = new HashMap<>();

static {
enhanceLambda();
initGetterSetterMap();
}

private static void initGetterSetterMap() {
GETTER_MAP.put(boolean.class, BoolGetter.class);
GETTER_MAP.put(byte.class, ByteGetter.class);
GETTER_MAP.put(short.class, ShortGetter.class);
GETTER_MAP.put(int.class, IntGetter.class);
GETTER_MAP.put(long.class, LongGetter.class);
GETTER_MAP.put(float.class, FloatGetter.class);
GETTER_MAP.put(double.class, DoubleGetter.class);

SETTER_MAP.put(boolean.class, BoolSetter.class);
SETTER_MAP.put(byte.class, ByteSetter.class);
SETTER_MAP.put(short.class, ShortSetter.class);
SETTER_MAP.put(int.class, IntSetter.class);
SETTER_MAP.put(long.class, LongSetter.class);
SETTER_MAP.put(float.class, FloatSetter.class);
SETTER_MAP.put(double.class, DoubleSetter.class);
}

private static void enhanceLambda() {
try {
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);

allowedModesField = Lookup.class.getDeclaredField("allowedModes");
allowedModesField.setAccessible(true);
int modifiers = allowedModesField.getModifiers();
modifiersField.setInt(allowedModesField, modifiers & ~Modifier.FINAL);
} catch (Throwable e) {
throw new IllegalStateException("Failed to init LambdaMetafactoryUtils.", e);
}
}

private LambdaMetafactoryUtils() {
}
Expand All @@ -45,67 +107,82 @@ protected static Method findAbstractMethod(Class<?> functionalInterface) {
return null;
}

public static <T> T createLambda(Object instance, Method instanceMethod, Class<?> functionalIntfCls)
throws Throwable {
Method intfMethod = findAbstractMethod(functionalIntfCls);
MethodHandle methodHandle = lookup.unreflect(instanceMethod);

MethodType intfMethodType = MethodType.methodType(intfMethod.getReturnType(), intfMethod.getParameterTypes());
MethodType instanceMethodType = MethodType
.methodType(instanceMethod.getReturnType(), instanceMethod.getParameterTypes());
CallSite callSite = LambdaMetafactory.metafactory(
lookup,
intfMethod.getName(),
MethodType.methodType(functionalIntfCls, instance.getClass()),
intfMethodType,
methodHandle,
instanceMethodType);

//noinspection unchecked
return (T) callSite.getTarget().bindTo(instance).invoke();
@SuppressWarnings("unchecked")
public static <T> T createLambda(Object instance, Method instanceMethod, Class<?> functionalIntfCls) {
try {
Lookup lookup = LOOKUP.in(instanceMethod.getDeclaringClass());
allowedModesField.set(lookup, ALL_MODES);

Method intfMethod = findAbstractMethod(functionalIntfCls);
MethodHandle methodHandle = lookup.unreflect(instanceMethod);

MethodType intfMethodType = MethodType.methodType(intfMethod.getReturnType(), intfMethod.getParameterTypes());
MethodType instanceMethodType = MethodType
.methodType(instanceMethod.getReturnType(), instanceMethod.getParameterTypes());
CallSite callSite = LambdaMetafactory.metafactory(
lookup,
intfMethod.getName(),
MethodType.methodType(functionalIntfCls, instance.getClass()),
intfMethodType,
methodHandle,
instanceMethodType);

return (T) callSite.getTarget().bindTo(instance).invoke();
} catch (Throwable e) {
throw new IllegalStateException("Failed to create lambda from " + instanceMethod, e);
}
}

public static <T> T createLambda(Method instanceMethod, Class<?> functionalIntfCls)
throws Throwable {
Method intfMethod = findAbstractMethod(functionalIntfCls);
MethodHandle methodHandle = lookup.unreflect(instanceMethod);

MethodType intfMethodType = MethodType.methodType(intfMethod.getReturnType(), intfMethod.getParameterTypes());
MethodType instanceMethodType = methodHandle.type();
CallSite callSite = LambdaMetafactory.metafactory(
lookup,
intfMethod.getName(),
MethodType.methodType(functionalIntfCls),
intfMethodType,
methodHandle,
instanceMethodType);

//noinspection unchecked
return (T) callSite.getTarget().invoke();
@SuppressWarnings("unchecked")
public static <T> T createLambda(Method instanceMethod, Class<?> functionalIntfCls) {
try {
Lookup lookup = LOOKUP.in(instanceMethod.getDeclaringClass());
allowedModesField.set(lookup, ALL_MODES);

Method intfMethod = findAbstractMethod(functionalIntfCls);
MethodHandle methodHandle = lookup.unreflect(instanceMethod);

MethodType intfMethodType = MethodType.methodType(intfMethod.getReturnType(), intfMethod.getParameterTypes());
MethodType instanceMethodType = methodHandle.type();
CallSite callSite = LambdaMetafactory.metafactory(
lookup,
intfMethod.getName(),
MethodType.methodType(functionalIntfCls),
intfMethodType,
methodHandle,
instanceMethodType);

return (T) callSite.getTarget().invoke();
} catch (Throwable e) {
throw new IllegalStateException("Failed to create lambda from " + instanceMethod, e);
}
}

public static Getter createGetter(Method getMethod) throws Throwable {
return createLambda(getMethod, Getter.class);
public static <T> T createGetter(Method getMethod) {
Class<?> getterCls = GETTER_MAP.getOrDefault(getMethod.getReturnType(), Getter.class);
return createLambda(getMethod, getterCls);
}

// slower than reflect directly
public static Getter createGetter(Field field) {
@SuppressWarnings("unchecked")
public static <C, F> Getter<C, F> createGetter(Field field) {
field.setAccessible(true);
return instance -> {
try {
return field.get(instance);
return (F) field.get(instance);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
};
}

public static Setter createSetter(Method setMethod) throws Throwable {
return createLambda(setMethod, Setter.class);
public static <T> T createSetter(Method setMethod) throws Throwable {
Class<?> setterCls = SETTER_MAP.getOrDefault(setMethod.getParameterTypes()[0], Setter.class);
return createLambda(setMethod, setterCls);
}

// slower than reflect directly
public static Setter createSetter(Field field) {
public static <C, F> Setter<C, F> createSetter(Field field) {
field.setAccessible(true);
return (instance, value) -> {
try {
Expand Down
Expand Up @@ -17,12 +17,19 @@

package org.apache.servicecomb.foundation.common.utils;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.springframework.util.ReflectionUtils;

import com.google.common.reflect.TypeToken;

public final class ReflectUtils {
private static final Field MODIFIERS_FIELD =
ReflectionUtils.findField(Field.class, "modifiers");
Expand Down Expand Up @@ -62,4 +69,26 @@ public static Method findMethod(Class<?> cls, String methodName) {

return null;
}

@SuppressWarnings("unchecked")
public static <T> Class<T> getFieldArgument(Class<?> genericCls, String fieldName) {
try {
Type generic = FieldUtils.getField(genericCls, fieldName).getGenericType();
TypeToken<?> token = TypeToken.of(genericCls).resolveType(generic);
Type fieldType = token.getType();
Type argument = ((ParameterizedType) fieldType).getActualTypeArguments()[0];
if (argument instanceof GenericArrayType) {
return (Class<T>) TypeToken.of(argument).getRawType();
}

return (Class<T>) argument;
} catch (Throwable e) {
throw new IllegalStateException("Failed to get generic argument.", e);
}
}

@SuppressWarnings("unchecked")
public static <T> T constructArrayType(Class<?> cls) {
return (T) Array.newInstance(cls, 0).getClass();
}
}
@@ -0,0 +1,30 @@
/*
* 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.servicecomb.foundation.common.utils.bean;

public class ArrayGetter<T> implements Getter<T[], T> {
private final int idx;

public ArrayGetter(int idx) {
this.idx = idx;
}

@Override
public T get(T[] instance) {
return instance[idx];
}
}
@@ -0,0 +1,30 @@
/*
* 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.servicecomb.foundation.common.utils.bean;

public class ArraySetter<T> implements Setter<T[], T> {
private final int idx;

public ArraySetter(int idx) {
this.idx = idx;
}

@Override
public void set(T[] instance, T value) {
instance[idx] = value;
}
}
@@ -0,0 +1,21 @@
/*
* 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.servicecomb.foundation.common.utils.bean;

public interface BoolGetter<T> {
boolean get(T instance);
}
@@ -0,0 +1,21 @@
/*
* 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.servicecomb.foundation.common.utils.bean;

public interface BoolSetter<T> {
void set(T instance, boolean value);
}