From e2f00f0eba79723388f2152db7b68c64872d7eb3 Mon Sep 17 00:00:00 2001 From: Ben Gruver Date: Sun, 14 Oct 2012 22:51:33 -0700 Subject: [PATCH] Add immutable implementations Immutable implementations for all interfaces defined so far --- .../immutable/ImmutableAnnotation.java | 92 +++++++++++ .../immutable/ImmutableAnnotationElement.java | 88 +++++++++++ .../immutable/ImmutableBaseAnnotation.java | 70 +++++++++ .../dexlib2/immutable/ImmutableClassDef.java | 114 ++++++++++++++ .../immutable/ImmutableExceptionHandler.java | 81 ++++++++++ .../jf/dexlib2/immutable/ImmutableField.java | 112 ++++++++++++++ .../jf/dexlib2/immutable/ImmutableMethod.java | 119 ++++++++++++++ .../ImmutableMethodImplementation.java | 79 ++++++++++ .../immutable/ImmutableMethodParameter.java | 89 +++++++++++ .../dexlib2/immutable/ImmutableTryBlock.java | 99 ++++++++++++ .../instruction/ImmutableInstruction.java | 138 +++++++++++++++++ .../instruction/ImmutableInstruction10t.java | 64 ++++++++ .../instruction/ImmutableInstruction10x.java | 57 +++++++ .../instruction/ImmutableInstruction11n.java | 71 +++++++++ .../instruction/ImmutableInstruction11x.java | 65 ++++++++ .../instruction/ImmutableInstruction12x.java | 70 +++++++++ .../instruction/ImmutableInstruction20t.java | 64 ++++++++ .../instruction/ImmutableInstruction21c.java | 70 +++++++++ .../instruction/ImmutableInstruction21ih.java | 72 +++++++++ .../instruction/ImmutableInstruction21lh.java | 71 +++++++++ .../instruction/ImmutableInstruction21s.java | 71 +++++++++ .../instruction/ImmutableInstruction21t.java | 70 +++++++++ .../instruction/ImmutableInstruction22b.java | 76 +++++++++ .../instruction/ImmutableInstruction22c.java | 75 +++++++++ .../instruction/ImmutableInstruction22s.java | 76 +++++++++ .../instruction/ImmutableInstruction22t.java | 75 +++++++++ .../instruction/ImmutableInstruction22x.java | 70 +++++++++ .../instruction/ImmutableInstruction23x.java | 75 +++++++++ .../instruction/ImmutableInstruction30t.java | 65 ++++++++ .../instruction/ImmutableInstruction31c.java | 70 +++++++++ .../instruction/ImmutableInstruction31i.java | 71 +++++++++ .../instruction/ImmutableInstruction31t.java | 71 +++++++++ .../instruction/ImmutableInstruction32x.java | 70 +++++++++ .../instruction/ImmutableInstruction35c.java | 95 ++++++++++++ .../instruction/ImmutableInstruction3rc.java | 77 +++++++++ .../instruction/ImmutableInstruction51l.java | 70 +++++++++ .../ImmutableAnnotationEncodedValue.java | 66 ++++++++ .../value/ImmutableArrayEncodedValue.java | 66 ++++++++ .../value/ImmutableBooleanEncodedValue.java | 55 +++++++ .../value/ImmutableByteEncodedValue.java | 55 +++++++ .../value/ImmutableCharEncodedValue.java | 55 +++++++ .../value/ImmutableDoubleEncodedValue.java | 55 +++++++ .../value/ImmutableEncodedValue.java | 108 +++++++++++++ .../value/ImmutableEnumEncodedValue.java | 57 +++++++ .../value/ImmutableFieldEncodedValue.java | 58 +++++++ .../value/ImmutableFloatEncodedValue.java | 55 +++++++ .../value/ImmutableIntEncodedValue.java | 55 +++++++ .../value/ImmutableLongEncodedValue.java | 55 +++++++ .../value/ImmutableMethodEncodedValue.java | 59 +++++++ .../value/ImmutableNullEncodedValue.java | 43 ++++++ .../value/ImmutableShortEncodedValue.java | 55 +++++++ .../value/ImmutableStringEncodedValue.java | 59 +++++++ .../value/ImmutableTypeEncodedValue.java | 59 +++++++ .../org/jf/dexlib2/util/Preconditions.java | 146 ++++++++++++++++++ 54 files changed, 4023 insertions(+) create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableAnnotation.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableAnnotationElement.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableBaseAnnotation.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableClassDef.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableExceptionHandler.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableField.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethod.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethodImplementation.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethodParameter.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableTryBlock.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction10t.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction10x.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction11n.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction11x.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction12x.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction20t.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21c.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21ih.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21lh.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21s.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21t.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22b.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22c.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22s.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22t.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22x.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction23x.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction30t.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31c.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31i.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31t.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction32x.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction35c.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction3rc.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction51l.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableAnnotationEncodedValue.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableArrayEncodedValue.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableBooleanEncodedValue.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableByteEncodedValue.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableCharEncodedValue.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableDoubleEncodedValue.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableEncodedValue.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableEnumEncodedValue.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableFieldEncodedValue.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableFloatEncodedValue.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableIntEncodedValue.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableLongEncodedValue.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableMethodEncodedValue.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableNullEncodedValue.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableShortEncodedValue.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableStringEncodedValue.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableTypeEncodedValue.java create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/util/Preconditions.java diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableAnnotation.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableAnnotation.java new file mode 100644 index 00000000..dd503bc9 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableAnnotation.java @@ -0,0 +1,92 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable; + +import com.google.common.collect.ImmutableList; +import org.jf.dexlib2.iface.Annotation; +import org.jf.dexlib2.iface.AnnotationElement; +import org.jf.util.ImmutableListConverter; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.List; + +public class ImmutableAnnotation extends ImmutableBaseAnnotation implements Annotation { + public final int visibility; + + public ImmutableAnnotation(int visibility, + @Nonnull String type, + @Nullable List elements) { + super(type, elements); + this.visibility = visibility; + } + + public ImmutableAnnotation(int visibility, + @Nonnull String type, + @Nullable ImmutableList elements) { + super(type, elements); + this.visibility = visibility; + } + + public static ImmutableAnnotation of(Annotation annotation) { + if (annotation instanceof ImmutableAnnotation) { + return (ImmutableAnnotation)annotation; + } + return new ImmutableAnnotation( + annotation.getVisibility(), + annotation.getType(), + annotation.getElements()); + } + + @Override + public int getVisibility() { + return visibility; + } + + @Nonnull + public static ImmutableList immutableListOf(List list) { + return CONVERTER.convert(list); + } + + private static final ImmutableListConverter CONVERTER = + new ImmutableListConverter() { + @Override + protected boolean isImmutable(Annotation item) { + return item instanceof ImmutableAnnotation; + } + + @Override + protected ImmutableAnnotation makeImmutable(Annotation item) { + return ImmutableAnnotation.of(item); + } + }; +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableAnnotationElement.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableAnnotationElement.java new file mode 100644 index 00000000..cc760fab --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableAnnotationElement.java @@ -0,0 +1,88 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable; + +import com.google.common.collect.ImmutableList; +import org.jf.dexlib2.immutable.value.ImmutableEncodedValue; +import org.jf.dexlib2.iface.AnnotationElement; +import org.jf.dexlib2.iface.value.EncodedValue; +import org.jf.util.ImmutableListConverter; + +import javax.annotation.Nonnull; +import java.util.List; + +public class ImmutableAnnotationElement implements AnnotationElement { + @Nonnull public final String name; + @Nonnull public final ImmutableEncodedValue value; + + public ImmutableAnnotationElement(@Nonnull String name, + @Nonnull EncodedValue value) { + this.name = name; + this.value = ImmutableEncodedValue.of(value); + } + + public ImmutableAnnotationElement(@Nonnull String name, + @Nonnull ImmutableEncodedValue value) { + this.name = name; + this.value = value; + } + + public static ImmutableAnnotationElement of(AnnotationElement annotationElement) { + if (annotationElement instanceof ImmutableAnnotationElement) { + return (ImmutableAnnotationElement)annotationElement; + } + return new ImmutableAnnotationElement( + annotationElement.getName(), + annotationElement.getValue()); + } + + @Nonnull @Override public String getName() { return name; } + @Nonnull @Override public EncodedValue getValue() { return value; } + + @Nonnull + public static ImmutableList immutableListOf(List list) { + return CONVERTER.convert(list); + } + + private static final ImmutableListConverter CONVERTER = + new ImmutableListConverter() { + @Override + protected boolean isImmutable(AnnotationElement item) { + return item instanceof ImmutableAnnotationElement; + } + + @Override + protected ImmutableAnnotationElement makeImmutable(AnnotationElement item) { + return ImmutableAnnotationElement.of(item); + } + }; +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableBaseAnnotation.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableBaseAnnotation.java new file mode 100644 index 00000000..d9bd47ff --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableBaseAnnotation.java @@ -0,0 +1,70 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable; + +import com.google.common.base.Objects; +import com.google.common.collect.ImmutableList; +import org.jf.dexlib2.iface.AnnotationElement; +import org.jf.dexlib2.iface.BaseAnnotation; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.List; + +public class ImmutableBaseAnnotation implements BaseAnnotation { + @Nonnull public final String type; + @Nonnull public final ImmutableList elements; + + public ImmutableBaseAnnotation(@Nonnull String type, + @Nullable List elements) { + this.type = type; + this.elements = ImmutableAnnotationElement.immutableListOf(elements); + } + + public ImmutableBaseAnnotation(@Nonnull String type, + @Nullable ImmutableList elements) { + this.type = type; + this.elements = Objects.firstNonNull(elements, ImmutableList.of()); + } + + public static ImmutableBaseAnnotation of(BaseAnnotation baseAnnotation) { + if (baseAnnotation instanceof ImmutableBaseAnnotation) { + return (ImmutableBaseAnnotation)baseAnnotation; + } + return new ImmutableBaseAnnotation( + baseAnnotation.getType(), + baseAnnotation.getElements()); + } + + @Nonnull @Override public String getType() { return type; } + @Nonnull @Override public ImmutableList getElements() { return elements; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableClassDef.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableClassDef.java new file mode 100644 index 00000000..e9313bb0 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableClassDef.java @@ -0,0 +1,114 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable; + +import com.google.common.base.Objects; +import com.google.common.collect.ImmutableList; +import org.jf.dexlib2.iface.Annotation; +import org.jf.dexlib2.iface.ClassDef; +import org.jf.dexlib2.iface.Field; +import org.jf.dexlib2.iface.Method; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.List; + +public class ImmutableClassDef implements ClassDef { + @Nonnull public final String name; + public final int accessFlags; + @Nullable public final String superclass; + @Nonnull public final ImmutableList interfaces; + @Nullable public final String sourceFile; + @Nonnull public final ImmutableList annotations; + @Nonnull public final ImmutableList fields; + @Nonnull public final ImmutableList methods; + + public ImmutableClassDef(@Nonnull String name, + int accessFlags, + @Nullable String superclass, + @Nullable List interfaces, + @Nullable String sourceFile, + @Nullable List annotations, + @Nullable List fields, + @Nullable List methods) { + this.name = name; + this.accessFlags = accessFlags; + this.superclass = superclass; + this.interfaces = interfaces==null ? ImmutableList.of() : ImmutableList.copyOf(interfaces); + this.sourceFile = sourceFile; + this.annotations = ImmutableAnnotation.immutableListOf(annotations); + this.fields = ImmutableField.immutableListOf(fields); + this.methods = ImmutableMethod.immutableListOf(methods); + } + + public ImmutableClassDef(@Nonnull String name, + int accessFlags, + @Nullable String superclass, + @Nullable ImmutableList interfaces, + @Nullable String sourceFile, + @Nullable ImmutableList annotations, + @Nullable ImmutableList fields, + @Nullable ImmutableList methods) { + this.name = name; + this.accessFlags = accessFlags; + this.superclass = superclass; + this.interfaces = Objects.firstNonNull(interfaces, ImmutableList.of()); + this.sourceFile = sourceFile; + this.annotations = Objects.firstNonNull(annotations, ImmutableList.of()); + this.fields = Objects.firstNonNull(fields, ImmutableList.of()); + this.methods = Objects.firstNonNull(methods, ImmutableList.of()); + } + + public static ImmutableClassDef of(ClassDef classDef) { + if (classDef instanceof ImmutableClassDef) { + return (ImmutableClassDef)classDef; + } + return new ImmutableClassDef( + classDef.getName(), + classDef.getAccessFlags(), + classDef.getSuperclass(), + classDef.getInterfaces(), + classDef.getSourceFile(), + classDef.getAnnotations(), + classDef.getFields(), + classDef.getMethods()); + } + + @Nonnull @Override public String getName() { return name; } + @Override public int getAccessFlags() { return accessFlags; } + @Nullable @Override public String getSuperclass() { return superclass; } + @Nonnull @Override public ImmutableList getInterfaces() { return interfaces; } + @Nullable @Override public String getSourceFile() { return sourceFile; } + @Nonnull @Override public ImmutableList getAnnotations() { return annotations; } + @Nonnull @Override public ImmutableList getFields() { return fields; } + @Nonnull @Override public ImmutableList getMethods() { return methods; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableExceptionHandler.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableExceptionHandler.java new file mode 100644 index 00000000..d9a1f238 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableExceptionHandler.java @@ -0,0 +1,81 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable; + +import com.google.common.collect.ImmutableList; +import org.jf.dexlib2.iface.ExceptionHandler; +import org.jf.util.ImmutableListConverter; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.List; + +public class ImmutableExceptionHandler implements ExceptionHandler { + @Nullable public final String exceptionType; + public final int handlerIndex; + + public ImmutableExceptionHandler(@Nullable String exceptionType, + int handlerIndex) { + this.exceptionType = exceptionType; + this.handlerIndex = handlerIndex; + } + + public static ImmutableExceptionHandler of(ExceptionHandler exceptionHandler) { + if (exceptionHandler instanceof ImmutableExceptionHandler) { + return (ImmutableExceptionHandler)exceptionHandler; + } + return new ImmutableExceptionHandler( + exceptionHandler.getExceptionType(), + exceptionHandler.getHandlerIndex()); + } + + @Nullable @Override public String getExceptionType() { return exceptionType; } + @Override public int getHandlerIndex() { return handlerIndex; } + + @Nonnull + public static ImmutableList immutableListOf(List list) { + return CONVERTER.convert(list); + } + + private static final ImmutableListConverter CONVERTER = + new ImmutableListConverter() { + @Override + protected boolean isImmutable(ExceptionHandler item) { + return item instanceof ImmutableExceptionHandler; + } + + @Override + protected ImmutableExceptionHandler makeImmutable(ExceptionHandler item) { + return ImmutableExceptionHandler.of(item); + } + }; +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableField.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableField.java new file mode 100644 index 00000000..a9f895f8 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableField.java @@ -0,0 +1,112 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable; + +import com.google.common.base.Objects; +import com.google.common.collect.ImmutableList; +import org.jf.dexlib2.immutable.value.ImmutableEncodedValue; +import org.jf.dexlib2.iface.Annotation; +import org.jf.dexlib2.iface.Field; +import org.jf.dexlib2.iface.value.EncodedValue; +import org.jf.util.ImmutableListConverter; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.List; + +public class ImmutableField implements Field { + @Nonnull public final String name; + @Nonnull public final String type; + public final int accessFlags; + @Nullable public final ImmutableEncodedValue initialValue; + @Nonnull public final ImmutableList annotations; + + public ImmutableField(@Nonnull String name, + @Nonnull String type, + int accessFlags, + @Nullable EncodedValue initialValue, + @Nullable List annotations) { + this.name = name; + this.type = type; + this.accessFlags = accessFlags; + this.initialValue = ImmutableEncodedValue.of(initialValue); + this.annotations = ImmutableAnnotation.immutableListOf(annotations); + } + + public ImmutableField(@Nonnull String name, + @Nonnull String type, + int accessFlags, + @Nullable ImmutableEncodedValue initialValue, + @Nullable ImmutableList annotations) { + this.name = name; + this.type = type; + this.accessFlags = accessFlags; + this.initialValue = initialValue; + this.annotations = Objects.firstNonNull(annotations, ImmutableList.of()); + } + + public static ImmutableField of(Field field) { + if (field instanceof ImmutableField) { + return (ImmutableField)field; + } + return new ImmutableField( + field.getName(), + field.getType(), + field.getAccessFlags(), + field.getInitialValue(), + field.getAnnotations()); + } + + @Nonnull @Override public String getName() { return name; } + @Nonnull @Override public String getType() { return type; } + @Override public int getAccessFlags() { return accessFlags; } + @Override public EncodedValue getInitialValue() { return initialValue;} + @Nonnull @Override public ImmutableList getAnnotations() { return annotations; } + + @Nonnull + public static ImmutableList immutableListOf(List list) { + return CONVERTER.convert(list); + } + + private static final ImmutableListConverter CONVERTER = + new ImmutableListConverter() { + @Override + protected boolean isImmutable(Field item) { + return item instanceof ImmutableField; + } + + @Override + protected ImmutableField makeImmutable(Field item) { + return ImmutableField.of(item); + } + }; +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethod.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethod.java new file mode 100644 index 00000000..ec195e6d --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethod.java @@ -0,0 +1,119 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable; + +import com.google.common.base.Objects; +import com.google.common.collect.ImmutableList; +import org.jf.dexlib2.iface.Annotation; +import org.jf.dexlib2.iface.Method; +import org.jf.dexlib2.iface.MethodImplementation; +import org.jf.dexlib2.iface.MethodParameter; +import org.jf.util.ImmutableListConverter; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.List; + +public class ImmutableMethod implements Method { + @Nonnull public final String name; + @Nonnull public final ImmutableList parameters; + @Nonnull public final String returnType; + public final int accessFlags; + @Nonnull public final ImmutableList annotations; + @Nullable public final ImmutableMethodImplementation methodImplementation; + + public ImmutableMethod(@Nonnull String name, + @Nullable List parameters, + @Nonnull String returnType, + int accessFlags, + @Nullable List annotations, + @Nullable MethodImplementation methodImplementation) { + this.name = name; + this.parameters = ImmutableMethodParameter.immutableListOf(parameters); + this.returnType = returnType; + this.accessFlags = accessFlags; + this.annotations = ImmutableAnnotation.immutableListOf(annotations); + this.methodImplementation = ImmutableMethodImplementation.of(methodImplementation); + } + + public ImmutableMethod(@Nonnull String name, + @Nullable ImmutableList parameters, + @Nonnull String returnType, + int accessFlags, + @Nullable ImmutableList annotations, + @Nullable ImmutableMethodImplementation methodImplementation) { + this.name = name; + this.parameters = Objects.firstNonNull(parameters, ImmutableList.of()); + this.returnType = returnType; + this.accessFlags = accessFlags; + this.annotations = Objects.firstNonNull(annotations, ImmutableList.of()); + this.methodImplementation = methodImplementation; + } + + public static ImmutableMethod of(Method method) { + if (method instanceof ImmutableMethod) { + return (ImmutableMethod)method; + } + return new ImmutableMethod( + method.getName(), + method.getParameters(), + method.getReturnType(), + method.getAccessFlags(), + method.getAnnotations(), + method.getImplementation()); + } + + @Nonnull public String getName() { return name; } + @Nonnull public ImmutableList getParameters() { return parameters; } + @Nonnull public String getReturnType() { return returnType; } + public int getAccessFlags() { return accessFlags; } + @Nonnull public ImmutableList getAnnotations() { return annotations; } + @Nullable public ImmutableMethodImplementation getImplementation() { return methodImplementation; } + + @Nonnull + public static ImmutableList immutableListOf(List list) { + return CONVERTER.convert(list); + } + + private static final ImmutableListConverter CONVERTER = + new ImmutableListConverter() { + @Override + protected boolean isImmutable(Method item) { + return item instanceof ImmutableMethod; + } + + @Override + protected ImmutableMethod makeImmutable(Method item) { + return ImmutableMethod.of(item); + } + }; +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethodImplementation.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethodImplementation.java new file mode 100644 index 00000000..57d0cac5 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethodImplementation.java @@ -0,0 +1,79 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable; + +import com.google.common.base.Objects; +import com.google.common.collect.ImmutableList; +import org.jf.dexlib2.iface.MethodImplementation; +import org.jf.dexlib2.iface.TryBlock; +import org.jf.dexlib2.iface.instruction.Instruction; +import org.jf.dexlib2.immutable.instruction.ImmutableInstruction; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.List; + +public class ImmutableMethodImplementation implements MethodImplementation { + public final int registerCount; + @Nonnull public final ImmutableList instructions; + @Nonnull public final ImmutableList tryBlocks; + + public ImmutableMethodImplementation(int registerCount, + @Nullable List instructions, + @Nullable List tryBlocks) { + this.registerCount = registerCount; + this.instructions = ImmutableInstruction.immutableListOf(instructions); + this.tryBlocks = ImmutableTryBlock.immutableListOf(tryBlocks); + } + + public ImmutableMethodImplementation(int registerCount, + @Nullable ImmutableList instructions, + @Nullable ImmutableList tryBlocks) { + this.registerCount = registerCount; + this.instructions = Objects.firstNonNull(instructions, ImmutableList.of()); + this.tryBlocks = Objects.firstNonNull(tryBlocks, ImmutableList.of()); + } + + public static ImmutableMethodImplementation of(MethodImplementation methodImplementation) { + if (methodImplementation instanceof ImmutableMethodImplementation) { + return (ImmutableMethodImplementation)methodImplementation; + } + return new ImmutableMethodImplementation( + methodImplementation.getRegisterCount(), + methodImplementation.getInstructions(), + methodImplementation.getTryBlocks()); + } + + @Override public int getRegisterCount() { return 0; } + @Nonnull @Override public ImmutableList getInstructions() { return instructions; } + @Nonnull @Override public ImmutableList getTryBlocks() { return tryBlocks; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethodParameter.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethodParameter.java new file mode 100644 index 00000000..73352615 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethodParameter.java @@ -0,0 +1,89 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable; + +import com.google.common.base.Objects; +import com.google.common.collect.ImmutableList; +import org.jf.dexlib2.iface.Annotation; +import org.jf.dexlib2.iface.MethodParameter; +import org.jf.util.ImmutableListConverter; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.List; + +public class ImmutableMethodParameter implements MethodParameter { + @Nonnull public final String type; + @Nonnull public final ImmutableList annotations; + + public ImmutableMethodParameter(@Nonnull String type, + @Nullable List annotations) { + this.type = type; + this.annotations = ImmutableAnnotation.immutableListOf(annotations); + } + + public ImmutableMethodParameter(@Nonnull String type, + @Nullable ImmutableList annotations) { + this.type = type; + this.annotations = Objects.firstNonNull(annotations, ImmutableList.of()); + } + + public static ImmutableMethodParameter of(MethodParameter methodParameter) { + if (methodParameter instanceof ImmutableMethodParameter) { + return (ImmutableMethodParameter)methodParameter; + } + return new ImmutableMethodParameter( + methodParameter.getType(), + methodParameter.getAnnotations()); + } + + @Nonnull @Override public String getType() { return type; } + @Nonnull @Override public List getAnnotations() { return annotations; } + + @Nonnull + public static ImmutableList immutableListOf(List list) { + return CONVERTER.convert(list); + } + + private static final ImmutableListConverter CONVERTER = + new ImmutableListConverter() { + @Override + protected boolean isImmutable(MethodParameter item) { + return item instanceof ImmutableMethodParameter; + } + + @Override + protected ImmutableMethodParameter makeImmutable(MethodParameter item) { + return ImmutableMethodParameter.of(item); + } + }; +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableTryBlock.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableTryBlock.java new file mode 100644 index 00000000..2c876cd1 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableTryBlock.java @@ -0,0 +1,99 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable; + +import com.google.common.base.Objects; +import com.google.common.collect.ImmutableList; +import org.jf.dexlib2.iface.ExceptionHandler; +import org.jf.dexlib2.iface.TryBlock; +import org.jf.util.ImmutableListConverter; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.List; + +public class ImmutableTryBlock implements TryBlock { + public final int startIndex; + public final int instructionCount; + @Nonnull public final ImmutableList exceptionHandlers; + + public ImmutableTryBlock(int startIndex, + int instructionCount, + @Nullable List exceptionHandlers) { + this.startIndex = startIndex; + this.instructionCount = instructionCount; + this.exceptionHandlers = ImmutableExceptionHandler.immutableListOf(exceptionHandlers); + } + + public ImmutableTryBlock(int startIndex, + int instructionCount, + @Nullable ImmutableList exceptionHandlers) { + this.startIndex = startIndex; + this.instructionCount = instructionCount; + this.exceptionHandlers = Objects.firstNonNull(exceptionHandlers, ImmutableList.of()); + } + + public static ImmutableTryBlock of(TryBlock tryBlock) { + if (tryBlock instanceof ImmutableTryBlock) { + return (ImmutableTryBlock)tryBlock; + } + return new ImmutableTryBlock( + tryBlock.getStartIndex(), + tryBlock.getInstructionCount(), + tryBlock.getExceptionHandlers()); + } + + @Override public int getStartIndex() { return startIndex; } + @Override public int getInstructionCount() { return instructionCount; } + + @Nonnull @Override public ImmutableList getExceptionHandlers() { + return exceptionHandlers; + } + + @Nonnull + public static ImmutableList immutableListOf(List list) { + return CONVERTER.convert(list); + } + + private static final ImmutableListConverter CONVERTER = + new ImmutableListConverter() { + @Override + protected boolean isImmutable(TryBlock item) { + return item instanceof ImmutableTryBlock; + } + + @Override + protected ImmutableTryBlock makeImmutable(TryBlock item) { + return ImmutableTryBlock.of(item); + } + }; +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction.java new file mode 100644 index 00000000..d6a06847 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction.java @@ -0,0 +1,138 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import com.google.common.collect.ImmutableList; +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.Instruction; +import org.jf.dexlib2.iface.instruction.formats.*; +import org.jf.dexlib2.util.Preconditions; +import org.jf.util.ImmutableListConverter; + +import javax.annotation.Nonnull; +import java.util.List; + +public abstract class ImmutableInstruction implements Instruction { + public final Opcode opcode; + + protected ImmutableInstruction(@Nonnull Opcode opcode) { + this.opcode = opcode; + //TODO: check performance. Move into subclasses if needed, where we can access the field directly + Preconditions.checkFormat(opcode, getFormat()); + } + + public static ImmutableInstruction of(Instruction instruction) { + if (instruction instanceof ImmutableInstruction) { + return (ImmutableInstruction)instruction; + } + + switch (instruction.getOpcode().format) { + case Format10t: + return ImmutableInstruction10t.of((Instruction10t)instruction); + case Format10x: + return ImmutableInstruction10x.of((Instruction10x)instruction); + case Format11n: + return ImmutableInstruction11n.of((Instruction11n)instruction); + case Format11x: + return ImmutableInstruction11x.of((Instruction11x)instruction); + case Format12x: + return ImmutableInstruction12x.of((Instruction12x)instruction); + case Format20t: + return ImmutableInstruction20t.of((Instruction20t)instruction); + case Format21c: + return ImmutableInstruction21c.of((Instruction21c)instruction); + case Format21ih: + return ImmutableInstruction21ih.of((Instruction21ih)instruction); + case Format21lh: + return ImmutableInstruction21lh.of((Instruction21lh)instruction); + case Format21s: + return ImmutableInstruction21s.of((Instruction21s)instruction); + case Format21t: + return ImmutableInstruction21t.of((Instruction21t)instruction); + case Format22b: + return ImmutableInstruction22b.of((Instruction22b)instruction); + case Format22c: + return ImmutableInstruction22c.of((Instruction22c)instruction); + case Format22s: + return ImmutableInstruction22s.of((Instruction22s)instruction); + case Format22t: + return ImmutableInstruction22t.of((Instruction22t)instruction); + case Format22x: + return ImmutableInstruction22x.of((Instruction22x)instruction); + case Format23x: + return ImmutableInstruction23x.of((Instruction23x)instruction); + case Format30t: + return ImmutableInstruction30t.of((Instruction30t)instruction); + case Format31c: + return ImmutableInstruction31c.of((Instruction31c)instruction); + case Format31i: + return ImmutableInstruction31i.of((Instruction31i)instruction); + case Format31t: + return ImmutableInstruction31t.of((Instruction31t)instruction); + case Format32x: + return ImmutableInstruction32x.of((Instruction32x)instruction); + case Format35c: + return ImmutableInstruction35c.of((Instruction35c)instruction); + case Format3rc: + return ImmutableInstruction3rc.of((Instruction3rc)instruction); + case Format51l: + return ImmutableInstruction51l.of((Instruction51l)instruction); + } + //TODO: temporary, until we get all instructions implemented + throw new RuntimeException("Unexpected instruction type"); + } + + public Opcode getOpcode() { + return opcode; + } + + public abstract Format getFormat(); + + @Nonnull + public static ImmutableList immutableListOf(List list) { + return CONVERTER.convert(list); + } + + private static final ImmutableListConverter CONVERTER = + new ImmutableListConverter() { + @Override + protected boolean isImmutable(Instruction item) { + return item instanceof ImmutableInstruction; + } + + @Override + protected ImmutableInstruction makeImmutable(Instruction item) { + return ImmutableInstruction.of(item); + } + }; +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction10t.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction10t.java new file mode 100644 index 00000000..01023f0e --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction10t.java @@ -0,0 +1,64 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction10t; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction10t extends ImmutableInstruction implements Instruction10t { + public static final Format FORMAT = Format.Format12x; + + public final int offset; + + public ImmutableInstruction10t(@Nonnull Opcode opcode, + int offset) { + super(opcode); + Preconditions.checkFormat(opcode, FORMAT); + this.offset = Preconditions.checkByteOffset(offset); + } + + public static ImmutableInstruction10t of(Instruction10t instruction) { + if (instruction instanceof ImmutableInstruction10t) { + return (ImmutableInstruction10t)instruction; + } + return new ImmutableInstruction10t( + instruction.getOpcode(), + instruction.getOffset()); + } + + @Override public int getOffset() { return offset; } + @Override public Format getFormat() { return FORMAT; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction10x.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction10x.java new file mode 100644 index 00000000..ec6cd770 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction10x.java @@ -0,0 +1,57 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction10x; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction10x extends ImmutableInstruction implements Instruction10x { + public static final Format FORMAT = Format.Format12x; + + public ImmutableInstruction10x(@Nonnull Opcode opcode) { + super(opcode); + Preconditions.checkFormat(opcode, FORMAT); + } + + public static ImmutableInstruction10x of(Instruction10x instruction) { + if (instruction instanceof ImmutableInstruction10x) { + return (ImmutableInstruction10x)instruction; + } + return new ImmutableInstruction10x(instruction.getOpcode()); + } + + @Override public Format getFormat() { return FORMAT; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction11n.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction11n.java new file mode 100644 index 00000000..fec1adfc --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction11n.java @@ -0,0 +1,71 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction11n; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction11n extends ImmutableInstruction implements Instruction11n { + public static final Format FORMAT = Format.Format11n; + + public final int registerA; + public final int literal; + + public ImmutableInstruction11n(@Nonnull Opcode opcode, + int registerA, + int literal) { + super(opcode); + Preconditions.checkFormat(opcode, FORMAT); + this.registerA = Preconditions.checkNibbleRegister(registerA); + this.literal = Preconditions.checkNibbleLiteral(literal); + } + + public static ImmutableInstruction11n of(Instruction11n instruction) { + if (instruction instanceof ImmutableInstruction11n) { + return (ImmutableInstruction11n)instruction; + } + return new ImmutableInstruction11n( + instruction.getOpcode(), + instruction.getRegisterA(), + instruction.getNarrowLiteral()); + } + + @Override public int getRegisterA() { return registerA; } + @Override public int getNarrowLiteral() { return literal; } + @Override public long getWideLiteral() { return literal; } + + @Override public Format getFormat() { return FORMAT; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction11x.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction11x.java new file mode 100644 index 00000000..e0957b6a --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction11x.java @@ -0,0 +1,65 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction11x; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction11x extends ImmutableInstruction implements Instruction11x { + public static final Format FORMAT = Format.Format11x; + + public final int registerA; + + public ImmutableInstruction11x(@Nonnull Opcode opcode, + int registerA) { + super(opcode); + Preconditions.checkFormat(opcode, FORMAT); + this.registerA = Preconditions.checkByteRegister(registerA); + } + + public static ImmutableInstruction11x of(Instruction11x instruction) { + if (instruction instanceof ImmutableInstruction11x) { + return (ImmutableInstruction11x)instruction; + } + return new ImmutableInstruction11x( + instruction.getOpcode(), + instruction.getRegisterA()); + } + + @Override public int getRegisterA() { return registerA; } + + @Override public Format getFormat() { return FORMAT; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction12x.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction12x.java new file mode 100644 index 00000000..d53bd7aa --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction12x.java @@ -0,0 +1,70 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction12x; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction12x extends ImmutableInstruction implements Instruction12x { + public static final Format FORMAT = Format.Format12x; + + public final int registerA; + public final int registerB; + + public ImmutableInstruction12x(@Nonnull Opcode opcode, + int registerA, + int registerB) { + super(opcode); + Preconditions.checkFormat(opcode, FORMAT); + this.registerA = Preconditions.checkNibbleRegister(registerA); + this.registerB = Preconditions.checkNibbleRegister(registerB); + } + + public static ImmutableInstruction12x of(Instruction12x instruction) { + if (instruction instanceof ImmutableInstruction12x) { + return (ImmutableInstruction12x)instruction; + } + return new ImmutableInstruction12x( + instruction.getOpcode(), + instruction.getRegisterA(), + instruction.getRegisterB()); + } + + @Override public int getRegisterA() { return registerA; } + @Override public int getRegisterB() { return registerB; } + + @Override public Format getFormat() { return FORMAT; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction20t.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction20t.java new file mode 100644 index 00000000..1a54fc67 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction20t.java @@ -0,0 +1,64 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction20t; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction20t extends ImmutableInstruction implements Instruction20t { + public static final Format FORMAT = Format.Format12x; + + public final int offset; + + public ImmutableInstruction20t(@Nonnull Opcode opcode, + int offset) { + super(opcode); + Preconditions.checkFormat(opcode, FORMAT); + this.offset = Preconditions.checkShortOffset(offset); + } + + public static ImmutableInstruction20t of(Instruction20t instruction) { + if (instruction instanceof ImmutableInstruction20t) { + return (ImmutableInstruction20t)instruction; + } + return new ImmutableInstruction20t( + instruction.getOpcode(), + (byte)instruction.getOffset()); + } + + @Override public int getOffset() { return offset; } + @Override public Format getFormat() { return FORMAT; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21c.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21c.java new file mode 100644 index 00000000..8f2b3926 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21c.java @@ -0,0 +1,70 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction21c; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction21c extends ImmutableInstruction implements Instruction21c { + public static final Format FORMAT = Format.Format21c; + + public final int registerA; + @Nonnull public final String reference; + + public ImmutableInstruction21c(@Nonnull Opcode opcode, + int registerA, + @Nonnull String reference) { + super(opcode); + Preconditions.checkFormat(opcode, FORMAT); + this.registerA = Preconditions.checkByteRegister(registerA); + this.reference = Preconditions.checkReference(reference, opcode.referenceType); + } + + public static ImmutableInstruction21c of(Instruction21c instruction) { + if (instruction instanceof ImmutableInstruction21c) { + return (ImmutableInstruction21c)instruction; + } + return new ImmutableInstruction21c( + instruction.getOpcode(), + instruction.getRegisterA(), + instruction.getReference()); + } + + @Override public int getRegisterA() { return registerA; } + @Nonnull @Override public String getReference() { return reference; } + + @Override public Format getFormat() { return FORMAT; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21ih.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21ih.java new file mode 100644 index 00000000..05dc40c8 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21ih.java @@ -0,0 +1,72 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction21ih; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction21ih extends ImmutableInstruction implements Instruction21ih { + public static final Format FORMAT = Format.Format21ih; + + public final int registerA; + public final int literal; + + public ImmutableInstruction21ih(@Nonnull Opcode opcode, + int registerA, + int literal) { + super(opcode); + Preconditions.checkFormat(opcode, FORMAT); + this.registerA = Preconditions.checkByteRegister(registerA); + this.literal = Preconditions.checkIntegerHatLiteral(literal); + } + + public static ImmutableInstruction21ih of(Instruction21ih instruction) { + if (instruction instanceof ImmutableInstruction21ih) { + return (ImmutableInstruction21ih)instruction; + } + return new ImmutableInstruction21ih( + instruction.getOpcode(), + instruction.getRegisterA(), + instruction.getNarrowLiteral()); + } + + @Override public int getRegisterA() { return registerA; } + @Override public int getNarrowLiteral() { return literal; } + @Override public long getWideLiteral() { return literal; } + @Override public short getHatLiteral() { return (short)(literal >>> 16); } + + @Override public Format getFormat() { return FORMAT; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21lh.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21lh.java new file mode 100644 index 00000000..42d1efae --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21lh.java @@ -0,0 +1,71 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction21lh; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction21lh extends ImmutableInstruction implements Instruction21lh { + public static final Format FORMAT = Format.Format21lh; + + public final int registerA; + public final long literal; + + public ImmutableInstruction21lh(@Nonnull Opcode opcode, + int registerA, + long literal) { + super(opcode); + Preconditions.checkFormat(opcode, FORMAT); + this.registerA = Preconditions.checkByteRegister(registerA); + this.literal = Preconditions.checkLongHatLiteral(literal); + } + + public static ImmutableInstruction21lh of(Instruction21lh instruction) { + if (instruction instanceof ImmutableInstruction21lh) { + return (ImmutableInstruction21lh)instruction; + } + return new ImmutableInstruction21lh( + instruction.getOpcode(), + instruction.getRegisterA(), + instruction.getWideLiteral()); + } + + @Override public int getRegisterA() { return registerA; } + @Override public long getWideLiteral() { return literal; } + @Override public short getHatLiteral() { return (short)(literal >>> 48); } + + @Override public Format getFormat() { return FORMAT; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21s.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21s.java new file mode 100644 index 00000000..5e57391a --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21s.java @@ -0,0 +1,71 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction21s; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction21s extends ImmutableInstruction implements Instruction21s { + public static final Format FORMAT = Format.Format21s; + + public final int registerA; + public final int literal; + + public ImmutableInstruction21s(@Nonnull Opcode opcode, + int registerA, + int literal) { + super(opcode); + Preconditions.checkFormat(opcode, FORMAT); + this.registerA = Preconditions.checkByteRegister(registerA); + this.literal = Preconditions.checkShortLiteral(literal); + } + + public static ImmutableInstruction21s of(Instruction21s instruction) { + if (instruction instanceof ImmutableInstruction21s) { + return (ImmutableInstruction21s)instruction; + } + return new ImmutableInstruction21s( + instruction.getOpcode(), + instruction.getRegisterA(), + instruction.getNarrowLiteral()); + } + + @Override public int getRegisterA() { return registerA; } + @Override public int getNarrowLiteral() { return literal; } + @Override public long getWideLiteral() { return literal; } + + @Override public Format getFormat() { return FORMAT; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21t.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21t.java new file mode 100644 index 00000000..c37f661c --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21t.java @@ -0,0 +1,70 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction21t; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction21t extends ImmutableInstruction implements Instruction21t { + public static final Format FORMAT = Format.Format21t; + + public final int registerA; + public final int offset; + + public ImmutableInstruction21t(@Nonnull Opcode opcode, + int registerA, + int offset) { + super(opcode); + Preconditions.checkFormat(opcode, FORMAT); + this.registerA = Preconditions.checkByteRegister(registerA); + this.offset = Preconditions.checkShortOffset(offset); + } + + public static ImmutableInstruction21t of(Instruction21t instruction) { + if (instruction instanceof ImmutableInstruction21t) { + return (ImmutableInstruction21t)instruction; + } + return new ImmutableInstruction21t( + instruction.getOpcode(), + instruction.getRegisterA(), + instruction.getOffset()); + } + + @Override public int getRegisterA() { return registerA; } + @Override public int getOffset() { return offset; } + + @Override public Format getFormat() { return FORMAT; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22b.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22b.java new file mode 100644 index 00000000..7400766c --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22b.java @@ -0,0 +1,76 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction22b; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction22b extends ImmutableInstruction implements Instruction22b { + public static final Format FORMAT = Format.Format22b; + + public final int registerA; + public final int registerB; + public final int literal; + + public ImmutableInstruction22b(@Nonnull Opcode opcode, + int registerA, + int registerB, + int literal) { + super(opcode); + Preconditions.checkFormat(opcode, Format.Format35c); + this.registerA = Preconditions.checkByteRegister(registerA); + this.registerB = Preconditions.checkByteRegister(registerB); + this.literal = Preconditions.checkByteLiteral(literal); + } + + public static ImmutableInstruction22b of(Instruction22b instruction) { + if (instruction instanceof ImmutableInstruction22b) { + return (ImmutableInstruction22b)instruction; + } + return new ImmutableInstruction22b( + instruction.getOpcode(), + instruction.getRegisterA(), + instruction.getRegisterB(), + instruction.getNarrowLiteral()); + } + + @Override public int getRegisterA() { return registerA; } + @Override public int getRegisterB() { return registerB; } + @Override public int getNarrowLiteral() { return literal; } + @Override public long getWideLiteral() { return literal; } + + @Override public Format getFormat() { return FORMAT; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22c.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22c.java new file mode 100644 index 00000000..40573791 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22c.java @@ -0,0 +1,75 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction22c; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction22c extends ImmutableInstruction implements Instruction22c { + public static final Format FORMAT = Format.Format22c; + + public final int registerA; + public final int registerB; + public final String reference; + + public ImmutableInstruction22c(@Nonnull Opcode opcode, + int registerA, + int registerB, + @Nonnull String reference) { + super(opcode); + Preconditions.checkFormat(opcode, FORMAT); + this.registerA = Preconditions.checkNibbleRegister(registerA); + this.registerB = Preconditions.checkNibbleRegister(registerB); + this.reference = Preconditions.checkReference(reference, opcode.referenceType); + } + + public static ImmutableInstruction22c of(Instruction22c instruction) { + if (instruction instanceof ImmutableInstruction22c) { + return (ImmutableInstruction22c)instruction; + } + return new ImmutableInstruction22c( + instruction.getOpcode(), + instruction.getRegisterA(), + instruction.getRegisterB(), + instruction.getReference()); + } + + @Override public int getRegisterA() { return registerA; } + @Override public int getRegisterB() { return registerB; } + @Nonnull @Override public String getReference() { return reference; } + + @Override public Format getFormat() { return FORMAT; } +} \ No newline at end of file diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22s.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22s.java new file mode 100644 index 00000000..a8db56fb --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22s.java @@ -0,0 +1,76 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction22s; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction22s extends ImmutableInstruction implements Instruction22s { + public static final Format FORMAT = Format.Format22s; + + public final int registerA; + public final int registerB; + public final int literal; + + public ImmutableInstruction22s(@Nonnull Opcode opcode, + int registerA, + int registerB, + int literal) { + super(opcode); + Preconditions.checkFormat(opcode, Format.Format35c); + this.registerA = Preconditions.checkNibbleRegister(registerA); + this.registerB = Preconditions.checkNibbleRegister(registerB); + this.literal = Preconditions.checkShortLiteral(literal); + } + + public static ImmutableInstruction22s of(Instruction22s instruction) { + if (instruction instanceof ImmutableInstruction22s) { + return (ImmutableInstruction22s)instruction; + } + return new ImmutableInstruction22s( + instruction.getOpcode(), + instruction.getRegisterA(), + instruction.getRegisterB(), + instruction.getNarrowLiteral()); + } + + @Override public int getRegisterA() { return registerA; } + @Override public int getRegisterB() { return registerB; } + @Override public int getNarrowLiteral() { return literal; } + @Override public long getWideLiteral() { return literal; } + + @Override public Format getFormat() { return FORMAT; } +} \ No newline at end of file diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22t.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22t.java new file mode 100644 index 00000000..537d392f --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22t.java @@ -0,0 +1,75 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction22t; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction22t extends ImmutableInstruction implements Instruction22t { + public static final Format FORMAT = Format.Format22t; + + public final int registerA; + public final int registerB; + public final int offset; + + public ImmutableInstruction22t(@Nonnull Opcode opcode, + int registerA, + int registerB, + int offset) { + super(opcode); + Preconditions.checkFormat(opcode, Format.Format35c); + this.registerA = Preconditions.checkNibbleRegister(registerA); + this.registerB = Preconditions.checkNibbleRegister(registerB); + this.offset = Preconditions.checkShortOffset(offset); + } + + public static ImmutableInstruction22t of(Instruction22t instruction) { + if (instruction instanceof ImmutableInstruction22t) { + return (ImmutableInstruction22t)instruction; + } + return new ImmutableInstruction22t( + instruction.getOpcode(), + instruction.getRegisterA(), + instruction.getRegisterB(), + instruction.getOffset()); + } + + @Override public int getRegisterA() { return registerA; } + @Override public int getRegisterB() { return registerB; } + @Override public int getOffset() { return offset; } + + @Override public Format getFormat() { return FORMAT; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22x.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22x.java new file mode 100644 index 00000000..d8499d96 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22x.java @@ -0,0 +1,70 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction22x; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction22x extends ImmutableInstruction implements Instruction22x { + public static final Format FORMAT = Format.Format22x; + + public final int registerA; + public final int registerB; + + public ImmutableInstruction22x(@Nonnull Opcode opcode, + int registerA, + int registerB) { + super(opcode); + Preconditions.checkFormat(opcode, Format.Format35c); + this.registerA = Preconditions.checkByteRegister(registerA); + this.registerB = Preconditions.checkShortRegister(registerB); + } + + public static ImmutableInstruction22x of(Instruction22x instruction) { + if (instruction instanceof ImmutableInstruction22x) { + return (ImmutableInstruction22x)instruction; + } + return new ImmutableInstruction22x( + instruction.getOpcode(), + instruction.getRegisterA(), + instruction.getRegisterB()); + } + + @Override public int getRegisterA() { return registerA; } + @Override public int getRegisterB() { return registerB; } + + @Override public Format getFormat() { return FORMAT; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction23x.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction23x.java new file mode 100644 index 00000000..1d7af45b --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction23x.java @@ -0,0 +1,75 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction23x; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction23x extends ImmutableInstruction implements Instruction23x { + public static final Format FORMAT = Format.Format23x; + + public final int registerA; + public final int registerB; + public final int registerC; + + public ImmutableInstruction23x(@Nonnull Opcode opcode, + int registerA, + int registerB, + int registerC) { + super(opcode); + Preconditions.checkFormat(opcode, Format.Format35c); + this.registerA = Preconditions.checkByteRegister(registerA); + this.registerB = Preconditions.checkByteRegister(registerB); + this.registerC = Preconditions.checkByteRegister(registerC); + } + + public static ImmutableInstruction23x of(Instruction23x instruction) { + if (instruction instanceof ImmutableInstruction23x) { + return (ImmutableInstruction23x)instruction; + } + return new ImmutableInstruction23x( + instruction.getOpcode(), + instruction.getRegisterA(), + instruction.getRegisterB(), + instruction.getRegisterC()); + } + + @Override public int getRegisterA() { return registerA; } + @Override public int getRegisterB() { return registerB; } + @Override public int getRegisterC() { return registerC; } + + @Override public Format getFormat() { return FORMAT; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction30t.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction30t.java new file mode 100644 index 00000000..6e3678a9 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction30t.java @@ -0,0 +1,65 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction30t; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction30t extends ImmutableInstruction implements Instruction30t { + public static final Format FORMAT = Format.Format12x; + + public final int offset; + + public ImmutableInstruction30t(@Nonnull Opcode opcode, + int offset) { + super(opcode); + Preconditions.checkFormat(opcode, FORMAT); + this.offset = offset; + } + + public static ImmutableInstruction30t of(Instruction30t instruction) { + if (instruction instanceof ImmutableInstruction30t) { + return (ImmutableInstruction30t)instruction; + } + return new ImmutableInstruction30t( + instruction.getOpcode(), + instruction.getOffset()); + } + + @Override public int getOffset() { return offset; } + @Override public Format getFormat() { return FORMAT; } +} + diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31c.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31c.java new file mode 100644 index 00000000..b3db36ed --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31c.java @@ -0,0 +1,70 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction31c; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction31c extends ImmutableInstruction implements Instruction31c { + public static final Format FORMAT = Format.Format31c; + + public final int registerA; + @Nonnull public final String reference; + + public ImmutableInstruction31c(@Nonnull Opcode opcode, + int registerA, + @Nonnull String reference) { + super(opcode); + Preconditions.checkFormat(opcode, FORMAT); + this.registerA = Preconditions.checkByteRegister(registerA); + this.reference = Preconditions.checkReference(reference, opcode.referenceType); + } + + public static ImmutableInstruction31c of(Instruction31c instruction) { + if (instruction instanceof ImmutableInstruction31c) { + return (ImmutableInstruction31c)instruction; + } + return new ImmutableInstruction31c( + instruction.getOpcode(), + instruction.getRegisterA(), + instruction.getReference()); + } + + @Override public int getRegisterA() { return registerA; } + @Nonnull @Override public String getReference() { return reference; } + + @Override public Format getFormat() { return FORMAT; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31i.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31i.java new file mode 100644 index 00000000..cefc3ce8 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31i.java @@ -0,0 +1,71 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction31i; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction31i extends ImmutableInstruction implements Instruction31i { + public static final Format FORMAT = Format.Format31i; + + public final int registerA; + public final int literal; + + public ImmutableInstruction31i(@Nonnull Opcode opcode, + int registerA, + int literal) { + super(opcode); + Preconditions.checkFormat(opcode, FORMAT); + this.registerA = Preconditions.checkByteRegister(registerA); + this.literal = literal; + } + + public static ImmutableInstruction31i of(Instruction31i instruction) { + if (instruction instanceof ImmutableInstruction31i) { + return (ImmutableInstruction31i)instruction; + } + return new ImmutableInstruction31i( + instruction.getOpcode(), + instruction.getRegisterA(), + instruction.getNarrowLiteral()); + } + + @Override public int getRegisterA() { return registerA; } + @Override public int getNarrowLiteral() { return literal; } + @Override public long getWideLiteral() { return literal; } + + @Override public Format getFormat() { return FORMAT; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31t.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31t.java new file mode 100644 index 00000000..3625fbcc --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31t.java @@ -0,0 +1,71 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction31t; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction31t extends ImmutableInstruction implements Instruction31t { + public static final Format FORMAT = Format.Format31t; + + public final int registerA; + public final int offset; + + public ImmutableInstruction31t(@Nonnull Opcode opcode, + int registerA, + int offset) { + super(opcode); + Preconditions.checkFormat(opcode, FORMAT); + this.registerA = Preconditions.checkByteRegister(registerA); + this.offset = offset; + } + + public static ImmutableInstruction31t of(Instruction31t instruction) { + if (instruction instanceof ImmutableInstruction31t) { + return (ImmutableInstruction31t)instruction; + } + return new ImmutableInstruction31t( + instruction.getOpcode(), + instruction.getRegisterA(), + instruction.getOffset()); + } + + @Override public int getRegisterA() { return registerA; } + @Override public int getOffset() { return offset; } + + @Override public Format getFormat() { return FORMAT; } +} + diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction32x.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction32x.java new file mode 100644 index 00000000..7fb69131 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction32x.java @@ -0,0 +1,70 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction32x; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction32x extends ImmutableInstruction implements Instruction32x { + public static final Format FORMAT = Format.Format32x; + + public final int registerA; + public final int registerB; + + public ImmutableInstruction32x(@Nonnull Opcode opcode, + int registerA, + int registerB) { + super(opcode); + Preconditions.checkFormat(opcode, Format.Format35c); + this.registerA = Preconditions.checkShortRegister(registerA); + this.registerB = Preconditions.checkShortRegister(registerB); + } + + public static ImmutableInstruction32x of(Instruction32x instruction) { + if (instruction instanceof ImmutableInstruction32x) { + return (ImmutableInstruction32x)instruction; + } + return new ImmutableInstruction32x( + instruction.getOpcode(), + instruction.getRegisterA(), + instruction.getRegisterB()); + } + + @Override public int getRegisterA() { return registerA; } + @Override public int getRegisterB() { return registerB; } + + @Override public Format getFormat() { return FORMAT; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction35c.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction35c.java new file mode 100644 index 00000000..6d5b9a2e --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction35c.java @@ -0,0 +1,95 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction35c; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction35c extends ImmutableInstruction implements Instruction35c { + public static final Format FORMAT = Format.Format35c; + + public final int registerCount; + public final int registerC; + public final int registerD; + public final int registerE; + public final int registerF; + public final int registerG; + @Nonnull public final String reference; + + public ImmutableInstruction35c(@Nonnull Opcode opcode, + int registerCount, + int registerC, + int registerD, + int registerE, + int registerF, + int registerG, + @Nonnull String reference) { + super(opcode); + Preconditions.checkFormat(opcode, Format.Format35c); + this.registerCount = Preconditions.check35cRegisterCount(registerCount); + this.registerC = (registerCount>0) ? Preconditions.checkNibbleRegister(registerC) : 0; + this.registerD = (registerCount>1) ? Preconditions.checkNibbleRegister(registerD) : 0; + this.registerE = (registerCount>2) ? Preconditions.checkNibbleRegister(registerE) : 0; + this.registerF = (registerCount>3) ? Preconditions.checkNibbleRegister(registerF) : 0; + this.registerG = (registerCount>4) ? Preconditions.checkNibbleRegister(registerG) : 0; + this.reference = Preconditions.checkReference(reference, opcode.referenceType); + } + + public static ImmutableInstruction35c of(Instruction35c instruction) { + if (instruction instanceof ImmutableInstruction35c) { + return (ImmutableInstruction35c)instruction; + } + return new ImmutableInstruction35c( + instruction.getOpcode(), + instruction.getRegisterCount(), + instruction.getRegisterC(), + instruction.getRegisterD(), + instruction.getRegisterE(), + instruction.getRegisterF(), + instruction.getRegisterG(), + instruction.getReference()); + } + + @Override public int getRegisterCount() { return registerCount; } + @Override public int getRegisterC() { return registerC; } + @Override public int getRegisterD() { return registerD; } + @Override public int getRegisterE() { return registerE; } + @Override public int getRegisterF() { return registerF; } + @Override public int getRegisterG() { return registerG; } + @Nonnull @Override public String getReference() { return reference; } + + @Override public Format getFormat() { return FORMAT; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction3rc.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction3rc.java new file mode 100644 index 00000000..26514921 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction3rc.java @@ -0,0 +1,77 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction3rc; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction3rc extends ImmutableInstruction implements Instruction3rc { + public static final Format FORMAT = Format.Format3rc; + + public final int startRegister; + public final int registerCount; + + @Nonnull public final String reference; + + public ImmutableInstruction3rc(@Nonnull Opcode opcode, + int startRegister, + int registerCount, + @Nonnull String reference) { + super(opcode); + Preconditions.checkFormat(opcode, Format.Format3rc); + this.startRegister = Preconditions.checkShortRegister(startRegister); + this.registerCount = Preconditions.check3rcRegisterCount(registerCount); + this.reference = Preconditions.checkReference(reference, opcode.referenceType); + } + + public static ImmutableInstruction3rc of(Instruction3rc instruction) { + if (instruction instanceof ImmutableInstruction3rc) { + return (ImmutableInstruction3rc)instruction; + } + return new ImmutableInstruction3rc( + instruction.getOpcode(), + instruction.getStartRegister(), + instruction.getRegisterCount(), + instruction.getReference()); + } + + @Override public int getStartRegister() { return startRegister; } + @Override public int getRegisterCount() { return registerCount; } + @Nonnull @Override public String getReference() { return reference; } + + @Override public Format getFormat() { return FORMAT; } +} + diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction51l.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction51l.java new file mode 100644 index 00000000..0857dc4d --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction51l.java @@ -0,0 +1,70 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.instruction; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.formats.Instruction51l; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; + +public class ImmutableInstruction51l extends ImmutableInstruction implements Instruction51l { + public static final Format FORMAT = Format.Format51l; + + public final int registerA; + public final long literal; + + public ImmutableInstruction51l(@Nonnull Opcode opcode, + int registerA, + long literal) { + super(opcode); + Preconditions.checkFormat(opcode, FORMAT); + this.registerA = Preconditions.checkByteRegister(registerA); + this.literal = literal; + } + + public static ImmutableInstruction51l of(Instruction51l instruction) { + if (instruction instanceof ImmutableInstruction51l) { + return (ImmutableInstruction51l)instruction; + } + return new ImmutableInstruction51l( + instruction.getOpcode(), + instruction.getRegisterA(), + instruction.getWideLiteral()); + } + + @Override public int getRegisterA() { return registerA; } + @Override public long getWideLiteral() { return literal; } + + @Override public Format getFormat() { return FORMAT; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableAnnotationEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableAnnotationEncodedValue.java new file mode 100644 index 00000000..6171a10d --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableAnnotationEncodedValue.java @@ -0,0 +1,66 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.value; + +import org.jf.dexlib2.iface.value.EncodedValue; +import org.jf.dexlib2.immutable.ImmutableBaseAnnotation; +import org.jf.dexlib2.iface.BaseAnnotation; +import org.jf.dexlib2.iface.value.AnnotationEncodedValue; + +import javax.annotation.Nonnull; + +public class ImmutableAnnotationEncodedValue extends ImmutableEncodedValue implements AnnotationEncodedValue { + @Nonnull + public final ImmutableBaseAnnotation value; + + public ImmutableAnnotationEncodedValue(@Nonnull BaseAnnotation value) { + super(EncodedValue.ANNOTATION); + this.value = ImmutableBaseAnnotation.of(value); + } + + public ImmutableAnnotationEncodedValue(@Nonnull ImmutableBaseAnnotation value) { + super(EncodedValue.ANNOTATION); + this.value = value; + } + + public static ImmutableAnnotationEncodedValue of(@Nonnull AnnotationEncodedValue annotationEncodedValue) { + if (annotationEncodedValue instanceof ImmutableAnnotationEncodedValue) { + return (ImmutableAnnotationEncodedValue)annotationEncodedValue; + } + return new ImmutableAnnotationEncodedValue(annotationEncodedValue.getValue()); + } + + @Nonnull + public ImmutableBaseAnnotation getValue() { + return value; + } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableArrayEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableArrayEncodedValue.java new file mode 100644 index 00000000..c5248b18 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableArrayEncodedValue.java @@ -0,0 +1,66 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.value; + +import com.google.common.collect.ImmutableList; +import org.jf.dexlib2.iface.value.ArrayEncodedValue; +import org.jf.dexlib2.iface.value.EncodedValue; + +import javax.annotation.Nonnull; +import java.util.List; + +public class ImmutableArrayEncodedValue extends ImmutableEncodedValue implements ArrayEncodedValue { + @Nonnull + public final ImmutableList value; + + public ImmutableArrayEncodedValue(@Nonnull List value) { + super(EncodedValue.ARRAY); + this.value = ImmutableEncodedValue.immutableListOf(value); + } + + public ImmutableArrayEncodedValue(@Nonnull ImmutableList value) { + super(EncodedValue.ARRAY); + this.value = value; + } + + public static ImmutableArrayEncodedValue of(@Nonnull ArrayEncodedValue arrayEncodedValue) { + if (arrayEncodedValue instanceof ImmutableArrayEncodedValue) { + return (ImmutableArrayEncodedValue)arrayEncodedValue; + } + return new ImmutableArrayEncodedValue(arrayEncodedValue.getValue()); + } + + @Nonnull + public ImmutableList getValue() { + return value; + } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableBooleanEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableBooleanEncodedValue.java new file mode 100644 index 00000000..27e1490b --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableBooleanEncodedValue.java @@ -0,0 +1,55 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.value; + +import org.jf.dexlib2.iface.value.BooleanEncodedValue; +import org.jf.dexlib2.iface.value.EncodedValue; + +public class ImmutableBooleanEncodedValue extends ImmutableEncodedValue implements BooleanEncodedValue { + public final boolean value; + + public ImmutableBooleanEncodedValue(boolean value) { + super(EncodedValue.BOOLEAN); + this.value = value; + } + + public static ImmutableBooleanEncodedValue of(BooleanEncodedValue booleanEncodedValue) { + if (booleanEncodedValue instanceof ImmutableBooleanEncodedValue) { + return (ImmutableBooleanEncodedValue)booleanEncodedValue; + } + return new ImmutableBooleanEncodedValue(booleanEncodedValue.getValue()); + } + + public boolean getValue() { + return value; + } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableByteEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableByteEncodedValue.java new file mode 100644 index 00000000..1411319a --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableByteEncodedValue.java @@ -0,0 +1,55 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.value; + +import org.jf.dexlib2.iface.value.ByteEncodedValue; +import org.jf.dexlib2.iface.value.EncodedValue; + +public class ImmutableByteEncodedValue extends ImmutableEncodedValue implements ByteEncodedValue { + public final byte value; + + public ImmutableByteEncodedValue(byte value) { + super(EncodedValue.BYTE); + this.value = value; + } + + public static ImmutableByteEncodedValue of(ByteEncodedValue byteEncodedValue) { + if (byteEncodedValue instanceof ImmutableByteEncodedValue) { + return (ImmutableByteEncodedValue)byteEncodedValue; + } + return new ImmutableByteEncodedValue(byteEncodedValue.getValue()); + } + + public byte getValue() { + return value; + } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableCharEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableCharEncodedValue.java new file mode 100644 index 00000000..2c142f3d --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableCharEncodedValue.java @@ -0,0 +1,55 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.value; + +import org.jf.dexlib2.iface.value.CharEncodedValue; +import org.jf.dexlib2.iface.value.EncodedValue; + +public class ImmutableCharEncodedValue extends ImmutableEncodedValue implements CharEncodedValue { + public final char value; + + public ImmutableCharEncodedValue(char value) { + super(EncodedValue.CHAR); + this.value = value; + } + + public static ImmutableCharEncodedValue of(CharEncodedValue charEncodedValue) { + if (charEncodedValue instanceof ImmutableCharEncodedValue) { + return (ImmutableCharEncodedValue)charEncodedValue; + } + return new ImmutableCharEncodedValue(charEncodedValue.getValue()); + } + + public char getValue() { + return value; + } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableDoubleEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableDoubleEncodedValue.java new file mode 100644 index 00000000..5a998c95 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableDoubleEncodedValue.java @@ -0,0 +1,55 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.value; + +import org.jf.dexlib2.iface.value.DoubleEncodedValue; +import org.jf.dexlib2.iface.value.EncodedValue; + +public class ImmutableDoubleEncodedValue extends ImmutableEncodedValue implements DoubleEncodedValue { + public final double value; + + public ImmutableDoubleEncodedValue(double value) { + super(EncodedValue.DOUBLE); + this.value = value; + } + + public static ImmutableDoubleEncodedValue of(DoubleEncodedValue doubleEncodedValue) { + if (doubleEncodedValue instanceof ImmutableDoubleEncodedValue) { + return (ImmutableDoubleEncodedValue)doubleEncodedValue; + } + return new ImmutableDoubleEncodedValue(doubleEncodedValue.getValue()); + } + + public double getValue() { + return value; + } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableEncodedValue.java new file mode 100644 index 00000000..c2836841 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableEncodedValue.java @@ -0,0 +1,108 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.value; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import org.jf.dexlib2.iface.value.*; +import org.jf.util.ImmutableListConverter; + +import javax.annotation.Nonnull; +import java.util.List; + +public class ImmutableEncodedValue implements EncodedValue { + public final int type; + + protected ImmutableEncodedValue(int type) { + this.type = type; + } + + public static ImmutableEncodedValue of(EncodedValue encodedValue) { + switch (encodedValue.getType()) { + case BYTE: + return ImmutableByteEncodedValue.of((ByteEncodedValue)encodedValue); + case SHORT: + return ImmutableShortEncodedValue.of((ShortEncodedValue)encodedValue); + case CHAR: + return ImmutableCharEncodedValue.of((CharEncodedValue)encodedValue); + case INT: + return ImmutableIntEncodedValue.of((IntEncodedValue)encodedValue); + case LONG: + return ImmutableLongEncodedValue.of((LongEncodedValue)encodedValue); + case FLOAT: + return ImmutableFloatEncodedValue.of((FloatEncodedValue)encodedValue); + case DOUBLE: + return ImmutableDoubleEncodedValue.of((DoubleEncodedValue)encodedValue); + case STRING: + return ImmutableStringEncodedValue.of((StringEncodedValue)encodedValue); + case TYPE: + return ImmutableTypeEncodedValue.of((TypeEncodedValue)encodedValue); + case FIELD: + return ImmutableFieldEncodedValue.of((FieldEncodedValue)encodedValue); + case METHOD: + return ImmutableMethodEncodedValue.of((MethodEncodedValue)encodedValue); + case ENUM: + return ImmutableEnumEncodedValue.of((EnumEncodedValue)encodedValue); + case ARRAY: + return ImmutableArrayEncodedValue.of((ArrayEncodedValue)encodedValue); + case ANNOTATION: + return ImmutableAnnotationEncodedValue.of((AnnotationEncodedValue)encodedValue); + case NULL: + return ImmutableNullEncodedValue.INSTANCE; + case BOOLEAN: + return ImmutableBooleanEncodedValue.of((BooleanEncodedValue)encodedValue); + default: + Preconditions.checkArgument(false); + return null; + } + } + + public int getType() { return type; } + + @Nonnull + public static ImmutableList immutableListOf(List list) { + return CONVERTER.convert(list); + } + + private static final ImmutableListConverter CONVERTER = + new ImmutableListConverter() { + @Override + protected boolean isImmutable(EncodedValue item) { + return item instanceof ImmutableEncodedValue; + } + + @Override + protected ImmutableEncodedValue makeImmutable(EncodedValue item) { + return ImmutableEncodedValue.of(item); + } + }; +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableEnumEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableEnumEncodedValue.java new file mode 100644 index 00000000..34686234 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableEnumEncodedValue.java @@ -0,0 +1,57 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.value; + +import org.jf.dexlib2.iface.value.EncodedValue; +import org.jf.dexlib2.iface.value.EnumEncodedValue; + +import javax.annotation.Nonnull; + +public class ImmutableEnumEncodedValue extends ImmutableEncodedValue implements EnumEncodedValue { + @Nonnull public final String value; + + public ImmutableEnumEncodedValue(@Nonnull String value) { + super(EncodedValue.ENUM); + this.value = value; + } + + public static ImmutableEnumEncodedValue of(EnumEncodedValue enumEncodedValue) { + if (enumEncodedValue instanceof ImmutableEnumEncodedValue) { + return (ImmutableEnumEncodedValue)enumEncodedValue; + } + return new ImmutableEnumEncodedValue(enumEncodedValue.getValue()); + } + + @Nonnull public String getValue() { + return value; + } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableFieldEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableFieldEncodedValue.java new file mode 100644 index 00000000..9680d3f4 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableFieldEncodedValue.java @@ -0,0 +1,58 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.value; + +import org.jf.dexlib2.iface.value.EncodedValue; +import org.jf.dexlib2.iface.value.FieldEncodedValue; + +import javax.annotation.Nonnull; + +public class ImmutableFieldEncodedValue extends ImmutableEncodedValue implements FieldEncodedValue { + @Nonnull public final String value; + + public ImmutableFieldEncodedValue(@Nonnull String value) { + super(EncodedValue.FIELD); + this.value = value; + } + + public static ImmutableFieldEncodedValue of(@Nonnull FieldEncodedValue fieldEncodedValue) { + if (fieldEncodedValue instanceof ImmutableFieldEncodedValue) { + return (ImmutableFieldEncodedValue)fieldEncodedValue; + } + return new ImmutableFieldEncodedValue(fieldEncodedValue.getValue()); + } + + @Nonnull + public String getValue() { + return value; + } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableFloatEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableFloatEncodedValue.java new file mode 100644 index 00000000..6974fc89 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableFloatEncodedValue.java @@ -0,0 +1,55 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.value; + +import org.jf.dexlib2.iface.value.EncodedValue; +import org.jf.dexlib2.iface.value.FloatEncodedValue; + +public class ImmutableFloatEncodedValue extends ImmutableEncodedValue implements FloatEncodedValue { + public final float value; + + public ImmutableFloatEncodedValue(float value) { + super(EncodedValue.FLOAT); + this.value = value; + } + + public static ImmutableFloatEncodedValue of(FloatEncodedValue floatEncodedValue) { + if (floatEncodedValue instanceof ImmutableFloatEncodedValue) { + return (ImmutableFloatEncodedValue)floatEncodedValue; + } + return new ImmutableFloatEncodedValue(floatEncodedValue.getValue()); + } + + public float getValue() { + return value; + } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableIntEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableIntEncodedValue.java new file mode 100644 index 00000000..85325314 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableIntEncodedValue.java @@ -0,0 +1,55 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.value; + +import org.jf.dexlib2.iface.value.EncodedValue; +import org.jf.dexlib2.iface.value.IntEncodedValue; + +public class ImmutableIntEncodedValue extends ImmutableEncodedValue implements IntEncodedValue { + public final int value; + + public ImmutableIntEncodedValue(int value) { + super(EncodedValue.INT); + this.value = value; + } + + public static ImmutableIntEncodedValue of(IntEncodedValue intEncodedValue) { + if (intEncodedValue instanceof ImmutableIntEncodedValue) { + return (ImmutableIntEncodedValue)intEncodedValue; + } + return new ImmutableIntEncodedValue(intEncodedValue.getValue()); + } + + public int getValue() { + return value; + } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableLongEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableLongEncodedValue.java new file mode 100644 index 00000000..b8911d83 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableLongEncodedValue.java @@ -0,0 +1,55 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.value; + +import org.jf.dexlib2.iface.value.EncodedValue; +import org.jf.dexlib2.iface.value.LongEncodedValue; + +public class ImmutableLongEncodedValue extends ImmutableEncodedValue implements LongEncodedValue { + public final long value; + + public ImmutableLongEncodedValue(long value) { + super(EncodedValue.LONG); + this.value = value; + } + + public static ImmutableLongEncodedValue of(LongEncodedValue longEncodedValue) { + if (longEncodedValue instanceof ImmutableLongEncodedValue) { + return (ImmutableLongEncodedValue)longEncodedValue; + } + return new ImmutableLongEncodedValue(longEncodedValue.getValue()); + } + + public long getValue() { + return value; + } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableMethodEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableMethodEncodedValue.java new file mode 100644 index 00000000..9fc89df0 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableMethodEncodedValue.java @@ -0,0 +1,59 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.value; + +import org.jf.dexlib2.iface.value.EncodedValue; +import org.jf.dexlib2.iface.value.MethodEncodedValue; + +import javax.annotation.Nonnull; + +public class ImmutableMethodEncodedValue extends ImmutableEncodedValue implements MethodEncodedValue { + @Nonnull + public final String value; + + public ImmutableMethodEncodedValue(@Nonnull String value) { + super(EncodedValue.METHOD); + this.value = value; + } + + public static ImmutableMethodEncodedValue of(@Nonnull MethodEncodedValue methodEncodedValue) { + if (methodEncodedValue instanceof ImmutableMethodEncodedValue) { + return (ImmutableMethodEncodedValue)methodEncodedValue; + } + return new ImmutableMethodEncodedValue(methodEncodedValue.getValue()); + } + + @Nonnull + public String getValue() { + return value; + } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableNullEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableNullEncodedValue.java new file mode 100644 index 00000000..8f74542b --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableNullEncodedValue.java @@ -0,0 +1,43 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.value; + +import org.jf.dexlib2.iface.value.EncodedValue; +import org.jf.dexlib2.iface.value.NullEncodedValue; + +public class ImmutableNullEncodedValue extends ImmutableEncodedValue implements NullEncodedValue { + public static final ImmutableNullEncodedValue INSTANCE = new ImmutableNullEncodedValue(); + + public ImmutableNullEncodedValue() { + super(EncodedValue.NULL); + } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableShortEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableShortEncodedValue.java new file mode 100644 index 00000000..980c4ec8 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableShortEncodedValue.java @@ -0,0 +1,55 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.value; + +import org.jf.dexlib2.iface.value.EncodedValue; +import org.jf.dexlib2.iface.value.ShortEncodedValue; + +public class ImmutableShortEncodedValue extends ImmutableEncodedValue implements ShortEncodedValue { + public final short value; + + public ImmutableShortEncodedValue(short value) { + super(EncodedValue.SHORT); + this.value = value; + } + + public static ImmutableShortEncodedValue of(ShortEncodedValue shortEncodedValue) { + if (shortEncodedValue instanceof ImmutableShortEncodedValue) { + return (ImmutableShortEncodedValue)shortEncodedValue; + } + return new ImmutableShortEncodedValue(shortEncodedValue.getValue()); + } + + public short getValue() { + return value; + } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableStringEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableStringEncodedValue.java new file mode 100644 index 00000000..32813e4c --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableStringEncodedValue.java @@ -0,0 +1,59 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.value; + +import org.jf.dexlib2.iface.value.EncodedValue; +import org.jf.dexlib2.iface.value.StringEncodedValue; + +import javax.annotation.Nonnull; + +public class ImmutableStringEncodedValue extends ImmutableEncodedValue implements StringEncodedValue { + @Nonnull + public final String value; + + public ImmutableStringEncodedValue(@Nonnull String value) { + super(EncodedValue.STRING); + this.value = value; + } + + public static ImmutableStringEncodedValue of(@Nonnull StringEncodedValue stringEncodedValue) { + if (stringEncodedValue instanceof ImmutableStringEncodedValue) { + return (ImmutableStringEncodedValue)stringEncodedValue; + } + return new ImmutableStringEncodedValue(stringEncodedValue.getValue()); + } + + @Nonnull + public String getValue() { + return value; + } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableTypeEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableTypeEncodedValue.java new file mode 100644 index 00000000..e38e910f --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableTypeEncodedValue.java @@ -0,0 +1,59 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.immutable.value; + +import org.jf.dexlib2.iface.value.EncodedValue; +import org.jf.dexlib2.iface.value.TypeEncodedValue; + +import javax.annotation.Nonnull; + +public class ImmutableTypeEncodedValue extends ImmutableEncodedValue implements TypeEncodedValue { + @Nonnull + public final String value; + + public ImmutableTypeEncodedValue(@Nonnull String value) { + super(EncodedValue.TYPE); + this.value = value; + } + + public static ImmutableTypeEncodedValue of(@Nonnull TypeEncodedValue typeEncodedValue) { + if (typeEncodedValue instanceof ImmutableTypeEncodedValue) { + return (ImmutableTypeEncodedValue)typeEncodedValue; + } + return new ImmutableTypeEncodedValue(typeEncodedValue.getValue()); + } + + @Nonnull + public String getValue() { + return value; + } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/util/Preconditions.java b/dexlib2/src/main/java/org/jf/dexlib2/util/Preconditions.java new file mode 100644 index 00000000..60073982 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/util/Preconditions.java @@ -0,0 +1,146 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.util; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.ReferenceType; + +public class Preconditions { + public static void checkFormat(Opcode opcode, Format expectedFormat) { + if (opcode.format != expectedFormat) { + throw new IllegalArgumentException( + String.format("Invalid opcode %s for %s", opcode.name, expectedFormat.name())); + } + } + + public static int checkNibbleRegister(int register) { + if ((register & 0xFFFFFFF0) != 0) { + throw new IllegalArgumentException( + String.format("Invalid register: v%d. Must be between v0 and v15, inclusive.", register)); + } + return register; + } + + public static int checkByteRegister(int register) { + if ((register & 0xFFFFFF00) != 0) { + throw new IllegalArgumentException( + String.format("Invalid register: v%d. Must be between v0 and v255, inclusive.", register)); + } + return register; + } + + public static int checkShortRegister(int register) { + if ((register & 0xFFFF0000) != 0) { + throw new IllegalArgumentException( + String.format("Invalid register: v%d. Must be between v0 and v65535, inclusive.", register)); + } + return register; + } + + public static int checkNibbleLiteral(int literal) { + if ((literal & 0xFFFFFFF0) != 0) { + throw new IllegalArgumentException( + String.format("Invalid literal value: %d. Must be between -8 and 7, inclusive.", literal)); + } + return literal; + } + + public static int checkByteLiteral(int literal) { + if ((literal & 0xFFFFFF00) != 0) { + throw new IllegalArgumentException( + String.format("Invalid literal value: %d. Must be between -128 and 127, inclusive.", literal)); + } + return literal; + } + + public static int checkShortLiteral(int literal) { + if ((literal & 0xFFFF0000) != 0) { + throw new IllegalArgumentException( + String.format("Invalid literal value: %d. Must be between -32768 and 32767, inclusive.", literal)); + } + return literal; + } + + public static int checkIntegerHatLiteral(int literal) { + if ((literal & 0xFFFF) != 0) { + throw new IllegalArgumentException( + String.format("Invalid literal value: %d. Low 16 bits must be zeroed out.", literal)); + } + return literal; + } + + public static long checkLongHatLiteral(long literal) { + if ((literal & 0xFFFFFFFFFFFFL) != 0) { + throw new IllegalArgumentException( + String.format("Invalid literal value: %d. Low 16 bits must be zeroed out.", literal)); + } + return literal; + } + + public static int checkByteOffset(int register) { + if ((register & 0xFFFFFF00) != 0) { + throw new IllegalArgumentException( + String.format("Invalid code offset: %d. Must be between -8 and 7, inclusive.", register)); + } + return register; + } + + public static int checkShortOffset(int register) { + if ((register & 0xFFFF0000) != 0) { + throw new IllegalArgumentException( + String.format("Invalid code offset: %d. Must be between -32768 and 32767, inclusive.", register)); + } + return register; + } + + public static String checkReference(String reference, int referenceType) { + //TODO: implement this + return reference; + } + + public static int check35cRegisterCount(int registerCount) { + if (registerCount < 0 || registerCount > 5) { + throw new IllegalArgumentException( + String.format("Invalid register count: %d. Must be between 0 and 5, inclusive.", registerCount)); + } + return registerCount; + } + + public static int check3rcRegisterCount(int registerCount) { + if ((registerCount & 0xFFFFFF00) == 0) { + throw new IllegalArgumentException( + String.format("Invalid register count: %d. Must be between 0 and 255, inclusive.", registerCount)); + } + return registerCount; + } +}