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.ImmutableSet;
     35 import com.google.common.collect.ImmutableSortedSet;
     36 import com.google.common.collect.Ordering;
     37 import org.jf.dexlib2.base.reference.BaseFieldReference;
     38 import org.jf.dexlib2.iface.Annotation;
     39 import org.jf.dexlib2.iface.Field;
     40 import org.jf.dexlib2.iface.value.EncodedValue;
     41 import org.jf.dexlib2.immutable.value.ImmutableEncodedValue;
     42 import org.jf.dexlib2.immutable.value.ImmutableEncodedValueFactory;
     43 import org.jf.util.ImmutableConverter;
     44 import org.jf.util.ImmutableUtils;
     45 
     46 import javax.annotation.Nonnull;
     47 import javax.annotation.Nullable;
     48 import java.util.Collection;
     49 
     50 public class ImmutableField extends BaseFieldReference implements Field {
     51     @Nonnull protected final String definingClass;
     52     @Nonnull protected final String name;
     53     @Nonnull protected final String type;
     54     protected final int accessFlags;
     55     @Nullable protected final ImmutableEncodedValue initialValue;
     56     @Nonnull protected final ImmutableSet<? extends ImmutableAnnotation> annotations;
     57 
     58     public ImmutableField(@Nonnull String definingClass,
     59                           @Nonnull String name,
     60                           @Nonnull String type,
     61                           int accessFlags,
     62                           @Nullable EncodedValue initialValue,
     63                           @Nullable Collection<? extends Annotation> annotations) {
     64         this.definingClass = definingClass;
     65         this.name = name;
     66         this.type = type;
     67         this.accessFlags = accessFlags;
     68         this.initialValue = ImmutableEncodedValueFactory.ofNullable(initialValue);
     69         this.annotations = ImmutableAnnotation.immutableSetOf(annotations);
     70     }
     71 
     72     public ImmutableField(@Nonnull String definingClass,
     73                           @Nonnull String name,
     74                           @Nonnull String type,
     75                           int accessFlags,
     76                           @Nullable ImmutableEncodedValue initialValue,
     77                           @Nullable ImmutableSet<? extends ImmutableAnnotation> annotations) {
     78         this.definingClass = definingClass;
     79         this.name = name;
     80         this.type = type;
     81         this.accessFlags = accessFlags;
     82         this.initialValue = initialValue;
     83         this.annotations = ImmutableUtils.nullToEmptySet(annotations);
     84     }
     85 
     86     public static ImmutableField of(Field field) {
     87         if (field instanceof  ImmutableField) {
     88             return (ImmutableField)field;
     89         }
     90         return new ImmutableField(
     91                 field.getDefiningClass(),
     92                 field.getName(),
     93                 field.getType(),
     94                 field.getAccessFlags(),
     95                 field.getInitialValue(),
     96                 field.getAnnotations());
     97     }
     98 
     99     @Nonnull @Override public String getDefiningClass() { return definingClass; }
    100     @Nonnull @Override public String getName() { return name; }
    101     @Nonnull @Override public String getType() { return type; }
    102     @Override public int getAccessFlags() { return accessFlags; }
    103     @Override public EncodedValue getInitialValue() { return initialValue;}
    104     @Nonnull @Override public ImmutableSet<? extends ImmutableAnnotation> getAnnotations() { return annotations; }
    105 
    106     @Nonnull
    107     public static ImmutableSortedSet<ImmutableField> immutableSetOf(@Nullable Iterable<? extends Field> list) {
    108         return CONVERTER.toSortedSet(Ordering.natural(), list);
    109     }
    110 
    111     private static final ImmutableConverter<ImmutableField, Field> CONVERTER =
    112             new ImmutableConverter<ImmutableField, Field>() {
    113                 @Override
    114                 protected boolean isImmutable(@Nonnull Field item) {
    115                     return item instanceof ImmutableField;
    116                 }
    117 
    118                 @Nonnull
    119                 @Override
    120                 protected ImmutableField makeImmutable(@Nonnull Field item) {
    121                     return ImmutableField.of(item);
    122                 }
    123             };
    124 }
    125