Skip to content

Commit 250e460

Browse files
committed
preliminary HttpClient
1 parent fd91ab3 commit 250e460

File tree

14 files changed

+573
-94
lines changed

14 files changed

+573
-94
lines changed
-1.07 KB
Binary file not shown.
137 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20200622135939
1+
20200623075216
-1.07 KB
Binary file not shown.
137 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20200622135939
1+
20200622145109

sources/net.sf.j2s.core/src/net/sf/j2s/core/CorePlugin.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class CorePlugin extends Plugin {
3131
// j2sApplet.js and also (Bob only) update.bat, update-clean.bat
3232

3333

34+
// BH 2020.06.22 -- 3.2.9.v1k fix for varargs not proper qualified arrays
3435
// BH 2020.06.17 -- 3.2.9-v1j fix for functional interface this::privateMethod
3536
// BH 2020.05.01 -- 3.2.9-v1i fix for nested lambda methods
3637
// BH 2020.04.26 -- 3.2.9-v1h fix for inner classes of interfaces duplicated; fix for api.js inner class method names unqualified

sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptVisitor.java

Lines changed: 18 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135

136136
// TODO: superclass inheritance for JAXB XmlAccessorType
137137

138+
//BH 2020.06.22 -- 3.2.9.v1k fix for varargs not proper qualified arrays
138139
//BH 2020.06.17 -- 3.2.9-v1j fix for functional interface this::privateMethod
139140
//BH 2020.05.01 -- 3.2.9-v1i fix for nested lambda methods
140141
//BH 2020.04.26 -- 3.2.9-v1h fix for inner classes of interfaces duplicated; fix for api.js inner class method names unqualified
@@ -2515,25 +2516,12 @@ && checkAnnotations(element, CHECK_J2S_IGNORE_AND_ANNOTATIONS)) {
25152516
// log("default method " + method.getKey());
25162517
defpt = buffer.length();
25172518
}
2518-
// boolean addUnqualifiedCurrent = temp_add$UnqualifiedMethod;
2519-
// if (unqualifiedMethods != null) {
2520-
// // check for all methods that override a functional interface abstract method,
2521-
// // as those methods are to be qualified only with $
2522-
//
2523-
// for (int i = unqualifiedMethods.size(); --i >= 0;) {
2524-
// if (method.overrides(unqualifiedMethods.get(i))) {
2525-
// temp_add$UnqualifiedMethod = true;
2526-
// break;
2527-
// }
2528-
// }
2529-
// }
25302519
processMethodDeclaration(mnode, method, mnode.parameters(), mnode.getBody(), mnode.isConstructor(),
25312520
abstractMethodList, NOT_LAMBDA);
25322521
if (defpt >= 0) {
25332522
defaults.append(buffer.substring(defpt));
25342523
buffer.setLength(defpt);
25352524
}
2536-
// temp_add$UnqualifiedMethod = addUnqualifiedCurrent;
25372525
} else if (element instanceof AnnotationTypeMemberDeclaration) {
25382526
processAnnotationTypeMemberDeclaration((AnnotationTypeMemberDeclaration) element);
25392527
}
@@ -2689,35 +2677,6 @@ private void processAnnotationTypeMemberDeclaration(AnnotationTypeMemberDeclarat
26892677
buffer.setLength(pt);
26902678
}
26912679

