Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ClassValueCompat to support systems without java.lang.ClassValue
- Loading branch information
Showing
5 changed files
with
73 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Scala (https://www.scala-lang.org) | ||
* | ||
* Copyright EPFL and Lightbend, Inc. | ||
* | ||
* Licensed under Apache License 2.0 | ||
* (http://www.apache.org/licenses/LICENSE-2.0). | ||
* | ||
* See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
*/ | ||
|
||
package scala.runtime; | ||
|
||
public abstract class ClassValueCompat<T> { | ||
|
||
private static final boolean classValueAvailable = checkClassValueAvailability(); | ||
|
||
private static boolean checkClassValueAvailability() { | ||
try { | ||
Class.forName("java.lang.ClassValue", false, ClassValueCompat.class.getClassLoader()); | ||
return true; | ||
} catch (ClassNotFoundException e) { | ||
return false; | ||
} | ||
} | ||
|
||
private final ClassValueInterface<T> instance; | ||
|
||
protected ClassValueCompat() { | ||
instance = classValueAvailable ? new JavaClassValue() : new FallbackClassValue(); | ||
} | ||
|
||
private class JavaClassValue extends ClassValue<T> implements ClassValueInterface<T> { | ||
@Override | ||
protected T computeValue(Class<?> aClass) { | ||
return ClassValueCompat.this.computeValue(aClass); | ||
} | ||
} | ||
|
||
private class FallbackClassValue implements ClassValueInterface<T> { | ||
@Override | ||
public T get(Class<?> type) { | ||
return ClassValueCompat.this.computeValue(type); | ||
} | ||
|
||
@Override | ||
public void remove(Class<?> type) {} | ||
} | ||
|
||
protected abstract T computeValue(Class<?> type); | ||
|
||
public T get(Class<?> type) { | ||
return instance.get(type); | ||
} | ||
|
||
public void remove(Class<?> type) { | ||
instance.remove(type); | ||
} | ||
|
||
public interface ClassValueInterface<T> { | ||
T get(Class<?> param1Class); | ||
|
||
void remove(Class<?> param1Class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters