a little immature generics support

git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@561 f5eafffb-2e1d-0410-98e4-8ec43c5233c4
This commit is contained in:
sjfink 2007-01-11 17:51:47 +00:00
parent b85c5fc3a5
commit d33489f2a8
4 changed files with 434 additions and 0 deletions

View File

@ -0,0 +1,128 @@
/*******************************************************************************
* Copyright (c) 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.wala.types.generics;
import java.util.ArrayList;
import java.util.Iterator;
import com.ibm.wala.types.TypeReference;
import com.ibm.wala.util.debug.Assertions;
/**
* UNDER CONSTRUCTION
*
* @author sjfink
*
*/
public class ClassTypeSignature extends TypeSignature {
ClassTypeSignature(String s) {
super(s);
}
public static ClassTypeSignature makeClassTypeSig(String s) {
return new ClassTypeSignature(s);
}
public TypeArgument[] getTypeArguments() {
String argString = rawString().replaceAll(".*<", "<").replaceAll(">.*", ">");
String[] args = parseForTypeArguments(argString);
TypeArgument[] result = new TypeArgument[args.length];
for (int i = 0; i < args.length; i++) {
result[i] = TypeArgument.make(args[i]);
}
return result;
}
static String[] parseForTypeArguments(String argsString) {
ArrayList<String> args = new ArrayList<String>(10);
int i = 1;
while (true) {
switch (argsString.charAt(i++)) {
case '*':
args.add(String.valueOf('*'));
continue;
case TypeReference.VoidTypeCode:
args.add(TypeReference.VoidName.toString());
continue;
case TypeReference.BooleanTypeCode:
args.add(TypeReference.BooleanName.toString());
continue;
case TypeReference.ByteTypeCode:
args.add(TypeReference.ByteName.toString());
continue;
case TypeReference.ShortTypeCode:
args.add(TypeReference.ShortName.toString());
continue;
case TypeReference.IntTypeCode:
args.add(TypeReference.IntName.toString());
continue;
case TypeReference.LongTypeCode:
args.add(TypeReference.LongName.toString());
continue;
case TypeReference.FloatTypeCode:
args.add(TypeReference.FloatName.toString());
continue;
case TypeReference.DoubleTypeCode:
args.add(TypeReference.DoubleName.toString());
continue;
case TypeReference.CharTypeCode:
args.add(TypeReference.CharName.toString());
continue;
case TypeReference.ClassTypeCode: {
int off = i - 1;
while (argsString.charAt(i++) != ';')
;
args.add(argsString.substring(off, i - off));
continue;
}
case TypeReference.ArrayTypeCode: {
int off = i - 1;
while (argsString.charAt(i) == TypeReference.ArrayTypeCode) {
++i;
}
if (argsString.charAt(i++) == TypeReference.ClassTypeCode) {
while (argsString.charAt(i++) != ';')
;
args.add(argsString.substring(off, i - off - 1));
} else {
args.add(argsString.substring(off, i - off));
}
continue;
}
case (byte) '>': // end of parameter list
int size = args.size();
if (size == 0) {
return null;
}
Iterator<String> it = args.iterator();
String[] result = new String[size];
for (int j = 0; j < size; j++) {
result[j] = it.next();
}
return result;
default:
if (Assertions.verifyAssertions) {
Assertions._assert(false, "bad type argument list " + argsString + " " + argsString.charAt(i-1));
}
}
}
}
@Override
public boolean isTypeVariable() {
// TODO Auto-generated method stub
return false;
}
}

View File