2692-
// /**
2693-
// * Collect all names of all functional interface abstract methods that this
2694-
// * class might refer to so that their unqualified. This is not perfect, as it is
2695-
// * possible to have implementations of specific subtypes of parameterized
2696-
// * methods. However, it will have to do for now.
2697-
// *
2698-
// * @param type
2699-
// * @param unqualifiedMethods
2700-
// * @return List of methods that should have raw unparameterized alias
2701-
// */
2702-
// private List<IMethodBinding> getUnqualifiedMethods(ITypeBinding type, List<IMethodBinding> unqualifiedMethods) {
2703-
// if (type.isArray() || type.isPrimitive()) {
2704-
// return unqualifiedMethods;
2705-
// }
2706-
// ITypeBinding superClass = type.getSuperclass();
2707-
// if (superClass != null)
2708-
// unqualifiedMethods = getUnqualifiedMethods(superClass, unqualifiedMethods);
2709-
// ITypeBinding[] superInterfaces = type.getInterfaces();
2710-
// for (int i = 0; i < superInterfaces.length; i++)
2711-
// unqualifiedMethods = getUnqualifiedMethods(superInterfaces[i], unqualifiedMethods);
2712-
// IMethodBinding functionalMethod = type.getFunctionalInterfaceMethod();
2713-
// if (functionalMethod != null) {
2714-
// if (unqualifiedMethods == null)
2715-
// unqualifiedMethods = new ArrayList<IMethodBinding>();
2716-
// unqualifiedMethods.add(functionalMethod);
2717-
// }
2718-
// return unqualifiedMethods;
2719-
// }
2720-
27212680
/**
27222681
* If there is no Foo() or Foo(xxx... array), then we need to provide our own
27232682
* constructor.
@@ -2906,7 +2865,7 @@ private void addMethodParameterList(List<?> arguments, IMethodBinding methodDecl
29062865
buffer.append(prefix);
29072866
prefix = null;
29082867
}
2909-
addMethodArguments(parameterTypes, methodIsVarArgs, arguments, flags);
2868+
addMethodArguments(methodDeclaration, parameterTypes, methodIsVarArgs, arguments, flags);
29102869
}
29112870
if (prefix == null && suffix != null)
29122871
buffer.append(suffix);
@@ -4388,13 +4347,14 @@ private void addLeftSidePrefixName(Expression left) {
43884347

43894348
/**
43904349
*
4350+
* @param methodDeclaration
43914351
* @param parameterTypes
43924352
* @param methodIsVarArgs
43934353
* @param arguments
43944354
* @param flags METHOD_INDEXOF | 0
43954355
*/
43964356
@SuppressWarnings("null")
4397-
private void addMethodArguments(ITypeBinding[] parameterTypes, boolean methodIsVarArgs, List<?> arguments,
4357+
private void addMethodArguments(IMethodBinding methodDeclaration, ITypeBinding[] parameterTypes, boolean methodIsVarArgs, List<?> arguments,
43984358
int flags) {
43994359
String post = ", ";
44004360
int nparam = parameterTypes.length;
@@ -4422,8 +4382,18 @@ private void addMethodArguments(ITypeBinding[] parameterTypes, boolean methodIsV
44224382
.isAssignmentCompatible(paramType.getComponentType().getErasure()))
44234383
// or it is not compatible
44244384
) {
4425-
buffer.append(clazzArray(paramType.getComponentType(), ARRAY_DIM_ONLY));
4426-
buffer.append(", -1, [");
4385+
String close;
4386+
if (NameMapper.isPackageOrClassNonqualified(methodDeclaration.getDeclaringClass().getQualifiedName())) {
4387+
// calls to DOMNode.setAttrs(DOMNode node, Object... attr) need not be wrapped by a Java array type
4388+
close = "";
4389+
} else {
4390+
buffer.append(clazzArray(paramType.getComponentType(), ARRAY_DIM_ONLY));
4391+
buffer.append(", -1, ");
4392+
close = ")";
4393+
}
4394+
4395+
4396+
buffer.append("[");
44274397
//
44284398
//
44294399
// buffer Debug(paramType.getComponentType().getName() + " "
@@ -4441,7 +4411,7 @@ private void addMethodArguments(ITypeBinding[] parameterTypes, boolean methodIsV
44414411
buffer.append(", ");
44424412
}
44434413
}
4444-
buffer.append("])");
4414+
buffer.append("]").append(close);
44454415
break;
44464416
}
44474417
post = "";
@@ -6660,7 +6630,7 @@ public static void setNonQualifiedNamePackages(String names) {
66606630
* @param className
66616631
* @return
66626632
*/
6663-
private static boolean isPackageOrClassNonqualified(String className) {
6633+
static boolean isPackageOrClassNonqualified(String className) {
66646634
if (className.indexOf("$") >= 0)
66656635
return false; // inner class
66666636
className += ".";
@@ -6673,9 +6643,6 @@ private static boolean isPackageOrClassNonqualified(String className) {
66736643
return false;
66746644
}
66756645

6676-
/**
6677-
* @param methodName not used but could be
6678-
*/
66796646
static boolean isMethodNonqualified(String className, String methodName) {
66806647
if (className.equals("java.lang.Math")) {
66816648
switch (methodName) {
-1.07 KB
Binary file not shown.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package javajs.http;
2+
3+
import java.io.Closeable;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.net.URI;
8+
import java.util.Map;
9+
import java.util.function.Consumer;
10+
11+
/**
12+
* A generic HttpClient interface that can work with org.apache.http classes for
13+
* Java or javajs.http.JSHttpClient for SwingJS.
14+
*
15+
* @author Mateusz Warowny
16+
*
17+
*/
18+
public interface HttpClient {
19+
20+
public interface HttpRequestBuilder {
21+
22+
public String getMethod();
23+
24+
public URI getUri();
25+
26+
public HttpRequestBuilder addHeader(String name, String value);
27+
28+
public HttpRequestBuilder addParameter(String name, String value);
29+
30+
public HttpRequestBuilder addFile(String name, File file);
31+
32+
public HttpRequestBuilder addFile(String name, InputStream stream);
33+
34+
/**
35+
* Send the request to the server and return the response.
36+
*/
37+
public HttpResponse execute() throws IOException;
38+
39+
HttpResponse executeAsync(Consumer<? super HttpResponse> done, Consumer<? super HttpResponse> fail,
40+
Consumer<? super HttpResponse> always);
41+
}
42+
43+
public interface HttpResponse extends Closeable {
44+
public int getStatusCode();
45+
46+
/**
47+
* Get the response headers, combining same-name headers with commas, preserving
48+
* order, as per RFC 2616
49+
*
50+
* @return
51+
*/
52+
public Map<String, String> getHeaders();
53+
54+
/**
55+
* Get the reply in the form of an InputStream.
56+
*
57+
* @return
58+
* @throws IOException
59+
*/
60+
public InputStream getContent() throws IOException;
61+
62+
/**
63+
* Get the reply in the form of a String.
64+
*
65+
* @return
66+
* @throws IOException
67+
*/
68+
public String getText() throws IOException;
69+
70+
/**
71+
* Close any open streams.
72+
*
73+
*/
74+
@Override
75+
public default void close() throws IOException {
76+
}
77+
}
78+
79+
/**
80+
* Initialises the GET request builder. Usually they have no request body and
81+
* parameters are passed in the URL query.
82+
*/
83+
public HttpRequestBuilder get(URI uri);
84+
85+
/**
86+
* Initialises the GET request builder. They have no request body and parameters
87+
* are passed in the URL query. They are identical to GET requests, the only
88+
* difference is that the returned response contains headers only.
89+
*/
90+
public HttpRequestBuilder head(URI uri);
91+
92+
/**
93+
* Initialises the POST request builder. Usually they contain data in the
94+
* request body either as a urlencoded form, a multipart form or raw bytes.
95+
* Currently, we only care about the multipart form.
96+
*/
97+
public HttpRequestBuilder post(URI uri);
98+
99+
/**
100+
* Initialises the PUT request builder which construct the same way as POST. The
101+
* only difference is the request method.
102+
*/
103+
public HttpRequestBuilder put(URI uri);
104+
105+
/**
106+
* Initialises the DELETE request builder. The DELETE requests have no body and
107+
* parameters are passed in the URL query, just like GET.
108+
*/
109+
public HttpRequestBuilder delete(URI uri);
110+
111+
}

0 commit comments

Comments
 (0)