Search in sources :

Example 36 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType in project cas by apereo.

the class ConfigurationMetadataGenerator method locatePropertiesClassForType.

private Class locatePropertiesClassForType(final ClassOrInterfaceType type) {
    if (cachedPropertiesClasses.containsKey(type.getNameAsString())) {
        return cachedPropertiesClasses.get(type.getNameAsString());
    }
    final Predicate<String> filterInputs = s -> s.contains(type.getNameAsString());
    final Predicate<String> filterResults = s -> s.endsWith(type.getNameAsString());
    final String packageName = ConfigurationMetadataGenerator.class.getPackage().getName();
    final Reflections reflections = new Reflections(new ConfigurationBuilder().filterInputsBy(filterInputs).setUrls(ClasspathHelper.forPackage(packageName)).setScanners(new TypeElementsScanner().includeFields(false).includeMethods(false).includeAnnotations(false).filterResultsBy(filterResults), new SubTypesScanner(false)));
    final Class clz = reflections.getSubTypesOf(Serializable.class).stream().filter(c -> c.getSimpleName().equalsIgnoreCase(type.getNameAsString())).findFirst().orElseThrow(() -> new IllegalArgumentException("Cant locate class for " + type.getNameAsString()));
    cachedPropertiesClasses.put(type.getNameAsString(), clz);
    return clz;
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Arrays(java.util.Arrays) SneakyThrows(lombok.SneakyThrows) VoidVisitorAdapter(com.github.javaparser.ast.visitor.VoidVisitorAdapter) Reflections(org.reflections.Reflections) StringUtils(org.apache.commons.lang3.StringUtils) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) LiteralStringValueExpr(com.github.javaparser.ast.expr.LiteralStringValueExpr) ClassUtils(org.apache.commons.lang3.ClassUtils) QueryType(org.apereo.services.persondir.support.QueryType) Matcher(java.util.regex.Matcher) Map(java.util.Map) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Resource(org.springframework.core.io.Resource) ValueHint(org.springframework.boot.configurationmetadata.ValueHint) Unchecked(org.jooq.lambda.Unchecked) Collection(java.util.Collection) Set(java.util.Set) AbstractLdapProperties(org.apereo.cas.configuration.model.support.ldap.AbstractLdapProperties) ConfigurationMetadataProperty(org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty) Collectors(java.util.stream.Collectors) PasswordPolicyProperties(org.apereo.cas.configuration.model.core.authentication.PasswordPolicyProperties) ClasspathHelper(org.reflections.util.ClasspathHelper) Modifier(com.github.javaparser.ast.Modifier) Serializable(java.io.Serializable) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Stream(java.util.stream.Stream) Predicate(com.google.common.base.Predicate) Pattern(java.util.regex.Pattern) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeElementsScanner(org.reflections.scanners.TypeElementsScanner) RequiresModule(org.apereo.cas.configuration.support.RequiresModule) HashMap(java.util.HashMap) PrincipalTransformationProperties(org.apereo.cas.configuration.model.core.authentication.PrincipalTransformationProperties) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) HashSet(java.util.HashSet) DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) StreamSupport(java.util.stream.StreamSupport) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) LinkedHashSet(java.util.LinkedHashSet) RelaxedNames(org.springframework.boot.bind.RelaxedNames) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FileInputStream(java.io.FileInputStream) RequiredProperty(org.apereo.cas.configuration.support.RequiredProperty) CaseCanonicalizationMode(org.apereo.services.persondir.util.CaseCanonicalizationMode) File(java.io.File) SubTypesScanner(org.reflections.scanners.SubTypesScanner) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) ReflectionUtils(org.springframework.util.ReflectionUtils) LdapSearchEntryHandlersProperties(org.apereo.cas.configuration.model.support.ldap.LdapSearchEntryHandlersProperties) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) PrettyPrinter(com.fasterxml.jackson.core.PrettyPrinter) JavaParser(com.github.javaparser.JavaParser) InputStream(java.io.InputStream) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeElementsScanner(org.reflections.scanners.TypeElementsScanner) Reflections(org.reflections.Reflections)

Example 37 with ClassOrInterfaceType

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

the class StubParser method annotate.

/**
 * Add to {@code atype}:
 *
 * <ol>
 *   <li>the annotations from {@code typeDef}, and
 *   <li>any type annotations that parsed as declaration annotations (ie those 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
 */
