ImmutableMethodParameter.java revision 8f14d3641c3df5b8d0c8b5b419884909be5a1421
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.BaseMethodParameter; 36import org.jf.dexlib2.iface.Annotation; 37import org.jf.dexlib2.iface.MethodParameter; 38import org.jf.util.ImmutableListConverter; 39import org.jf.util.ImmutableListUtils; 40 41import javax.annotation.Nonnull; 42import javax.annotation.Nullable; 43import java.util.Collection; 44import java.util.List; 45 46public class ImmutableMethodParameter extends BaseMethodParameter { 47 @Nonnull public final String type; 48 @Nonnull public final ImmutableList<? extends ImmutableAnnotation> annotations; 49 @Nullable public final String name; 50 51 public ImmutableMethodParameter(@Nonnull String type, 52 @Nullable Collection<? extends Annotation> annotations, 53 @Nullable String name) { 54 this.type = type; 55 this.annotations = ImmutableAnnotation.immutableListOf(annotations); 56 this.name = name; 57 } 58 59 public ImmutableMethodParameter(@Nonnull String type, 60 @Nullable ImmutableList<? extends ImmutableAnnotation> annotations, 61 @Nullable String name) { 62 this.type = type; 63 this.annotations = ImmutableListUtils.nullToEmptyList(annotations); 64 this.name = name; 65 } 66 67 public static ImmutableMethodParameter of(MethodParameter methodParameter) { 68 if (methodParameter instanceof ImmutableMethodParameter) { 69 return (ImmutableMethodParameter)methodParameter; 70 } 71 return new ImmutableMethodParameter( 72 methodParameter.getType(), 73 methodParameter.getAnnotations(), 74 methodParameter.getName()); 75 } 76 77 @Nonnull @Override public String getType() { return type; } 78 @Nonnull @Override public List<? extends Annotation> getAnnotations() { return annotations; } 79 @Nullable @Override public String getName() { return name; } 80 81 //TODO: iterate over the annotations to get the signature 82 @Nullable @Override public String getSignature() { return null; } 83 84 @Nonnull 85 public static ImmutableList<ImmutableMethodParameter> immutableListOf( 86 @Nullable Iterable<? extends MethodParameter> list) { 87 return CONVERTER.convert(list); 88 } 89 90 private static final ImmutableListConverter<ImmutableMethodParameter, MethodParameter> CONVERTER = 91 new ImmutableListConverter<ImmutableMethodParameter, MethodParameter>() { 92 @Override 93 protected boolean isImmutable(MethodParameter item) { 94 return item instanceof ImmutableMethodParameter; 95 } 96 97 @Override 98 protected ImmutableMethodParameter makeImmutable(MethodParameter item) { 99 return ImmutableMethodParameter.of(item); 100 } 101 }; 102} 103