Skip to content

Commit

Permalink
Cleanups and suggestions from IntelliJ
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Nov 21, 2024
1 parent b9d21a8 commit c29d9ae
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 302 deletions.
111 changes: 46 additions & 65 deletions java/src/json/ext/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
package json.ext;

import org.jcodings.Encoding;
import org.jcodings.specific.UTF8Encoding;
import org.jruby.Ruby;
import org.jruby.RubyArray;
Expand All @@ -15,7 +14,6 @@
import org.jruby.RubyFixnum;
import org.jruby.RubyFloat;
import org.jruby.RubyHash;
import org.jruby.RubyIO;
import org.jruby.RubyString;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.ThreadContext;
Expand All @@ -29,7 +27,8 @@
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;

import static java.nio.charset.StandardCharsets.*;

public final class Generator {

Expand All @@ -44,12 +43,12 @@ private Generator() {
*/
static <T extends IRubyObject> RubyString generateJson(ThreadContext context, T object, Handler<? super T> handler) {
Session session = new Session(null);
return session.infect(handler.generateNew(context, session, object));
return handler.generateNew(context, session, object);
}

static <T extends IRubyObject> RubyString generateJson(ThreadContext context, T object, Handler<? super T> handler, IRubyObject arg0) {
Session session = new Session(arg0);
return session.infect(handler.generateNew(context, session, object));
return handler.generateNew(context, session, object);
}

/**
Expand Down Expand Up @@ -93,21 +92,21 @@ static <T extends IRubyObject> RubyString generateJson(ThreadContext context, T
@SuppressWarnings("unchecked")
private static <T extends IRubyObject> Handler<? super T> getHandlerFor(Ruby runtime, T object) {
switch (((RubyBasicObject) object).getNativeClassIndex()) {
case NIL : return (Handler) NIL_HANDLER;
case TRUE : return (Handler) TRUE_HANDLER;
case FALSE : return (Handler) FALSE_HANDLER;
case FLOAT : return (Handler) FLOAT_HANDLER;
case FIXNUM : return (Handler) FIXNUM_HANDLER;
case BIGNUM : return (Handler) BIGNUM_HANDLER;
case NIL : return NIL_HANDLER;
case TRUE : return (Handler<T>) TRUE_HANDLER;
case FALSE : return (Handler<T>) FALSE_HANDLER;
case FLOAT : return (Handler<T>) FLOAT_HANDLER;
case FIXNUM : return (Handler<T>) FIXNUM_HANDLER;
case BIGNUM : return (Handler<T>) BIGNUM_HANDLER;
case STRING :
if (((RubyBasicObject) object).getMetaClass() != runtime.getString()) break;
return (Handler) STRING_HANDLER;
if (Helpers.metaclass(object) != runtime.getString()) break;
return (Handler<T>) STRING_HANDLER;
case ARRAY :
if (((RubyBasicObject) object).getMetaClass() != runtime.getArray()) break;
return (Handler) ARRAY_HANDLER;
if (Helpers.metaclass(object) != runtime.getArray()) break;
return (Handler<T>) ARRAY_HANDLER;
case HASH :
if (((RubyBasicObject) object).getMetaClass() != runtime.getHash()) break;
return (Handler) HASH_HANDLER;
if (Helpers.metaclass(object) != runtime.getHash()) break;
return (Handler<T>) HASH_HANDLER;
}
return GENERIC_HANDLER;
}
Expand All @@ -132,9 +131,6 @@ static class Session {
private RuntimeInfo info;
private StringEncoder stringEncoder;

private boolean tainted = false;
private boolean untrusted = false;

Session(GeneratorState state) {
this.state = state;
}
Expand Down Expand Up @@ -163,17 +159,6 @@ public StringEncoder getStringEncoder(ThreadContext context) {
}
return stringEncoder;
}

public void infectBy(IRubyObject object) {
if (object.isTaint()) tainted = true;
if (object.isUntrusted()) untrusted = true;
}

public <T extends IRubyObject> T infect(T object) {
if (tainted) object.setTaint(true);
if (untrusted) object.setUntrusted(true);
return object;
}
}


Expand Down Expand Up @@ -212,10 +197,10 @@ void generateToBuffer(ThreadContext context, Session session, T object, OutputSt
*/
private static class KeywordHandler<T extends IRubyObject>
extends Handler<T> {
private byte[] keyword;
private final byte[] keyword;

private KeywordHandler(String keyword) {
this.keyword = keyword.getBytes(StandardCharsets.UTF_8);
this.keyword = keyword.getBytes(UTF_8);
}

@Override
Expand All @@ -242,7 +227,7 @@ void generate(ThreadContext context, Session session, T object, OutputStream buf
@Override
void generate(ThreadContext context, Session session, RubyBignum object, OutputStream buffer) throws IOException {
BigInteger bigInt = object.getValue();
buffer.write(bigInt.toString().getBytes(StandardCharsets.UTF_8));
buffer.write(bigInt.toString().getBytes(UTF_8));
}
};

Expand All @@ -266,15 +251,15 @@ void generate(ThreadContext context, Session session, RubyFloat object, OutputSt
}
}

buffer.write(Double.toString(value).getBytes(StandardCharsets.UTF_8));
buffer.write(Double.toString(value).getBytes(UTF_8));
}
};

private static final byte[] EMPTY_ARRAY_BYTES = "[]".getBytes();
static final Handler<RubyArray> ARRAY_HANDLER =
new Handler<RubyArray>() {
static final Handler<RubyArray<IRubyObject>> ARRAY_HANDLER =
new Handler<RubyArray<IRubyObject>>() {
@Override
int guessSize(ThreadContext context, Session session, RubyArray object) {
int guessSize(ThreadContext context, Session session, RubyArray<IRubyObject> object) {
GeneratorState state = session.getState(context);
int depth = state.getDepth();
int perItem =
Expand All @@ -285,9 +270,9 @@ int guessSize(ThreadContext context, Session session, RubyArray object) {
}

@Override
void generate(ThreadContext context, Session session, RubyArray object, OutputStream buffer) throws IOException {
void generate(ThreadContext context, Session session, RubyArray<IRubyObject> object, OutputStream buffer) throws IOException {
GeneratorState state = session.getState(context);
int depth = state.increaseDepth();
int depth = state.increaseDepth(context);

if (object.isEmpty()) {
buffer.write(EMPTY_ARRAY_BYTES);
Expand All @@ -306,27 +291,24 @@ void generate(ThreadContext context, Session session, RubyArray object, OutputSt
System.arraycopy(arrayNl.unsafeBytes(), arrayNl.begin(), delim, 1,
arrayNl.length());

session.infectBy(object);

buffer.write((byte)'[');
buffer.write(arrayNl.bytes());
boolean firstItem = true;

for (int i = 0, t = object.getLength(); i < t; i++) {
IRubyObject element = object.eltInternal(i);
session.infectBy(element);
if (firstItem) {
firstItem = false;
} else {
buffer.write(delim);
}
buffer.write(shift);
Handler<IRubyObject> handler = (Handler<IRubyObject>) getHandlerFor(runtime, element);
Handler<? super IRubyObject> handler = getHandlerFor(runtime, element);
handler.generate(context, session, element, buffer);
}

state.decreaseDepth();
if (arrayNl.length() != 0) {
if (!arrayNl.isEmpty()) {
buffer.write(arrayNl.bytes());
buffer.write(shift, 0, state.getDepth() * indentUnit.length());
}
Expand All @@ -352,62 +334,61 @@ int guessSize(ThreadContext context, Session session, RubyHash object) {
@Override
void generate(ThreadContext context, final Session session, RubyHash object, final OutputStream buffer) throws IOException {
final GeneratorState state = session.getState(context);
final int depth = state.increaseDepth();
final int depth = state.increaseDepth(context);

if (object.isEmpty()) {
buffer.write(EMPTY_HASH_BYTES);
state.decreaseDepth();
return;
}

final Ruby runtime = context.runtime;

final ByteList objectNl = state.getObjectNl();
byte[] objectNLBytes = objectNl.unsafeBytes();
final byte[] indent = Utils.repeat(state.getIndent(), depth);
final ByteList spaceBefore = state.getSpaceBefore();
final ByteList space = state.getSpace();

buffer.write((byte)'{');
buffer.write(objectNl.bytes());
buffer.write(objectNLBytes);

final boolean[] firstPair = new boolean[]{true};
object.visitAll(new RubyHash.Visitor() {
object.visitAll(context, new RubyHash.VisitorWithState<boolean[]>() {
@Override
public void visit(IRubyObject key, IRubyObject value) {
public void visit(ThreadContext context, RubyHash self, IRubyObject key, IRubyObject value, int index, boolean[] firstPair) {
try {
if (firstPair[0]) {
firstPair[0] = false;
} else {
buffer.write((byte) ',');
buffer.write(objectNl.bytes());
buffer.write(objectNLBytes);
}
if (objectNl.length() != 0) buffer.write(indent);
if (!objectNl.isEmpty()) buffer.write(indent);

Ruby runtime = context.runtime;

IRubyObject keyStr = key.callMethod(context, "to_s");
if (keyStr.getMetaClass() == runtime.getString()) {
STRING_HANDLER.generate(context, session, (RubyString) keyStr, buffer);
} else {
Utils.ensureString(keyStr);
Handler<IRubyObject> keyHandler = (Handler<IRubyObject>) getHandlerFor(runtime, keyStr);
Handler<? super IRubyObject> keyHandler = getHandlerFor(runtime, keyStr);
keyHandler.generate(context, session, keyStr, buffer);
}
session.infectBy(key);

buffer.write(spaceBefore.bytes());
buffer.write(spaceBefore.unsafeBytes());
buffer.write((byte) ':');
buffer.write(space.bytes());
buffer.write(space.unsafeBytes());

Handler<IRubyObject> valueHandler = (Handler<IRubyObject>) getHandlerFor(runtime, value);
Handler<? super IRubyObject> valueHandler = getHandlerFor(runtime, value);
valueHandler.generate(context, session, value, buffer);
session.infectBy(value);
} catch (Throwable t) {
Helpers.throwException(t);
}
}
});
}, firstPair);
state.decreaseDepth();
if (!firstPair[0] && objectNl.length() != 0) {
buffer.write(objectNl.bytes());
if (!firstPair[0] && !objectNl.isEmpty()) {
buffer.write(objectNLBytes);
}
buffer.write(Utils.repeat(state.getIndent(), state.getDepth()));
buffer.write((byte)'}');
Expand Down Expand Up @@ -445,11 +426,11 @@ void generate(ThreadContext context, Session session, RubyString object, OutputS
};

static final Handler<RubyBoolean> TRUE_HANDLER =
new KeywordHandler<RubyBoolean>("true");
new KeywordHandler<>("true");
static final Handler<RubyBoolean> FALSE_HANDLER =
new KeywordHandler<RubyBoolean>("false");
new KeywordHandler<>("false");
static final Handler<IRubyObject> NIL_HANDLER =
new KeywordHandler<IRubyObject>("null");
new KeywordHandler<>("null");

/**
* The default handler (<code>Object#to_json</code>): coerces the object
Expand Down
26 changes: 12 additions & 14 deletions java/src/json/ext/GeneratorMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.jruby.RubyFixnum;
import org.jruby.RubyFloat;
import org.jruby.RubyHash;
import org.jruby.RubyInteger;
import org.jruby.RubyModule;
import org.jruby.RubyNumeric;
import org.jruby.RubyString;
Expand All @@ -30,8 +29,8 @@
class GeneratorMethods {
/**
* Populates the given module with all modules and their methods
* @param info
* @param generatorMethodsModule The module to populate
* @param info The current RuntimeInfo
* @param module The module to populate
* (normally <code>JSON::Generator::GeneratorMethods</code>)
*/
static void populate(RuntimeInfo info, RubyModule module) {
Expand All @@ -45,19 +44,18 @@ static void populate(RuntimeInfo info, RubyModule module) {
defineMethods(module, "String", RbString.class);
defineMethods(module, "TrueClass", RbTrue.class);

info.stringExtendModule = new WeakReference<RubyModule>(module.defineModuleUnder("String")
.defineModuleUnder("Extend"));
info.stringExtendModule = new WeakReference<>(module.defineModuleUnder("String").defineModuleUnder("Extend"));
info.stringExtendModule.get().defineAnnotatedMethods(StringExtend.class);
}

/**
* Convenience method for defining methods on a submodule.
* @param parentModule
* @param submoduleName
* @param klass
* @param parentModule the parent module
* @param submoduleName the submodule
* @param klass the class from which to define methods
*/
private static void defineMethods(RubyModule parentModule,
String submoduleName, Class klass) {
String submoduleName, Class<?> klass) {
RubyModule submodule = parentModule.defineModuleUnder(submoduleName);
submodule.defineAnnotatedMethods(klass);
}
Expand All @@ -77,12 +75,12 @@ public static IRubyObject to_json(ThreadContext context, IRubyObject vSelf, IRub
public static class RbArray {
@JRubyMethod
public static IRubyObject to_json(ThreadContext context, IRubyObject vSelf) {
return Generator.generateJson(context, (RubyArray)vSelf, Generator.ARRAY_HANDLER);
return Generator.generateJson(context, (RubyArray<IRubyObject>)vSelf, Generator.ARRAY_HANDLER);
}

@JRubyMethod
public static IRubyObject to_json(ThreadContext context, IRubyObject vSelf, IRubyObject arg0) {
return Generator.generateJson(context, (RubyArray)vSelf, Generator.ARRAY_HANDLER, arg0);
return Generator.generateJson(context, (RubyArray<IRubyObject>)vSelf, Generator.ARRAY_HANDLER, arg0);
}
}

Expand Down Expand Up @@ -154,7 +152,7 @@ public static IRubyObject to_json_raw_object(ThreadContext context, IRubyObject

private static RubyHash toJsonRawObject(ThreadContext context,
RubyString self) {
Ruby runtime = context.getRuntime();
Ruby runtime = context.runtime;
RubyHash result = RubyHash.newHash(runtime);

IRubyObject createId = RuntimeInfo.forRuntime(runtime)
Expand All @@ -174,7 +172,7 @@ private static RubyHash toJsonRawObject(ThreadContext context,

@JRubyMethod(module=true)
public static IRubyObject included(ThreadContext context, IRubyObject vSelf, IRubyObject module) {
RuntimeInfo info = RuntimeInfo.forRuntime(context.getRuntime());
RuntimeInfo info = RuntimeInfo.forRuntime(context.runtime);
return module.callMethod(context, "extend", info.stringExtendModule.get());
}
}
Expand All @@ -190,7 +188,7 @@ public static class StringExtend {
@JRubyMethod
public static IRubyObject json_create(ThreadContext context,
IRubyObject vSelf, IRubyObject vHash) {
Ruby runtime = context.getRuntime();
Ruby runtime = context.runtime;
RubyHash o = vHash.convertToHash();
IRubyObject rawData = o.fastARef(runtime.newString("raw"));
if (rawData == null) {
Expand Down
Loading

0 comments on commit c29d9ae

Please sign in to comment.