use of com.github.javaparser.ast.type.ReferenceType in project checker-framework by typetools.
the class StubParser method annotateAsArray.
/**
* Add the annotations from {@code type} to {@code atype}. Type annotations that parsed as
* declaration annotations (ie those in {@code declAnnos} are applied to the innermost component
* type.
*
* @param atype annotated type to which to add annotations
* @param type parsed type
* @param declAnnos annotations stored on the declaration of the variable with this type or null
*/
private void annotateAsArray(AnnotatedArrayType atype, ReferenceType type, NodeList<AnnotationExpr> declAnnos) {
annotateInnermostComponentType(atype, declAnnos);
Type typeDef = type;
AnnotatedTypeMirror currentAtype = atype;
while (typeDef.isArrayType() && currentAtype.getKind() == TypeKind.ARRAY) {
// handle generic type
clearAnnotations(currentAtype, typeDef);
List<AnnotationExpr> annotations = typeDef.getAnnotations();
if (annotations != null) {
annotate(currentAtype, annotations);
}
typeDef = ((com.github.javaparser.ast.type.ArrayType) typeDef).getComponentType();
currentAtype = ((AnnotatedArrayType) currentAtype).getComponentType();
if (typeDef.isArrayType() ^ currentAtype.getKind() == TypeKind.ARRAY) {
stubWarn("Mismatched array lengths; atype: " + atype + "%n type: " + type);
}
}
}
use of com.github.javaparser.ast.type.ReferenceType in project checker-framework by typetools.
the class AnnotationFileParser method annotateAsArray.
/**
* Add the annotations from {@code type} to {@code atype}. Type annotations that parsed as
* declaration annotations (i.e., type annotations in {@code declAnnos}) are applied to the
* innermost component type.
*
* @param atype annotated type to which to add annotations
* @param type parsed type
* @param declAnnos annotations stored on the declaration of the variable with this type or null
* @param astNode where to report errors
*/
private void annotateAsArray(AnnotatedArrayType atype, ReferenceType type, @Nullable NodeList<AnnotationExpr> declAnnos, NodeWithRange<?> astNode) {
annotateInnermostComponentType(atype, declAnnos, astNode);
Type typeDef = type;
AnnotatedTypeMirror currentAtype = atype;
while (typeDef.isArrayType()) {
if (currentAtype.getKind() != TypeKind.ARRAY) {
warn(astNode, "Mismatched array lengths; atype: " + atype + "%n type: " + type);
return;
}
// handle generic type
clearAnnotations(currentAtype, typeDef);
List<AnnotationExpr> annotations = typeDef.getAnnotations();
if (annotations != null) {
annotate(currentAtype, annotations, astNode);
}
typeDef = ((com.github.javaparser.ast.type.ArrayType) typeDef).getComponentType();
currentAtype = ((AnnotatedArrayType) currentAtype).getComponentType();
}
if (currentAtype.getKind() == TypeKind.ARRAY) {
warn(astNode, "Mismatched array lengths; atype: " + atype + "%n type: " + type);
}
}
use of com.github.javaparser.ast.type.ReferenceType in project javaparser by javaparser.
the class CallableDeclaration method appendThrowsIfRequested.
protected String appendThrowsIfRequested(boolean includingThrows) {
StringBuilder sb = new StringBuilder();
if (includingThrows) {
boolean firstThrow = true;
for (ReferenceType thr : getThrownExceptions()) {
if (firstThrow) {
firstThrow = false;
sb.append(" throws ");
} else {
sb.append(", ");
}
sb.append(thr.toString(prettyPrinterNoCommentsConfiguration));
}
}
return sb.toString();
}
use of com.github.javaparser.ast.type.ReferenceType in project checker-framework by typetools.
the class AnnotationFileParser method annotate.
/**
* Add to formal parameter {@code atype}:
*
* <ol>
* <li>the annotations from {@code typeDef}, and
* <li>any type annotations that parsed as declaration annotations (i.e., type annotations in
* {@code declAnnos}).
* </ol>
*
* @param atype annotated type to which to add annotations
* @param typeDef parsed type
* @param declAnnos annotations stored on the declaration of the variable with this type, or null
* @param astNode where to report errors
*/
private void annotate(AnnotatedTypeMirror atype, Type typeDef, @Nullable NodeList<AnnotationExpr> declAnnos, NodeWithRange<?> astNode) {
if (atype.getKind() == TypeKind.ARRAY) {
if (typeDef instanceof ReferenceType) {
annotateAsArray((AnnotatedArrayType) atype, (ReferenceType) typeDef, declAnnos, astNode);
} else {
warn(astNode, "expected ReferenceType but found: " + typeDef);
}
return;
}
clearAnnotations(atype, typeDef);
// Primary annotations for the type of a variable declaration are not stored in typeDef, but
// rather as declaration annotations (passed as declAnnos to this method). But, if typeDef
// is not the type of a variable, then the primary annotations are stored in typeDef.
NodeList<AnnotationExpr> primaryAnnotations;
if (typeDef.getAnnotations().isEmpty() && declAnnos != null) {
primaryAnnotations = declAnnos;
} else {
primaryAnnotations = typeDef.getAnnotations();
}
if (atype.getKind() != TypeKind.WILDCARD) {
// The primary annotation on a wildcard applies to the super or extends bound and
// are added below.
annotate(atype, primaryAnnotations, astNode);
}
switch(atype.getKind()) {
case DECLARED:
ClassOrInterfaceType declType = unwrapDeclaredType(typeDef);
if (declType == null) {
break;
}
AnnotatedDeclaredType adeclType = (AnnotatedDeclaredType) atype;
// Process type arguments.
if (declType.getTypeArguments().isPresent() && !declType.getTypeArguments().get().isEmpty() && !adeclType.getTypeArguments().isEmpty()) {
if (declType.getTypeArguments().get().size() != adeclType.getTypeArguments().size()) {
warn(astNode, String.format("Mismatch in type argument size between %s (%d) and %s (%d)", declType, declType.getTypeArguments().get().size(), adeclType, adeclType.getTypeArguments().size()));
break;
}
for (int i = 0; i < declType.getTypeArguments().get().size(); ++i) {
annotate(adeclType.getTypeArguments().get(i), declType.getTypeArguments().get().get(i), null, astNode);
}
}
break;
case WILDCARD:
AnnotatedWildcardType wildcardType = (AnnotatedWildcardType) atype;
// Ensure that the file also has a wildcard type, report an error otherwise
if (!typeDef.isWildcardType()) {
// We throw an error here, as otherwise we are just getting a generic cast error
// on the very next line.
warn(astNode, "Wildcard type <" + atype + "> does not match type in stubs file" + filename + ": <" + typeDef + ">" + " while parsing " + typeBeingParsed);
return;
}
WildcardType wildcardDef = (WildcardType) typeDef;
if (wildcardDef.getExtendedType().isPresent()) {
annotate(wildcardType.getExtendsBound(), wildcardDef.getExtendedType().get(), null, astNode);
annotate(wildcardType.getSuperBound(), primaryAnnotations, astNode);
} else if (wildcardDef.getSuperType().isPresent()) {
annotate(wildcardType.getSuperBound(), wildcardDef.getSuperType().get(), null, astNode);
annotate(wildcardType.getExtendsBound(), primaryAnnotations, astNode);
} else {
annotate(atype, primaryAnnotations, astNode);
}
break;
case TYPEVAR:
// Add annotations from the declaration of the TypeVariable
AnnotatedTypeVariable typeVarUse = (AnnotatedTypeVariable) atype;
Types typeUtils = processingEnv.getTypeUtils();
for (AnnotatedTypeVariable typePar : typeParameters) {
if (typeUtils.isSameType(typePar.getUnderlyingType(), atype.getUnderlyingType())) {
atypeFactory.replaceAnnotations(typePar.getUpperBound(), typeVarUse.getUpperBound());
atypeFactory.replaceAnnotations(typePar.getLowerBound(), typeVarUse.getLowerBound());
}
}
break;
default:
}
}
use of com.github.javaparser.ast.type.ReferenceType in project drools by kiegroup.
the class ExpressionTyper method toTypedExpressionFromMethodCallOrField.
private TypedExpressionResult toTypedExpressionFromMethodCallOrField(Expression drlxExpr) {
if (drlxExpr instanceof FieldAccessExpr) {
// try to see if it's a constant
final Optional<TypedExpression> typedExpression = tryParseAsConstantField(ruleContext.getTypeResolver(), ((FieldAccessExpr) drlxExpr).getScope(), ((FieldAccessExpr) drlxExpr).getNameAsString());
if (typedExpression.isPresent()) {
return new TypedExpressionResult(typedExpression, context);
}
}
if (patternType == null && drlxExpr instanceof NullSafeFieldAccessExpr) {
// try to see if it's a constant
final Optional<TypedExpression> typedExpression = tryParseAsConstantField(ruleContext.getTypeResolver(), ((NullSafeFieldAccessExpr) drlxExpr).getScope(), ((NullSafeFieldAccessExpr) drlxExpr).getNameAsString());
if (typedExpression.isPresent()) {
return new TypedExpressionResult(typedExpression, context);
}
}
final List<Node> childrenNodes = flattenScope(ruleContext.getTypeResolver(), drlxExpr);
final Node firstChild = childrenNodes.get(0);
boolean isInLineCast = firstChild instanceof InlineCastExpr;
java.lang.reflect.Type originalTypeCursor;
final Node firstNode;
if (isInLineCast) {
InlineCastExpr inlineCast = (InlineCastExpr) firstChild;
originalTypeCursor = originalTypeCursorFromInlineCast(inlineCast);
firstNode = inlineCast.getExpression();
if (inlineCast.getExpression().isThisExpr()) {
context.setInlineCastExpression(Optional.of(new InstanceOfExpr(new NameExpr(THIS_PLACEHOLDER), (ReferenceType) inlineCast.getType())));
} else {
context.setInlineCastExpression(toTypedExpression(inlineCast.getExpression()).getTypedExpression().map(TypedExpression::getExpression).map(expr -> new InstanceOfExpr(expr, (ReferenceType) inlineCast.getType())));
}
} else {
originalTypeCursor = patternType;
firstNode = firstChild;
}
if (originalTypeCursor != null && originalTypeCursor.equals(Object.class)) {
// try infer type from the declarations
final Optional<DeclarationSpec> declarationById = ruleContext.getDeclarationById(printNode(firstChild));
originalTypeCursor = declarationById.map(d -> (java.lang.reflect.Type) d.getDeclarationClass()).orElse(originalTypeCursor);
}
final Optional<TypedExpressionCursor> teCursor = processFirstNode(drlxExpr, childrenNodes, firstNode, isInLineCast, originalTypeCursor);
if (firstNode instanceof MethodCallExpr) {
MethodCallExpr me = (MethodCallExpr) firstNode;
addReactOnProperty(me.getNameAsString(), me.getArguments());
}
if (firstNode instanceof NullSafeMethodCallExpr) {
NullSafeMethodCallExpr me = (NullSafeMethodCallExpr) firstNode;
addReactOnProperty(me.getNameAsString(), me.getArguments());
}
if (!teCursor.isPresent()) {
return new TypedExpressionResult(empty(), context);
}
Expression previous = teCursor.get().expressionCursor;
java.lang.reflect.Type typeCursor = teCursor.get().typeCursor;
List<Node> childrenWithoutFirst = childrenNodes.subList(1, childrenNodes.size());
for (Node part : childrenWithoutFirst) {
if (part instanceof SimpleName) {
String field = part.toString();
TypedExpression expression = nameExprToMethodCallExpr(field, typeCursor, previous, ruleContext);
if (expression == null) {
ruleContext.addCompilationError(new InvalidExpressionErrorResult("Unknown field " + field + " on " + typeCursor));
break;
}
typeCursor = expression.getType();
previous = expression.getExpression();
} else if (part instanceof MethodCallExpr) {
TypedExpressionCursor typedExpr = methodCallExpr((MethodCallExpr) part, typeCursor, previous);
typeCursor = typedExpr.typeCursor;
previous = typedExpr.expressionCursor;
} else if (part instanceof NullSafeMethodCallExpr) {
TypedExpressionCursor typedExpr = nullSafeMethodCallExpr((NullSafeMethodCallExpr) part, typeCursor, previous);
typeCursor = typedExpr.typeCursor;
previous = typedExpr.expressionCursor;
} else if (part instanceof InlineCastExpr && ((InlineCastExpr) part).getExpression() instanceof FieldAccessExpr) {
InlineCastExpr inlineCastExprPart = (InlineCastExpr) part;
final FieldAccessExpr fieldAccessExpr = (FieldAccessExpr) inlineCastExprPart.getExpression();
final TypedExpression toMethodCallExpr = nameExprToMethodCallExpr(fieldAccessExpr.getNameAsString(), typeCursor, previous, ruleContext);
if (toMethodCallExpr == null) {
ruleContext.addCompilationError(new InvalidExpressionErrorResult("Unknown field " + fieldAccessExpr.getNameAsString() + " on " + typeCursor));
break;
}
final Class<?> castClass = getClassFromType(ruleContext.getTypeResolver(), inlineCastExprPart.getType());
previous = addCastToExpression(castClass, toMethodCallExpr.getExpression(), false);
typeCursor = castClass;
} else if (part instanceof ArrayAccessExpr) {
final ArrayAccessExpr inlineCastExprPart = (ArrayAccessExpr) part;
TypedExpressionCursor typedExpr = arrayAccessExpr(inlineCastExprPart, typeCursor, previous).orElseThrow(() -> new NoSuchElementException("ArrayAccessExpr doesn't contain TypedExpressionCursor!"));
typeCursor = typedExpr.typeCursor;
previous = typedExpr.expressionCursor;
} else {
throw new UnsupportedOperationException();
}
}
return new TypedExpressionResult(of(new TypedExpression(previous, typeCursor, accessorToFieldName(drlxExpr))), context);
}
Aggregations