@ -0,0 +1,152 @@
/*******************************************************************************
* Copyright (c) 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.wala.types.generics;
import java.util.ArrayList;
import java.util.Iterator;
import com.ibm.wala.types.TypeReference;
import com.ibm.wala.util.debug.Assertions;
/**
* UNDER CONSTRUCTION
*
* @author sjfink
*
*/
public class MethodTypeSignature {
private final String s;
public MethodTypeSignature(String s) {
this.s = s;
}
public static MethodTypeSignature make(String genericsSignature) {
return new MethodTypeSignature(genericsSignature);
}
public TypeSignature[] getArguments() {
String typeSig = s.replace("*(","(").replace(")*", ")");
String[] args = parseForTypeSignatures(typeSig);
TypeSignature[] result = new TypeSignature[args.length];
for (int i = 0; i<args.length; i++) {
result[i] = TypeSignature.make(args[i]);
}
return result;
}
static String[] parseForTypeSignatures(String typeSigs) {
ArrayList<String> sigs = new ArrayList<String>(10);
int i = 1;
while (true) {
switch (typeSigs.charAt(i++)) {
case TypeReference.VoidTypeCode:
sigs.add(TypeReference.VoidName.toString());
continue;
case TypeReference.BooleanTypeCode:
sigs.add(TypeReference.BooleanName.toString());
continue;
case TypeReference.ByteTypeCode:
sigs.add(TypeReference.ByteName.toString());
continue;
case TypeReference.ShortTypeCode:
sigs.add(TypeReference.ShortName.toString());
continue;
case TypeReference.IntTypeCode:
sigs.add(TypeReference.IntName.toString());
continue;
case TypeReference.LongTypeCode:
sigs.add(TypeReference.LongName.toString());
continue;
case TypeReference.FloatTypeCode:
sigs.add(TypeReference.FloatName.toString());
continue;
case TypeReference.DoubleTypeCode:
sigs.add(TypeReference.DoubleName.toString());
continue;
case TypeReference.CharTypeCode:
sigs.add(TypeReference.CharName.toString());
continue;
case TypeReference.ClassTypeCode: {
int off = i - 1;
while (typeSigs.charAt(i++) != ';')
;
sigs.add(typeSigs.substring(off, i - off));
continue;
}
case TypeReference.ArrayTypeCode: {
int off = i - 1;
while (typeSigs.charAt(i) == TypeReference.ArrayTypeCode) {
++i;
}
if (typeSigs.charAt(i++) == TypeReference.ClassTypeCode) {
while (typeSigs.charAt(i++) != ';')
;
sigs.add(typeSigs.substring(off, i - off - 1));
} else {
sigs.add(typeSigs.substring(off, i - off));
}
continue;
}
case (byte) ')': // end of parameter list
int size = sigs.size();
if (size == 0) {
return null;
}
Iterator<String> it = sigs.iterator();
String[] result = new String[size];
for (int j = 0; j < size; j++) {
result[j] = it.next();
}
return result;
default:
if (Assertions.verifyAssertions) {
Assertions._assert(false, "bad type signature list " + typeSigs);
}
}
}
}
@Override
public String toString() {
return s;
}
@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((s == null) ? 0 : s.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final MethodTypeSignature other = (MethodTypeSignature) obj;
if (s == null) {
if (other.s != null)
return false;
} else if (!s.equals(other.s))
return false;
return true;
}
}

View File

@ -0,0 +1,78 @@
/*******************************************************************************
* Copyright (c) 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.wala.types.generics;
import com.ibm.wala.util.debug.Assertions;
/**
* UNDER CONSTRUCTION
*
* @author sjfink
*
*/
public class TypeArgument {
private final static TypeArgument WILDCARD = new TypeArgument("*") {
public boolean isWildcard() {
return true;
}
};
private final String s;
TypeArgument(String s) {
this.s = s;
}
public boolean isWildcard() {
return false;
}
@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((s == null) ? 0 : s.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final TypeArgument other = (TypeArgument) obj;
if (s == null) {
if (other.s != null)
return false;
} else if (!s.equals(other.s))
return false;
return true;
}
@Override
public String toString() {
return s;
}
public static TypeArgument make(String string) {
if (string.equals("*")) {
return WILDCARD;
} else {
Assertions.UNREACHABLE();
return null;
}
}
}

View File

@ -0,0 +1,76 @@
/*******************************************************************************
* Copyright (c) 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.wala.types.generics;
import com.ibm.wala.util.debug.Assertions;
/**
* UNDER CONSTRUCTION
*
* @author sjfink
*
*/
public abstract class TypeSignature {
private final String s;
TypeSignature(String s) {
this.s = s;
}
protected String rawString() {
return s;
}
public static TypeSignature make(String s) {
if (s.charAt(0) == 'L') {
return ClassTypeSignature.makeClassTypeSig(s);
} else {
Assertions.UNREACHABLE();
return null;
}
}
public abstract TypeArgument[] getTypeArguments();
@Override
public String toString() {
return s;
}
@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((s == null) ? 0 : s.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final TypeSignature other = (TypeSignature) obj;
if (s == null) {
if (other.s != null)
return false;
} else if (!s.equals(other.s))
return false;
return true;
}
public abstract boolean isTypeVariable();
}