Test to ensure resolved types in JavaData

This commit is contained in:
aboyko
2023-03-31 11:47:29 -04:00
parent 8067e384ea
commit 81ce79345d
5 changed files with 157 additions and 12 deletions

View File

@@ -12,6 +12,7 @@ package org.springframework.tooling.jdt.ls.commons.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.eclipse.core.resources.IProject;
import org.eclipse.jdt.core.IJavaElement;
@@ -21,8 +22,10 @@ import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.springframework.ide.vscode.commons.protocol.java.JavaTypeData.JavaTypeKind;
import org.springframework.ide.vscode.commons.protocol.java.TypeData;
import org.springframework.ide.vscode.commons.protocol.java.TypeData.FieldData;
import org.springframework.ide.vscode.commons.protocol.java.TypeData.MethodData;
import org.springframework.tooling.jdt.ls.commons.Logger;
import org.springframework.tooling.jdt.ls.commons.java.JavaData;
@@ -106,4 +109,61 @@ public class JavaDataTest {
assertEquals("roles", f.getName());
assertEquals("Ljava/util/List<Ljava/lang/String;>;", f.getType().getName());
}
@Test public void resolvedTypeForMembers() throws Exception {
IProject project = TestUtils.createTestProject("java-data", tmp);
TypeData d = javaData.typeData(project.getLocationURI().toASCIIString(), "Lcom/java/data/ExampleProperties$SomeProperties;", false);
assertNotNull(d);
assertEquals(2, d.getFields().size());
assertEquals(4, d.getMethods().size());
FieldData field = d.getFields().get(0);
assertEquals("enumValue", field.getName());
assertEquals(JavaTypeKind.CLASS, field.getType().getKind());
assertEquals("Lcom/java/data/C$E;", field.getType().getName());
TypeData enumData = javaData.typeData(project.getLocationURI().toASCIIString(), "Lcom/java/data/C$E;", false);
assertNotNull(enumData);
assertTrue(enumData.isEnum());
assertEquals(2, enumData.getFields().size());
field = d.getFields().get(1);
assertEquals("listOfEnums", field.getName());
assertEquals(JavaTypeKind.PARAMETERIZED, field.getType().getKind());
assertEquals("Ljava/util/Set<Lcom/java/data/C$E;>;", field.getType().getName());
MethodData m = d.getMethods().get(2);
assertEquals("getListOfEnums", m.getName());
assertEquals(JavaTypeKind.PARAMETERIZED, m.getReturnType().getKind());
assertEquals("Ljava/util/Set<Lcom/java/data/C$E;>;", m.getReturnType().getName());
d = javaData.typeData(project.getLocationURI().toASCIIString(), "Lcom/java/data/ExampleProperties;", false);
assertNotNull(d);
assertEquals(5, d.getFields().size());
field = d.getFields().get(0);
assertEquals("PREFIX", field.getName());
assertEquals(JavaTypeKind.CLASS, field.getType().getKind());
assertEquals("Ljava/lang/String;", field.getType().getName());
field = d.getFields().get(1);
assertEquals("enumValue", field.getName());
assertEquals(JavaTypeKind.CLASS, field.getType().getKind());
assertEquals("Lcom/java/data/C$E;", field.getType().getName());
field = d.getFields().get(2);
assertEquals("listOfEnums", field.getName());
assertEquals(JavaTypeKind.PARAMETERIZED, field.getType().getKind());
assertEquals("Ljava/util/Set<Lcom/java/data/C$E;>;", field.getType().getName());
field = d.getFields().get(3);
assertEquals("listProperties", field.getName());
assertEquals(JavaTypeKind.PARAMETERIZED, field.getType().getKind());
assertEquals("Ljava/util/Set<Lcom/java/data/ExampleProperties$SomeProperties;>;", field.getType().getName());
field = d.getFields().get(4);
assertEquals("mapProperties", field.getName());
assertEquals(JavaTypeKind.PARAMETERIZED, field.getType().getKind());
assertEquals("Ljava/util/Map<Ljava/lang/String;Lcom/java/data/ExampleProperties$SomeProperties;>;", field.getType().getName());
}
}

View File

@@ -27,6 +27,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>

View File

@@ -0,0 +1,17 @@
package com.java.data;
public class C {
public enum E {
/**
* Javadoc for YES here
*/
YES,
/**
* Javadoc for NO here
*/
NO
}
}

View File

@@ -0,0 +1,72 @@
package com.java.data;
import java.util.Map;
import java.util.Set;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
@ConfigurationProperties(prefix = ExampleProperties.PREFIX)
@Validated
public class ExampleProperties {
public static final String PREFIX = "app";
public C.E enumValue;
private Set<C.E> listOfEnums;
private Set<SomeProperties> listProperties;
private Map<String, SomeProperties> mapProperties;
public C.E getEnumValue() {
return enumValue;
}
public void setEnumValue(C.E enumValue) {
this.enumValue = enumValue;
}
public Set<C.E> getListOfEnums() {
return listOfEnums;
}
public void setListOfEnums(Set<C.E> listOfEnums) {
this.listOfEnums = listOfEnums;
}
public Set<SomeProperties> getListProperties() {
return listProperties;
}
public void setListProperties(Set<SomeProperties> listProperties) {
this.listProperties = listProperties;
}
public Map<String, SomeProperties> getMapProperties() {
return mapProperties;
}
public void setMapProperties(Map<String, SomeProperties> mapProperties) {
this.mapProperties = mapProperties;
}
public static class SomeProperties {
private C.E enumValue;
private Set<C.E> listOfEnums;
public C.E getEnumValue() {
return enumValue;
}
public void setEnumValue(C.E enumValue) {
this.enumValue = enumValue;
}
public Set<C.E> getListOfEnums() {
return listOfEnums;
}
public void setListOfEnums(Set<C.E> listOfEnums) {
this.listOfEnums = listOfEnums;
}
}
}

View File

@@ -486,21 +486,12 @@ public class JavaData {
if (type != null) {
// Attempt to resolve type. For some reason JDT has them unresolved for type members
try {
String[][] resolved = type.resolveType(signature.substring(1, signature.length() - 1));
if (resolved == null) {
String signatureSimpleName = Signature.getSignatureSimpleName(signature);
String resolvedType = resolveFQName(type, signatureSimpleName);
if (resolvedType != null) {
data.setKind(JavaTypeKind.CLASS);
nameToSet = "L" + resolvedType + ";";
break;
}
} else {
String resolvedType = resolveFQName(type, signature.substring(1, signature.length() - 1));
if (resolvedType != null) {
data.setKind(JavaTypeKind.CLASS);
nameToSet = "L" + resolved[0][0] + '.' + resolved[0][1] + ";";
nameToSet = "L" + resolvedType + ";";
break;
}
} catch (JavaModelException e) {
data.setKind(JavaTypeKind.UNRESOLVED);
}