Replace BasicMethodParameter with TypeReference

This commit is contained in:
Ben Gruver 2012-11-10 13:30:07 -08:00
parent f81150ad43
commit b7b15efc03
12 changed files with 47 additions and 212 deletions

View File

@ -1,61 +0,0 @@
/*
* Copyright 2012, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jf.dexlib2.base.reference;
import org.jf.dexlib2.iface.reference.BasicMethodParameter;
import javax.annotation.Nonnull;
public abstract class BaseBasicMethodParameter implements BasicMethodParameter {
@Nonnull public abstract String getType();
@Override
public int hashCode() {
return hashCode(this);
}
@Override
public boolean equals(Object o) {
if (o != null && o instanceof BasicMethodParameter) {
return equals(this, (BasicMethodParameter)o);
}
return false;
}
public static int hashCode(@Nonnull BasicMethodParameter param) {
return param.getType().hashCode();
}
public static boolean equals(@Nonnull BasicMethodParameter param1, @Nonnull BasicMethodParameter param2) {
return param1.getType().equals(param2.getType());
}
}

View File

@ -31,8 +31,8 @@
package org.jf.dexlib2.base.reference;
import org.jf.dexlib2.iface.reference.BasicMethodParameter;
import org.jf.dexlib2.iface.reference.MethodReference;
import org.jf.dexlib2.iface.reference.TypeReference;
import javax.annotation.Nonnull;
import java.util.List;
@ -40,7 +40,7 @@ import java.util.List;
public abstract class BaseMethodReference implements MethodReference {
@Nonnull public abstract String getContainingClass();
@Nonnull public abstract String getName();
@Nonnull public abstract List<? extends BasicMethodParameter> getParameters();
@Nonnull public abstract List<? extends TypeReference> getParameters();
@Nonnull
public abstract String getReturnType();
@ -61,7 +61,7 @@ public abstract class BaseMethodReference implements MethodReference {
int hashCode = methodRef.getContainingClass().hashCode();
hashCode = hashCode*31 + methodRef.getName().hashCode();
hashCode = hashCode*31 + methodRef.getReturnType().hashCode();
for (BasicMethodParameter param: methodRef.getParameters()) {
for (TypeReference param: methodRef.getParameters()) {
hashCode = hashCode*31 + param.hashCode();
}
return hashCode;

View File

@ -35,7 +35,7 @@ import com.google.common.collect.ImmutableList;
import org.jf.dexlib2.base.reference.BaseMethodReference;
import org.jf.dexlib2.dexbacked.DexBuffer;
import org.jf.dexlib2.dexbacked.util.FixedSizeList;
import org.jf.dexlib2.iface.reference.BasicMethodParameter;
import org.jf.dexlib2.iface.reference.TypeReference;
import javax.annotation.Nonnull;
import java.util.List;
@ -64,17 +64,17 @@ public class DexBackedMethodReference extends BaseMethodReference {
@Nonnull
@Override
public List<? extends BasicMethodParameter> getParameters() {
public List<? extends TypeReference> getParameters() {
int protoIdItemOffset = getProtoIdItemOffset();
final int parametersOffset = dexBuf.readSmallUint(protoIdItemOffset + DexBuffer.PROTO_PARAM_LIST_OFF_OFFSET);
if (parametersOffset > 0) {
final int parameterCount = dexBuf.readSmallUint(parametersOffset + DexBuffer.TYPE_LIST_SIZE_OFFSET);
final int paramListStart = parametersOffset + DexBuffer.TYPE_LIST_LIST_OFFSET;
return new FixedSizeList<BasicMethodParameter>() {
return new FixedSizeList<TypeReference>() {
@Nonnull
@Override
public BasicMethodParameter readItem(final int index) {
return new BasicMethodParameter() {
public TypeReference readItem(final int index) {
return new TypeReference() {
@Nonnull
@Override
public String getType() {

View File

@ -32,13 +32,13 @@
package org.jf.dexlib2.iface;
import org.jf.dexlib2.iface.debug.LocalInfo;
import org.jf.dexlib2.iface.reference.BasicMethodParameter;
import org.jf.dexlib2.iface.reference.TypeReference;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
public interface MethodParameter extends BasicMethodParameter, LocalInfo {
public interface MethodParameter extends TypeReference, LocalInfo {
@Nonnull String getType();
@Nonnull List<? extends Annotation> getAnnotations();
@Nullable String getName();

View File

@ -1,38 +0,0 @@
/*
* Copyright 2012, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jf.dexlib2.iface.reference;
import javax.annotation.Nonnull;
public interface BasicMethodParameter {
@Nonnull String getType();
}

View File

@ -37,6 +37,6 @@ import java.util.List;
public interface MethodReference extends Reference {
@Nonnull String getContainingClass();
@Nonnull String getName();
@Nonnull List<? extends BasicMethodParameter> getParameters();
@Nonnull List<? extends TypeReference> getParameters();
@Nonnull String getReturnType();
}

View File

@ -32,7 +32,7 @@
package org.jf.dexlib2.immutable;
import com.google.common.collect.ImmutableList;
import org.jf.dexlib2.base.reference.BaseBasicMethodParameter;
import org.jf.dexlib2.base.reference.BaseTypeReference;
import org.jf.dexlib2.iface.Annotation;
import org.jf.dexlib2.iface.MethodParameter;
import org.jf.util.ImmutableListConverter;
@ -42,7 +42,7 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
public class ImmutableMethodParameter extends BaseBasicMethodParameter implements MethodParameter {
public class ImmutableMethodParameter extends BaseTypeReference implements MethodParameter {
@Nonnull public final String type;
@Nonnull public final ImmutableList<? extends ImmutableAnnotation> annotations;
@Nullable public final String name;

View File

@ -1,78 +0,0 @@
/*
* Copyright 2012, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jf.dexlib2.immutable.reference;
import com.google.common.collect.ImmutableList;
import org.jf.dexlib2.base.reference.BaseBasicMethodParameter;
import org.jf.dexlib2.iface.reference.BasicMethodParameter;
import org.jf.util.ImmutableListConverter;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
public class ImmutableBasicMethodParameter extends BaseBasicMethodParameter {
@Nonnull public final String type;
public ImmutableBasicMethodParameter(@Nonnull String type) {
this.type = type;
}
@Nonnull
public static ImmutableBasicMethodParameter of(@Nonnull BasicMethodParameter param) {
if (param instanceof ImmutableBasicMethodParameter) {
return (ImmutableBasicMethodParameter)param;
}
return new ImmutableBasicMethodParameter(param.getType());
}
@Nonnull @Override public String getType() { return type; }
@Nonnull
public static ImmutableList<ImmutableBasicMethodParameter> immutableListOf(
@Nullable List<? extends BasicMethodParameter> list) {
return CONVERTER.convert(list);
}
private static final ImmutableListConverter<ImmutableBasicMethodParameter, BasicMethodParameter> CONVERTER =
new ImmutableListConverter<ImmutableBasicMethodParameter, BasicMethodParameter>() {
@Override
protected boolean isImmutable(BasicMethodParameter item) {
return item instanceof ImmutableBasicMethodParameter;
}
@Override
protected ImmutableBasicMethodParameter makeImmutable(BasicMethodParameter item) {
return ImmutableBasicMethodParameter.of(item);
}
};
}

View File

@ -33,8 +33,8 @@ package org.jf.dexlib2.immutable.reference;
import com.google.common.collect.ImmutableList;
import org.jf.dexlib2.base.reference.BaseMethodReference;
import org.jf.dexlib2.iface.reference.BasicMethodParameter;
import org.jf.dexlib2.iface.reference.MethodReference;
import org.jf.dexlib2.iface.reference.TypeReference;
import org.jf.util.ImmutableListUtils;
import javax.annotation.Nonnull;
@ -44,22 +44,22 @@ import java.util.List;
public class ImmutableMethodReference extends BaseMethodReference implements ImmutableReference {
@Nonnull public final String containingClass;
@Nonnull public final String name;
@Nonnull public final ImmutableList<? extends ImmutableBasicMethodParameter> parameters;
@Nonnull public final ImmutableList<? extends ImmutableTypeReference> parameters;
@Nonnull public final String returnType;
public ImmutableMethodReference(@Nonnull String containingClass,
@Nonnull String name,
@Nullable List<? extends BasicMethodParameter> parameters,
@Nullable List<? extends TypeReference> parameters,
@Nonnull String returnType) {
this.containingClass = containingClass;
this.name = name;
this.parameters = ImmutableBasicMethodParameter.immutableListOf(parameters);
this.parameters = ImmutableTypeReference.immutableListOf(parameters);
this.returnType = returnType;
}
public ImmutableMethodReference(@Nonnull String containingClass,
@Nonnull String name,
@Nullable ImmutableList<? extends ImmutableBasicMethodParameter> parameters,
@Nullable ImmutableList<? extends ImmutableTypeReference> parameters,
@Nonnull String returnType) {
this.containingClass = containingClass;
this.name = name;
@ -81,6 +81,6 @@ public class ImmutableMethodReference extends BaseMethodReference implements Imm
@Nonnull @Override public String getContainingClass() { return containingClass; }
@Nonnull @Override public String getName() { return name; }
@Nonnull @Override public List<? extends BasicMethodParameter> getParameters() { return parameters; }
@Nonnull @Override public List<? extends ImmutableTypeReference> getParameters() { return parameters; }
@Nonnull @Override public String getReturnType() { return returnType; }
}

View File

@ -31,10 +31,14 @@
package org.jf.dexlib2.immutable.reference;
import com.google.common.collect.ImmutableList;
import org.jf.dexlib2.base.reference.BaseTypeReference;
import org.jf.dexlib2.iface.reference.TypeReference;
import org.jf.util.ImmutableListConverter;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
public class ImmutableTypeReference extends BaseTypeReference implements ImmutableReference {
@Nonnull public final String type;
@ -52,4 +56,22 @@ public class ImmutableTypeReference extends BaseTypeReference implements Immutab
}
@Nonnull @Override public String getType() { return type; }
@Nonnull
public static ImmutableList<ImmutableTypeReference> immutableListOf(@Nullable List<? extends TypeReference> list) {
return CONVERTER.convert(list);
}
private static final ImmutableListConverter<ImmutableTypeReference, TypeReference> CONVERTER =
new ImmutableListConverter<ImmutableTypeReference, TypeReference>() {
@Override
protected boolean isImmutable(TypeReference item) {
return item instanceof ImmutableTypeReference;
}
@Override
protected ImmutableTypeReference makeImmutable(TypeReference item) {
return ImmutableTypeReference.of(item);
}
};
}

View File

@ -31,9 +31,9 @@
package org.jf.dexlib2.util;
import org.jf.dexlib2.iface.reference.BasicMethodParameter;
import org.jf.dexlib2.iface.reference.FieldReference;
import org.jf.dexlib2.iface.reference.MethodReference;
import org.jf.dexlib2.iface.reference.TypeReference;
import java.io.IOException;
import java.io.Writer;
@ -46,7 +46,7 @@ public abstract class ReferenceUtil {
sb.append("->");
sb.append(methodReference.getName());
sb.append('(');
for (BasicMethodParameter param: methodReference.getParameters()) {
for (TypeReference param: methodReference.getParameters()) {
sb.append(param.getType());
}
sb.append(')');
@ -59,7 +59,7 @@ public abstract class ReferenceUtil {
writer.write("->");
writer.write(methodReference.getName());
writer.write('(');
for (BasicMethodParameter param: methodReference.getParameters()) {
for (TypeReference param: methodReference.getParameters()) {
writer.write(param.getType());
}
writer.write(')');

View File

@ -143,18 +143,8 @@ public class SyntheticAccessorResolver {
private static boolean methodReferenceEquals(@Nonnull MethodReference ref1, @Nonnull MethodReference ref2) {
// we already know the containing class matches
if ((!ref1.getName().equals(ref2.getName())) ||
(!ref1.getReturnType().equals(ref2.getReturnType()))) {
return false;
}
Iterator<? extends BasicMethodParameter> params1 = ref1.getParameters().iterator();
Iterator<? extends BasicMethodParameter> params2 = ref2.getParameters().iterator();
while (params1.hasNext() && params2.hasNext()) {
if (!params1.next().getType().equals(params2.next().getType())) {
return false;
}
}
return (!params1.hasNext()) && (!params2.hasNext());
return ref1.getName().equals(ref2.getName()) &&
ref1.getReturnType().equals(ref2.getReturnType()) &&
ref1.getParameters().equals(ref2.getParameters());
}
}