Skip to content

Commit

Permalink
Sync with upstream branch
Browse files Browse the repository at this point in the history
  • Loading branch information
warunalakshitha committed Oct 20, 2023
2 parents 3e5e0c8 + a015cb9 commit e1b4173
Show file tree
Hide file tree
Showing 54 changed files with 836 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
import java.util.zip.ZipInputStream;

import static io.ballerina.runtime.profiler.util.Constants.CPU_PRE_JSON;
import static io.ballerina.runtime.profiler.util.Constants.CURRENT_DIR_KEY;
import static io.ballerina.runtime.profiler.util.Constants.OUT_STREAM;
import static io.ballerina.runtime.profiler.util.Constants.PERFORMANCE_JSON;

/**
* This class is used to as the driver class of the Ballerina profiler.
Expand All @@ -67,10 +69,12 @@ public class Profiler {
private int balFunctionCount = 0;
private int moduleCount = 0;
private final ProfilerMethodWrapper profilerMethodWrapper;
private final String currentDir;

public Profiler(long profilerStartTime) {
this.profilerStartTime = profilerStartTime;
this.profilerMethodWrapper = new ProfilerMethodWrapper();
this.currentDir = System.getenv(CURRENT_DIR_KEY);
}

private void addShutdownHookAndCleanup() {
Expand All @@ -83,11 +87,12 @@ private void addShutdownHookAndCleanup() {
OUT_STREAM.printf("%s[6/6] Generating output...%s%n", Constants.ANSI_CYAN, Constants.ANSI_RESET);
JsonParser jsonParser = new JsonParser();
HttpServer httpServer = new HttpServer();
jsonParser.initializeCPUParser();
deleteFileIfExists(CPU_PRE_JSON);
String cpuFilePath = Paths.get(currentDir, CPU_PRE_JSON).toString();
jsonParser.initializeCPUParser(cpuFilePath);
deleteFileIfExists(cpuFilePath);
OUT_STREAM.printf(" ○ Execution time: %d seconds %n", profilerTotalTime / 1000);
httpServer.initializeHTMLExport(this.sourceRoot);
deleteFileIfExists("performance_report.json");
deleteFileIfExists(PERFORMANCE_JSON);
OUT_STREAM.println("--------------------------------------------------------------------------------");
} catch (IOException e) {
throw new ProfilerException("Error occurred while generating the output", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import static io.ballerina.runtime.profiler.util.Constants.CURRENT_DIR_KEY;
import static io.ballerina.runtime.profiler.util.Constants.ERROR_STREAM;
import static io.ballerina.runtime.profiler.util.Constants.OUT_STREAM;
import static io.ballerina.runtime.profiler.util.Constants.USER_DIR;

/**
* This class is used as the method wrapper for the Ballerina profiler.
Expand All @@ -57,12 +60,13 @@ public void invokeMethods(String debugArg) throws IOException, InterruptedExcept
if (debugArg != null) {
commands.add(debugArg);
}
commands.add(Constants.TEMP_JAR_FILE_NAME);
commands.add(Paths.get(System.getProperty(USER_DIR), Constants.TEMP_JAR_FILE_NAME).toString());
if (balJarArgs != null) {
commands.add(balJarArgs);
}
ProcessBuilder processBuilder = new ProcessBuilder(commands);
processBuilder.inheritIO();
processBuilder.directory(new File(System.getenv(CURRENT_DIR_KEY)));
Process process = processBuilder.start();
OUT_STREAM.printf(Constants.ANSI_CYAN + "[5/6] Running executable..." + Constants.ANSI_RESET + "%n");
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.nio.charset.StandardCharsets;

import static io.ballerina.runtime.profiler.util.Constants.OUT_STREAM;
import static io.ballerina.runtime.profiler.util.Constants.PERFORMANCE_JSON;

/**
* This class contains the HTTP server of the Ballerina profiler.
Expand All @@ -36,7 +37,7 @@ public class HttpServer {
public void initializeHTMLExport(String sourceRoot) throws IOException {
OUT_STREAM.printf(" ○ Output: " + Constants.ANSI_YELLOW +
"%s/ProfilerOutput.html" + Constants.ANSI_RESET + "%n", sourceRoot);
String content = FileUtils.readFileAsString("performance_report.json");
String content = FileUtils.readFileAsString(PERFORMANCE_JSON);
FrontEnd frontEnd = new FrontEnd();
String htmlData = frontEnd.getSiteData(content);
String fileName = "ProfilerOutput.html";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import java.util.ArrayList;
import java.util.List;

import static io.ballerina.runtime.profiler.util.Constants.CPU_PRE_JSON;
import static io.ballerina.runtime.profiler.util.Constants.OUT_STREAM;
import static io.ballerina.runtime.profiler.util.Constants.PERFORMANCE_JSON;

/**
* This class contains the JSON parser of the Ballerina profiler.
Expand All @@ -41,9 +41,9 @@ public class JsonParser {

private static final String VALUE_KEY = "value";

public void initializeCPUParser() {
public void initializeCPUParser(String cpuFilePath) {
try {
String jsonInput = FileUtils.readFileAsString(CPU_PRE_JSON);
String jsonInput = FileUtils.readFileAsString(cpuFilePath);
List<StackTrace> input = populateStackTraceItems(jsonInput);
// Create a Data object to store the output
Data output = new Data("Root", input.get(0).time, new ArrayList<>());
Expand Down Expand Up @@ -71,7 +71,7 @@ private int getTotalTime(JsonObject node) {

private void writePerformanceJson(String parsedJson) {
parsedJson = "var data = " + parsedJson;
try (FileWriter myWriter = new FileWriter("performance_report.json", StandardCharsets.UTF_8)) {
try (FileWriter myWriter = new FileWriter(PERFORMANCE_JSON, StandardCharsets.UTF_8)) {
myWriter.write(parsedJson);
myWriter.flush();
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Constants {

public static final String CLASS_SUFFIX = ".class";
public static final String CPU_PRE_JSON = "cpu_pre.json";
public static final String PERFORMANCE_JSON = "performance_report.json";
public static final String TEMP_JAR_FILE_NAME = "temp.jar";
public static final String STRAND_PROFILER_STACK_PROPERTY = "b7a.profile.stack";

Expand All @@ -31,6 +32,8 @@ public class Constants {
"(L" + STRAND_CLASS + ";L" + DATA_CLASS + ";)V";
public static final String PROFILE_ANALYZER = "io/ballerina/runtime/profiler/runtime/ProfileAnalyzer";
public static final String GET_INSTANCE_DESCRIPTOR = "()L" + PROFILE_ANALYZER + ";";
public static final String CURRENT_DIR_KEY = "current.dir";
public static final String USER_DIR = "user.dir";

private Constants() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
public class RunProfilerTask implements Task {
private final PrintStream err;
private static final String JAVA_OPTS = "JAVA_OPTS";
private static final String CURRENT_DIR_KEY = "current.dir";

public RunProfilerTask(PrintStream errStream) {
this.err = errStream;
Expand Down Expand Up @@ -84,6 +85,7 @@ private void initiateProfiler(Project project) {
}
ProcessBuilder pb = new ProcessBuilder(commands).inheritIO();
pb.environment().put(JAVA_OPTS, getAgentArgs());
pb.environment().put(CURRENT_DIR_KEY, System.getProperty(USER_DIR));
setWorkingDirectory(project, projectKind, pb);
Process process = pb.start();
process.waitFor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public void testRunBalProjectWithProfileFlag() throws IOException {
Assert.assertTrue(htmlPath.toFile().exists());
try {
String htmlContent = Files.readString(htmlPath);
Assert.assertTrue(htmlContent.contains("foo/package_a/0/main.main()"));
Assert.assertTrue(htmlContent.contains("foo/package_a/0/$_init.$moduleExecute()"));
Assert.assertTrue(htmlContent.contains("foo/package_a/0/main.main"));
Assert.assertTrue(htmlContent.contains("foo/package_a/0/$_init.$moduleInit"));
} catch (IOException e) {
Assert.fail("Error reading html file");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,8 @@ public enum DiagnosticErrorCode implements DiagnosticCode {
INVALID_QUERY_CONSTRUCT_TYPE("BCE4055", "invalid.error.query.construct.type"),
CANNOT_USE_ALTERNATE_WAIT_ACTION_WITHIN_MULTIPLE_WAIT_ACTION("BCE4056",
"cannot.use.alternate.wait.action.within.multiple.wait.action"),
EXPRESSION_OF_FUTURE_TYPE_EXPECTED("BCE4057", "future.expression.expected")
EXPRESSION_OF_FUTURE_TYPE_EXPECTED("BCE4057", "future.expression.expected"),
INSTANTIATION_ERROR("BCE4058", "instantiation.error")
;

private String diagnosticId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
Expand Down Expand Up @@ -897,6 +898,10 @@ private Executable resolveMethod(Class<?> clazz, String name, Class<?>... paramT
private List<Executable> getExecutables(Class<?> clazz, String methodName, JMethodKind kind) {

if (kind == JMethodKind.CONSTRUCTOR) {
if (Modifier.isAbstract(clazz.getModifiers())) {
throw new JInteropException(DiagnosticErrorCode.INSTANTIATION_ERROR,
"'" + clazz.getName() + "' is abstract, and cannot be instantiated");
}
return Arrays.asList(getConstructors(clazz));
} else {
List<Executable> list = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,9 @@ error.unsupported.primitive.type.reason=\
error.method.signature.not.match=\
'{ballerina/jballerina.java}'METHOD_SIGNATURE_DOES_NOT_MATCH ''{0}''

error.instantiation.error=\
'{ballerina/jballerina.java}'INSTANTIATION_ERROR ''{0}''

error.invalid.attribute.reference=\
invalid attribute reference

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
"leadingMinutiae": [
{
"kind": "WHITESPACE_MINUTIAE",
"value": " "
"value": " "
}
],
"trailingMinutiae": [
Expand Down Expand Up @@ -203,7 +203,7 @@
"leadingMinutiae": [
{
"kind": "WHITESPACE_MINUTIAE",
"value": " "
"value": " "
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
public function main() {
foo(object {
int i = 1;
});
int i = 1;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

import io.ballerina.compiler.api.SemanticModel;
import io.ballerina.compiler.api.symbols.Symbol;
import io.ballerina.compiler.syntax.tree.ModulePartNode;
import io.ballerina.compiler.syntax.tree.NonTerminalNode;
import io.ballerina.compiler.syntax.tree.SyntaxKind;
import io.ballerina.projects.Document;
import io.ballerina.projects.Module;
import io.ballerina.projects.Project;
import io.ballerina.tools.diagnostics.Location;
import io.ballerina.tools.text.LinePosition;
import io.ballerina.tools.text.TextRange;
import org.ballerinalang.langserver.common.utils.CommonUtil;
import org.ballerinalang.langserver.common.utils.PathUtil;
import org.ballerinalang.langserver.common.utils.PositionUtil;
Expand Down Expand Up @@ -120,7 +122,22 @@ public static Optional<Symbol> getSymbolAtCursor(PositionedOperationContext cont
return Optional.empty();
}

Document document = srcFile.get();
Position position = context.getCursorPosition();
TextRange range = TextRange.from(
document.textDocument().textPositionFrom(PositionUtil.getLinePosition(position)), 0);
NonTerminalNode nonTerminalNode = ((ModulePartNode) document.syntaxTree().rootNode()).findNode(range);
SyntaxKind parentKind = nonTerminalNode.parent().kind();

if (parentKind == SyntaxKind.TYPE_PARAMETER || parentKind == SyntaxKind.STREAM_TYPE_PARAMS) {
if (nonTerminalNode.lineRange().endLine().offset() == position.getCharacter()) {
// When there is a type parameter and cursor is at the end of the type parameter, semantic API does not
// provide the correct symbol. Therefore, here we search for the symbol at (col - 1).
return semanticModel.get().symbol(document,
LinePosition.from(position.getLine(), position.getCharacter() - 1));
}
}

Optional<Symbol> symbolAtCursor = semanticModel.get().symbol(srcFile.get(),
LinePosition.from(position.getLine(), position.getCharacter()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ protected Object[][] testDataProvider() throws IOException {
{"def_annotation_on_obj_func_config1.json", "project"},
{"def_typereference.json", "project"},
{"def_typereference2.json", "project"},
{"def_typereference3.json", "project"}
{"def_typereference3.json", "project"},
{"defProject15.json", "project"},
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private Object[][] testDataProvider() {
// Negative/invalid cases
{"rename_keyword_result1.json", "kw"},
{"rename_invalid_token_result1.json", "token"},
{"rename_table_row_type_and_stream_type.json", "Student"},
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ public Object[][] testDataProvider() {
{"rename_self.json", "this"},
{"rename_invalid_qname_ref.json", "io"},
// {"rename_resource_method_path_segment.json", "path1"} //TODO: Fix #41041

{"rename_table_row_type_parameter1.json", "Student"},
{"rename_table_row_type_parameter2.json", "Student"},
{"rename_table_row_type_parameter3.json", "Student"},
{"rename_stream_type_parameter.json", "Student"},
{"rename_map_type_parameter.json", "Student"},
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"source": {
"file": "projectls/defmodsource6.bal"
},
"position": {
"line": 3,
"character": 19
},
"result": [
{
"range": {
"start": {
"line": 9,
"character": 12
},
"end": {
"line": 9,
"character": 17
}
},
"uri": "projectls/modules/lsmod3/lsmod3.bal"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import projectls.lsmod3;

function foo() {
table<lsmod3:Human> tHuman = table [{id: 1, name: "Jane"}];
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ public function mod3Function1() {

public type MyType lsmod1:MyType;
public type MyType2 MyType;

public type Human record {|
readonly int id;
string name;
|};
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"source": {
"file": "main.bal"
},
"position": {
"line": 19,
"character": 23
},
"prepareRename": {
"valid": true
},
"result": {
"changes": {
"modules/module2/types.bal": [{
"range": {
"start": {
"line": 2,
"character": 12
},
"end": {
"line": 2,
"character": 17
}
},
"newText": "Student"
}],
"main.bal": [{
"range": {
"start": {
"line": 18,
"character": 19
},
"end": {
"line": 18,
"character": 24
}
},
"newText": "Student"
}, {
"range": {
"start": {
"line": 19,
"character": 18
},
"end": {
"line": 19,
"character": 23
}
},
"newText": "Student"
}]
}
}
}
Loading

0 comments on commit e1b4173

Please sign in to comment.