我正在使用一个特定的API,它将一个类作为字符串返回给我。我需要在给定的字符串的某个块上插入一个字符串。所以基本上是一个函数,它接受整个字符串,并将我想要添加的字符串附加到特定块之后。
传递给我的字符串是一个java类,我想在所有现有函数之后在它的末尾输入我自己的函数。如果你感到困惑…我没有访问java文件,这是唯一的方法来修改文件,当你使用配置插件在expo react native。
我相信某种正则表达式应该被用来得到这个结果?但是我真的不知道如何针对字符串的特定部分。
我要添加的字符串:
'@Overridenprotected List getPackages() {nreturn Arrays。asList ( nnew MainReactPackage ()//& lt;——添加逗号 nnew RNFSPackage ()//& lt ;---------- 添加包 n); n}’
传递给我的字符串
import expo.modules.updates.UpdatesDevLauncherController;
import expo.modules.devlauncher.DevLauncherController;
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import androidx.annotation.NonNull;
import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.soloader.SoLoader;
import expo.modules.ApplicationLifecycleDispatcher;
import expo.modules.ReactNativeHostWrapper;
import com.facebook.react.bridge.JSIModulePackage;
import com.swmansion.reanimated.ReanimatedJSIModulePackage;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost = new ReactNativeHostWrapper(
this,
new ReactNativeHost(this) {
@Override
public boolean getUseDeveloperSupport() {
return DevLauncherController.getInstance().getUseDeveloperSupport();
}
@Override
protected List<ReactPackage> getPackages() {
@SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
return packages;
}
@Override
protected String getJSMainModuleName() {
return "index";
}
@Override
protected JSIModulePackage getJSIModulePackage() {
return new ReanimatedJSIModulePackage();
}
});
@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
@Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
DevLauncherController.initialize(this, getReactNativeHost());
if (BuildConfig.DEBUG) {
DevLauncherController.getInstance().setUpdatesInterface(UpdatesDevLauncherController.initialize(this));
}
initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
ApplicationLifecycleDispatcher.onApplicationCreate(this);
}
@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
ApplicationLifecycleDispatcher.onConfigurationChanged(this, newConfig);
}
<--- I WANT TO INSERT MY STRING HERE
/**
* Loads Flipper in React Native templates. Call this in the onCreate method with something like
* initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
*
* @param context
* @param reactInstanceManager
*/
private static void initializeFlipper(
Context context, ReactInstanceManager reactInstanceManager) {
if (BuildConfig.DEBUG) {
try {
/*
We use reflection here to pick up the class that initializes Flipper,
since Flipper library is not available in release mode
*/
Class<?> aClass = Class.forName("com.haibert.GitTest.ReactNativeFlipper");
aClass
.getMethod("initializeFlipper", Context.class, ReactInstanceManager.class)
.invoke(null, context, reactInstanceManager);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
const addToMainApp = (content) => {
const regexpPackagingOptions = /s*?(?=/**n * Loads Flipper)/
const insertLocation = content.match(regexpPackagingOptions)
const newContent =
content.substring(0, insertLocation.index) +
'//INSERTED n@Overridenprotected List<ReactPackage> getPackages() {nreturn Arrays.<ReactPackage>asList(nnew MainReactPackage(), // <---- add commannew RNFSPackage() // <---------- add packagen);n}' +
content.substring(insertLocation.index, content.length)
return newContent
}