com.github.javaparser.symbolsolver.model.resolution.SymbolReference Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javaparser-symbol-solver-model Show documentation
Show all versions of javaparser-symbol-solver-model Show documentation
A Symbol Solver for Java, built on top of JavaParser (model)
/*
* 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 extends S> correspondingDeclaration;
private SymbolReference(Optional extends S> 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 extends O> ref, Class clazz) {
if (ref.isSolved()) {
return SymbolReference.solved(ref.getCorrespondingDeclaration());
} else {
return SymbolReference.unsolved(clazz);
}
}
}