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

com.github.javaparser.symbolsolver.model.resolution.SymbolReference Maven / Gradle / Ivy

There is a newer version: 3.15.15
Show newest version
/*
 * Copyright 2016 Federico Tomassetti
 *
 * 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 com.github.javaparser.symbolsolver.model.resolution;

import com.github.javaparser.resolution.declarations.ResolvedDeclaration;

import java.util.Optional;

/**
 * A reference to a symbol. It can solved or not solved. If solved the corresponding
 * declaration will be provided.
 *
 * @author Federico Tomassetti
 */
public class SymbolReference {

    private Optional correspondingDeclaration;

    private SymbolReference(Optional correspondingDeclaration) {
        this.correspondingDeclaration = correspondingDeclaration;
    }

    /**
     * Create a solve reference to the given symbol.
     */
    public static  SymbolReference solved(S2 symbolDeclaration) {
        return new SymbolReference(Optional.of(symbolDeclaration));
    }

    /**
     * Create an unsolved reference specifying the type of the value expected.
     */
    public static  SymbolReference unsolved(Class clazz) {
        return new SymbolReference<>(Optional.empty());
    }

    @Override
    public String toString() {
        return "SymbolReference{" + correspondingDeclaration + "}";
    }

    /**
     * The corresponding declaration. If not solve this throws UnsupportedOperationException.
     */
    public S getCorrespondingDeclaration() {
        if (!isSolved()) {
            throw new UnsupportedOperationException("CorrespondingDeclaration not available for unsolved symbol.");
        }
        return correspondingDeclaration.get();
    }

    /**
     * Is the reference solved?
     */
    public boolean isSolved() {
        return correspondingDeclaration.isPresent();
    }

    public static  SymbolReference adapt(SymbolReference ref, Class clazz) {
        if (ref.isSolved()) {
            return SymbolReference.solved(ref.getCorrespondingDeclaration());
        } else {
            return SymbolReference.unsolved(clazz);
        }
    }
}