mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Change class names to modules names in packages
Reviewed By: achen1 Differential Revision: D9508095 fbshipit-source-id: e3973ea417c803110eb8612c854a6374a849474b
This commit is contained in:
committed by
Facebook Github Bot
parent
5eaa2d29c0
commit
48169b28e2
@@ -1,73 +1,45 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
* <p>This source code is licensed under the MIT license found in the LICENSE file in the root
|
||||
* directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import com.facebook.react.common.build.ReactBuildConfig;
|
||||
import java.lang.reflect.Constructor;
|
||||
import com.facebook.common.logging.FLog;
|
||||
import com.facebook.react.module.annotations.ReactModule;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Provider;
|
||||
|
||||
/**
|
||||
* A specification for a native module. This exists so that we don't have to pay the cost
|
||||
* for creation until/if the module is used.
|
||||
*
|
||||
* If your module either has a default constructor or one taking ReactApplicationContext you can use
|
||||
* {@link #simple(Class)} or {@link #simple(Class, ReactApplicationContext)}} methods.
|
||||
* A specification for a native module. This exists so that we don't have to pay the cost for
|
||||
* creation until/if the module is used.
|
||||
*/
|
||||
public class ModuleSpec {
|
||||
private static final Class[] EMPTY_SIGNATURE = {};
|
||||
private static final Class[] CONTEXT_SIGNATURE = { ReactApplicationContext.class };
|
||||
|
||||
private static final String TAG = "ModuleSpec";
|
||||
private final @Nullable Class<? extends NativeModule> mType;
|
||||
private final Provider<? extends NativeModule> mProvider;
|
||||
private final String mClassName;
|
||||
|
||||
/**
|
||||
* Simple spec for modules with a default constructor.
|
||||
*/
|
||||
public static ModuleSpec simple(final Class<? extends NativeModule> type) {
|
||||
return new ModuleSpec(type, new ConstructorProvider(type, EMPTY_SIGNATURE) {
|
||||
@Override
|
||||
public NativeModule get() {
|
||||
try {
|
||||
return getConstructor(type, EMPTY_SIGNATURE).newInstance();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("ModuleSpec with class: " + type.getName(), e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple spec for modules with a constructor taking ReactApplicationContext.
|
||||
*/
|
||||
public static ModuleSpec simple(
|
||||
final Class<? extends NativeModule> type,
|
||||
final ReactApplicationContext context) {
|
||||
return new ModuleSpec(type, new ConstructorProvider(type, CONTEXT_SIGNATURE) {
|
||||
@Override
|
||||
public NativeModule get() {
|
||||
try {
|
||||
return getConstructor(type, CONTEXT_SIGNATURE).newInstance(context);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("ModuleSpec with class: " + type.getName(), e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
private final String mName;
|
||||
|
||||
public static ModuleSpec viewManagerSpec(Provider<? extends NativeModule> provider) {
|
||||
return new ModuleSpec(null, provider);
|
||||
return new ModuleSpec(provider);
|
||||
}
|
||||
|
||||
public static ModuleSpec nativeModuleSpec(
|
||||
Class<? extends NativeModule> type, Provider<? extends NativeModule> provider) {
|
||||
return new ModuleSpec(provider, type.getName());
|
||||
ReactModule annotation = type.getAnnotation(ReactModule.class);
|
||||
if (annotation == null) {
|
||||
FLog.w(
|
||||
TAG,
|
||||
"Could not find @ReactModule annotation on "
|
||||
+ type.getName()
|
||||
+ ". So creating the module eagerly to get the name. Consider adding an annotation to make this Lazy");
|
||||
NativeModule nativeModule = provider.get();
|
||||
return new ModuleSpec(provider, nativeModule.getName());
|
||||
} else {
|
||||
return new ModuleSpec(provider, annotation.name());
|
||||
}
|
||||
}
|
||||
|
||||
public static ModuleSpec nativeModuleSpec(
|
||||
@@ -75,50 +47,32 @@ public class ModuleSpec {
|
||||
return new ModuleSpec(provider, className);
|
||||
}
|
||||
|
||||
private ModuleSpec(
|
||||
@Nullable Class<? extends NativeModule> type, Provider<? extends NativeModule> provider) {
|
||||
mType = type;
|
||||
mProvider = provider;
|
||||
mClassName = type == null ? null : type.getName();
|
||||
}
|
||||
|
||||
public ModuleSpec(Provider<? extends NativeModule> provider, String name) {
|
||||
/**
|
||||
* Called by View Managers
|
||||
*
|
||||
* @param provider
|
||||
*/
|
||||
private ModuleSpec(Provider<? extends NativeModule> provider) {
|
||||
mType = null;
|
||||
mProvider = provider;
|
||||
mClassName = name;
|
||||
mName = null;
|
||||
}
|
||||
|
||||
private ModuleSpec(Provider<? extends NativeModule> provider, String name) {
|
||||
mType = null;
|
||||
mProvider = provider;
|
||||
mName = name;
|
||||
}
|
||||
|
||||
public @Nullable Class<? extends NativeModule> getType() {
|
||||
return mType;
|
||||
}
|
||||
|
||||
public String getClassName(){return mClassName;}
|
||||
public String getName() {
|
||||
return mName;
|
||||
}
|
||||
|
||||
public Provider<? extends NativeModule> getProvider() {
|
||||
return mProvider;
|
||||
}
|
||||
|
||||
private static abstract class ConstructorProvider implements Provider<NativeModule> {
|
||||
protected @Nullable Constructor<? extends NativeModule> mConstructor;
|
||||
|
||||
public ConstructorProvider(Class<? extends NativeModule> type, Class[] signature) {
|
||||
if (ReactBuildConfig.DEBUG) {
|
||||
try {
|
||||
mConstructor = getConstructor(type, signature);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new IllegalArgumentException("No such constructor", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected Constructor<? extends NativeModule> getConstructor(
|
||||
Class<? extends NativeModule> mType,
|
||||
Class[] signature) throws NoSuchMethodException {
|
||||
if (mConstructor != null) {
|
||||
return mConstructor;
|
||||
} else {
|
||||
return mType.getConstructor(signature);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user