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

com.tngtech.archunit.core.domain.JavaModifier Maven / Gradle / Ivy

/*
 * Copyright 2019 TNG Technology Consulting GmbH
 *
 * 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 com.tngtech.archunit.core.domain;

import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;

import com.google.common.collect.Sets;
import com.tngtech.archunit.PublicAPI;
import org.objectweb.asm.Opcodes;

import static com.tngtech.archunit.PublicAPI.Usage.ACCESS;

public enum JavaModifier {
    @PublicAPI(usage = ACCESS)
    PUBLIC(EnumSet.allOf(ApplicableType.class), Opcodes.ACC_PUBLIC),
    @PublicAPI(usage = ACCESS)
    PROTECTED(EnumSet.allOf(ApplicableType.class), Opcodes.ACC_PROTECTED),
    @PublicAPI(usage = ACCESS)
    PRIVATE(EnumSet.allOf(ApplicableType.class), Opcodes.ACC_PRIVATE),
    @PublicAPI(usage = ACCESS)
    STATIC(EnumSet.of(ApplicableType.FIELD, ApplicableType.METHOD), Opcodes.ACC_STATIC),
    @PublicAPI(usage = ACCESS)
    FINAL(EnumSet.allOf(ApplicableType.class), Opcodes.ACC_FINAL),
    @PublicAPI(usage = ACCESS)
    VOLATILE(EnumSet.of(ApplicableType.FIELD), Opcodes.ACC_VOLATILE),
    @PublicAPI(usage = ACCESS)
    TRANSIENT(EnumSet.of(ApplicableType.FIELD), Opcodes.ACC_TRANSIENT),
    @PublicAPI(usage = ACCESS)
    ABSTRACT(EnumSet.of(ApplicableType.CLASS, ApplicableType.METHOD), Opcodes.ACC_ABSTRACT),
    @PublicAPI(usage = ACCESS)
    SYNCHRONIZED(EnumSet.of(ApplicableType.METHOD), Opcodes.ACC_SYNCHRONIZED),
    @PublicAPI(usage = ACCESS)
    NATIVE(EnumSet.of(ApplicableType.METHOD), Opcodes.ACC_NATIVE);

    private final Set applicableTo;
    private final int asmAccessFlag;

    JavaModifier(Set applicableTo, int asmAccessFlag) {
        this.applicableTo = Sets.immutableEnumSet(applicableTo);
        this.asmAccessFlag = asmAccessFlag;
    }

    @PublicAPI(usage = ACCESS)
    public static Set getModifiersForClass(int asmAccess) {
        return getModifiersFor(ApplicableType.CLASS, asmAccess);
    }

    @PublicAPI(usage = ACCESS)
    public static Set getModifiersForField(int asmAccess) {
        return getModifiersFor(ApplicableType.FIELD, asmAccess);
    }

    @PublicAPI(usage = ACCESS)
    public static Set getModifiersForMethod(int asmAccess) {
        return getModifiersFor(ApplicableType.METHOD, asmAccess);
    }

    private static Set getModifiersFor(ApplicableType type, int asmAccess) {
        Set result = new HashSet<>();
        for (JavaModifier modifier : JavaModifier.values()) {
            if (modifier.applicableTo.contains(type) && modifierPresent(modifier, asmAccess)) {
                result.add(modifier);
            }
        }
        return result.isEmpty() ? Collections.emptySet() : Sets.immutableEnumSet(result);
    }

    private static boolean modifierPresent(JavaModifier modifier, int asmAccess) {
        return (modifier.asmAccessFlag & asmAccess) != 0;
    }

    private enum ApplicableType {
        CLASS, METHOD, FIELD
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy