Search in sources :

Example 71 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.

the class KiePMMLRegressionTableFactory method getResultUpdaterSupportedExpression.

/**
 * Create a <b>resultUpdater</b> <code>CastExpr</code>
 * @param normalizationMethod
 * @return
 */
static MethodReferenceExpr getResultUpdaterSupportedExpression(final RegressionModel.NormalizationMethod normalizationMethod) {
    final String thisExpressionMethodName = String.format("update%sResult", normalizationMethod.name());
    final CastExpr castExpr = new CastExpr();
    final String doubleClassName = Double.class.getSimpleName();
    final ClassOrInterfaceType consumerType = getTypedClassOrInterfaceTypeByTypeNames(SerializableFunction.class.getCanonicalName(), Arrays.asList(doubleClassName, doubleClassName));
    castExpr.setType(consumerType);
    castExpr.setExpression(KiePMMLRegressionTable.class.getSimpleName());
    final MethodReferenceExpr toReturn = new MethodReferenceExpr();
    toReturn.setScope(castExpr);
    toReturn.setIdentifier(thisExpressionMethodName);
    return toReturn;
}
Also used : SerializableFunction(org.kie.pmml.api.iinterfaces.SerializableFunction) KiePMMLRegressionTable(org.kie.pmml.models.regression.model.KiePMMLRegressionTable) CastExpr(com.github.javaparser.ast.expr.CastExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr)

Example 72 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.

the class KiePMMLScorecardModelFactory method setConstructor.

static void setConstructor(final ScorecardCompilationDTO compilationDTO, final ClassOrInterfaceDeclaration modelTemplate, final String fullCharacteristicsClassName) {
    KiePMMLModelFactoryUtils.init(compilationDTO, modelTemplate);
    final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
    final BlockStmt body = constructorDeclaration.getBody();
    final ExplicitConstructorInvocationStmt superStatement = CommonCodegenUtils.getExplicitConstructorInvocationStmt(body).orElseThrow(() -> new KiePMMLException(String.format(MISSING_CONSTRUCTOR_IN_BODY, body)));
    ClassOrInterfaceType characteristicsClass = parseClassOrInterfaceType(fullCharacteristicsClassName);
    ObjectCreationExpr characteristicsReference = new ObjectCreationExpr();
    characteristicsReference.setType(characteristicsClass);
    superStatement.setArgument(2, characteristicsReference);
    superStatement.setArgument(3, getExpressionForObject(compilationDTO.getInitialScore()));
    superStatement.setArgument(4, getExpressionForObject(compilationDTO.isUseReasonCodes()));
    REASONCODE_ALGORITHM reasoncodeAlgorithm = compilationDTO.getREASONCODE_ALGORITHM();
    NameExpr reasonCodeExpr = new NameExpr(REASONCODE_ALGORITHM.class.getName() + "." + reasoncodeAlgorithm.name());
    superStatement.setArgument(5, reasonCodeExpr);
    superStatement.setArgument(6, getExpressionForObject(compilationDTO.getBaselineScore()));
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) REASONCODE_ALGORITHM(org.kie.pmml.api.enums.REASONCODE_ALGORITHM) NameExpr(com.github.javaparser.ast.expr.NameExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType)

Example 73 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType in project checker-framework by typetools.

the class JointJavacJavaParserVisitor method visitMemberSelect.

@Override
public Void visitMemberSelect(MemberSelectTree javacTree, Node javaParserNode) {
    if (javaParserNode instanceof FieldAccessExpr) {
        FieldAccessExpr node = (FieldAccessExpr) javaParserNode;
        processMemberSelect(javacTree, node);
        javacTree.getExpression().accept(this, node.getScope());
    } else if (javaParserNode instanceof Name) {
        Name node = (Name) javaParserNode;
        processMemberSelect(javacTree, node);
        assert node.getQualifier().isPresent();
        javacTree.getExpression().accept(this, node.getQualifier().get());
    } else if (javaParserNode instanceof ClassOrInterfaceType) {
        ClassOrInterfaceType node = (ClassOrInterfaceType) javaParserNode;
        processMemberSelect(javacTree, node);
        assert node.getScope().isPresent();
        javacTree.getExpression().accept(this, node.getScope().get());
    } else if (javaParserNode instanceof ClassExpr) {
        ClassExpr node = (ClassExpr) javaParserNode;
        processMemberSelect(javacTree, node);
        javacTree.getExpression().accept(this, node.getType());
    } else if (javaParserNode instanceof ThisExpr) {
        ThisExpr node = (ThisExpr) javaParserNode;
        processMemberSelect(javacTree, node);
        assert node.getTypeName().isPresent();
        javacTree.getExpression().accept(this, node.getTypeName().get());
    } else if (javaParserNode instanceof SuperExpr) {
        SuperExpr node = (SuperExpr) javaParserNode;
        processMemberSelect(javacTree, node);
        assert node.getTypeName().isPresent();
        javacTree.getExpression().accept(this, node.getTypeName().get());
    } else {
        throwUnexpectedNodeType(javacTree, javaParserNode);
    }
    return null;
}
Also used : SuperExpr(com.github.javaparser.ast.expr.SuperExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) SimpleName(com.github.javaparser.ast.expr.SimpleName) Name(com.github.javaparser.ast.expr.Name)

Example 74 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType 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:
    }
}
Also used : Types(javax.lang.model.util.Types) AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) WildcardType(com.github.javaparser.ast.type.WildcardType) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) ReferenceType(com.github.javaparser.ast.type.ReferenceType)

Example 75 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType in project checker-framework by typetools.

the class AnnotationTransferVisitor method visit.

@Override
public void visit(TypeParameter target, AnnotatedTypeMirror type) {
    AnnotatedTypeVariable annotatedTypeVar = (AnnotatedTypeVariable) type;
    NodeList<ClassOrInterfaceType> bounds = target.getTypeBound();
    if (bounds.size() == 1) {
        bounds.get(0).accept(this, annotatedTypeVar.getUpperBound());
    }
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Aggregations

ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)76 Expression (com.github.javaparser.ast.expr.Expression)33 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)29 Type (com.github.javaparser.ast.type.Type)24 NameExpr (com.github.javaparser.ast.expr.NameExpr)23 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)21 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)21 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)17 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)17 NodeList (com.github.javaparser.ast.NodeList)14 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)13 TypeParameter (com.github.javaparser.ast.type.TypeParameter)13 StaticJavaParser.parseClassOrInterfaceType (com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType)12 Test (org.junit.Test)11 StaticJavaParser.parseType (com.github.javaparser.StaticJavaParser.parseType)9 List (java.util.List)9 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)8 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)7 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)7 HashMap (java.util.HashMap)7