Skip to content
Permalink
Browse files

Refactor most of the utility classes from util into dexlib2

Now, util only contains the utilities that are needed by smali/baksmali,
but not dexlib2. E.g. the common command line parsing utilities, some path
utilities, etc.

This also reverses the dependency between the util module and dexlib2.
  • Loading branch information...
JesusFreke committed Jun 30, 2018
1 parent 239b64b commit fd3e00ab3585096b8a8f7d0cd0222a6cdced36d8
Showing with 133 additions and 395 deletions.
  1. +3 −1 baksmali/src/main/java/org/jf/baksmali/DisassembleCommand.java
  2. +2 −3 baksmali/src/test/java/org/jf/baksmali/AnalysisTest.java
  3. +65 −6 baksmali/src/test/java/org/jf/baksmali/BaksmaliTestUtils.java
  4. +0 −1 dexlib2/build.gradle
  5. +1 −1 dexlib2/src/main/java/org/jf/dexlib2/analysis/ClassProto.java
  6. +1 −1 dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedMethodImplementation.java
  7. +1 −1 dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/SectionAnnotator.java
  8. +44 −0 dexlib2/src/main/java/org/jf/dexlib2/util/AlignmentUtils.java
  9. 0 {util → dexlib2}/src/main/java/org/jf/util/AbstractForwardSequentialList.java
  10. 0 {util → dexlib2}/src/main/java/org/jf/util/AbstractListIterator.java
  11. 0 {util → dexlib2}/src/main/java/org/jf/util/ArraySortedSet.java
  12. 0 {util → dexlib2}/src/main/java/org/jf/util/BitSetUtils.java
  13. 0 {util → dexlib2}/src/main/java/org/jf/util/CharSequenceUtils.java
  14. 0 {util → dexlib2}/src/main/java/org/jf/util/CollectionUtils.java
  15. 0 {util → dexlib2}/src/main/java/org/jf/util/ExceptionWithContext.java
  16. 0 {util → dexlib2}/src/main/java/org/jf/util/Hex.java
  17. 0 {util → dexlib2}/src/main/java/org/jf/util/ImmutableConverter.java
  18. 0 {util → dexlib2}/src/main/java/org/jf/util/ImmutableUtils.java
  19. 0 {util → dexlib2}/src/main/java/org/jf/util/IndentingWriter.java
  20. 0 {util → dexlib2}/src/main/java/org/jf/util/NibbleUtils.java
  21. 0 {util → dexlib2}/src/main/java/org/jf/util/NumberUtils.java
  22. 0 {util → dexlib2}/src/main/java/org/jf/util/RandomAccessFileInputStream.java
  23. 0 {util → dexlib2}/src/main/java/org/jf/util/RandomAccessFileOutputStream.java
  24. 0 {util → dexlib2}/src/main/java/org/jf/util/SparseArray.java
  25. 0 {util → dexlib2}/src/main/java/org/jf/util/SparseIntArray.java
  26. 0 {util → dexlib2}/src/main/java/org/jf/util/StringUtils.java
  27. +4 −8 {util → dexlib2}/src/main/java/org/jf/util/StringWrapper.java
  28. 0 {util → dexlib2}/src/main/java/org/jf/util/TwoColumnOutput.java
  29. 0 {util → dexlib2}/src/main/java/org/jf/util/Utf8Utils.java
  30. 0 {util → dexlib2}/src/main/java/org/jf/util/WrappedIndentingWriter.java
  31. +0 −1 dexlib2/src/test/java/org/jf/dexlib2/writer/DexDataWriterTest.java
  32. +0 −1 dexlib2/src/test/java/org/jf/dexlib2/writer/DexWriterSleb128Test.java
  33. +0 −1 dexlib2/src/test/java/org/jf/dexlib2/writer/DexWriterUleb128Test.java
  34. +5 −5 ...n/java/org/jf/util → dexlib2/src/test/java/org/jf/dexlib2/writer}/NakedByteArrayOutputStream.java
  35. 0 {util → dexlib2}/src/test/java/org/jf/util/AbstractForwardSequentialListTest.java
  36. 0 {util → dexlib2}/src/test/java/org/jf/util/IndentingWriterTest.java
  37. 0 {util → dexlib2}/src/test/java/org/jf/util/NumberUtilsTest.java
  38. 0 {util → dexlib2}/src/test/java/org/jf/util/StringWrapperTest.java
  39. +5 −5 {util/src/main/java/org/jf → smali/src/main/java/org/jf/smali}/util/BlankReader.java
  40. +1 −0 smali/src/main/jflex/smaliLexer.jflex
  41. +1 −0 util/build.gradle
  42. +0 −41 util/src/main/java/org/jf/util/AlignmentUtils.java
  43. +0 −184 util/src/main/java/org/jf/util/OldWrappedIndentingWriter.java
  44. +0 −82 util/src/main/java/org/jf/util/TextUtils.java
  45. +0 −53 util/src/test/java/org/jf/util/TextUtilsTest.java
