/*
 * Copyright 2015 Michael Rozumyanskiy
 *
 * 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 io.michaelrocks.lightsaber.processor.signature;

import io.michaelrocks.lightsaber.processor.ProcessorContext;
import org.objectweb.asm.Type;
import org.objectweb.asm.signature.SignatureVisitor;

import static org.objectweb.asm.Opcodes.ASM5;

public class TypeSignatureParser extends SignatureVisitor {
    private final ProcessorContext processorContext;

    private Type classType;
    private Type classTypeParameter;
    private boolean isValid = true;

    private TypeSignature typeSignature;

    public TypeSignatureParser(final ProcessorContext processorContext) {
        super(ASM5);
        this.processorContext = processorContext;
    }

    public TypeSignature getTypeSignature() {
        return typeSignature;
    }

    @Override
    public void visitClassType(final String name) {
        if (classType == null) {
            classType = Type.getObjectType(name);
        } else if (classTypeParameter == null) {
            classTypeParameter = Type.getObjectType(name);
        } else {
            reportError(classType + " has multiple type arguments");
        }
    }

    @Override
    public SignatureVisitor visitTypeArgument(final char wildcard) {
        if (wildcard == INSTANCEOF) {
            return this;
        }

        reportError("Injectable field cannot have wildcards in its signature");
        return this;
    }

    @Override
    public void visitEnd() {
        typeSignature = isValid ? new TypeSignature(classType, classTypeParameter) : null;
    }

    // Prohibited callbacks.

    @Override
    public void visitInnerClassType(final String name) {
        reportError("Injectable field cannot have an inner class type in its signature");
    }

    @Override
    public void visitBaseType(final char descriptor) {
        reportError("Injectable field cannot be a primitive type");
    }

    @Override
    public SignatureVisitor visitArrayType() {
        reportError("Injectable field cannot be an array");
        return this;
    }

    @Override
    public void visitTypeVariable(final String name) {
        reportError("Injectable field cannot have type variables in its signature");
    }

    @Override
    public void visitTypeArgument() {
        reportError("Injectable field cannot have unbounded type arguments in its signature");
    }

    private void reportError(final String message) {
        processorContext.reportError(message);
        isValid = false;
    }
}

@@ -1301,24 +1301,160 @@
 gnature() %7B%0A
+        if (isValid && typeSignature == null) %7B%0A            typeSignature = new TypeSignature(classType, classTypeParameter);%0A        %7D%0A
         retu
@@ -2107,149 +2107,8 @@
  %7D%0A%0A
-    @Override%0A    public void visitEnd() %7B%0A        typeSignature = isValid ? new TypeSignature(classType, classTypeParameter) : null;%0A    %7D%0A%0A
     

Create TypeSignature lazily in TypeSignatureParser