提交 1243231e 编写于 作者: B Ben Gruver

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.
上级 239b64ba
......@@ -39,6 +39,7 @@ import com.beust.jcommander.validators.PositiveInteger;
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 class DisassembleCommand extends DexInputCommand {
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.analysis.ClassProvider;
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 class AnalysisTest {
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 @@ package org.jf.baksmali;
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 class BaksmaliTestUtils {
@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 class BaksmaliTestUtils {
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.*;
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.dexbacked.util.VariableSizeLookaheadIterator;
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 @@ package org.jf.dexlib2.dexbacked.raw;
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;
......
/*
* 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;
}
}
/*
* 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 class StringWrapper {
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 @@ package org.jf.dexlib2.writer;
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;
......
/*
* 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;
......
/*
* 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
......
/*
* [The "BSD licence"]
* Copyright (c) 2010 Ben Gruver (JesusFreke)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.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;
}
}
/*
* Copyright 2013, 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.util;
import java.io.FilterWriter;
import java.io.IOException;
import java.io.Writer;
/**
* Writer that wraps another writer and passes width-limited and
* optionally-prefixed output to its subordinate. When lines are
* wrapped they are automatically indented based on the start of the
* line.
*/
public final class OldWrappedIndentingWriter extends FilterWriter {
/** null-ok; optional prefix for every line */
private final String prefix;
/** > 0; the maximum output width */
private final int width;
/** > 0; the maximum indent */
private final int maxIndent;
/** >= 0; current output column (zero-based) */
private int column;
/** whether indent spaces are currently being collected */
private boolean collectingIndent;
/** >= 0; current indent amount */
private int indent;
/**
* Constructs an instance.
*
* @param out non-null; writer to send final output to
* @param width >= 0; the maximum output width (not including
* <code>prefix</code>), or <code>0</code> for no maximum
* @param prefix non-null; the prefix for each line
*/
public OldWrappedIndentingWriter(Writer out, int width, String prefix) {
super(out);
if (out == null) {
throw new NullPointerException("out == null");
}
if (width < 0) {
throw new IllegalArgumentException("width < 0");
}
if (prefix == null) {
throw new NullPointerException("prefix == null");
}
this.width = (width != 0) ? width : Integer.MAX_VALUE;
this.maxIndent = width >> 1;
this.prefix = (prefix.length() == 0) ? null : prefix;
bol();
}
/**
* Constructs a no-prefix instance.
*
* @param out non-null; writer to send final output to
* @param width &gt;= 0; the maximum output width (not including
* <code>prefix</code>), or <code>0</code> for no maximum
*/
public OldWrappedIndentingWriter(Writer out, int width) {
this(out, width, "");
}
/** {@inheritDoc} */
@Override
public void write(int c) throws IOException {
synchronized (lock) {
if (collectingIndent) {
if (c == ' ') {
indent++;
if (indent >= maxIndent) {
indent = maxIndent;
collectingIndent = false;
}
} else {
collectingIndent = false;
}
}
if ((column == width) && (c != '\n')) {
out.write('\n');
column = 0;
/*
* Note: No else, so this should fall through to the next
* if statement.
*/
}
if (column == 0) {
if (prefix != null) {
out.write(prefix);
}
if (!collectingIndent) {
for (int i = 0; i < indent; i++) {
out.write(' ');
}
column = indent;
}
}
out.write(c);
if (c == '\n') {
bol();
} else {
column++;
}
}
}