vendredi 12 octobre 2018

Iterating over reflected subclasses, variables don't appear to be shadowing, IntelliJ seems conflicted

I'm working on a little Altair emulator in my free time. I decided to try working in Java again after a VERY long time working with C#, so excuse any major idiocy on my part.

Essentially, I'm using a command pattern and org.reflections to make implementing machine instructions easy. Here's my reflection code.

        instructions = new ArrayList<IInstruction>();
        Reflections reflections = new Reflections("com.cjm.eaglestar.instructions");

        Set<Class<? extends IInstruction>> subTypesOf = reflections.getSubTypesOf(IInstruction.class);
        for(Class<? extends IInstruction> command : subTypesOf){
            try {
                instructions.add(command.getDeclaredConstructor().newInstance());
            }
            catch(Exception e){
                System.out.println(e.toString());
            }
        } 

Here's my interface for Instructions:

package com.cjm.eaglestar.instructions;

public interface IInstruction{
    byte OpCode = 0;
    String Mnemonic = "XXX";
    void Execute();
}

Here's a concrete implementation:

package com.cjm.eaglestar.instructions;

import static com.cjm.eaglestar.Eaglestar.machineState;

public class LDAInstruction implements IInstruction {
    public byte OpCode = Byte.valueOf("00111010",2);
    public String Mnemonic = "LDA";

    public void Execute(){
        machineState.programCounter++;
        byte lowByte = machineState.memory[machineState.programCounter & 0xFF];
        machineState.programCounter++;
        byte highByte = machineState.memory[machineState.programCounter & 0xFF];
        machineState.registers.A = machineState.memory[(lowByte & 0xFF) + ((highByte & 0xFF) * 256)];
    }

    public LDAInstruction(){
    }
}

Here's where things get weird. I use the OpCode to iterate over all the instructions, and when I come to compare the executing OpCode to the reflected instructions, the comparison uses the superclass's default opcode (0), even though I'm not saying super.

The effect in IntelliJ is even weirder. Mousing over shows the super opcode, but digging down into the instructions collection shows the correct concrete opcode. Here's some pictures of that weirdness.

Superclass on mouseover Subclass on drilldown Any help would be appreciated, as I'm just insanely lost and this is blocking any forward progress. Thanks.





Aucun commentaire:

Enregistrer un commentaire