/** * Get simple unqualified type name. * * @param typ Type Reference. * @return String Unqualified type name. */ private static String getSimpleTypeName(TypeReference typ) { final Map<String, String> mapFullTypeName = ImmutableMap.<String, String>builder() .put("B", "byte") .put("C", "char") .put("D", "double") .put("F", "float") .put("I", "int") .put("J", "long") .put("S", "short") .put("Z", "boolean") .build(); if (typ.isArrayType()) return "Array"; String typName = typ.getName().toString(); if (typName.startsWith("L")) { typName = typName.split("<")[0].substring(1); // handle generics typName = typName.substring(typName.lastIndexOf('/') + 1); // get unqualified name typName = typName.substring(typName.lastIndexOf('$') + 1); // handle inner classes } else { typName = mapFullTypeName.get(typName); } return typName; } }
/** * Return the innermost element type reference for an array */ public final TypeName getInnermostElementType() { short newDim = ((key.dim&ElementMask) == PrimitiveMask) ? (short) -1 : 0; return findOrCreate(key.packageName, key.className, newDim); }
/** * Find or create the canonical {@link TypeReference} instance for the given pair. * * @param cl the classloader (defining/initiating depending on usage) * @param typeName something like "Ljava/util/Arrays" */ public static synchronized TypeReference findOrCreate(ClassLoaderReference cl, String typeName) { return findOrCreate(cl, TypeName.string2TypeName(typeName)); }
/** * @param T a type reference * @return a synthetic class name to represent the synthetic form of this type * @throws IllegalArgumentException if T is null */ public static TypeName getName(TypeReference T) { if (T == null) { throw new IllegalArgumentException("T is null"); } String s = "L$" + T.getName().toString().substring(1); return TypeName.string2TypeName(s); }
@Override public String toString() { StringBuilder sb = new StringBuilder("<src-class: " ); sb.append(getName().toString()); if (enclosingClass != null) { sb.append(" (within ").append(enclosingClass.getName()).append(')'); } if (annotations != null && !annotations.isEmpty()) { for(Annotation a : annotations) { sb.append('[').append(a.getType().getName().getClassName()).append(']'); } } return sb.toString(); } }
/** * Get the first paramNo of a given type. * * @see com.ibm.wala.util.ssa.ParameterAccessor */ private static int firstOf(TypeName type, IMethod inCallTo) { for (int i = 0; i < inCallTo.getNumberOfParameters(); ++i) { if (inCallTo.getParameterType(i).getName().equals(type)) { return i; } } throw new IllegalArgumentException(type.toString() + " is not a parameter to " + inCallTo); }
@Override public boolean equals(Object o) { if (o == null) { return false; } else if (o instanceof TypeKey) { TypeKey other = (TypeKey) o; return this.type.equals(other.type); } else if (o instanceof WeaklyNamedKey) { WeaklyNamedKey other = (WeaklyNamedKey) o; return this.type.equals(other.type); } else { return false; } } @Override
@Override public String toString() { StringBuilder sb = new StringBuilder("Annotation type " + type); if (unnamedArguments != null) { sb.append('['); for (Pair<TypeReference, Object> arg : unnamedArguments) { sb.append(' ').append(arg.fst.getName().getClassName()).append(':').append(arg.snd); } sb.append(" ]"); } if (!namedArguments.isEmpty()) { sb.append(' ').append(new TreeMap<>(namedArguments)); } return sb.toString(); }
private void addDependencies(TypeReference tr) { String packageName = 'L' +tr.getName().getPackage().toString()+ '/'; String outerClassName; String innerClassName = tr.getName().getClassName().toString(); LinkedList<TypeReference> trLL = new LinkedList<> (); trLL.push(tr); int index = innerClassName.lastIndexOf('$'); while (index != -1) { outerClassName = innerClassName.substring(0, index); TypeReference innerTR = TypeReference.findOrCreate(ClassLoaderReference.Application, packageName+outerClassName); trLL.push(innerTR); if (!typeToID.containsKey(innerTR)) { typeToID.put(innerTR, nextLocal++); aClassToTR.put(innerTR, tr); } innerClassName = outerClassName; index = outerClassName.lastIndexOf('$'); } icDependencies.put(tr, trLL); }
/** * @return true iff we can treat m as a no-op method */ protected boolean canIgnore(MemberReference m) { TypeReference T = m.getDeclaringClass(); TypeName n = T.getName(); Atom p = n.getPackage(); return (ignoredPackages.contains(p)); }
/** * Find or create the canonical {@link TypeReference} instance for the given pair. * * @param cl the classloader (defining/initiating depending on usage) * @param typeName something like "Ljava/util/Arrays" */ public static synchronized TypeReference findOrCreate(ClassLoaderReference cl, String typeName) { return findOrCreate(cl, TypeName.string2TypeName(typeName)); }
@Override public String toString() { StringBuilder sb = new StringBuilder("<src-class: " ); sb.append(getName().toString()); if (enclosingClass != null) { sb.append(" (within ").append(enclosingClass.getName()).append(')'); } if (annotations != null && !annotations.isEmpty()) { for(Annotation a : annotations) { sb.append('[').append(a.getType().getName().getClassName()).append(']'); } } return sb.toString(); } }
/** * Get the first paramNo of a given type. * * @see com.ibm.wala.util.ssa.ParameterAccessor */ private static int firstOf(TypeName type, IMethod inCallTo) { for (int i = 0; i < inCallTo.getNumberOfParameters(); ++i) { if (inCallTo.getParameterType(i).getName().equals(type)) { return i; } } throw new IllegalArgumentException(type.toString() + " is not a parameter to " + inCallTo); }
@Override public boolean equals(Object o) { if (o == null) { return false; } else if (o instanceof TypeKey) { TypeKey other = (TypeKey) o; return this.type.equals(other.type); } else if (o instanceof WeaklyNamedKey) { WeaklyNamedKey other = (WeaklyNamedKey) o; return this.type.equals(other.type); } else { return false; } } @Override
@Override public String toString() { StringBuilder sb = new StringBuilder("Annotation type " + type); if (unnamedArguments != null) { sb.append('['); for (Pair<TypeReference, Object> arg : unnamedArguments) { sb.append(' ').append(arg.fst.getName().getClassName()).append(':').append(arg.snd); } sb.append(" ]"); } if (!namedArguments.isEmpty()) { sb.append(' ').append(new TreeMap<>(namedArguments)); } return sb.toString(); }
@SuppressWarnings("unused") private static String typeRefToStr(TypeReference fieldType) throws UTFDataFormatException { Atom className = fieldType.getName().getClassName(); Atom pkgName = fieldType.getName().getPackage(); if ( null == pkgName && null != className ) { return className.toUnicodeString(); } if (null == className ) { } return pkgName.toUnicodeString() + '/' + className.toUnicodeString(); }
/** * @return true iff we can treat m as a no-op method */ protected boolean canIgnore(MemberReference m) { TypeReference T = m.getDeclaringClass(); TypeName n = T.getName(); Atom p = n.getPackage(); return (ignoredPackages.contains(p)); }
/** * Get astubx style method signature. {FullyQualifiedEnclosingType}: {UnqualifiedMethodReturnType} * {methodName} ([{UnqualifiedArgumentType}*]) * * @param mtd Method reference. * @return String Method signature. */ // TODO: handle generics and inner classes private static String getSignature(IMethod mtd) { String classType = mtd.getDeclaringClass().getName().toString().replaceAll("/", "\\.").substring(1); classType = classType.replaceAll("\\$", "\\."); // handle inner class String returnType = mtd.isInit() ? null : getSimpleTypeName(mtd.getReturnType()); String strArgTypes = ""; int argi = mtd.isStatic() ? 0 : 1; // Skip 'this' parameter for (; argi < mtd.getNumberOfParameters(); argi++) { strArgTypes += getSimpleTypeName(mtd.getParameterType(argi)); if (argi < mtd.getNumberOfParameters() - 1) strArgTypes += ", "; } return classType + ":" + (returnType == null ? "void " : returnType + " ") + mtd.getName().toString() + "(" + strArgTypes + ")"; } /**
public JavaClass(String typeName, Collection<TypeName> superTypeNames, CAstSourcePositionMap.Position position, Collection<CAstQualifier> qualifiers, JavaSourceLoaderImpl loader, IClass enclosingClass, Collection<Annotation> annotations) { super(position, TypeName.string2TypeName(typeName), loader, (short) mapToInt(qualifiers), new HashMap<Atom, IField>(), new HashMap<Selector, IMethod>()); this.superTypeNames = superTypeNames; this.enclosingClass = enclosingClass; this.annotations = annotations; }
@Override public boolean equals(Object o) { if (o == null) { return false; } else if (o instanceof NamedKey) { NamedKey other = (NamedKey) o; return (this.type.equals(other.type) && this.name.equals(other.name)); } else { return false; } } @Override