2 Commits

Author SHA1 Message Date
Ruben
d5d562cee1 Merge remote-tracking branch 'origin/main' 2025-01-08 16:37:05 +01:00
Ruben
a236add9f6 feat: add TypeHints for Parameters 2025-01-08 16:36:54 +01:00
4 changed files with 136 additions and 17 deletions

View File

@@ -18,7 +18,7 @@ public class LanguageServerInterface {
* */ * */
public LanguageServerTransferObject getResultSetAndAbstractSyntax(String input) throws IOException, ClassNotFoundException { public LanguageServerTransferObject getResultSetAndAbstractSyntax(String input) throws IOException, ClassNotFoundException {
System.setOut(new PrintStream(OutputStream.nullOutputStream())); //System.setOut(new PrintStream(OutputStream.nullOutputStream()));
File tempSourcefile = File.createTempFile("temp", ".java"); File tempSourcefile = File.createTempFile("temp", ".java");
tempSourcefile.deleteOnExit(); tempSourcefile.deleteOnExit();
@@ -28,7 +28,7 @@ public class LanguageServerInterface {
JavaTXCompiler tx = new JavaTXCompiler(tempSourcefile); JavaTXCompiler tx = new JavaTXCompiler(tempSourcefile);
var test = tx.getResultSetAndAbstractSyntax(tempSourcefile); var test = tx.getResultSetAndAbstractSyntax(tempSourcefile);
System.setOut(System.out); //System.setOut(System.out);
return test; return test;
} }
} }

View File

@@ -3,6 +3,8 @@ package de.dhbw.helper;
import de.dhbw.compiler.languageServerInterface.LanguageServerInterface; import de.dhbw.compiler.languageServerInterface.LanguageServerInterface;
import de.dhbw.compiler.languageServerInterface.LanguageServerTransferObject; import de.dhbw.compiler.languageServerInterface.LanguageServerTransferObject;
import de.dhbw.model.MethodNameWithType; import de.dhbw.model.MethodNameWithType;
import de.dhbw.model.ParameterNameWithType;
import org.checkerframework.checker.units.qual.A;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
@@ -52,9 +54,11 @@ public class TypeFinder {
if (el2.getRight().toString().toLowerCase().contains("TPH")) { if (el2.getRight().toString().toLowerCase().contains("TPH")) {
queue.add(el2.getRight().toString()); queue.add(el2.getRight().toString());
} else { } else {
if (!finalTypes.contains(el2.getRight().toString())) {
finalTypes.add(el2.getRight().toString()); finalTypes.add(el2.getRight().toString());
} }
} }
}
})); }));
} }
@@ -77,13 +81,13 @@ public class TypeFinder {
if (method.getReturnType().toString().contains("TPH ")) { if (method.getReturnType().toString().contains("TPH ")) {
StringBuilder possibleTypes = new StringBuilder(); StringBuilder possibleTypes = new StringBuilder();
for(var type : typeHashMap.get(method.getReturnType().toString())){ for (var type : typeHashMap.get(method.getReturnType().toString())) {
possibleTypes.append(" | ").append(type); possibleTypes.append(" | ").append(type);
} }
return possibleTypes.toString().substring(3); return possibleTypes.toString().substring(3);
}else{ } else {
return method.getReturnType().toString(); return method.getReturnType().toString();
} }
@@ -93,6 +97,34 @@ public class TypeFinder {
return "java.lang.Object"; return "java.lang.Object";
} }
public ArrayList<String> findTypeOfParameter(LanguageServerTransferObject transferObj, String methodName) throws IOException, ClassNotFoundException {
var typeHashMap = findAvailableTypes(transferObj);
ArrayList<String> paramTypes = new ArrayList<>();
for (var method : transferObj.getAst().getAllMethods()) {
if (method.name.equals(methodName)) {
for (var param : method.getParameterList()) {
if (param.getType().toString().contains("TPH ")) {
StringBuilder possibleTypes = new StringBuilder();
try {
for (var type : typeHashMap.get(param.getType().toString())) {
possibleTypes.append(" | ").append(type);
}
paramTypes.add(possibleTypes.toString().substring(3));
}catch (Exception e) {
paramTypes.add("java.lang.Object");
}
} else {
paramTypes.add(method.getReturnType().toString());
}
}
}
}
return paramTypes;
}
public ArrayList<MethodNameWithType> infereMethodType(String input) throws IOException, ClassNotFoundException { public ArrayList<MethodNameWithType> infereMethodType(String input) throws IOException, ClassNotFoundException {
@@ -101,6 +133,7 @@ public class TypeFinder {
ArrayList<MethodNameWithType> methodNameWithTypeList = new ArrayList<>(); ArrayList<MethodNameWithType> methodNameWithTypeList = new ArrayList<>();
for (var method : transferObj.getAst().getAllMethods()) { for (var method : transferObj.getAst().getAllMethods()) {
var type = findTypeOfMethod(transferObj, method.name); var type = findTypeOfMethod(transferObj, method.name);
methodNameWithTypeList.add(new MethodNameWithType(method.name, type, method.getOffset().getLine(), method.getOffset().getStartIndex())); methodNameWithTypeList.add(new MethodNameWithType(method.name, type, method.getOffset().getLine(), method.getOffset().getStartIndex()));
} }
@@ -108,4 +141,27 @@ public class TypeFinder {
return methodNameWithTypeList; return methodNameWithTypeList;
} }
public ArrayList<ParameterNameWithType> infereParameterType(String input) throws IOException, ClassNotFoundException {
var transferObj = languageServer.getResultSetAndAbstractSyntax(input);
System.out.println(transferObj.getResultSets().toString());
System.out.println(transferObj.getPrintedAst());
//Für RefTypes und so dann entsprechend InstanceOf durch den Syntaxbaum denke ich
ArrayList<ParameterNameWithType> ParameterNameWithType = new ArrayList<>();
for (var method : transferObj.getAst().getAllMethods()) {
int index = 0;
for (var param : method.getParameterList()) {
var type = findTypeOfParameter(transferObj, method.name);
ParameterNameWithType.add(new ParameterNameWithType(method.name, type.get(index), param.getOffset().getLine(), param.getOffset().getStartIndex()));
index++;
}
}
return ParameterNameWithType;
}
} }

