private static void getAllMethods( ClassOrInterface c, TypeDeclaration t, List<MethodWithContainer> members) { for (Declaration d : t.getMembers()) { if (d instanceof Function) { Function m = (Function) d; boolean contains = false; for (MethodWithContainer member : members) { if (member.getMethod().getName().equals(m.getName())) { contains = true; break; } } if (!contains) { members.add(new MethodWithContainer(c, m)); } } } Type et = t.getExtendedType(); if (et != null) { getAllMethods(c, et.getDeclaration(), members); } for (Type st : t.getSatisfiedTypes()) { getAllMethods(c, st.getDeclaration(), members); } }
static String asIntersectionTypeString(List<Type> types) { StringBuffer missingSatisfiedTypesText = new StringBuffer(); for (Type missingSatisfiedType : types) { if (missingSatisfiedTypesText.length() != 0) { missingSatisfiedTypesText.append(" & "); } missingSatisfiedTypesText.append(missingSatisfiedType.asString()); } return missingSatisfiedTypesText.toString(); }
public static boolean isInBounds(List<Type> upperBounds, Type t) { boolean ok = true; for (Type ub : upperBounds) { if (!t.isSubtypeOf(ub) && !(ub.involvesTypeParameters() && t.getDeclaration().inherits(ub.getDeclaration()))) { ok = false; break; } } return ok; }
public static Declaration getRefinedDeclaration(Declaration declaration) { // Reproduces the algorithm used to build the type hierarchy // first walk up the superclass hierarchy if (declaration.isClassOrInterfaceMember() && declaration.isShared()) { TypeDeclaration dec = (TypeDeclaration) declaration.getContainer(); List<Type> signature = getSignature(declaration); Declaration refined = declaration.getRefinedDeclaration(); while (dec != null) { Type extended = dec.getExtendedType(); if (extended != null) { TypeDeclaration superDec = extended.getDeclaration(); Declaration superMemberDec = superDec.getDirectMember(declaration.getName(), signature, false); if (superMemberDec != null) { Declaration superRefined = superMemberDec.getRefinedDeclaration(); if (superRefined != null && refined != null && !isAbstraction(superMemberDec) && superRefined.equals(refined)) { return superMemberDec; } } dec = superDec; } else { dec = null; } } // now look at the very top of the hierarchy, even if it is an interface Declaration refinedDeclaration = refined; if (refinedDeclaration != null && !declaration.equals(refinedDeclaration)) { List<Declaration> directlyInheritedMembers = getInterveningRefinements( declaration.getName(), signature, refinedDeclaration, (TypeDeclaration) declaration.getContainer(), (TypeDeclaration) refinedDeclaration.getContainer()); directlyInheritedMembers.remove(refinedDeclaration); // TODO: do something for the case of // multiple intervening interfaces? if (directlyInheritedMembers.size() == 1) { // exactly one intervening interface return directlyInheritedMembers.get(0); } else { // no intervening interfaces return refinedDeclaration; } } } return null; }
static String anonFunctionHeader(Type requiredType, Unit unit) { StringBuilder text = new StringBuilder(); text.append("("); boolean first = true; char c = 'a'; List<Type> argTypes = unit.getCallableArgumentTypes(requiredType); for (Type paramType : argTypes) { if (first) { first = false; } else { text.append(", "); } text.append(paramType.asSourceCodeString(unit)).append(" ").append(c++); } text.append(")"); return text.toString(); }
@Override public void visit(Tree.TypedDeclaration that) { super.visit(that); Tree.Identifier id = that.getIdentifier(); if (id != null) { Type type = that.getType().getTypeModel(); if (type != null) { TypeDeclaration td = type.getDeclaration(); if ((td instanceof ClassOrInterface || td instanceof TypeParameter) && td.equals(declaration)) { String text = id.getText(); String name = declaration.getName(); if (text.equalsIgnoreCase(name) || text.endsWith(name)) { identifiers.add(id); } } } } }
static String defaultValue(Unit unit, Type t) { if (isTypeUnknown(t)) { return "nothing"; } if (unit.isOptionalType(t)) { return "null"; } if (t.isTypeAlias() || t.isClassOrInterface() && t.getDeclaration().isAlias()) { return defaultValue(unit, t.getExtendedType()); } if (t.isClass()) { TypeDeclaration c = t.getDeclaration(); if (c.equals(unit.getBooleanDeclaration())) { return "false"; } else if (c.equals(unit.getIntegerDeclaration())) { return "0"; } else if (c.equals(unit.getFloatDeclaration())) { return "0.0"; } else if (c.equals(unit.getStringDeclaration())) { return "\"\""; } else if (c.equals(unit.getByteDeclaration())) { return "0.byte"; } else if (c.equals(unit.getTupleDeclaration())) { final int minimumLength = unit.getTupleMinimumLength(t); final List<Type> tupleTypes = unit.getTupleElementTypes(t); final StringBuilder sb = new StringBuilder(); for (int i = 0; i < minimumLength; i++) { sb.append(sb.length() == 0 ? "[" : ", "); Type currentType = tupleTypes.get(i); if (unit.isSequentialType(currentType)) { currentType = unit.getSequentialElementType(currentType); } sb.append(defaultValue(unit, currentType)); } sb.append(']'); return sb.toString(); } else if (unit.isSequentialType(t)) { final StringBuilder sb = new StringBuilder(); sb.append('['); if (!unit.getEmptyType().isSubtypeOf(t)) { sb.append(defaultValue(unit, unit.getSequentialElementType(t))); } sb.append(']'); return sb.toString(); } else if (unit.isIterableType(t)) { final StringBuilder sb = new StringBuilder(); sb.append('{'); if (!unit.getEmptyType().isSubtypeOf(t)) { sb.append(defaultValue(unit, unit.getIteratedType(t))); } sb.append('}'); return sb.toString(); } else { return "nothing"; } } else { return "nothing"; } }
/** * See #547 - a generic actual method that has different names in its type parameters as the one * it's refining, can receive the type arguments of the parent instead. */ static void addParentMethodTypeParameters(final Function m, final GenerateJsVisitor gen) { List<TypeParameter> tps = m.getTypeParameters(); if (m.isActual() && tps != null && !tps.isEmpty()) { // This gives us the root declaration Function sm = (Function) m.getRefinedDeclaration(); for (int i = 0; i < tps.size(); i++) { boolean end = false; end |= copyMissingTypeParameters(m, sm, i, true, gen); // We still need to find intermediate declarations if (m.isClassOrInterfaceMember()) { final Set<Declaration> decs = new HashSet<Declaration>(); decs.add(sm); decs.add(m); // This gives us the containing type TypeDeclaration cont = ModelUtil.getContainingClassOrInterface(m); for (TypeDeclaration sup : cont.getSupertypeDeclarations()) { Declaration d = sup.getDirectMember(m.getName(), null, false); if (d instanceof Function && !decs.contains(d)) { decs.add(d); end |= copyMissingTypeParameters(m, (Function) d, i, false, gen); } } for (Type sup : cont.getSatisfiedTypes()) { Declaration d = sup.getDeclaration().getDirectMember(m.getName(), null, false); if (d instanceof Function && !decs.contains(d)) { decs.add(d); end |= copyMissingTypeParameters(m, (Function) d, i, false, gen); } } } if (end) { gen.endLine(true); } } } }