private void annotate(AnnotatedTypeMirror atype, Type typeDef, NodeList<AnnotationExpr> declAnnos) {
    if (atype.getKind() == TypeKind.ARRAY) {
        annotateAsArray((AnnotatedArrayType) atype, (ReferenceType) typeDef, declAnnos);
        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);
    }
    switch(atype.getKind()) {
        case DECLARED:
            ClassOrInterfaceType declType = unwrapDeclaredType(typeDef);
            if (declType == null) {
                break;
            }
            AnnotatedDeclaredType adeclType = (AnnotatedDeclaredType) atype;
            if (declType.getTypeArguments().isPresent() && !declType.getTypeArguments().get().isEmpty() && !adeclType.getTypeArguments().isEmpty()) {
                assert declType.getTypeArguments().get().size() == adeclType.getTypeArguments().size() : String.format("Mismatch in type argument size between %s (%d) and %s (%d)", declType, declType.getTypeArguments().get().size(), adeclType, adeclType.getTypeArguments().size());
                for (int i = 0; i < declType.getTypeArguments().get().size(); ++i) {
                    annotate(adeclType.getTypeArguments().get(i), declType.getTypeArguments().get().get(i), null);
                }
            }
            break;
        case WILDCARD:
            AnnotatedWildcardType wildcardType = (AnnotatedWildcardType) atype;
            WildcardType wildcardDef = (WildcardType) typeDef;
            if (wildcardDef.getExtendedType().isPresent()) {
                annotate(wildcardType.getExtendsBound(), wildcardDef.getExtendedType().get(), null);
                annotate(wildcardType.getSuperBound(), primaryAnnotations);
            } else if (wildcardDef.getSuperType().isPresent()) {
                annotate(wildcardType.getSuperBound(), wildcardDef.getSuperType().get(), null);
                annotate(wildcardType.getExtendsBound(), primaryAnnotations);
            } else {
                annotate(atype, primaryAnnotations);
            }
            break;
        case TYPEVAR:
            // Add annotations from the declaration of the TypeVariable
            AnnotatedTypeVariable typeVarUse = (AnnotatedTypeVariable) atype;
            for (AnnotatedTypeVariable typePar : typeParameters) {
                if (typePar.getUnderlyingType() == atype.getUnderlyingType()) {
                    AnnotatedTypeMerger.merge(typePar.getUpperBound(), typeVarUse.getUpperBound());
                    AnnotatedTypeMerger.merge(typePar.getLowerBound(), typeVarUse.getLowerBound());
                }
            }
            break;
        default:
    }
}
Also used : 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)

Example 38 with ClassOrInterfaceType

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

the class StubParser method annotateSupertypes.

private void annotateSupertypes(ClassOrInterfaceDeclaration typeDecl, AnnotatedDeclaredType type) {
    if (typeDecl.getExtendedTypes() != null) {
        for (ClassOrInterfaceType superType : typeDecl.getExtendedTypes()) {
            AnnotatedDeclaredType foundType = findType(superType, type.directSuperTypes());
            if (foundType == null) {
                throw new Error("StubParser: could not find superclass " + superType + " from type " + type + LINE_SEPARATOR + "Stub file does not match bytecode");
            }
            annotate(foundType, superType, null);
        }
    }
    if (typeDecl.getImplementedTypes() != null) {
        for (ClassOrInterfaceType superType : typeDecl.getImplementedTypes()) {
            AnnotatedDeclaredType foundType = findType(superType, type.directSuperTypes());
            if (foundType == null) {
                throw new Error("StubParser: could not find superinterface " + superType + " from type " + type + LINE_SEPARATOR + "Stub file does not match bytecode");
            }
            annotate(foundType, superType, null);
        }
    }
}
Also used : AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType)

Example 39 with ClassOrInterfaceType

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

the class JavaParserClassDeclaration method getAncestors.

@Override
public List<ResolvedReferenceType> getAncestors() {
    List<ResolvedReferenceType> ancestors = new ArrayList<>();
    // We want to avoid infinite recursion in case of Object having Object as ancestor
    if (!(Object.class.getCanonicalName().equals(getQualifiedName()))) {
        ResolvedReferenceType superclass = getSuperClass();
        if (superclass != null) {
            ancestors.add(superclass);
        }
        if (wrappedNode.getImplementedTypes() != null) {
            for (ClassOrInterfaceType implemented : wrappedNode.getImplementedTypes()) {
                ResolvedReferenceType ancestor = toReferenceType(implemented);
                ancestors.add(ancestor);
            }
        }
    }
    return ancestors;
}
Also used : ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType)

Example 40 with ClassOrInterfaceType

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

the class JavaParserEnumDeclaration method getAncestors.

@Override
public List<ResolvedReferenceType> getAncestors() {
    List<ResolvedReferenceType> ancestors = new ArrayList<>();
    ResolvedReferenceType enumClass = ReflectionFactory.typeUsageFor(Enum.class, typeSolver).asReferenceType();
    ResolvedTypeParameterDeclaration eTypeParameter = enumClass.getTypeDeclaration().getTypeParameters().get(0);
    enumClass = enumClass.deriveTypeParameters(new ResolvedTypeParametersMap.Builder().setValue(eTypeParameter, new ReferenceTypeImpl(this, typeSolver)).build());
    ancestors.add(enumClass);
    if (wrappedNode.getImplementedTypes() != null) {
        for (ClassOrInterfaceType implementedType : wrappedNode.getImplementedTypes()) {
            SymbolReference<ResolvedTypeDeclaration> implementedDeclRef = new SymbolSolver(typeSolver).solveTypeInType(this, implementedType.getName().getId());
            if (!implementedDeclRef.isSolved()) {
                throw new UnsolvedSymbolException(implementedType.getName().getId());
            }
            ancestors.add(new ReferenceTypeImpl((ResolvedReferenceTypeDeclaration) implementedDeclRef.getCorrespondingDeclaration(), typeSolver));
        }
    }
    return ancestors;
}
Also used : UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) SymbolSolver(com.github.javaparser.symbolsolver.resolution.SymbolSolver)

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