Skip to content

Commit 7344ee0

Browse files
committed
Add the ObjectType enum.
Add a new enum to keep track of the types of LinkML objects we are dealing with (classes, scalars, enums). This replaces the `isClass` helper method previously used in ClassInfo. It is still not ideal but at least we have a centralized place to address the question of "what type of LinkML object is that Java type supposed to represent?".
1 parent 0de5846 commit 7344ee0

3 files changed

Lines changed: 159 additions & 22 deletions

File tree

core/src/main/java/org/incenp/linkml/core/ClassInfo.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@
3434

3535
package org.incenp.linkml.core;
3636

37-
import java.time.LocalDate;
38-
import java.time.LocalTime;
39-
import java.time.ZonedDateTime;
4037
import java.util.Collection;
4138
import java.util.HashMap;
4239
import java.util.Map;
@@ -351,7 +348,7 @@ public static ClassInfo get(Class<?> type) {
351348
// reflection, and even I do not do things _that_ dubious), so there should not
352349
// be any issue here.
353350
ClassInfo ci = cache.get(type);
354-
if ( ci == null && isClass(type) ) {
351+
if ( ci == null && ObjectType.get(type) == ObjectType.CLASS ) {
355352
ci = new ClassInfo(type);
356353
cache.put(type, ci);
357354
if ( ci.getURI() != null ) {
@@ -375,22 +372,4 @@ public static ClassInfo get(Class<?> type) {
375372
public static ClassInfo get(String uri) {
376373
return cacheByURI.get(uri);
377374
}
378-
379-
/**
380-
* Helper method to check that the given Java type can represent a LinkML class.
381-
*
382-
* @param type The Java type to check.
383-
* @return <code>true</code> if the type is possibly a LinkML class, otherwise
384-
* <code>false</code>.
385-
*/
386-
public static boolean isClass(Class<?> type) {
387-
// FIXME: At some point we will need a more sustainable way of recognising
388-
// classes that represent scalar types...
389-
if ( type.getSuperclass() == null || type.isEnum() || type == String.class || type == Boolean.class
390-
|| type == Integer.class || type == Float.class || type == Double.class
391-
|| type == ZonedDateTime.class || type == LocalDate.class || type == LocalTime.class ) {
392-
return false;
393-
}
394-
return true;
395-
}
396375
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* LinkML-Java - LinkML library for Java
3+
* Copyright © 2026 Damien Goutte-Gattat
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
*
9+
* (1) Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* (2) Redistributions in binary form must reproduce the above
13+
* copyright notice, this list of conditions and the following
14+
* disclaimer in the documentation and/or other materials provided
15+
* with the distribution.
16+
*
17+
* (3) Neither the name of the copyright holder nor the names its
18+
* contributors may be used to endorse or promote products derived
19+
* from this software without specific prior written permission.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
22+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25+
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
31+
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*/
34+
35+
package org.incenp.linkml.core;
36+
37+
import java.net.URI;
38+
import java.time.LocalDate;
39+
import java.time.LocalTime;
40+
import java.time.ZonedDateTime;
41+
42+
/**
43+
* Represents the type of a LinkML object.
44+
*/
45+
public enum ObjectType {
46+
/**
47+
* A LinkML class.
48+
*/
49+
CLASS,
50+
51+
/**
52+
* A LinkML enumeration.
53+
* <p>
54+
* This does not cover <em>dynamic</em> enumerations, which are currently
55+
* completely unsupported.
56+
*/
57+
ENUM,
58+
59+
/**
60+
* A scalar type (excluding enumerations).
61+
*/
62+
TYPE,
63+
64+
/**
65+
* Not a LinkML object.
66+
*/
67+
NONE;
68+
69+
/**
70+
* Gets the type of LinkML object that is represented by a Java type.
71+
*
72+
* @param type The Java type to query.
73+
* @return The corresponding LinkML type.
74+
*/
75+
public static ObjectType get(Class<?> type) {
76+
// FIXME: We might have to deal with custom types as well at some point.
77+
if ( type.isPrimitive() || type == String.class || type == Boolean.class || type == Integer.class
78+
|| type == Float.class || type == Double.class || type == ZonedDateTime.class || type == LocalDate.class
79+
|| type == LocalTime.class || type == URI.class ) {
80+
return TYPE;
81+
} else if ( type.isEnum() ) {
82+
// FIXME: Not necessarily a *LinkML* enum
83+
return ENUM;
84+
} else if ( type.getSuperclass() == null ) {
85+
// Could be Object, an interface, or void -- in any case, cannot be a LinkML
86+
// object at all
87+
return NONE;
88+
} else {
89+
// FIXME: Not necessarily a *LinkML* class
90+
return CLASS;
91+
}
92+
}
93+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* LinkML-Java - LinkML library for Java
3+
* Copyright © 2026 Damien Goutte-Gattat
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
*
9+
* (1) Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* (2) Redistributions in binary form must reproduce the above
13+
* copyright notice, this list of conditions and the following
14+
* disclaimer in the documentation and/or other materials provided
15+
* with the distribution.
16+
*
17+
* (3) Neither the name of the copyright holder nor the names its
18+
* contributors may be used to endorse or promote products derived
19+
* from this software without specific prior written permission.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
22+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25+
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
31+
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*/
34+
35+
package org.incenp.linkml.core;
36+
37+
import org.incenp.linkml.core.sample.SampleEnum;
38+
import org.incenp.linkml.core.sample.SimpleClass;
39+
import org.junit.jupiter.api.Assertions;
40+
import org.junit.jupiter.api.Test;
41+
42+
public class ObjectTypeTest {
43+
44+
@Test
45+
void testDetectScalars() {
46+
Assertions.assertEquals(ObjectType.TYPE, ObjectType.get(Boolean.class));
47+
Assertions.assertEquals(ObjectType.TYPE, ObjectType.get(Integer.valueOf(32).getClass()));
48+
}
49+
50+
@Test
51+
void testDetectEnums() {
52+
Assertions.assertEquals(ObjectType.ENUM, ObjectType.get(SampleEnum.class));
53+
}
54+
55+
@Test
56+
void testDetectClasses() {
57+
Assertions.assertEquals(ObjectType.CLASS, ObjectType.get(SimpleClass.class));
58+
}
59+
60+
@Test
61+
void testDetectNonLinkMLObjects() {
62+
Assertions.assertEquals(ObjectType.NONE, ObjectType.get(IConverter.class));
63+
Assertions.assertEquals(ObjectType.NONE, ObjectType.get(Object.class));
64+
}
65+
}

0 commit comments

Comments
 (0)