diff options
Diffstat (limited to 'javaparser-symbol-solver-model/src/main')
4 files changed, 278 insertions, 0 deletions
diff --git a/javaparser-symbol-solver-model/src/main/java/com/github/javaparser/symbolsolver/model/resolution/SymbolReference.java b/javaparser-symbol-solver-model/src/main/java/com/github/javaparser/symbolsolver/model/resolution/SymbolReference.java new file mode 100644 index 000000000..f454f8ac6 --- /dev/null +++ b/javaparser-symbol-solver-model/src/main/java/com/github/javaparser/symbolsolver/model/resolution/SymbolReference.java @@ -0,0 +1,80 @@ +/* + * 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<S extends ResolvedDeclaration> { + + private Optional<? extends S> correspondingDeclaration; + + private SymbolReference(Optional<? extends S> correspondingDeclaration) { + this.correspondingDeclaration = correspondingDeclaration; + } + + /** + * Create a solve reference to the given symbol. + */ + public static <S extends ResolvedDeclaration, S2 extends S> SymbolReference<S> solved(S2 symbolDeclaration) { + return new SymbolReference<S>(Optional.of(symbolDeclaration)); + } + + /** + * Create an unsolved reference specifying the type of the value expected. + */ + public static <S extends ResolvedDeclaration, S2 extends S> SymbolReference<S> unsolved(Class<S2> 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 <O extends ResolvedDeclaration> SymbolReference<O> adapt(SymbolReference<? extends O> ref, Class<O> clazz) { + if (ref.isSolved()) { + return SymbolReference.solved(ref.getCorrespondingDeclaration()); + } else { + return SymbolReference.unsolved(clazz); + } + } +} diff --git a/javaparser-symbol-solver-model/src/main/java/com/github/javaparser/symbolsolver/model/resolution/TypeSolver.java b/javaparser-symbol-solver-model/src/main/java/com/github/javaparser/symbolsolver/model/resolution/TypeSolver.java new file mode 100644 index 000000000..bfa43aead --- /dev/null +++ b/javaparser-symbol-solver-model/src/main/java/com/github/javaparser/symbolsolver/model/resolution/TypeSolver.java @@ -0,0 +1,70 @@ +/* + * 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.UnsolvedSymbolException; +import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration; +import com.github.javaparser.resolution.types.ResolvedType; + +/** + * An element able to find TypeDeclaration from their name. + * TypeSolvers are organized in hierarchies. + * + * @author Federico Tomassetti + */ +public interface TypeSolver { + + /** + * Get the root of the hierarchy of type solver. + */ + default TypeSolver getRoot() { + if (getParent() == null) { + return this; + } else { + return getParent().getRoot(); + } + } + + /** + * Parent of the this TypeSolver. This can return null. + */ + TypeSolver getParent(); + + /** + * Set the parent of this TypeSolver. + */ + void setParent(TypeSolver parent); + + /** + * Try to solve the type with the given name. It always return a SymbolReference which can be solved + * or unsolved. + */ + SymbolReference<ResolvedReferenceTypeDeclaration> tryToSolveType(String name); + + /** + * Solve the given type. Either the type is found and returned or an UnsolvedSymbolException is thrown. + */ + default ResolvedReferenceTypeDeclaration solveType(String name) throws UnsolvedSymbolException { + SymbolReference<ResolvedReferenceTypeDeclaration> ref = tryToSolveType(name); + if (ref.isSolved()) { + return ref.getCorrespondingDeclaration(); + } else { + throw new UnsolvedSymbolException(name, this.toString()); + } + } + +} diff --git a/javaparser-symbol-solver-model/src/main/java/com/github/javaparser/symbolsolver/model/resolution/Value.java b/javaparser-symbol-solver-model/src/main/java/com/github/javaparser/symbolsolver/model/resolution/Value.java new file mode 100644 index 000000000..3c5388e18 --- /dev/null +++ b/javaparser-symbol-solver-model/src/main/java/com/github/javaparser/symbolsolver/model/resolution/Value.java @@ -0,0 +1,60 @@ +/* + * 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.ResolvedValueDeclaration; +import com.github.javaparser.resolution.types.ResolvedType; + +/** + * Any type of value. + * + * @author Federico Tomassetti + */ +public class Value { + private ResolvedType type; + private String name; + + public Value(ResolvedType type, String name) { + this.type = type; + this.name = name; + } + + /** + * Create a Value from a ValueDeclaration. + */ + public static Value from(ResolvedValueDeclaration decl) { + ResolvedType type = decl.getType(); + return new Value(type, decl.getName()); + } + + @Override + public String toString() { + return "Value{" + + "typeUsage=" + type + + ", name='" + name + '\'' + + '}'; + } + + public String getName() { + return name; + } + + public ResolvedType getType() { + return type; + } + +} diff --git a/javaparser-symbol-solver-model/src/main/java/com/github/javaparser/symbolsolver/model/typesystem/NullType.java b/javaparser-symbol-solver-model/src/main/java/com/github/javaparser/symbolsolver/model/typesystem/NullType.java new file mode 100644 index 000000000..4fccfaad1 --- /dev/null +++ b/javaparser-symbol-solver-model/src/main/java/com/github/javaparser/symbolsolver/model/typesystem/NullType.java @@ -0,0 +1,68 @@ +/* + * 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.typesystem; + +import com.github.javaparser.resolution.types.ResolvedType; + +/** + * This is a virtual type used to represent null values. + * + * @author Federico Tomassetti + */ +public class NullType implements ResolvedType { + + public static final NullType INSTANCE = new NullType(); + + private NullType() { + // prevent instantiation + } + + @Override + public boolean isArray() { + return false; + } + + @Override + public boolean isPrimitive() { + return false; + } + + public boolean isNull() { + return true; + } + + @Override + public boolean isReferenceType() { + return false; + } + + @Override + public String describe() { + return "null"; + } + + @Override + public boolean isTypeVariable() { + return false; + } + + @Override + public boolean isAssignableBy(ResolvedType other) { + throw new UnsupportedOperationException("It does not make sense to assign a value to null, it can only be assigned"); + } + +} |