Home | History | Annotate | Download | only in immutable
      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 
     32 package org.jf.dexlib2.immutable;
     33 
     34 import com.google.common.collect.ImmutableList;
     35 import com.google.common.collect.ImmutableSet;
     36 import org.jf.dexlib2.base.BaseMethodParameter;
     37 import org.jf.dexlib2.iface.Annotation;
     38 import org.jf.dexlib2.iface.MethodParameter;
     39 import org.jf.util.ImmutableConverter;
     40 import org.jf.util.ImmutableUtils;
     41 
     42 import javax.annotation.Nonnull;
     43 import javax.annotation.Nullable;
     44 import java.util.Set;
     45 
     46 public class ImmutableMethodParameter extends BaseMethodParameter {
     47     @Nonnull protected final String type;
     48     @Nonnull protected final ImmutableSet<? extends ImmutableAnnotation> annotations;
     49     @Nullable protected final String name;
     50 
     51     public ImmutableMethodParameter(@Nonnull String type,
     52                                     @Nullable Set<? extends Annotation> annotations,
     53                                     @Nullable String name) {
     54         this.type = type;
     55         this.annotations = ImmutableAnnotation.immutableSetOf(annotations);
     56         this.name = name;
     57     }
     58 
     59     public ImmutableMethodParameter(@Nonnull String type,
     60                                     @Nullable ImmutableSet<? extends ImmutableAnnotation> annotations,
     61                                     @Nullable String name) {
     62         this.type = type;
     63         this.annotations = ImmutableUtils.nullToEmptySet(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 Set<? 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.toList(list);
     88     }
     89 
     90     private static final ImmutableConverter<ImmutableMethodParameter, MethodParameter> CONVERTER =
     91             new ImmutableConverter<ImmutableMethodParameter, MethodParameter>() {
     92                 @Override
     93                 protected boolean isImmutable(@Nonnull MethodParameter item) {
     94                     return item instanceof ImmutableMethodParameter;
     95                 }
     96 
     97                 @Nonnull
     98                 @Override
     99                 protected ImmutableMethodParameter makeImmutable(@Nonnull MethodParameter item) {
    100                     return ImmutableMethodParameter.of(item);
    101                 }
    102             };
    103 }
    104