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