ImmutableMethod.java revision 2d7e1111358e2b8cc951a46dc8b0217a7fa0dead
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 org.jf.dexlib2.base.reference.BaseMethodReference;
36import org.jf.dexlib2.iface.Annotation;
37import org.jf.dexlib2.iface.Method;
38import org.jf.dexlib2.iface.MethodImplementation;
39import org.jf.dexlib2.iface.MethodParameter;
40import org.jf.util.ImmutableListConverter;
41import org.jf.util.ImmutableUtils;
42
43import javax.annotation.Nonnull;
44import javax.annotation.Nullable;
45import java.util.Collection;
46
47public class ImmutableMethod extends BaseMethodReference implements Method {
48    @Nonnull public final String containingClass;
49    @Nonnull public final String name;
50    @Nonnull public final ImmutableList<? extends ImmutableMethodParameter> parameters;
51    @Nonnull public final String returnType;
52    public final int accessFlags;
53    @Nonnull public final ImmutableList<? extends ImmutableAnnotation> annotations;
54    @Nullable public final ImmutableMethodImplementation methodImplementation;
55
56    public ImmutableMethod(@Nonnull String containingClass,
57                           @Nonnull String name,
58                           @Nullable Collection<? extends MethodParameter> parameters,
59                           @Nonnull String returnType,
60                           int accessFlags,
61                           @Nullable Collection<? extends Annotation> annotations,
62                           @Nullable MethodImplementation methodImplementation) {
63        this.containingClass = containingClass;
64        this.name = name;
65        this.parameters = ImmutableMethodParameter.immutableListOf(parameters);
66        this.returnType = returnType;
67        this.accessFlags = accessFlags;
68        this.annotations = ImmutableAnnotation.immutableListOf(annotations);
69        this.methodImplementation = ImmutableMethodImplementation.of(methodImplementation);
70    }
71
72    public ImmutableMethod(@Nonnull String containingClass,
73                           @Nonnull String name,
74                           @Nullable ImmutableList<? extends ImmutableMethodParameter> parameters,
75                           @Nonnull String returnType,
76                           int accessFlags,
77                           @Nullable ImmutableList<? extends ImmutableAnnotation> annotations,
78                           @Nullable ImmutableMethodImplementation methodImplementation) {
79        this.containingClass = containingClass;
80        this.name = name;
81        this.parameters = ImmutableUtils.nullToEmptyList(parameters);
82        this.returnType = returnType;
83        this.accessFlags = accessFlags;
84        this.annotations = ImmutableUtils.nullToEmptyList(annotations);
85        this.methodImplementation = methodImplementation;
86    }
87
88    public static ImmutableMethod of(Method method) {
89        if (method instanceof ImmutableMethod) {
90            return (ImmutableMethod)method;
91        }
92        return new ImmutableMethod(
93                method.getContainingClass(),
94                method.getName(),
95                method.getParameters(),
96                method.getReturnType(),
97                method.getAccessFlags(),
98                method.getAnnotations(),
99                method.getImplementation());
100    }
101
102    @Nonnull public String getContainingClass() { return containingClass; }
103    @Nonnull public String getName() { return name; }
104    @Nonnull public ImmutableList<? extends ImmutableMethodParameter> getParameters() { return parameters; }
105    @Nonnull public String getReturnType() { return returnType; }
106    public int getAccessFlags() { return accessFlags; }
107    @Nonnull public ImmutableList<? extends ImmutableAnnotation> getAnnotations() { return annotations; }
108    @Nullable public ImmutableMethodImplementation getImplementation() { return methodImplementation; }
109
110    @Nonnull
111    public static ImmutableList<ImmutableMethod> immutableListOf(@Nullable Iterable<? extends Method> list) {
112        return CONVERTER.convert(list);
113    }
114
115    private static final ImmutableListConverter<ImmutableMethod, Method> CONVERTER =
116            new ImmutableListConverter<ImmutableMethod, Method>() {
117                @Override
118                protected boolean isImmutable(@Nonnull Method item) {
119                    return item instanceof ImmutableMethod;
120                }
121
122                @Nonnull
123                @Override
124                protected ImmutableMethod makeImmutable(@Nonnull Method item) {
125                    return ImmutableMethod.of(item);
126                }
127            };
128}
129