1/* 2 * Copyright 2012, Google Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32package org.jf.dexlib2.immutable; 33 34import com.google.common.collect.ImmutableList; 35import com.google.common.collect.ImmutableSet; 36import com.google.common.collect.ImmutableSortedSet; 37import com.google.common.collect.Ordering; 38import org.jf.dexlib2.base.reference.BaseMethodReference; 39import org.jf.dexlib2.iface.Annotation; 40import org.jf.dexlib2.iface.Method; 41import org.jf.dexlib2.iface.MethodImplementation; 42import org.jf.dexlib2.iface.MethodParameter; 43import org.jf.util.ImmutableConverter; 44import org.jf.util.ImmutableUtils; 45 46import javax.annotation.Nonnull; 47import javax.annotation.Nullable; 48import java.util.Set; 49 50public class ImmutableMethod extends BaseMethodReference implements Method { 51 @Nonnull protected final String definingClass; 52 @Nonnull protected final String name; 53 @Nonnull protected final ImmutableList<? extends ImmutableMethodParameter> parameters; 54 @Nonnull protected final String returnType; 55 protected final int accessFlags; 56 @Nonnull protected final ImmutableSet<? extends ImmutableAnnotation> annotations; 57 @Nullable protected final ImmutableMethodImplementation methodImplementation; 58 59 public ImmutableMethod(@Nonnull String definingClass, 60 @Nonnull String name, 61 @Nullable Iterable<? extends MethodParameter> parameters, 62 @Nonnull String returnType, 63 int accessFlags, 64 @Nullable Set<? extends Annotation> annotations, 65 @Nullable MethodImplementation methodImplementation) { 66 this.definingClass = definingClass; 67 this.name = name; 68 this.parameters = ImmutableMethodParameter.immutableListOf(parameters); 69 this.returnType = returnType; 70 this.accessFlags = accessFlags; 71 this.annotations = ImmutableAnnotation.immutableSetOf(annotations); 72 this.methodImplementation = ImmutableMethodImplementation.of(methodImplementation); 73 } 74 75 public ImmutableMethod(@Nonnull String definingClass, 76 @Nonnull String name, 77 @Nullable ImmutableList<? extends ImmutableMethodParameter> parameters, 78 @Nonnull String returnType, 79 int accessFlags, 80 @Nullable ImmutableSet<? extends ImmutableAnnotation> annotations, 81 @Nullable ImmutableMethodImplementation methodImplementation) { 82 this.definingClass = definingClass; 83 this.name = name; 84 this.parameters = ImmutableUtils.nullToEmptyList(parameters); 85 this.returnType = returnType; 86 this.accessFlags = accessFlags; 87 this.annotations = ImmutableUtils.nullToEmptySet(annotations); 88 this.methodImplementation = methodImplementation; 89 } 90 91 public static ImmutableMethod of(Method method) { 92 if (method instanceof ImmutableMethod) { 93 return (ImmutableMethod)method; 94 } 95 return new ImmutableMethod( 96 method.getDefiningClass(), 97 method.getName(), 98 method.getParameters(), 99 method.getReturnType(), 100 method.getAccessFlags(), 101 method.getAnnotations(), 102 method.getImplementation()); 103 } 104 105 @Override @Nonnull public String getDefiningClass() { return definingClass; } 106 @Override @Nonnull public String getName() { return name; } 107 @Override @Nonnull public ImmutableList<? extends CharSequence> getParameterTypes() { return parameters; } 108 @Override @Nonnull public ImmutableList<? extends ImmutableMethodParameter> getParameters() { return parameters; } 109 @Override @Nonnull public String getReturnType() { return returnType; } 110 @Override public int getAccessFlags() { return accessFlags; } 111 @Override @Nonnull public ImmutableSet<? extends ImmutableAnnotation> getAnnotations() { return annotations; } 112 @Override @Nullable public ImmutableMethodImplementation getImplementation() { return methodImplementation; } 113 114 @Nonnull 115 public static ImmutableSortedSet<ImmutableMethod> immutableSetOf(@Nullable Iterable<? extends Method> list) { 116 return CONVERTER.toSortedSet(Ordering.natural(), list); 117 } 118 119 private static final ImmutableConverter<ImmutableMethod, Method> CONVERTER = 120 new ImmutableConverter<ImmutableMethod, Method>() { 121 @Override 122 protected boolean isImmutable(@Nonnull Method item) { 123 return item instanceof ImmutableMethod; 124 } 125 126 @Nonnull 127 @Override 128 protected ImmutableMethod makeImmutable(@Nonnull Method item) { 129 return ImmutableMethod.of(item); 130 } 131 }; 132} 133