public static int getArrayRank(final TypeReference t) { if (t == null) { return 0; } int rank = 0; TypeReference current = t; while (current.isArray()) { ++rank; current = current.getElementType(); } return rank; }
public static int getArrayRank(final TypeReference t) { if (t == null) { return 0; } int rank = 0; TypeReference current = t; while (current.isArray()) { ++rank; current = current.getElementType(); } return rank; }
public static int getArrayRank(final TypeReference t) { if (t == null) { return 0; } int rank = 0; TypeReference current = t; while (current.isArray()) { ++rank; current = current.getElementType(); } return rank; }
@Override public Void visitArrayType(final ArrayType t, final Map<TypeReference, TypeReference> map) { final TypeReference a = argumentType; if (a.isArray()) { argumentType = a.getElementType(); visit(t.getElementType(), map); } return null; }
public static TypeReference mergeTypes(TypeReference t1, TypeReference t2) { if (t1 == null || t2 == null) return null; if (t1 == BuiltinTypes.Null) return t2; if (t2 == BuiltinTypes.Null) return t1; if (t1.isEquivalentTo(t2)) return t1; if(t1.isArray() ^ t2.isArray()) return null; if(t1.isArray()) { TypeReference merged = mergeTypes(t1.getElementType(), t2.getElementType()); return merged == null ? null : merged.makeArrayType(); } List<TypeReference> chain1 = getBaseTypes(t1); List<TypeReference> chain2 = getBaseTypes(t2); for (int i = Math.min(chain1.size(), chain2.size()) - 1; i >= 0; i--) { if (chain1.get(i).equals(chain2.get(i))) return chain1.get(i); } return null; }
public static TypeReference getElementType(final TypeReference t) { if (t.isArray()) { return t.getElementType(); } if (t.isWildcardType()) { return getElementType(getUpperBound(t)); } return null; }
public static TypeReference getElementType(final TypeReference t) { if (t.isArray()) { return t.getElementType(); } if (t.isWildcardType()) { return getElementType(getUpperBound(t)); } return null; }
@Override public Void visitArrayType(final ArrayType t, final Map<TypeReference, TypeReference> map) { final TypeReference a = argumentType; if (a.isArray() && t.isArray()) { argumentType = a.getElementType(); visit(t.getElementType(), map); } return null; }
@Override public Void visitArrayType(final ArrayType t, final Map<TypeReference, TypeReference> map) { final TypeReference a = argumentType; if (a.isArray() && t.isArray()) { argumentType = a.getElementType(); visit(t.getElementType(), map); } return null; }
@Override public Void visitArrayType(final ArrayType t, final Map<TypeReference, TypeReference> map) { final TypeReference a = argumentType; if (a.isArray() && t.isArray()) { argumentType = a.getElementType(); visit(t.getElementType(), map); } return null; }
@Override public Void visitArrayType(final ArrayType t, final Map<TypeReference, TypeReference> map) { final TypeReference a = argumentType; if (a.isArray() && t.isArray()) { argumentType = a.getElementType(); visit(t.getElementType(), map); } return null; }
public static TypeReference getElementType(final TypeReference t) { if (t.isArray()) { return t.getElementType(); } if (t.isWildcardType()) { return getElementType(getUpperBound(t)); } return null; }
@Override public Void visitArrayType(final ArrayType t, final Map<TypeReference, TypeReference> map) { final TypeReference a = argumentType; if (a.isArray() && t.isArray()) { argumentType = a.getElementType(); visit(t.getElementType(), map); } return null; }
@Override public ResolveResult visitIndexerExpression(final IndexerExpression node, final Void data) { final ResolveResult childResult = node.getTarget().acceptVisitor(this, data); if (childResult == null || childResult.getType() == null || !childResult.getType().isArray()) { return null; } final TypeReference elementType = childResult.getType().getElementType(); if (elementType == null) { return null; } return new ResolveResult(elementType); }
@Override public ResolveResult visitIndexerExpression(final IndexerExpression node, final Void data) { final ResolveResult childResult = node.getTarget().acceptVisitor(this, data); if (childResult == null || childResult.getType() == null || !childResult.getType().isArray()) { return null; } final TypeReference elementType = childResult.getType().getElementType(); if (elementType == null) { return null; } return new ResolveResult(elementType); }
@Override public ResolveResult visitIndexerExpression(final IndexerExpression node, final Void data) { final ResolveResult childResult = node.getTarget().acceptVisitor(this, data); if (childResult == null || childResult.getType() == null || !childResult.getType().isArray()) { return null; } final TypeReference elementType = childResult.getType().getElementType(); if (elementType == null) { return null; } return new ResolveResult(elementType); }
@AstVisitor(nodes = AstNodes.EXPRESSIONS) public void visit(Expression expr, MethodContext mc) { if (expr.getCode() != AstCode.CheckCast) return; TypeReference targetType = (TypeReference) expr.getOperand(); if (!targetType.isArray() || Types.isObject(targetType.getElementType())) return; Expression arg = Exprs.getChild(expr, 0); if (arg.getCode() != AstCode.InvokeVirtual && arg.getCode() != AstCode.InvokeInterface) return; MethodReference mr = (MethodReference) arg.getOperand(); if (!mr.getName().equals("toArray") || !mr.getSignature().equals("()[Ljava/lang/Object;")) return; Expression target = Exprs.getChild(arg, 0); if (!Types.isInstance(target.getInferredType(), "java/util/Collection")) return; mc.report("ImpossibleToArrayDowncast", 0, target, Roles.TARGET_TYPE.create(targetType), TARGET_ELEMENT_TYPE.create(targetType.getElementType())); } }
static EType of(TypeReference tr, What what) { if (tr == null || tr.isPrimitive() || (what == What.SUBTYPE && Types.isObject(tr))) return UNKNOWN; TypeDefinition td = tr.resolve(); if (td == null) return UNKNOWN; if (td.isFinal() || td.isPrimitive()) { if (what == What.SUBTYPE) what = What.EXACT; if (what == What.NOT) what = What.NOT_SUBTYPE; } TypeReference newTr = td; while (tr.isArray()) { tr = tr.getElementType(); newTr = newTr.makeArrayType(); } boolean complete = Types.hasCompleteHierarchy(td); return new SingleType(newTr, what, complete); }
@ClassVisitor public void visitClass(TypeDefinition td, ClassContext cc) { isSerializable = Types.isInstance(td, "java/io/Serializable"); if(Types.isInstance(td, "java/util/Comparator") && !td.isAnonymous() && !td.isLocalClass() && !isSerializable) { int priority = 0; for(FieldDefinition fd : td.getDeclaredFields()) { TypeReference fieldType = fd.getFieldType(); while(fieldType.isArray()) fieldType = fieldType.getElementType(); if(fieldType.isPrimitive()) continue; if(Types.isInstance(fieldType, "java/io/Serializable")) { priority+=10; if(priority > 20) break; } } cc.report("ComparatorIsNotSerializable", priority, SHOULD_IMPLEMENT.create("java/io/Serializable")); } }
@FieldVisitor public void checkField(FieldDefinition fd, FieldStats fs, FieldContext fc) { if(fd.getFieldType().isArray() && Flags.testAny(fd.getFlags(), Flags.VOLATILE)) { int priority = 0; int flags = fs.getFlags(fd); if(!Flags.testAny(flags, FieldStats.UNRESOLVED)) { if(!Flags.testAny(flags, FieldStats.WRITE_NONNULL)) return; // will be reported as unwritten if(Flags.testAny(flags, FieldStats.WRITE_CLASS | FieldStats.WRITE_PACKAGE | FieldStats.WRITE_OUTSIDE)) { priority += 20; } } fc.report("VolatileArray", priority, Roles.REPLACEMENT_CLASS.create(getAtomicArrayReplacement(fd .getFieldType().getElementType().getSimpleType()))); } }