@@ -39,6 +39,7 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.jf.dexlib2.util.SyntheticAccessorResolver;
import org.jf.util.ConsoleUtil;
import org.jf.util.StringWrapper;
import org.jf.util.jcommander.ExtendedParameter;
import org.jf.util.jcommander.ExtendedParameters;
@@ -163,7 +164,8 @@ public void run() {
if (showDeodexWarning() && dexFile.hasOdexOpcodes()) {
StringWrapper.printWrappedString(System.err,
"Warning: You are disassembling an odex/oat file without deodexing it. You won't be able to " +
"re-assemble the results unless you deodex it. See \"baksmali help deodex\"");
"re-assemble the results unless you deodex it. See \"baksmali help deodex\"",
ConsoleUtil.getConsoleWidth());
}

File outputDirectoryFile = new File(outputDir);
@@ -42,7 +42,6 @@
import org.jf.dexlib2.iface.ClassDef;
import org.jf.dexlib2.iface.DexFile;
import org.jf.util.IndentingWriter;
import org.jf.util.TextUtils;
import org.junit.Test;

import javax.annotation.Nonnull;
@@ -122,8 +121,8 @@ public void runTest(String test, boolean registerInfo, boolean isArt) throws IOE
className.substring(1, className.length() - 1));
String smaliContents = readResource(smaliPath);

Assert.assertEquals(TextUtils.normalizeWhitespace(smaliContents),
TextUtils.normalizeWhitespace((stringWriter.toString())));
Assert.assertEquals(BaksmaliTestUtils.normalizeWhitespace(smaliContents),
BaksmaliTestUtils.normalizeWhitespace((stringWriter.toString())));
}
}

@@ -33,20 +33,24 @@

import com.google.common.io.ByteStreams;
import junit.framework.Assert;

