Replace two methods not() and negate() with a new type UnaryOp and an op() method. I'm hoping this simplifies life for compiler authors who won't need to do manual dispatch.
diff --git a/src/main/java/com/google/dexmaker/Code.java b/src/main/java/com/google/dexmaker/Code.java
index 9afe0af..5803cfa 100644
--- a/src/main/java/com/google/dexmaker/Code.java
+++ b/src/main/java/com/google/dexmaker/Code.java
@@ -43,6 +43,21 @@
/**
* Builds a sequence of instructions.
*/
+/*
+ * TODO: documentation:
+ * UnaryOp, BinaryOp
+ * Try/catch
+ * Arrays
+ * Comparisons
+ * Locals
+ * Fields
+ * Type: Instanceof, cast, typeCast, numericCast
+ * Invoke
+ * Jump
+ * Constants
+ * NewInstance
+ * Return
+ */
public final class Code {
private final MethodId<?, ?> method;
/**
@@ -323,15 +338,12 @@
// instructions: unary
- public <T> void negate(Local<T> target, Local<T> source) {
- unary(Rops.opNeg(source.type.ropType), target, source);
- }
-
- public <T> void not(Local<T> target, Local<T> source) {
- unary(Rops.opNot(source.type.ropType), target, source);
+ public <T> void op(UnaryOp op, Local<T> target, Local<T> source) {
+ unary(op.rop(source.type), target, source);
}
public void numericCast(Local<?> target, Local<?> source) {
+ // TODO: overload the cast op?
unary(getCastRop(source.type.ropType, target.type.ropType), target, source);
}
diff --git a/src/main/java/com/google/dexmaker/DexMaker.java b/src/main/java/com/google/dexmaker/DexMaker.java
index f931df8..be33313 100644
--- a/src/main/java/com/google/dexmaker/DexMaker.java
+++ b/src/main/java/com/google/dexmaker/DexMaker.java
@@ -45,7 +45,7 @@
/**
* Generates a </i><strong>D</strong>alvik <strong>EX</strong>ecutable (dex)
- * file for execution on Android. dex files defines classes and interfaces,
+ * file for execution on Android. Dex files define classes and interfaces,
* including their member methods and fields, executable code, and debugging
* information. They also define annotations, though this API currently has no
* facility to create a dex file that contains annotations.
@@ -153,7 +153,8 @@
* add {@code @SuppressWarnings("unsafe")} on your calling code. This will yield
* the same result but you won't get IDE support if you make a type error.
*
- * <p>We're ready to start defining our method's instructions: <pre> {@code
+ * <p>We're ready to start defining our method's instructions. The {@link Code}
+ * class catalogs the available instructions and their use. <pre> {@code
*
* code.loadConstant(constant1, 1);
* code.loadConstant(constant2, 2);
diff --git a/src/main/java/com/google/dexmaker/UnaryOp.java b/src/main/java/com/google/dexmaker/UnaryOp.java
new file mode 100644
index 0000000..9d6f32e
--- /dev/null
+++ b/src/main/java/com/google/dexmaker/UnaryOp.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed 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 com.google.dexmaker;
+
+import com.android.dx.rop.code.Rop;
+import com.android.dx.rop.code.Rops;
+
+/**
+ * An operation on one value.
+ */
+public enum UnaryOp {
+
+ /** {@code ~a}. Supports int or long. */
+ NOT() {
+ @Override Rop rop(TypeId<?> type) {
+ return Rops.opNot(type.ropType);
+ }
+ },
+
+ /** {@code -a}. Supports int, long, float or double. */
+ NEGATE() {
+ @Override Rop rop(TypeId<?> type) {
+ return Rops.opNeg(type.ropType);
+ }
+ };
+
+ abstract Rop rop(TypeId<?> type);
+}
diff --git a/src/test/java/com/google/dexmaker/DexMakerTest.java b/src/test/java/com/google/dexmaker/DexMakerTest.java
index 9c347eb..f877373 100644
--- a/src/test/java/com/google/dexmaker/DexMakerTest.java
+++ b/src/test/java/com/google/dexmaker/DexMakerTest.java
@@ -656,7 +656,7 @@
MethodId<?, T> methodId = GENERATED.getMethod(valueType, "call", valueType);
Code code = dexMaker.declare(methodId, PUBLIC | STATIC);
Local<T> localSource = code.getParameter(0, valueType);
- code.not(localSource, localSource);
+ code.op(UnaryOp.NOT, localSource, localSource);
code.returnValue(localSource);
return getMethod();
}
@@ -697,7 +697,7 @@
MethodId<?, T> methodId = GENERATED.getMethod(valueType, "call", valueType);
Code code = dexMaker.declare(methodId, PUBLIC | STATIC);
Local<T> localSource = code.getParameter(0, valueType);
- code.negate(localSource, localSource);
+ code.op(UnaryOp.NEGATE, localSource, localSource);
code.returnValue(localSource);
return getMethod();
}