بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
فراخوانی دستورات و کدهای جاوا از یک فایل متنی
سلام دوستان من میخواستم پروژه ایی رو بنویسم که بتونه دستوراتی رو از خارج از برنامه فراخوانی کنه و اجرا کنه <
مثلا دستور
Log.i("LOG INSERT COD", " THIS COD IS FROM COD.TXT");
رو از یه فایل متنی بگیره و اجرا کنه <
ممکن هست ؟
0
0
dexmaker میتونه موقع اجرا کد تولید کنه لینک منظورت همینه؟ (12 سال پیش)
0
0
متاسفانه لینک برای من باز نمیشه.
یچیزی شبیه بچ فایل < مثلا همین کد بالا رو که نوشتم رو بگیره و اجرا کنه (12 سال پیش)
برای این سوال 1 پاسخ وجود دارد.
پاسخ به سوال
محمد حسین زاده
12 سال پیش
+2
0
این سمپل معرفی محصوله :
Runtime Code Generation
This example generates a class and a method. It then loads that class into the current process and invokes its method.
public final class HelloWorldMaker {
public static void main(String[] args) throws Exception {
DexMaker dexMaker = new DexMaker();
// Generate a HelloWorld class.
TypeId<?> helloWorld = TypeId.get("LHelloWorld;");
dexMaker.declare(helloWorld, "HelloWorld.generated", Modifier.PUBLIC, TypeId.OBJECT);
generateHelloMethod(dexMaker, helloWorld);
// Create the dex file and load it.
File outputDir = new File(".");
ClassLoader loader = dexMaker.generateAndLoad(HelloWorldMaker.class.getClassLoader(),
outputDir, outputDir);
Class<?> helloWorldClass = loader.loadClass("HelloWorld");
// Execute our newly-generated code in-process.
helloWorldClass.getMethod("hello").invoke(null);
}
/**
* Generates Dalvik bytecode equivalent to the following method.
* public static void hello() {
* int a = 0xabcd;
* int b = 0xaaaa;
* int c = a - b;
* String s = Integer.toHexString(c);
* System.out.println(s);
* return;
* }
*/
private static void generateHelloMethod(DexMaker dexMaker, TypeId<?> declaringType) {
// Lookup some types we'll need along the way.
TypeId<System> systemType = TypeId.get(System.class);
TypeId<PrintStream> printStreamType = TypeId.get(PrintStream.class);
// Identify the 'hello()' method on declaringType.
MethodId hello = declaringType.getMethod(TypeId.VOID, "hello");
// Declare that method on the dexMaker. Use the returned Code instance
// as a builder that we can append instructions to.
Code code = dexMaker.declare(hello, Modifier.STATIC | Modifier.PUBLIC);
// Declare all the locals we'll need up front. The API requires this.
Local<Integer> a = code.newLocal(TypeId.INT);
Local<Integer> b = code.newLocal(TypeId.INT);
Local<Integer> c = code.newLocal(TypeId.INT);
Local<String> s = code.newLocal(TypeId.STRING);
Local<PrintStream> localSystemOut = code.newLocal(printStreamType);
// int a = 0xabcd;
code.loadConstant(a, 0xabcd);
// int b = 0xaaaa;
code.loadConstant(b, 0xaaaa);
// int c = a - b;
code.op(BinaryOp.SUBTRACT, c, a, b);
// String s = Integer.toHexString(c);
MethodId<Integer, String> toHexString
= TypeId.get(Integer.class).getMethod(TypeId.STRING, "toHexString", TypeId.INT);
code.invokeStatic(toHexString, s, c);
// System.out.println(s);
FieldId<System, PrintStream> systemOutField = systemType.getField(printStreamType, "out");
code.sget(systemOutField, localSystemOut);
MethodId<PrintStream, Void> printlnMethod = printStreamType.getMethod(
TypeId.VOID, "println", TypeId.STRING);
code.invokeVirtual(printlnMethod, null, localSystemOut, s);
// return;
code.returnVoid();
}
}
Use it in your app
Maven users can get dexmaker from Sonatype's central repository. The Mockito dependency is optional.
<dependency>
<groupId>com.google.dexmaker</groupId>
<artifactId>dexmaker</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>com.google.dexmaker</groupId>
<artifactId>dexmaker-mockito</artifactId>
<version>1.1</version>
</dependency>
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .