我正在编写一个没有XSD编译的JAXB类.
任何人都可以告诉我两者之间的区别
@XmlSchemaType(name = "normalizedString")
private String normalized;
Run Code Online (Sandbox Code Playgroud)
和
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
private String normalized;
Run Code Online (Sandbox Code Playgroud)
?
我有一个实体类看起来像这样。
@XmlRootElement
public class ImageSuffix {
@XmlAttribute
private boolean canRead;
@XmlAttribute
private boolean canWrite;
@XmlValue;
private String value;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用以下依赖项来生成 JSON。
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.1.4</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
当我尝试使用以下代码时,(引用自Generating JSON Schemas with Jackson)
@Path("/imageSuffix.jsd")
public class ImageSuffixJsdResource {
@GET
@Produces({MediaType.APPLICATION_JSON})
public String read() throws JsonMappingException {
final ObjectMapper objectMapper = new ObjectMapper();
final JsonSchema jsonSchema =
objectMapper.generateJsonSchema(ImageSuffix.class);
final String jsonSchemaString = jsonSchema.toString();
return jsonSchemaString;
}
}
Run Code Online (Sandbox Code Playgroud)
服务器抱怨以下错误消息
java.lang.IllegalArgumentException: Class com.googlecode.jinahya.test.ImageSuffix would not be serialized as a JSON object and therefore …Run Code Online (Sandbox Code Playgroud) private static int testReturn(final boolean flag) {
return flag ? 1 : 2;
}
private static void testThrow1(final boolean flag)
throws IOException, SQLException {
if (flag ) {
throw new IOException("IO");
} else {
throw new SQLException("SQL");
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试用?:操作员更改它时,
private static void testThrow2(final boolean flag)
throws
//IOException, SQLException, // not even required
Exception { // compiler wants this.
throw flag ? new IOException("IO") : new SQLException("SQL");
}
Run Code Online (Sandbox Code Playgroud)
这是正常的吗?
谢意
当我准备Java 7功能的演示文稿时,我实际上遇到了这个代码,例如multiple-catch和typed rethrowal.有趣.谢谢大家的好消息.我学到了很多.
更新
Java 7已针对特定类型的重新抛出进行了改进,对吧?
void throwIoOrSql() …Run Code Online (Sandbox Code Playgroud) 我正在编写一个JAX-RS库(不是应用程序).
我有:
abstract class A {
@PostConstruct
private void constructed_a() {} // not invoked
@Inject
private Some some;
}
public abstract class B extends A {
@PostConstruct
private void constructed_b() {} // not invoked
}
Run Code Online (Sandbox Code Playgroud)
和测试类:
@Path("c")
public class C extends B {
@PostConstrct
private void constructed_c() {} // invoked
}
Run Code Online (Sandbox Code Playgroud)
我正在测试jersey测试框架v2.17
我发现只constructed_c()调用它并且不调用祖先定义的那些方法.请注意,在类中some声明的field()已正确注入.@InjectA
这是正常的吗?我该怎么办?
结论
我使用embedded-glassfish进行了测试,发现正如Antonin Stefanutti指出的那样,这些回调方法按预期顺序调用.
constructed_a()
constructed_b()
constructed_c()
Run Code Online (Sandbox Code Playgroud) 我在使用 JNI 时遇到问题。
jboolean z = (jboolean) NULL; // ok
jbyte b = (jbyte) NULL; // ok
jchar c = (jchar) NULL; // ok
jshort s = (jshort) NULL; // ok
jint i = (jint) NULL; // ok
jlong j = (jlong) NULL; // ok
jobject l = (jobjec) NULL; // ok
jfloat f = (jlong) NULL; // NOT OK
jdouble d = (jdouble) NULL; // NOT OK
Run Code Online (Sandbox Code Playgroud)
编译器抱怨
[ERROR] /....c:112:28: error: pointer cannot be cast to type 'jfloat' (aka …Run Code Online (Sandbox Code Playgroud) 我正在为FileChannel编写一个实用程序类。
下面的方法看起来可能有效。
// tries to transfer as many bytes as specified
public static long transferTo(final FileChannel src, long position,
long count, final WritableByteChannel dst)
throws IOException {
long accumulated = 0L;
while (position < src.size()) {
final long transferred = src.transferTo(position, count, dst);
position += transferred;
count -= transferred;
accumulated += transferred;
}
return accumulated;
}
Run Code Online (Sandbox Code Playgroud)
但是版本transferFrom有问题。
// tries to transfer as many bytes as specified
public static long transferFrom(final FileChannel dst,
final ReadableByteChannel src,
long …Run Code Online (Sandbox Code Playgroud) 我写了一个方法将字节从 an 复制InputStream到 an OutputStream。
// copies bytes from given input stream to specified output stream
// returns the number of bytes copied.
private static long copy(final InputStream input,
final OutputStream output)
throws IOException {
long count = 0L;
final byte[] buffer = new byte[4096];
for (int length; (length = input.read(buffer)) != -1; count += length) {
output.write(buffer, 0, length);
}
return count;
}
Run Code Online (Sandbox Code Playgroud)
SonarQube 抱怨。
此循环的停止条件测试“长度、输入、缓冲区”,但增量器更新“计数”。
当 for 循环的停止条件和增量器不作用于同一个变量时,它几乎总是一个错误。即使不是,它也可能混淆代码的未来维护者,应该避免。
有没有更好的代码用于相同的目的?
更新
正如答案所推荐的那样,我确实喜欢这个并且问题消失了。
// copies bytes from …Run Code Online (Sandbox Code Playgroud) 我有很长的孩子.
// ordered by parent.id / child.id
Stream<Child> childStream;
Run Code Online (Sandbox Code Playgroud)
说,
Child(id = 1, parent(id = 1))
Child(id = 2, parent(id = 1))
Child(id = 3, parent(id = 2))
Child(id = 4, parent(id = 2))
Child(id = 5, parent(id = 3))
Run Code Online (Sandbox Code Playgroud)
每个人Child都有父母.
class Child {
Parent parent;
}
Run Code Online (Sandbox Code Playgroud)
现在,我如何将流映射到一个流Family?
class Family {
Parent parent;
List<Child> children;
}
Run Code Online (Sandbox Code Playgroud)
我已经知道了Collectors.groupingBy,但是流很长,将它们全部收集到一个中Map是不适用的.
我试图让参数信息不可修改.
#include <stdio.h>
#include <stdlib.h>
int main(const int argc, const char* const argv[]) {
//argc = 1; // error: assignment of read-only parameter 'argc'
//argv[0] = "argv"; // error: assignment of read-only location '*argv'
//argv[0][0] = 'a'; // error: assignment of read-only location '**argv'
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
现在我这样做的时候
argv = NULL; // no compile-time error
Run Code Online (Sandbox Code Playgroud)
编译器使沉默.
该声明实际上做了什么?我怎样才能禁止我的代码呢?
java ×6
c ×2
jaxb ×2
cdi ×1
filechannel ×1
for-loop ×1
jackson ×1
java-stream ×1
jax-rs ×1
jersey ×1
json ×1
jsonschema ×1
mapping ×1
nio ×1
sonarqube ×1
unit-testing ×1
unsafe ×1