Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class MethodInfo extends MemberInfo implements AccessFlags {
/**
* Cached return type.
*/
private String returnType;
private String fullyQualifiedReturnType;

/**
* Cached string representing the name and parameters for this method.
Expand Down Expand Up @@ -138,7 +138,7 @@ private void appendParamDescriptors(StringBuilder sb) {
*/
void clearParamTypeInfo() {
paramTypes = null;
returnType = null;
fullyQualifiedReturnType = null;
}


Expand Down Expand Up @@ -437,18 +437,18 @@ public String[] getParameterTypes() {
* @return The return type of this method.
*/
public String getReturnTypeString(boolean fullyQualified) {
if (returnType==null) {
returnType = getReturnTypeStringFromTypeSignature(fullyQualified);
if (returnType==null) {
returnType = getReturnTypeStringFromDescriptor(fullyQualified);
if (fullyQualifiedReturnType == null) {
fullyQualifiedReturnType = getReturnTypeStringFromTypeSignature(true);
if (fullyQualifiedReturnType == null) {
fullyQualifiedReturnType = getReturnTypeStringFromDescriptor(true);
}
}
if (!fullyQualified) {
if (returnType != null && returnType.contains(".")) {
return returnType.substring(returnType.lastIndexOf(".") +1);
if (fullyQualifiedReturnType != null && fullyQualifiedReturnType.contains(".")) {
return fullyQualifiedReturnType.substring(fullyQualifiedReturnType.lastIndexOf(".") +1);
}
}
return returnType;
return fullyQualifiedReturnType;
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* This library is distributed under a modified BSD license. See the included
* LICENSE.md file for details.
*/
package org.fife.rsta.ac.java.classreader;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doReturn;

/**
* Unit tests for the {@code MethodInfo} class.
*/
class MethodInfoTest {

@Test
void testGetReturnTypeString_fullyQualified_true() {
// A method taking a String and an int as inputs that returns an Object
String methodDescriptor = "(Ljava/lang/String;I)Ljava/lang/Object;";

ClassFile cf = Mockito.mock(ClassFile.class);
doReturn(methodDescriptor).when(cf).getUtf8ValueFromConstantPool(anyInt());

MethodInfo mi = new MethodInfo(cf, 0, 0, 0);
assertEquals("java.lang.Object", mi.getReturnTypeString(true));
}

@Test
void testGetReturnTypeString_fullyQualified_false() {
// A method taking a String and an int as inputs that returns an Object
String methodDescriptor = "(Ljava/lang/String;I)Ljava/lang/Object;";

ClassFile cf = Mockito.mock(ClassFile.class);
doReturn(methodDescriptor).when(cf).getUtf8ValueFromConstantPool(anyInt());

MethodInfo mi = new MethodInfo(cf, 0, 0, 0);
assertEquals("Object", mi.getReturnTypeString(false));
}

// Verifies fix for https://github.com/bobbylight/RSTALanguageSupport/issues/68
@Test
void testGetReturnTypeString_fullyQualified_falseThenTrue() {
// A method taking a String and an int as inputs that returns an Object
String methodDescriptor = "(Ljava/lang/String;I)Ljava/lang/Object;";

ClassFile cf = Mockito.mock(ClassFile.class);
doReturn(methodDescriptor).when(cf).getUtf8ValueFromConstantPool(anyInt());

MethodInfo mi = new MethodInfo(cf, 0, 0, 0);

// First call returns unqualified, second returns qualified
assertEquals("Object", mi.getReturnTypeString(false));
assertEquals("java.lang.Object", mi.getReturnTypeString(true));
}
}
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ subprojects {
testImplementation platform('org.junit:junit-bom:5.13.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
testImplementation 'org.mockito:mockito-core:5.15.2'
}

compileJava {
Expand Down