mirror of
https://github.com/Eaglercraft-Archive/Eaglercraftx-1.8.8-src.git
synced 2025-06-27 18:38:14 -05:00
Update #40 - FPS boost and fixed IndexOutOfBoundsException
This commit is contained in:
@ -20,6 +20,8 @@ import java.lang.ref.WeakReference;
|
||||
|
||||
import com.google.common.annotations.GwtCompatible;
|
||||
|
||||
import net.lax1dude.eaglercraft.v1_8.EagRuntime;
|
||||
|
||||
/**
|
||||
* Methods factored out so that they can be emulated differently in GWT.
|
||||
*
|
||||
@ -32,7 +34,7 @@ final class Platform {
|
||||
|
||||
/** Calls {@link System#nanoTime()}. */
|
||||
static long systemNanoTime() {
|
||||
return System.nanoTime();
|
||||
return EagRuntime.nanoTime();
|
||||
}
|
||||
|
||||
static CharMatcher precomputeCharMatcher(CharMatcher matcher) {
|
||||
|
@ -75,18 +75,43 @@ public class Display {
|
||||
PlatformInput.update();
|
||||
}
|
||||
|
||||
public static void update(int limitFramerate) {
|
||||
PlatformInput.update(limitFramerate);
|
||||
}
|
||||
|
||||
private static final long[] defaultSyncPtr = new long[1];
|
||||
|
||||
public static void sync(int limitFramerate) {
|
||||
sync(limitFramerate, defaultSyncPtr);
|
||||
}
|
||||
|
||||
public static boolean sync(int limitFramerate, long[] timerPtr) {
|
||||
boolean limitFPS = limitFramerate > 0 && limitFramerate < 1000;
|
||||
boolean blocked = false;
|
||||
|
||||
if(limitFPS) {
|
||||
long millis = EagRuntime.steadyTimeMillis();
|
||||
long frameMillis = (1000l / limitFramerate) - (millis - lastSwap);
|
||||
if(frameMillis > 0l) {
|
||||
EagUtils.sleep(frameMillis);
|
||||
if(timerPtr[0] == 0l) {
|
||||
timerPtr[0] = EagRuntime.steadyTimeMillis();
|
||||
}else {
|
||||
long millis = EagRuntime.steadyTimeMillis();
|
||||
long frameMillis = (1000l / limitFramerate);
|
||||
long frameTime = millis - timerPtr[0];
|
||||
if(frameTime > 2000l || frameTime < 0l) {
|
||||
frameTime = frameMillis;
|
||||
timerPtr[0] = millis;
|
||||
}else {
|
||||
timerPtr[0] += frameMillis;
|
||||
}
|
||||
if(frameTime >= 0l && frameTime < frameMillis) {
|
||||
EagUtils.sleep(frameMillis - frameTime);
|
||||
blocked = true;
|
||||
}
|
||||
}
|
||||
}else {
|
||||
timerPtr[0] = 0l;
|
||||
}
|
||||
|
||||
lastSwap = EagRuntime.steadyTimeMillis();
|
||||
return blocked;
|
||||
}
|
||||
|
||||
public static boolean contextLost() {
|
||||
|
@ -5,6 +5,8 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import net.lax1dude.eaglercraft.v1_8.internal.PlatformRuntime;
|
||||
|
||||
/**
|
||||
* Copyright (c) 2022-2023 lax1dude, ayunami2000. All Rights Reserved.
|
||||
*
|
||||
@ -57,11 +59,14 @@ public class EagUtils {
|
||||
return str.length() < off + 2 ? decodeHex(str.subSequence(off, 2)) : 0;
|
||||
}
|
||||
|
||||
public static void sleep(int millis) {
|
||||
PlatformRuntime.sleep(millis);
|
||||
}
|
||||
|
||||
public static void sleep(long millis) {
|
||||
try {
|
||||
Thread.sleep(millis);
|
||||
}catch(InterruptedException ex) {
|
||||
}
|
||||
int reduced = (int)millis;
|
||||
if(reduced != millis) throw new IllegalArgumentException();
|
||||
PlatformRuntime.sleep(reduced);
|
||||
}
|
||||
|
||||
public static String toASCIIEagler(String str) {
|
||||
|
@ -5,6 +5,7 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import net.lax1dude.eaglercraft.v1_8.internal.EnumPlatformType;
|
||||
import net.lax1dude.eaglercraft.v1_8.internal.IAudioCacheLoader;
|
||||
import net.lax1dude.eaglercraft.v1_8.internal.IAudioHandle;
|
||||
import net.lax1dude.eaglercraft.v1_8.internal.IAudioResource;
|
||||
import net.lax1dude.eaglercraft.v1_8.internal.PlatformAudio;
|
||||
@ -286,7 +287,7 @@ public class EaglercraftSoundManager {
|
||||
}
|
||||
}
|
||||
|
||||
private final PlatformAudio.IAudioCacheLoader browserResourcePackLoader = filename -> {
|
||||
private final IAudioCacheLoader browserResourcePackLoader = filename -> {
|
||||
try {
|
||||
return EaglerInputStream.inputStreamToBytesQuiet(Minecraft.getMinecraft().getResourceManager()
|
||||
.getResource(new ResourceLocation(filename)).getInputStream());
|
||||
|
@ -10,7 +10,7 @@ public class EaglercraftVersion {
|
||||
/// Customize these to fit your fork:
|
||||
|
||||
public static final String projectForkName = "EaglercraftX";
|
||||
public static final String projectForkVersion = "u39";
|
||||
public static final String projectForkVersion = "u40";
|
||||
public static final String projectForkVendor = "lax1dude";
|
||||
|
||||
public static final String projectForkURL = "https://gitlab.com/lax1dude/eaglercraftx-1.8";
|
||||
@ -20,20 +20,20 @@ public class EaglercraftVersion {
|
||||
public static final String projectOriginName = "EaglercraftX";
|
||||
public static final String projectOriginAuthor = "lax1dude";
|
||||
public static final String projectOriginRevision = "1.8";
|
||||
public static final String projectOriginVersion = "u39";
|
||||
public static final String projectOriginVersion = "u40";
|
||||
|
||||
public static final String projectOriginURL = "https://gitlab.com/lax1dude/eaglercraftx-1.8"; // rest in peace
|
||||
|
||||
// EPK Version Identifier
|
||||
|
||||
public static final String EPKVersionIdentifier = "u39"; // Set to null to disable EPK version check
|
||||
public static final String EPKVersionIdentifier = "u40"; // Set to null to disable EPK version check
|
||||
|
||||
// Updating configuration
|
||||
|
||||
public static final boolean enableUpdateService = true;
|
||||
|
||||
public static final String updateBundlePackageName = "net.lax1dude.eaglercraft.v1_8.client";
|
||||
public static final int updateBundlePackageVersionInt = 39;
|
||||
public static final int updateBundlePackageVersionInt = 40;
|
||||
|
||||
public static final String updateLatestLocalStorageKey = "latestUpdate_" + updateBundlePackageName;
|
||||
|
||||
|
@ -65,4 +65,8 @@ public class Keyboard {
|
||||
PlatformInput.keyboardFireEvent(eventType, eagKey, keyChar);
|
||||
}
|
||||
|
||||
public static boolean areKeysLocked() {
|
||||
return PlatformInput.keyboardAreKeysLocked();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,22 @@
|
||||
package net.lax1dude.eaglercraft.v1_8.internal;
|
||||
|
||||
/**
|
||||
* Copyright (c) 2024 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
public interface IAudioCacheLoader {
|
||||
|
||||
byte[] loadFile(String filename);
|
||||
|
||||
}
|
@ -50,6 +50,8 @@ public interface IClientConfigAdapter {
|
||||
|
||||
List<RelayEntry> getRelays();
|
||||
|
||||
boolean isCheckGLErrors();
|
||||
|
||||
boolean isCheckShaderGLErrors();
|
||||
|
||||
boolean isDemo();
|
||||
|
@ -3,6 +3,7 @@ package net.lax1dude.eaglercraft.v1_8.internal;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import net.lax1dude.eaglercraft.v1_8.EagRuntime;
|
||||
import net.lax1dude.eaglercraft.v1_8.EagUtils;
|
||||
|
||||
/**
|
||||
* Copyright (c) 2022 lax1dude. All Rights Reserved.
|
||||
@ -79,10 +80,7 @@ public interface IServerQuery {
|
||||
default boolean awaitResponseAvailable(long timeout) {
|
||||
long start = EagRuntime.steadyTimeMillis();
|
||||
while(isOpen() && responsesAvailable() <= 0 && (timeout <= 0l || EagRuntime.steadyTimeMillis() - start < timeout)) {
|
||||
try {
|
||||
Thread.sleep(0l, 250000);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
EagUtils.sleep(5);
|
||||
}
|
||||
return responsesAvailable() > 0;
|
||||
}
|
||||
@ -94,10 +92,7 @@ public interface IServerQuery {
|
||||
default boolean awaitResponseBinaryAvailable(long timeout) {
|
||||
long start = EagRuntime.steadyTimeMillis();
|
||||
while(isOpen() && binaryResponsesAvailable() <= 0 && (timeout <= 0l || EagRuntime.steadyTimeMillis() - start < timeout)) {
|
||||
try {
|
||||
Thread.sleep(0l, 250000);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
EagUtils.sleep(5);
|
||||
}
|
||||
return binaryResponsesAvailable() > 0;
|
||||
}
|
||||
|
@ -4065,6 +4065,6 @@ public class EaglerDeferredPipeline {
|
||||
GlStateManager.popMatrix();
|
||||
GlStateManager.matrixMode(GL_MODELVIEW);
|
||||
GlStateManager.popMatrix();
|
||||
EagUtils.sleep(10l);
|
||||
EagUtils.sleep(10);
|
||||
}
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ public class GuiScreenImportExportProfile extends GuiScreen {
|
||||
waitingForFile = false;
|
||||
FileChooserResult result = EagRuntime.getFileChooserResult();
|
||||
if(result != null) {
|
||||
mc.loadingScreen.eaglerShow(I18n.format("settingsBackup.importing.1"), "settingsBackup.importing.2");
|
||||
mc.loadingScreen.eaglerShow(I18n.format("settingsBackup.importing.1"), I18n.format("settingsBackup.importing.2"));
|
||||
ProfileImporter importer = new ProfileImporter(result.fileData);
|
||||
try {
|
||||
importer.readHeader();
|
||||
|
@ -12,6 +12,7 @@ import java.util.Map.Entry;
|
||||
|
||||
import net.lax1dude.eaglercraft.v1_8.ArrayUtils;
|
||||
import net.lax1dude.eaglercraft.v1_8.EagRuntime;
|
||||
import net.lax1dude.eaglercraft.v1_8.EagUtils;
|
||||
import net.lax1dude.eaglercraft.v1_8.EaglerInputStream;
|
||||
import net.lax1dude.eaglercraft.v1_8.EaglerOutputStream;
|
||||
import net.lax1dude.eaglercraft.v1_8.EaglercraftUUID;
|
||||
@ -441,10 +442,7 @@ public class ConnectionHandshake {
|
||||
if(client.getState().isClosed()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
Thread.sleep(50l);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
EagUtils.sleep(50);
|
||||
if(EagRuntime.steadyTimeMillis() - millis > timeout) {
|
||||
client.close();
|
||||
return null;
|
||||
|
@ -144,9 +144,11 @@ public class GameProtocolMessageController {
|
||||
pkt = sendQueueV4.remove(0);
|
||||
sendFunction.sendPluginMessage(GamePluginMessageConstants.V4_CHANNEL, pkt);
|
||||
}else {
|
||||
int i, j, sendCount = 0, totalLen = 0;
|
||||
int i, j, sendCount, totalLen;
|
||||
PacketBuffer sendBuffer;
|
||||
while(sendQueueV4.size() > 0) {
|
||||
sendCount = 0;
|
||||
totalLen = 0;
|
||||
do {
|
||||
i = sendQueueV4.get(sendCount++).readableBytes();
|
||||
totalLen += GamePacketOutputBuffer.getVarIntSize(i) + i;
|
||||
|
@ -126,7 +126,7 @@ public class LANClientNetworkManager extends EaglercraftNetworkManager {
|
||||
connectState = SENT_DESCRIPTION;
|
||||
continue mainLoop;
|
||||
}
|
||||
EagUtils.sleep(20l);
|
||||
EagUtils.sleep(20);
|
||||
}while(EagRuntime.steadyTimeMillis() - lm < 5000l);
|
||||
|
||||
// no description was sent
|
||||
@ -163,7 +163,7 @@ public class LANClientNetworkManager extends EaglercraftNetworkManager {
|
||||
return new LANClientNetworkManager(displayCode, displayRelay);
|
||||
|
||||
}
|
||||
EagUtils.sleep(20l);
|
||||
EagUtils.sleep(20);
|
||||
}while(EagRuntime.steadyTimeMillis() - lm < 5000l);
|
||||
|
||||
// no channel was opened
|
||||
@ -202,7 +202,7 @@ public class LANClientNetworkManager extends EaglercraftNetworkManager {
|
||||
connectState = SENT_ICE_CANDIDATE;
|
||||
continue mainLoop;
|
||||
}
|
||||
EagUtils.sleep(20l);
|
||||
EagUtils.sleep(20);
|
||||
}while(EagRuntime.steadyTimeMillis() - lm < 5000l);
|
||||
|
||||
// no ice candidates were sent
|
||||
@ -237,7 +237,7 @@ public class LANClientNetworkManager extends EaglercraftNetworkManager {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
EagUtils.sleep(20l);
|
||||
EagUtils.sleep(20);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -64,7 +64,7 @@ class LANClientPeer {
|
||||
disconnect();
|
||||
return;
|
||||
}
|
||||
EagUtils.sleep(20l);
|
||||
EagUtils.sleep(20);
|
||||
}while(EagRuntime.steadyTimeMillis() - millis < 5000l);
|
||||
logger.error("Getting server ICE candidates for '{}' timed out!", clientId);
|
||||
disconnect();
|
||||
@ -92,7 +92,7 @@ class LANClientPeer {
|
||||
disconnect();
|
||||
return;
|
||||
}
|
||||
EagUtils.sleep(20l);
|
||||
EagUtils.sleep(20);
|
||||
}while(EagRuntime.steadyTimeMillis() - millis < 5000l);
|
||||
logger.error("Getting server description for '{}' timed out!", clientId);
|
||||
disconnect();
|
||||
@ -122,7 +122,7 @@ class LANClientPeer {
|
||||
disconnect();
|
||||
return;
|
||||
}
|
||||
EagUtils.sleep(20l);
|
||||
EagUtils.sleep(20);
|
||||
}while(EagRuntime.steadyTimeMillis() - millis < 5000l);
|
||||
logger.error("Getting server description for '{}' timed out!", clientId);
|
||||
disconnect();
|
||||
|
@ -81,7 +81,7 @@ public class LANServerController {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
EagUtils.sleep(50l);
|
||||
EagUtils.sleep(50);
|
||||
}while(EagRuntime.steadyTimeMillis() - millis < 1000l);
|
||||
logger.info("Relay [{}] relay provide ICE servers timeout", sock.getURI());
|
||||
closeLAN();
|
||||
|
@ -298,10 +298,10 @@ public class RelayManager {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
EagUtils.sleep(20l);
|
||||
EagUtils.sleep(20);
|
||||
}
|
||||
}
|
||||
EagUtils.sleep(20l);
|
||||
EagUtils.sleep(20);
|
||||
}
|
||||
logger.error("Relay [{}] connection failed!", relay.address);
|
||||
Throwable t;
|
||||
|
@ -81,7 +81,7 @@ public class RelayServer {
|
||||
public void pingBlocking() {
|
||||
ping();
|
||||
while(getPing() < 0l) {
|
||||
EagUtils.sleep(250l);
|
||||
EagUtils.sleep(250);
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
@ -468,7 +468,7 @@ public class EaglerIntegratedServerWorker {
|
||||
}
|
||||
}else {
|
||||
if(!singleThreadMode) {
|
||||
EagUtils.sleep(50l);
|
||||
EagUtils.sleep(50);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ import net.minecraft.util.ResourceLocation;
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
public class GuiScreenPhishingWaring extends GuiScreen {
|
||||
public class GuiScreenPhishingWarning extends GuiScreen {
|
||||
|
||||
public static boolean hasShownMessage = false;
|
||||
|
||||
@ -33,7 +33,7 @@ public class GuiScreenPhishingWaring extends GuiScreen {
|
||||
private boolean mouseOverCheck;
|
||||
private boolean hasCheckedBox;
|
||||
|
||||
public GuiScreenPhishingWaring(GuiScreen cont) {
|
||||
public GuiScreenPhishingWarning(GuiScreen cont) {
|
||||
this.cont = cont;
|
||||
}
|
||||
|
@ -22,6 +22,8 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import net.lax1dude.eaglercraft.v1_8.EagRuntime;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* {@code StopWatch} provides a convenient API for timings.
|
||||
@ -314,7 +316,7 @@ public class StopWatch {
|
||||
} else if (this.runningState == State.UNSTARTED) {
|
||||
return 0;
|
||||
} else if (this.runningState == State.RUNNING) {
|
||||
return System.nanoTime() - this.startTimeNanos;
|
||||
return EagRuntime.nanoTime() - this.startTimeNanos;
|
||||
}
|
||||
throw new IllegalStateException("Illegal running state has occurred.");
|
||||
}
|
||||
@ -497,7 +499,7 @@ public class StopWatch {
|
||||
if (this.runningState != State.SUSPENDED) {
|
||||
throw new IllegalStateException("Stopwatch must be suspended to resume. ");
|
||||
}
|
||||
this.startTimeNanos += System.nanoTime() - this.stopTimeNanos;
|
||||
this.startTimeNanos += EagRuntime.nanoTime() - this.stopTimeNanos;
|
||||
this.runningState = State.RUNNING;
|
||||
}
|
||||
|
||||
@ -518,7 +520,7 @@ public class StopWatch {
|
||||
if (this.runningState != State.RUNNING) {
|
||||
throw new IllegalStateException("Stopwatch is not running. ");
|
||||
}
|
||||
this.stopTimeNanos = System.nanoTime();
|
||||
this.stopTimeNanos = EagRuntime.nanoTime();
|
||||
this.splitState = SplitState.SPLIT;
|
||||
}
|
||||
|
||||
@ -540,7 +542,7 @@ public class StopWatch {
|
||||
if (this.runningState != State.UNSTARTED) {
|
||||
throw new IllegalStateException("Stopwatch already started. ");
|
||||
}
|
||||
this.startTimeNanos = System.nanoTime();
|
||||
this.startTimeNanos = EagRuntime.nanoTime();
|
||||
this.startTimeMillis = System.currentTimeMillis();
|
||||
this.runningState = State.RUNNING;
|
||||
}
|
||||
@ -561,7 +563,7 @@ public class StopWatch {
|
||||
throw new IllegalStateException("Stopwatch is not running. ");
|
||||
}
|
||||
if (this.runningState == State.RUNNING) {
|
||||
this.stopTimeNanos = System.nanoTime();
|
||||
this.stopTimeNanos = EagRuntime.nanoTime();
|
||||
this.stopTimeMillis = System.currentTimeMillis();
|
||||
}
|
||||
this.runningState = State.STOPPED;
|
||||
@ -583,7 +585,7 @@ public class StopWatch {
|
||||
if (this.runningState != State.RUNNING) {
|
||||
throw new IllegalStateException("Stopwatch must be running to suspend. ");
|
||||
}
|
||||
this.stopTimeNanos = System.nanoTime();
|
||||
this.stopTimeNanos = EagRuntime.nanoTime();
|
||||
this.stopTimeMillis = System.currentTimeMillis();
|
||||
this.runningState = State.SUSPENDED;
|
||||
}
|
||||
|
@ -4,22 +4,17 @@ package org.json;
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
@ -378,15 +373,15 @@ public class JSONObject {
|
||||
* @throws JSONException
|
||||
* If a getter returned a non-finite number.
|
||||
*/
|
||||
public JSONObject(Object bean) {
|
||||
this();
|
||||
this.populateMap(bean);
|
||||
}
|
||||
|
||||
private JSONObject(Object bean, Set<Object> objectsRecord) {
|
||||
this();
|
||||
this.populateMap(bean, objectsRecord);
|
||||
}
|
||||
// public JSONObject(Object bean) {
|
||||
// this();
|
||||
// this.populateMap(bean);
|
||||
// }
|
||||
//
|
||||
// private JSONObject(Object bean, Set<Object> objectsRecord) {
|
||||
// this();
|
||||
// this.populateMap(bean, objectsRecord);
|
||||
// }
|
||||
|
||||
/**
|
||||
* Construct a JSONObject from an Object, using reflection to find the
|
||||
@ -1725,104 +1720,104 @@ public class JSONObject {
|
||||
* @throws JSONException
|
||||
* If a getter returned a non-finite number.
|
||||
*/
|
||||
private void populateMap(Object bean) {
|
||||
populateMap(bean, Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>()));
|
||||
}
|
||||
// private void populateMap(Object bean) {
|
||||
// populateMap(bean, Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>()));
|
||||
// }
|
||||
|
||||
private void populateMap(Object bean, Set<Object> objectsRecord) {
|
||||
Class<?> klass = bean.getClass();
|
||||
|
||||
// If klass is a System class then set includeSuperClass to false.
|
||||
|
||||
boolean includeSuperClass = klass.getClassLoader() != null;
|
||||
|
||||
Method[] methods = includeSuperClass ? klass.getMethods() : klass.getDeclaredMethods();
|
||||
for (final Method method : methods) {
|
||||
final int modifiers = method.getModifiers();
|
||||
if (Modifier.isPublic(modifiers)
|
||||
&& !Modifier.isStatic(modifiers)
|
||||
&& method.getParameterTypes().length == 0
|
||||
&& !method.isBridge()
|
||||
&& method.getReturnType() != Void.TYPE
|
||||
&& isValidMethodName(method.getName())) {
|
||||
final String key = getKeyNameFromMethod(method);
|
||||
if (key != null && !key.isEmpty()) {
|
||||
try {
|
||||
final Object result = method.invoke(bean);
|
||||
if (result != null) {
|
||||
// check cyclic dependency and throw error if needed
|
||||
// the wrap and populateMap combination method is
|
||||
// itself DFS recursive
|
||||
if (objectsRecord.contains(result)) {
|
||||
throw recursivelyDefinedObjectException(key);
|
||||
}
|
||||
|
||||
objectsRecord.add(result);
|
||||
|
||||
testValidity(result);
|
||||
this.map.put(key, wrap(result, objectsRecord));
|
||||
|
||||
objectsRecord.remove(result);
|
||||
|
||||
// we don't use the result anywhere outside of wrap
|
||||
// if it's a resource we should be sure to close it
|
||||
// after calling toString
|
||||
if (result instanceof Closeable) {
|
||||
try {
|
||||
((Closeable) result).close();
|
||||
} catch (IOException ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IllegalAccessException ignore) {
|
||||
} catch (IllegalArgumentException ignore) {
|
||||
} catch (InvocationTargetException ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isValidMethodName(String name) {
|
||||
return !"getClass".equals(name) && !"getDeclaringClass".equals(name);
|
||||
}
|
||||
|
||||
private static String getKeyNameFromMethod(Method method) {
|
||||
final int ignoreDepth = getAnnotationDepth(method, JSONPropertyIgnore.class);
|
||||
if (ignoreDepth > 0) {
|
||||
final int forcedNameDepth = getAnnotationDepth(method, JSONPropertyName.class);
|
||||
if (forcedNameDepth < 0 || ignoreDepth <= forcedNameDepth) {
|
||||
// the hierarchy asked to ignore, and the nearest name override
|
||||
// was higher or non-existent
|
||||
return null;
|
||||
}
|
||||
}
|
||||
JSONPropertyName annotation = getAnnotation(method, JSONPropertyName.class);
|
||||
if (annotation != null && annotation.value() != null && !annotation.value().isEmpty()) {
|
||||
return annotation.value();
|
||||
}
|
||||
String key;
|
||||
final String name = method.getName();
|
||||
if (name.startsWith("get") && name.length() > 3) {
|
||||
key = name.substring(3);
|
||||
} else if (name.startsWith("is") && name.length() > 2) {
|
||||
key = name.substring(2);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
// if the first letter in the key is not uppercase, then skip.
|
||||
// This is to maintain backwards compatibility before PR406
|
||||
// (https://github.com/stleary/JSON-java/pull/406/)
|
||||
if (key.length() == 0 || Character.isLowerCase(key.charAt(0))) {
|
||||
return null;
|
||||
}
|
||||
if (key.length() == 1) {
|
||||
key = key.toLowerCase(Locale.ROOT);
|
||||
} else if (!Character.isUpperCase(key.charAt(1))) {
|
||||
key = key.substring(0, 1).toLowerCase(Locale.ROOT) + key.substring(1);
|
||||
}
|
||||
return key;
|
||||
}
|
||||
// private void populateMap(Object bean, Set<Object> objectsRecord) {
|
||||
// Class<?> klass = bean.getClass();
|
||||
//
|
||||
// // If klass is a System class then set includeSuperClass to false.
|
||||
//
|
||||
// boolean includeSuperClass = klass.getClassLoader() != null;
|
||||
//
|
||||
// Method[] methods = includeSuperClass ? klass.getMethods() : klass.getDeclaredMethods();
|
||||
// for (final Method method : methods) {
|
||||
// final int modifiers = method.getModifiers();
|
||||
// if (Modifier.isPublic(modifiers)
|
||||
// && !Modifier.isStatic(modifiers)
|
||||
// && method.getParameterTypes().length == 0
|
||||
// && !method.isBridge()
|
||||
// && method.getReturnType() != Void.TYPE
|
||||
// && isValidMethodName(method.getName())) {
|
||||
// final String key = getKeyNameFromMethod(method);
|
||||
// if (key != null && !key.isEmpty()) {
|
||||
// try {
|
||||
// final Object result = method.invoke(bean);
|
||||
// if (result != null) {
|
||||
// // check cyclic dependency and throw error if needed
|
||||
// // the wrap and populateMap combination method is
|
||||
// // itself DFS recursive
|
||||
// if (objectsRecord.contains(result)) {
|
||||
// throw recursivelyDefinedObjectException(key);
|
||||
// }
|
||||
//
|
||||
// objectsRecord.add(result);
|
||||
//
|
||||
// testValidity(result);
|
||||
// this.map.put(key, wrap(result, objectsRecord));
|
||||
//
|
||||
// objectsRecord.remove(result);
|
||||
//
|
||||
// // we don't use the result anywhere outside of wrap
|
||||
// // if it's a resource we should be sure to close it
|
||||
// // after calling toString
|
||||
// if (result instanceof Closeable) {
|
||||
// try {
|
||||
// ((Closeable) result).close();
|
||||
// } catch (IOException ignore) {
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// } catch (IllegalAccessException ignore) {
|
||||
// } catch (IllegalArgumentException ignore) {
|
||||
// } catch (InvocationTargetException ignore) {
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private static boolean isValidMethodName(String name) {
|
||||
// return !"getClass".equals(name) && !"getDeclaringClass".equals(name);
|
||||
// }
|
||||
//
|
||||
// private static String getKeyNameFromMethod(Method method) {
|
||||
// final int ignoreDepth = getAnnotationDepth(method, JSONPropertyIgnore.class);
|
||||
// if (ignoreDepth > 0) {
|
||||
// final int forcedNameDepth = getAnnotationDepth(method, JSONPropertyName.class);
|
||||
// if (forcedNameDepth < 0 || ignoreDepth <= forcedNameDepth) {
|
||||
// // the hierarchy asked to ignore, and the nearest name override
|
||||
// // was higher or non-existent
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
// JSONPropertyName annotation = getAnnotation(method, JSONPropertyName.class);
|
||||
// if (annotation != null && annotation.value() != null && !annotation.value().isEmpty()) {
|
||||
// return annotation.value();
|
||||
// }
|
||||
// String key;
|
||||
// final String name = method.getName();
|
||||
// if (name.startsWith("get") && name.length() > 3) {
|
||||
// key = name.substring(3);
|
||||
// } else if (name.startsWith("is") && name.length() > 2) {
|
||||
// key = name.substring(2);
|
||||
// } else {
|
||||
// return null;
|
||||
// }
|
||||
// // if the first letter in the key is not uppercase, then skip.
|
||||
// // This is to maintain backwards compatibility before PR406
|
||||
// // (https://github.com/stleary/JSON-java/pull/406/)
|
||||
// if (key.length() == 0 || Character.isLowerCase(key.charAt(0))) {
|
||||
// return null;
|
||||
// }
|
||||
// if (key.length() == 1) {
|
||||
// key = key.toLowerCase(Locale.ROOT);
|
||||
// } else if (!Character.isUpperCase(key.charAt(1))) {
|
||||
// key = key.substring(0, 1).toLowerCase(Locale.ROOT) + key.substring(1);
|
||||
// }
|
||||
// return key;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Searches the class hierarchy to see if the method or it's super
|
||||
@ -2734,10 +2729,11 @@ public class JSONObject {
|
||||
|| object.getClass().getClassLoader() == null) {
|
||||
return object.toString();
|
||||
}
|
||||
if (objectsRecord != null) {
|
||||
return new JSONObject(object, objectsRecord);
|
||||
}
|
||||
return new JSONObject(object);
|
||||
// if (objectsRecord != null) {
|
||||
// return new JSONObject(object, objectsRecord);
|
||||
// }
|
||||
// return new JSONObject(object);
|
||||
throw new UnsupportedOperationException("Unsupported type: " + object.getClass().getSimpleName());
|
||||
}
|
||||
catch (JSONException exception) {
|
||||
throw exception;
|
||||
|
Reference in New Issue
Block a user