ReferenceFormatter.java revision 4f2620415d505a35d2d14b866cde10a54b1b7c8c
19f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/*
29f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * [The "BSD licence"]
39f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Copyright (c) 2010 Ben Gruver
49f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * All rights reserved.
59f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
69f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Redistribution and use in source and binary forms, with or without
79f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * modification, are permitted provided that the following conditions
89f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * are met:
99f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1. Redistributions of source code must retain the above copyright
109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *    notice, this list of conditions and the following disclaimer.
119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 2. Redistributions in binary form must reproduce the above copyright
129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *    notice, this list of conditions and the following disclaimer in the
139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *    documentation and/or other materials provided with the distribution.
149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 3. The name of the author may not be used to endorse or promote products
159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *    derived from this software without specific prior written permission.
169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpackage org.jf.baksmali.Adaptors;
309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.jf.dexlib2.ReferenceType;
329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.jf.dexlib2.iface.reference.*;
339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.jf.dexlib2.util.ReferenceUtil;
349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.jf.util.IndentingWriter;
359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.jf.util.StringUtils;
369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport java.io.IOException;
389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpublic class ReferenceFormatter {
409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public static void writeStringReference(IndentingWriter writer, String item) throws IOException {
419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        writer.write('"');
429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        StringUtils.writeEscapedString(writer, item);
439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        writer.write('"');
449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public static void writeReference(IndentingWriter writer, int referenceType,
479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                                      Reference reference) throws IOException {
489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        switch (referenceType) {
499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            case ReferenceType.STRING:
509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                writeStringReference(writer, ((StringReference)reference).getString());
519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                return;
529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            case ReferenceType.TYPE:
539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                writer.write(((TypeReference)reference).getType());
549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                return;
559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            case ReferenceType.METHOD:
569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                ReferenceUtil.writeMethodDescriptor(writer, (MethodReference)reference);
579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                return;
589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            case ReferenceType.FIELD:
599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                ReferenceUtil.writeFieldDescriptor(writer, (FieldReference)reference);
609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                return;
619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            default:
629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                throw new IllegalStateException("Unknown reference type");
639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }
649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson