问一个问题 ,关于shiro的?

添加jar包org.apache.shiro:shiro-core:1.3.2
SimpleByteSource simpleByteSource = new SimpleByteSource("c6c6ff38f906f7822070edc2fe2eff7c".getBytes());
这个对象里的成员变量 cachedBase64是什么时候被赋值的。

  1. 执行SimpleByteSource simpleByteSource = new SimpleByteSource("c6c6ff38f906f7822070edc2fe2eff7c".getBytes());会默认执行toString()方法,因为SimpleByteSource类中重写了toString方法:会执行this.toBase64();,因此会调用toBase64()方法,因为是默认调用,所以打断点是不会跳进去的;
  2. 为什么new一个对象会默认执行toString方法,因为所有类的基类都是Object,即都是继承了Object对象,创建对象时就自动调用xx的toString()方法。
public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

如果不重写toString()方法:会得到输出:xxxx@xxxxxxx的类名加地址形式;
如果重写toString()方法,则输出重写后的值。
SimpleByteSource类中重写了toString()方法:

public String toString() {
        return this.toBase64();
    }

    public int hashCode() {
        return this.bytes != null && this.bytes.length != 0 ? Arrays.hashCode(this.bytes) : 0;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (o instanceof ByteSource) {
            ByteSource bs = (ByteSource)o;
            return Arrays.equals(this.getBytes(), bs.getBytes());
        } else {
            return false;
        }
    }
 public String toBase64() {
        if (this.cachedBase64 == null) {
            this.cachedBase64 = Base64.encodeToString(this.getBytes());
        }

        return this.cachedBase64;
    }
  1. 举个例子:
    重写一个类SimpleByteSourceTest,该类里面的成员变量与方法都与SimpleByteSource相同,不同的是SimpleByteSourceTest类不重写toString()方法。看一下运行结果:
    SimpleByteSourceTest代码如下:
public class SimpleByteSourceTest implements ByteSource {
    private final byte[] bytes;
    private String cachedHex;
    private String cachedBase64;

    public SimpleByteSourceTest(byte[] bytes) {
        this.bytes = bytes;
    }

    public SimpleByteSourceTest(char[] chars) {
        this.bytes = CodecSupport.toBytes(chars);
    }

    public SimpleByteSourceTest(String string) {
        this.bytes = CodecSupport.toBytes(string);
    }

    public SimpleByteSourceTest(ByteSource source) {
        this.bytes = source.getBytes();
    }

    public SimpleByteSourceTest(File file) {
        this.bytes = (new SimpleByteSourceTest.BytesHelper()).getBytes(file);
    }

    public SimpleByteSourceTest(InputStream stream) {
        this.bytes = (new SimpleByteSourceTest.BytesHelper()).getBytes(stream);
    }

    public static boolean isCompatible(Object o) {
        return o instanceof byte[] || o instanceof char[] || o instanceof String || o instanceof ByteSource || o instanceof File || o instanceof InputStream;
    }

    @Override
    public byte[] getBytes() {
        return this.bytes;
    }

    @Override
    public boolean isEmpty() {
        return this.bytes == null || this.bytes.length == 0;
    }

    @Override
    public String toHex() {
        if (this.cachedHex == null) {
            this.cachedHex = Hex.encodeToString(this.getBytes());
        }

        return this.cachedHex;
    }

    @Override
    public String toBase64() {
        if (this.cachedBase64 == null) {
            this.cachedBase64 = Base64.encodeToString(this.getBytes());
        }

        return this.cachedBase64;
    }
    
    private static final class BytesHelper extends CodecSupport {
        private BytesHelper() {
        }

        public byte[] getBytes(File file) {
            return this.toBytes(file);
        }

        public byte[] getBytes(InputStream stream) {
            return this.toBytes(stream);
        }
    }
}

运行结果图:
img

img

1.当调用 SimpleByteSource simpleByteSource = new SimpleByteSource("c6c6ff38f906f7822070edc2fe2eff7c".getBytes());
创建了SimpleByteSource 对象,并且给bytes属性赋值;
SimpleByteSource 类中成员变量如下:

    private final byte[] bytes;
    private String cachedHex;
    private String cachedBase64;

调用的是有参构造,bytes属性就被赋值了赋值

  public SimpleByteSource(byte[] bytes) {
        this.bytes = bytes;
    }

当执行simpleByteSource.toBase64()时或者调用simpleByteSource.toString(),成员变量 cachedBase64就被赋值了,SimpleByteSource的原码如下:
this.cachedBase64 = Base64.encodeToString(this.getBytes());

  public String toBase64() {
        if (this.cachedBase64 == null) {
            this.cachedBase64 = Base64.encodeToString(this.getBytes());
        }

        return this.cachedBase64;
    }

    public String toString() {
        return this.toBase64();
    }
  1. 如果对您有帮助,请点击【采纳】