Type capabilities introduced to facilitate extensibility of the type representation

This approach: returning adapter objects by class is crucial, because it is delegation-proof.
Simply extending interfaces in particular implementations fails when we wrap types in DelegatedType subclasses
This commit is contained in:
Andrey Breslav
2014-09-24 18:02:40 +04:00
parent 6564231040
commit 4b24c96cca
6 changed files with 60 additions and 0 deletions
@@ -16,12 +16,24 @@
package org.jetbrains.jet.lang.types;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
import java.util.Iterator;
import java.util.List;
public abstract class AbstractJetType extends InflexibleType {
@Nullable
@Override
public <T extends TypeCapability> T getCapability(@NotNull Class<T> capabilityClass) {
if (capabilityClass.isInstance(this)) {
//noinspection unchecked
return (T) this;
}
return null;
}
@Override
public final int hashCode() {
int result = getConstructor().hashCode();
@@ -17,6 +17,7 @@
package org.jetbrains.jet.lang.types;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.annotations.Annotations;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
@@ -76,6 +77,16 @@ public abstract class DelegatingType implements JetType {
return getDelegate().getAnnotations();
}
@Override
@Nullable
public <T extends TypeCapability> T getCapability(@NotNull Class<T> capabilityClass) {
if (capabilityClass.isInstance(this)) {
//noinspection unchecked
return (T) this;
}
return getDelegate().getCapability(capabilityClass);
}
@Override
public int hashCode() {
return getDelegate().hashCode();
@@ -421,6 +421,12 @@ public class ErrorUtils {
return Annotations.EMPTY;
}
@Nullable
@Override
public <T extends TypeCapability> T getCapability(@NotNull Class<T> capabilityClass) {
return null;
}
@Override
public String toString() {
return constructor.toString();
@@ -17,6 +17,7 @@
package org.jetbrains.jet.lang.types;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.ReadOnly;
import org.jetbrains.jet.lang.descriptors.annotations.Annotated;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
@@ -53,4 +54,7 @@ public interface JetType extends Annotated {
// isFlexible() == false <=> getLowerBound() == getUpperBound()
boolean isFlexible();
@Nullable
<T extends TypeCapability> T getCapability(@NotNull Class<T> capabilityClass);
}
@@ -0,0 +1,21 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* 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 org.jetbrains.jet.lang.types
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor
public trait TypeCapability
@@ -85,6 +85,12 @@ public class TypeUtils {
throw new IllegalStateException(name);
}
@Nullable
@Override
public <T extends TypeCapability> T getCapability(@NotNull Class<T> capabilityClass) {
return null;
}
@Override
public String toString() {
return name;