View File

@@ -0,0 +1,43 @@
package de.dhbw.model;
public class ParameterNameWithType {
String name;
String type;
int line;
int charPosition;
public ParameterNameWithType(String name, String type, int line, int charPosition) {
this.name = name;
this.type = type;
this.line = line;
this.charPosition = charPosition;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getLine() {
return line;
}
public void setLine(int line) {
this.line = line;
}
public int getCharPosition() {
return charPosition;
}
public void setCharPosition(int charPosition) {
this.charPosition = charPosition;
}
}

View File

@@ -29,16 +29,36 @@ public class CompilerInterfaceTest {
public void testTypeFinder() throws IOException, ClassNotFoundException { public void testTypeFinder() throws IOException, ClassNotFoundException {
TypeFinder typeFinder = new TypeFinder(); TypeFinder typeFinder = new TypeFinder();
var inferedMethods = typeFinder.infereMethodType("import java.lang.Integer; import java.lang.String;" + var inferedMethods = typeFinder.infereMethodType("import java.lang.Integer; import java.lang.String;" +
"public class test{"+ "public class test{" +
"public main(test){" + "public main(test){" +
"if(0>1){" + "if(0>1){" +
"return \"w\";" + "return \"w\";" +
"}"+ "}" +
"Integer i = 0;"+ "Integer i = 0;" +
"return i;"+ "return i;" +
"}"+ "}" +
"}" "}"
); );
inferedMethods.forEach(el -> System.out.println(el.getName() + ": " + el.getType()));
}
@Test
public void testTypeFinderParameter() throws IOException, ClassNotFoundException {
TypeFinder typeFinder = new TypeFinder();
var inferedMethods = typeFinder.infereParameterType("import java.lang.Integer; import java.lang.String;" +
"public class test{" +
"public main(test, test2){" +
"if(0>1){" +
"return \"w\";" +
"}" +
"Integer t = test;" +
"String i = \"test\";" +
"return i;" +
"}" +
"}"
);
inferedMethods.forEach(el -> System.out.println(el.getName() + ": " + el.getType())); inferedMethods.forEach(el -> System.out.println(el.getName() + ": " + el.getType()));