From 0d3d6e883d0315cb14c111ce3d588d571786199f Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Thu, 21 Apr 2016 22:29:40 +0300 Subject: [PATCH] Improving build diagnostics in IDEA. Fix minor problems in JSO --- .../org/teavm/jso/impl/JSClassProcessor.java | 20 ++++------- .../java/org/teavm/idea/jps/TeaVMBuild.java | 22 +++++++++++-- .../idea/jps/remote/TeaVMElementLocation.java | 8 ++++- .../org/teavm/idea/TeaVMJPSRemoteService.java | 33 +++++++++++-------- 4 files changed, 52 insertions(+), 31 deletions(-) diff --git a/jso/impl/src/main/java/org/teavm/jso/impl/JSClassProcessor.java b/jso/impl/src/main/java/org/teavm/jso/impl/JSClassProcessor.java index 5c70de17c..1d8379e78 100644 --- a/jso/impl/src/main/java/org/teavm/jso/impl/JSClassProcessor.java +++ b/jso/impl/src/main/java/org/teavm/jso/impl/JSClassProcessor.java @@ -329,11 +329,11 @@ class JSClassProcessor { } if (method.getProgram() != null && method.getProgram().basicBlockCount() > 0) { - MethodReader overriden = getOverridenMethod(method); - if (overriden != null) { + MethodReader overridden = getOverridenMethod(method); + if (overridden != null) { diagnostics.error(callLocation, "JS final method {{m0}} overrides {{m1}}. " + "Overriding final method of overlay types is prohibited.", - method.getReference(), overriden.getReference()); + method.getReference(), overridden.getReference()); } if (method.getProgram() != null && method.getProgram().basicBlockCount() > 0) { invoke.setMethod(new MethodReference(method.getOwnerName(), method.getName() + "$static", @@ -545,7 +545,7 @@ class JSClassProcessor { int jsParamCount = bodyAnnot.getValue("params").getList().size(); if (methodToProcess.parameterCount() != jsParamCount) { diagnostics.error(location, "JSBody method {{m0}} declares " + methodToProcess.parameterCount() - + " parameters, but annotation specifies " + jsParamCount, methodToProcess); + + " parameters, but annotation specifies " + jsParamCount, methodToProcess.getReference()); return; } @@ -554,11 +554,8 @@ class JSClassProcessor { if (!isStatic) { ++paramCount; } - ValueType[] paramTypes = new ValueType[paramCount]; - int offset = 0; if (!isStatic) { ValueType paramType = ValueType.object(methodToProcess.getOwnerName()); - paramTypes[offset++] = paramType; if (!typeHelper.isSupportedType(paramType)) { diagnostics.error(location, "Non-static JSBody method {{m0}} is owned by non-JS class {{c1}}", methodToProcess.getReference(), methodToProcess.getOwnerName()); @@ -571,9 +568,6 @@ class JSClassProcessor { } // generate parameter types for proxy method - for (int i = 0; i < methodToProcess.parameterCount(); ++i) { - paramTypes[offset++] = methodToProcess.parameterType(i); - } ValueType[] proxyParamTypes = new ValueType[paramCount + 1]; for (int i = 0; i < paramCount; ++i) { proxyParamTypes[i] = ValueType.parse(JSObject.class); @@ -587,7 +581,7 @@ class JSClassProcessor { methodToProcess.getName() + "$js_body$_" + methodIndexGenerator++, proxyParamTypes); String script = bodyAnnot.getValue("script").getString(); String[] parameterNames = bodyAnnot.getValue("params").getList().stream() - .map(ann -> ann.getString()) + .map(AnnotationValue::getString) .toArray(String[]::new); // Parse JS script @@ -598,15 +592,13 @@ class JSClassProcessor { env.setLanguageVersion(Context.VERSION_1_8); env.setIdeMode(true); JSParser parser = new JSParser(env, errorReporter); - //parser.enterFunction(); AstRoot rootNode; try { rootNode = parser.parse(new StringReader("function(){" + script + "}"), null, 0); } catch (IOException e) { - throw new RuntimeException("IO Error occured", e); + throw new RuntimeException("IO Error occurred", e); } AstNode body = ((FunctionNode) rootNode.getFirstChild()).getBody(); - //parser.exitFunction(); repository.methodMap.put(methodToProcess.getReference(), proxyMethod); if (errorReporter.hasErrors()) { diff --git a/tools/idea/jps-plugin/src/main/java/org/teavm/idea/jps/TeaVMBuild.java b/tools/idea/jps-plugin/src/main/java/org/teavm/idea/jps/TeaVMBuild.java index f2b61ed26..aa183f62a 100644 --- a/tools/idea/jps-plugin/src/main/java/org/teavm/idea/jps/TeaVMBuild.java +++ b/tools/idea/jps-plugin/src/main/java/org/teavm/idea/jps/TeaVMBuild.java @@ -26,6 +26,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -160,6 +161,17 @@ public class TeaVMBuild { line = location.getLine(); startOffset = location.getStartOffset(); endOffset = location.getEndOffset(); + file = new File(location.getPath()); + + if (line <= 0) { + int[] lines = getLineOffsets(file); + if (lines != null) { + line = Arrays.binarySearch(lines, (int) startOffset + 1); + if (line < 0) { + line = -line - 1; + } + } + } } catch (Exception e) { // just don't fill location fields } @@ -169,9 +181,13 @@ public class TeaVMBuild { DefaultProblemTextConsumer textConsumer = new DefaultProblemTextConsumer(); problem.render(textConsumer); - if (path != null) { - file = lookupSource(path); - path = file != null ? file.getPath() : null; + if (file == null) { + if (path != null) { + file = lookupSource(path); + path = file != null ? file.getPath() : null; + } + } else { + path = file.getPath(); } if (startOffset < 0 && file != null && line > 0) { diff --git a/tools/idea/jps-plugin/src/main/java/org/teavm/idea/jps/remote/TeaVMElementLocation.java b/tools/idea/jps-plugin/src/main/java/org/teavm/idea/jps/remote/TeaVMElementLocation.java index d4649e77d..2ef745a53 100644 --- a/tools/idea/jps-plugin/src/main/java/org/teavm/idea/jps/remote/TeaVMElementLocation.java +++ b/tools/idea/jps-plugin/src/main/java/org/teavm/idea/jps/remote/TeaVMElementLocation.java @@ -22,12 +22,14 @@ public class TeaVMElementLocation implements Serializable { private int endOffset; private int line; private int column; + private String path; - public TeaVMElementLocation(int startOffset, int endOffset, int line, int column) { + public TeaVMElementLocation(int startOffset, int endOffset, int line, int column, String path) { this.startOffset = startOffset; this.endOffset = endOffset; this.line = line; this.column = column; + this.path = path; } public int getStartOffset() { @@ -45,4 +47,8 @@ public class TeaVMElementLocation implements Serializable { public int getColumn() { return column; } + + public String getPath() { + return path; + } } diff --git a/tools/idea/src/main/java/org/teavm/idea/TeaVMJPSRemoteService.java b/tools/idea/src/main/java/org/teavm/idea/TeaVMJPSRemoteService.java index c13279dd1..38e0d72ff 100644 --- a/tools/idea/src/main/java/org/teavm/idea/TeaVMJPSRemoteService.java +++ b/tools/idea/src/main/java/org/teavm/idea/TeaVMJPSRemoteService.java @@ -15,6 +15,7 @@ */ package org.teavm.idea; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.components.ApplicationComponent; import com.intellij.openapi.project.Project; import com.intellij.openapi.project.ProjectManager; @@ -87,26 +88,32 @@ public class TeaVMJPSRemoteService extends UnicastRemoteObject implements Applic @Override public TeaVMElementLocation getMethodLocation(String className, String methodName, String methodDesc) throws RemoteException { - for (Project project : projectManager.getOpenProjects()) { - JavaPsiFacade psi = JavaPsiFacade.getInstance(project); - PsiClass cls = psi.findClass(className, GlobalSearchScope.allScope(project)); - if (cls == null) { - continue; - } + TeaVMElementLocation[] resultHolder = new TeaVMElementLocation[1]; - for (PsiMethod method : cls.getAllMethods()) { - if (!method.getName().equals(methodName)) { + ApplicationManager.getApplication().runReadAction(() -> { + for (Project project : projectManager.getOpenProjects()) { + JavaPsiFacade psi = JavaPsiFacade.getInstance(project); + PsiClass cls = psi.findClass(className, GlobalSearchScope.allScope(project)); + if (cls == null) { continue; } - // TODO: check method raw signature - return getMethodLocation(method); + + for (PsiMethod method : cls.getAllMethods()) { + if (!method.getName().equals(methodName)) { + continue; + } + // TODO: check method raw signature + resultHolder[0] = getMethodLocation(method); + return; + } } - } - return null; + }); + + return resultHolder[0]; } private TeaVMElementLocation getMethodLocation(PsiMethod method) { return new TeaVMElementLocation(method.getTextOffset(), method.getTextOffset() + method.getTextLength(), - -1, -1); + -1, -1, method.getContainingFile().getVirtualFile().getPath()); } }