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

com.github.veithen.phos.enforcer.ClassProcessor Maven / Gradle / Ivy

The newest version!
/*-
 * #%L
 * Phos
 * %%
 * Copyright (C) 2016 - 2023 Andreas Veithen
 * %%
 * 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.
 * #L%
 */
package com.github.veithen.phos.enforcer;

import java.io.IOException;
import java.io.InputStream;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

final class ClassProcessor extends ClassVisitor {
    private final ReferenceCollector referenceCollector;
    private boolean deprecated;
    private ReferenceProcessor referenceProcessor;

    private ClassProcessor(ReferenceCollector referenceCollector) {
        super(Opcodes.ASM9);
        this.referenceCollector = referenceCollector;
    }

    private static boolean isPublic(int access) {
        return (access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) != 0;
    }

    @Override
    public void visit(
            int version,
            int access,
            String name,
            String signature,
            String superName,
            String[] interfaces) {
        deprecated = (access & Opcodes.ACC_DEPRECATED) != 0;
        if (!deprecated) {
            referenceProcessor =
                    new ReferenceProcessor(
                            referenceCollector,
                            new Clazz(Type.getObjectType(name).getClassName()),
                            isPublic(access));
            referenceProcessor.processType(Type.getObjectType(superName), true);
            for (String iface : interfaces) {
                referenceProcessor.processType(Type.getObjectType(iface), true);
            }
        }
    }

    @Override
    public FieldVisitor visitField(
            int access, String name, String desc, String signature, Object value) {
        if (!deprecated) {
            referenceProcessor.processType(Type.getType(desc), isPublic(access));
        }
        return null;
    }

    @Override
    public MethodVisitor visitMethod(
            int access, String name, String desc, String signature, String[] exceptions) {
        if (!deprecated && (access & Opcodes.ACC_DEPRECATED) == 0) {
            boolean isPublic = isPublic(access);
            referenceProcessor.processType(Type.getMethodType(desc), isPublic);
            if (exceptions != null) {
                for (String exception : exceptions) {
                    referenceProcessor.processType(Type.getObjectType(exception), isPublic);
                }
            }
            return new MethodProcessor(referenceProcessor);
        } else {
            return null;
        }
    }

    static void processDefinition(InputStream in, ReferenceCollector referenceCollector)
            throws IOException {
        new ClassReader(in)
                .accept(
                        new ClassProcessor(referenceCollector),
                        ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
    }

    static void processClass(Class clazz, ReferenceCollector referenceCollector) {
        try (InputStream in =
                clazz.getClassLoader()
                        .getResourceAsStream(clazz.getName().replace('.', '/') + ".class")) {
            processDefinition(in, referenceCollector);
        } catch (IOException ex) {
            throw new Error(ex);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy