Gson complaining of no default constructor, yet one exists, and it's not an inner class

949 views
Skip to first unread message

Mike

unread,
Jul 4, 2011, 7:59:09 AM7/4/11
to google-gson
Hi there --

I get a java.lang.RuntimeException: Unable to invoke no-args
constructor for class error when trying to instantiate from JSON the
following class, even though it does indeed have a no-args
constructor. Would love to know what I'm doing wrong!

public class Upload {

@SerializedName("id") private int _id;
@SerializedName("description") private String
_description;
@SerializedName("created_at") private Date
_createdAt;
@SerializedName("updated_at") private Date
_updatedAt;
@SerializedName("csv_file_file_name") private String
_csvFileFileName;
@SerializedName("csv_file_content_type") private String
_csvFileContentType;
@SerializedName("csv_file_file_size") private int
_csvFileFileSize;
@SerializedName("csv_file_updated_at") private Date
_csvFileUpdatedAt;
@SerializedName("status_id") private int _statusId;
@SerializedName("source_path") private String
_sourcePath;
@SerializedName("source_file_size") private long
_sourceFileSize;

public Upload() {
_id = -1;
_csvFileFileSize = -1;
_statusId = -1;
_sourceFileSize = -1;
}

public int id() {
return _id;
}

public void setId(int id) {
_id = id;
}

// ... additional getters and setters for all the above private
fields ...

public void save(...) throws Throwable {
// ... writes to the server via an HTTP POST
}

/*
* Restore from JSON
*/
private void restore(String result) {
Gson g = new Gson();
updateAttributes(g.fromJson(result, Upload.class));
}

private void updateAttributes(Upload upload) {
_id = upload.id();
_description = upload.description();
_createdAt = upload.createdAt();
_updatedAt = upload.updatedAt();
_csvFileFileName = upload.csvFileFileName();
_csvFileContentType = upload.csvFileContentType();
_csvFileFileSize = upload.csvFileFileSize();
_csvFileUpdatedAt = upload.csvFileUpdatedAt();
_statusId = upload.statusId();
_sourcePath = upload.sourcePath();
_sourceFileSize = upload.sourceFileSize();
}

limpb...@gmail.com

unread,
Jul 5, 2011, 3:52:47 AM7/5/11
to google-gson
Weird. Could you please provide us with a (minimal) test case?

Mike

unread,
Jul 9, 2011, 8:17:02 PM7/9/11
to google-gson
I've dug into this further and found that it's to do with a security
exception thrown by the fact that I'm trying to do this inside an
applet.

The exception

java.security.AccessControlException: access denied
(java.lang.reflect.ReflectPermission suppressAccessChecks)

is thrown by the call to setAccessible(true) in the
DefaultConstructorAllocator class here:

private static <T> Constructor<T> getNoArgsConstructor(Class<T> c) {
try {
Constructor<T> declaredConstructor = c.getDeclaredConstructor();
declaredConstructor.setAccessible(true);
return declaredConstructor;
} catch (Exception e) {
return null;
}
}

I've tried this inside a doPrivileged context, and it still throws the
error.

I'm assuming this is a limitation of applets, and so therefore I won't
be able to use Gson here.

limpb...@gmail.com

unread,
Jul 10, 2011, 4:08:00 PM7/10/11
to google-gson
We should just fix GSON to not call setAccessible(true) unless it
actually needs to. Your class and constructor will both need to be
public, but I don't anticipate that'll be a problem.

I've opened a bug for this. We'll fix it shortly. In the interim; you
may want to make a custom build of GSON that doesn't call
setAccessible().
http://code.google.com/p/google-gson/issues/detail?id=344
Reply all
Reply to author
Forward
0 new messages