import org.antlr.runtime.RecognitionException;
import org.jf.baksmali.Adaptors.ClassDefinition;
import org.jf.dexlib2.iface.ClassDef;
import org.jf.smali.SmaliTestUtils;
import org.jf.util.IndentingWriter;
import org.jf.util.TextUtils;
import org.junit.Test;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BaksmaliTestUtils {

private static String newline = System.getProperty("line.separator");

public static void assertSmaliCompiledEquals(String source, String expected,
BaksmaliOptions options, boolean stripComments) throws IOException,
RecognitionException {
@@ -74,9 +78,9 @@ public static void assertSmaliCompiledEquals(String source, String expected)
@Nonnull
public static String normalizeSmali(@Nonnull String smaliText, boolean stripComments) {
if (stripComments) {
smaliText = TextUtils.stripComments(smaliText);
smaliText = stripComments(smaliText);
}
return TextUtils.normalizeWhitespace(smaliText);
return normalizeWhitespace(smaliText);
}

@Nonnull
@@ -113,6 +117,61 @@ public static String readResourceFully(@Nonnull String fileName, @Nonnull String
return new String(readResourceBytesFully(fileName), encoding);
}

// Static helpers class; do not instantiate.
private BaksmaliTestUtils() { throw new AssertionError(); }
@Nonnull
public static String normalizeNewlines(@Nonnull String source) {
return normalizeNewlines(source, newline);
}

@Nonnull
public static String normalizeNewlines(@Nonnull String source, String newlineValue) {
return source.replace("\r", "").replace("\n", newlineValue);
}

@Nonnull
public static String normalizeWhitespace(@Nonnull String source) {
// Go to native system new lines so that ^/$ work correctly
source = normalizeNewlines(source);

// Remove all suffix/prefix whitespace
Pattern pattern = Pattern.compile("((^[ \t]+)|([ \t]+$))", Pattern.MULTILINE);
Matcher matcher = pattern.matcher(source);
source = matcher.replaceAll("");

// Remove all empty lines
Pattern pattern2 = Pattern.compile("^\r?\n?", Pattern.MULTILINE);
Matcher matcher2 = pattern2.matcher(source);
source = matcher2.replaceAll("");

// Remove a trailing new line, if present
Pattern pattern3 = Pattern.compile("\r?\n?$");
Matcher matcher3 = pattern3.matcher(source);
source = matcher3.replaceAll("");

// Go back to unix-style \n newlines
source = normalizeNewlines(source, "\n");
return source;
}

@Nonnull
public static String stripComments(@Nonnull String source) {
Pattern pattern = Pattern.compile("#(.*)");
Matcher matcher = pattern.matcher(source);
return matcher.replaceAll("");
}

@Test
public void testStripComments() {
Assert.assertEquals("", stripComments("#world"));
Assert.assertEquals("hello", stripComments("hello#world"));
Assert.assertEquals("multi\nline", stripComments("multi#hello world\nline#world"));
}

@Test
public void testNormalizeWhitespace() {
Assert.assertEquals("", normalizeWhitespace(" "));
Assert.assertEquals("hello", normalizeWhitespace("hello "));
Assert.assertEquals("hello", normalizeWhitespace(" hello"));
Assert.assertEquals("hello", normalizeWhitespace(" hello "));
Assert.assertEquals("hello\nworld", normalizeWhitespace("hello \n \n world"));
}
}
@@ -46,7 +46,6 @@ configurations {
}

dependencies {
compile project(':util')
compile depends.findbugs
compile depends.guava

@@ -44,7 +44,7 @@
import org.jf.dexlib2.iface.reference.FieldReference;
import org.jf.dexlib2.iface.reference.MethodReference;
import org.jf.dexlib2.util.MethodUtil;
import org.jf.util.AlignmentUtils;
import org.jf.dexlib2.util.AlignmentUtils;
import org.jf.util.ExceptionWithContext;
import org.jf.util.SparseArray;

@@ -41,7 +41,7 @@
import org.jf.dexlib2.iface.MethodImplementation;
import org.jf.dexlib2.iface.debug.DebugItem;
import org.jf.dexlib2.iface.instruction.Instruction;
import org.jf.util.AlignmentUtils;
import org.jf.dexlib2.util.AlignmentUtils;
import org.jf.util.ExceptionWithContext;

import javax.annotation.Nonnull;
@@ -34,7 +34,7 @@
import com.google.common.collect.Maps;
import org.jf.dexlib2.dexbacked.raw.util.DexAnnotator;
import org.jf.dexlib2.util.AnnotatedBytes;
import org.jf.util.AlignmentUtils;
import org.jf.dexlib2.util.AlignmentUtils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@@ -0,0 +1,44 @@
/*
* Copyright 2018, 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;

public abstract class AlignmentUtils {
public static int alignOffset(int offset, int alignment) {
int mask = alignment - 1;
assert (alignment >= 0) && ((mask & alignment) == 0);
return (offset + mask) & ~mask;
}

public static boolean isAligned(int offset, int alignment) {
return (offset % alignment) == 0;
}
}
@@ -1,18 +1,18 @@
/*
* Copyright 2013, Google Inc.
* Copyright 2018, 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
* 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
* 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
* 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.
*
@@ -187,10 +187,6 @@ public void remove() {
return newArr;
}

public static void printWrappedString(@Nonnull PrintStream stream, @Nonnull String string) {
printWrappedString(stream, string, ConsoleUtil.getConsoleWidth());
}

public static void printWrappedString(@Nonnull PrintStream stream, @Nonnull String string, int maxWidth) {
for (String str: wrapStringOnBreaks(string, maxWidth)) {
stream.println(str);
@@ -34,7 +34,6 @@
import junit.framework.Assert;
import org.jf.dexlib2.ValueType;
import org.jf.util.ExceptionWithContext;
import org.jf.util.NakedByteArrayOutputStream;
import org.junit.Before;
import org.junit.Test;

@@ -32,7 +32,6 @@
package org.jf.dexlib2.writer;

import junit.framework.Assert;
import org.jf.util.NakedByteArrayOutputStream;
import org.junit.Test;

import java.io.IOException;
@@ -32,7 +32,6 @@
package org.jf.dexlib2.writer;

import junit.framework.Assert;
import org.jf.util.NakedByteArrayOutputStream;
import org.junit.Test;

import java.io.IOException;
@@ -1,18 +1,18 @@
/*
* Copyright 2012, Google Inc.
* Copyright 2018, 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
* 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
* 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
* 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.
*
@@ -29,7 +29,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.jf.util;
package org.jf.dexlib2.writer;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -1,18 +1,18 @@
/*
* Copyright 2014, Google Inc.
* Copyright 2018, 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
* 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
* 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
* 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.
*
@@ -29,7 +29,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.jf.util;
package org.jf.smali.util;

import javax.annotation.Nonnull;
import java.io.IOException;
@@ -2,6 +2,7 @@ package org.jf.smali;

import java.io.*;
import org.antlr.runtime.*;
import org.jf.smali.util.*;
import org.jf.util.*;
import static org.jf.smali.smaliParser.*;

@@ -30,6 +30,7 @@
*/

dependencies {
compile project(':dexlib2')
compile depends.findbugs
compile depends.guava
compile depends.jcommander

This file was deleted.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit fd3e00a

Please sign in to comment.
You can’t perform that action at this time.