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