/*
 * This file is part of Mixin, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered <https://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.spongepowered.asm.mixin.transformer;

import java.util.HashSet;
import java.util.Set;

import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.MethodNode;

/**
 * Coprocessor which handles synthetic inner classes in mixins. Since synthetic
 * inner classes are usually just switch tables, it is reasonably safe to share
 * them amongst all mixin targets. Therefore the class is simply transformed so
 * that all members are public.
 * 
 * <p>Nest based-access control could potentially be used where a mixin has a
 * single target, and Java 11 or higher is in use, but at the moment it's more
 * straightforward to just make everything public.</p>
 */
class MixinCoprocessorSyntheticInner extends MixinCoprocessor {

    /**
     * Synthetic inner classes in mixins
     */
    private final Set<String> syntheticInnerClasses = new HashSet<String>();

    MixinCoprocessorSyntheticInner() {
    }
    
    @Override
    String getName() {
        return "syntheticinner";
    }
    
    @Override
    public void onInit(MixinInfo mixin) {
        for (String innerClass : mixin.getSyntheticInnerClasses()) {
            this.registerSyntheticInner(innerClass.replace('/', '.'));
        }
    }

    void registerSyntheticInner(String className) {
        this.syntheticInnerClasses.add(className);
    }

    @Override
    public boolean couldTransform(String className) {
        return this.syntheticInnerClasses.contains(className);
    }

    /**
     * "Pass through" a synthetic inner class. Transforms package-private
     * members in the class into public so that they are accessible from their
     * new home in the target class
     */
    @Override
    ProcessResult process(String className, ClassNode classNode) {
        if (!this.syntheticInnerClasses.contains(className)) {
            return ProcessResult.NONE;
        }

        classNode.access |= Opcodes.ACC_PUBLIC;
        
        for (FieldNode field : classNode.fields) {
            if ((field.access & (Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) == 0) {
                field.access |= Opcodes.ACC_PUBLIC;
            }
        }

        for (MethodNode method : classNode.methods) {
            if ((method.access & (Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) == 0) {
                method.access |= Opcodes.ACC_PUBLIC;
            }
        }
        
        return ProcessResult.PASSTHROUGH_TRANSFORMED;
    }

}
