All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.helidon.codegen.RoundContextImpl Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2023, 2024 Oracle and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.helidon.codegen;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import io.helidon.codegen.classmodel.ClassModel;
import io.helidon.common.types.TypeInfo;
import io.helidon.common.types.TypeName;
import io.helidon.common.types.TypedElementInfo;

class RoundContextImpl implements RoundContext {
    private final Map newTypes = new HashMap<>();
    private final Map> annotationToTypes;
    private final List types;
    private final CodegenContext ctx;
    private final Collection annotations;

    RoundContextImpl(CodegenContext ctx,
                     Set annotations,
                     Map> annotationToTypes,
                     List types) {
        this.ctx = ctx;
        this.annotations = annotations;
        this.annotationToTypes = annotationToTypes;
        this.types = types;
    }

    @Override
    public Collection availableAnnotations() {
        return annotations;
    }

    @Override
    public Collection types() {
        return types;
    }

    @Override
    public Collection annotatedElements(TypeName annotationType) {
        List typeInfos = annotationToTypes.get(annotationType);
        if (typeInfos == null) {
            return Set.of();
        }

        List result = new ArrayList<>();

        for (TypeInfo typeInfo : typeInfos) {
            typeInfo.elementInfo()
                    .stream()
                    .filter(it -> it.hasAnnotation(annotationType))
                    .forEach(result::add);
        }

        return result;
    }

    @Override
    public Collection annotatedTypes(TypeName annotationType) {
        List typeInfos = annotationToTypes.get(annotationType);
        if (typeInfos == null) {
            return Set.of();
        }

        List result = new ArrayList<>();

        for (TypeInfo typeInfo : typeInfos) {
            if (typeInfo.hasAnnotation(annotationType) || TypeHierarchy.hierarchyAnnotations(ctx, typeInfo)
                    .stream()
                    .anyMatch(it -> it.typeName().equals(annotationType))) {
                result.add(typeInfo);
            }
        }

        return result;
    }

    @Override
    public void addGeneratedType(TypeName type,
                                 ClassModel.Builder newClass,
                                 TypeName mainTrigger,
                                 Object... originatingElements) {
        this.newTypes.put(type, new ClassCode(type, newClass, mainTrigger, originatingElements));
    }

    @Override
    public Optional generatedType(TypeName type) {
        return Optional.ofNullable(newTypes.get(type)).map(ClassCode::classModel);
    }

    Collection newTypes() {
        return newTypes.values();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy