Skip to content

Commit

Permalink
update VariableMirrorTranslator to bring it up to standards (apache#3647
Browse files Browse the repository at this point in the history
)

I initially started cleaning up VariableMirrorTranslator.java as part of
some mass deprecation cleanup work I am doing.

The changes for handling the autobox/unbox of the floating-point class turned
into a general cleanup of the VariableMirrorTranslator class.

- updated autobox/unbox
- using multi-catch now
- using strings in switch
- update a couple of places should be using diamond operator
  • Loading branch information
BradWalker authored Feb 20, 2022
1 parent 31085b5 commit de854f8
Showing 1 changed file with 70 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ public class VariableMirrorTranslator {

private static final Logger logger = Logger.getLogger(VariableMirrorTranslator.class.getName());

private static final Object NO_MIRROR = new String("NO_MIRROR");
private static final Object NO_MIRROR = "NO_MIRROR";

private VariableMirrorTranslator() {}

static Object createMirrorObject(Value value) {
return createMirrorObject(value, new HashMap<Value, Object>());
return createMirrorObject(value, new HashMap<>());
}

private static Object createMirrorObject(Value value, Map<Value, Object> mirrorsMap) {
Expand All @@ -112,33 +112,33 @@ private static Object createMirrorObject(Value value, Map<Value, Object> mirrors
return ShortenedStrings.getStringWithLengthControl((StringReference) value);
}
return createMirrorObject((ObjectReference) value, (ReferenceType) type, clazz, mirrorsMap);
} catch (ClassNotFoundException ex) {
} catch (ClassNotPreparedExceptionWrapper ex) {
} catch (ClassNotFoundException | ClassNotPreparedExceptionWrapper ex) {
}
} else {
if ("boolean".equals(typeStr)) {
return Boolean.valueOf(((BooleanValue) value).booleanValue());
} else if ("byte".equals(typeStr)) {
return new Byte(((ByteValue) value).byteValue());
} else if ("char".equals(typeStr)) {
return new Character(((CharValue) value).charValue());
} else if ("short".equals(typeStr)) {
return new Short(((ShortValue) value).shortValue());
} else if ("int".equals(typeStr)) {
return new Integer(((IntegerValue) value).intValue());
} else if ("long".equals(typeStr)) {
return new Long(((LongValue) value).longValue());
} else if ("float".equals(typeStr)) {
return new Float(((FloatValue) value).floatValue());
} else if ("double".equals(typeStr)) {
return new Double(((DoubleValue) value).doubleValue());
} else {
throw new IllegalArgumentException("Unknown primitive type: "+typeStr+" from "+type);
if (null == typeStr) {
throw new IllegalArgumentException("Unknown primitive type: " + typeStr + " from " + type);
} else switch (typeStr) {
case "boolean":
return ((BooleanValue) value).booleanValue();
case "byte":
return ((ByteValue) value).byteValue();
case "char":
return ((CharValue) value).charValue();
case "short":
return ((ShortValue) value).shortValue();
case "int":
return ((IntegerValue) value).intValue();
case "long":
return ((LongValue) value).longValue();
case "float":
return ((FloatValue) value).floatValue();
case "double":
return ((DoubleValue) value).doubleValue();
default:
throw new IllegalArgumentException("Unknown primitive type: "+typeStr+" from "+type);
}
}
} catch (InternalExceptionWrapper ex) {
} catch (VMDisconnectedExceptionWrapper ex) {
} catch (ObjectCollectedExceptionWrapper ex) {
} catch (InternalExceptionWrapper | VMDisconnectedExceptionWrapper | ObjectCollectedExceptionWrapper ex) {
}
return null;
}
Expand All @@ -156,17 +156,7 @@ private static Object createPristineInstanceOf(Class clazz) {
Object newInstance = constructor.newInstance();
return newInstance;

} catch (InstantiationException ex) {
logger.log(Level.INFO, "Problem while creating a pristine instance of "+clazz, ex);
} catch (IllegalAccessException ex) {
logger.log(Level.INFO, "Problem while creating a pristine instance of "+clazz, ex);
} catch (IllegalArgumentException ex) {
logger.log(Level.INFO, "Problem while creating a pristine instance of "+clazz, ex);
} catch (InvocationTargetException ex) {
logger.log(Level.INFO, "Problem while creating a pristine instance of "+clazz, ex);
} catch (NoSuchMethodException ex) {
logger.log(Level.INFO, "Problem while creating a pristine instance of "+clazz, ex);
} catch (SecurityException ex) {
} catch (ReflectiveOperationException | IllegalArgumentException | SecurityException ex) {
logger.log(Level.INFO, "Problem while creating a pristine instance of "+clazz, ex);
}
return null;
Expand Down Expand Up @@ -235,7 +225,7 @@ private static Object createMirrorObject(ObjectReference value, ReferenceType ty
}
mirrorsMap.put(value, newInstance);
List<Field> fields = ReferenceTypeWrapper.allFields0(type);
List<Field> fieldsToAskFor = new ArrayList<Field>();
List<Field> fieldsToAskFor = new ArrayList<>();
for (Field f : fields) {
if (!f.isStatic()) {
fieldsToAskFor.add(f);
Expand Down Expand Up @@ -322,7 +312,7 @@ private static Object createSpecialized(ObjectReference value, ReferenceType typ

private static Map<Field, Value> getFieldValues(ObjectReference value, ReferenceType type, String... names) throws InternalExceptionWrapper, VMDisconnectedExceptionWrapper, ObjectCollectedExceptionWrapper, ClassNotPreparedExceptionWrapper {
final int n = names.length;
List<Field> fieldsToAskFor = new ArrayList<Field>(n);
List<Field> fieldsToAskFor = new ArrayList<>(n);
for (String name : names) {
Field field = ReferenceTypeWrapper.fieldByName(type, name);
if (field == null) {
Expand Down Expand Up @@ -377,18 +367,10 @@ public static Value createValueFromMirror(Object mirror, boolean isObject, JPDAD

return createValueFromMirror(mirror, clazz, isObject, vm, threadReference);

} catch (IncompatibleThreadStateException ex) {
} catch (IncompatibleThreadStateException | UnsupportedOperationExceptionWrapper | PropertyVetoException ex) {
InvalidObjectException ioex = new InvalidObjectException(ex.getLocalizedMessage());
ioex.initCause(ex);
throw ioex;
} catch (UnsupportedOperationExceptionWrapper ex) {
InvalidObjectException ioex = new InvalidObjectException(ex.getLocalizedMessage());
ioex.initCause(ex);
throw ioex;
} catch (PropertyVetoException pvex) {
InvalidObjectException ioex = new InvalidObjectException(pvex.getLocalizedMessage());
ioex.initCause(pvex);
throw ioex;
} finally {
if (invoking) {
currentThread.notifyMethodInvokeDone();
Expand All @@ -407,28 +389,28 @@ private static Value createValueFromMirror(Object mirror, Class clazz, boolean i
boolean isPrimitive = clazz.isPrimitive();
if (!isObject || isPrimitive) { // Primitive type
if (Boolean.TYPE.equals(clazz) || Boolean.class.equals(clazz)) {
return vm.mirrorOf(((Boolean) mirror).booleanValue());
return vm.mirrorOf(((Boolean) mirror));
}
if (Byte.TYPE.equals(clazz) || Byte.class.equals(clazz)) {
return vm.mirrorOf(((Byte) mirror).byteValue());
return vm.mirrorOf(((Byte) mirror));
}
if (Character.TYPE.equals(clazz) || Character.class.equals(clazz)) {
return vm.mirrorOf(((Character) mirror).charValue());
return vm.mirrorOf(((Character) mirror));
}
if (Short.TYPE.equals(clazz) || Short.class.equals(clazz)) {
return vm.mirrorOf(((Short) mirror).shortValue());
return vm.mirrorOf(((Short) mirror));
}
if (Integer.TYPE.equals(clazz) || Integer.class.equals(clazz)) {
return vm.mirrorOf(((Integer) mirror).intValue());
return vm.mirrorOf(((Integer) mirror));
}
if (Long.TYPE.equals(clazz) || Long.class.equals(clazz)) {
return vm.mirrorOf(((Long) mirror).longValue());
return vm.mirrorOf(((Long) mirror));
}
if (Float.TYPE.equals(clazz) || Float.class.equals(clazz)) {
return vm.mirrorOf(((Float) mirror).floatValue());
return vm.mirrorOf(((Float) mirror));
}
if (Double.TYPE.equals(clazz) || Double.class.equals(clazz)) {
return vm.mirrorOf(((Double) mirror).doubleValue());
return vm.mirrorOf(((Double) mirror));
}
throw new InvalidObjectException("Unknown primitive type "+clazz);
} else {
Expand Down Expand Up @@ -464,7 +446,7 @@ private static Value createValueFromMirror(Object mirror, Class clazz, boolean i
throw ioex;
}
List<Field> fields = ReferenceTypeWrapper.allFields0(type);
List<Field> fieldsToSet = new ArrayList<Field>();
List<Field> fieldsToSet = new ArrayList<>();
for (Field f : fields) {
if (!f.isStatic()) {
fieldsToSet.add(f);
Expand All @@ -483,10 +465,6 @@ private static Value createValueFromMirror(Object mirror, Class clazz, boolean i
field.setAccessible(true);
fm = field.get(mirror);
fieldType = field.getType();
//} catch (NoSuchFieldException nsfex) {
// InvalidObjectException ioex = new InvalidObjectException("No field "+name+" of class "+clazz);
// ioex.initCause(nsfex);
// throw ioex;
} catch (SecurityException seex) {
InvalidObjectException ioex = new InvalidObjectException("Retrieving field "+name+" of class "+clazz);
ioex.initCause(seex);
Expand Down Expand Up @@ -652,27 +630,41 @@ private static void setValueToFinalField(ObjectReference obj, String name, Class
}

private static Type boxType(Type t) {
Type rval = t;

if (t instanceof ClassType) {
String name = ((ClassType) t).name();
if (name.equals("java.lang.Boolean")) {
t = t.virtualMachine().mirrorOf(true).type();
} else if (name.equals("java.lang.Byte")) {
t = t.virtualMachine().mirrorOf((byte) 10).type();
} else if (name.equals("java.lang.Character")) {
t = t.virtualMachine().mirrorOf('a').type();
} else if (name.equals("java.lang.Integer")) {
t = t.virtualMachine().mirrorOf(10).type();
} else if (name.equals("java.lang.Long")) {
t = t.virtualMachine().mirrorOf(10l).type();
} else if (name.equals("java.lang.Short")) {
t = t.virtualMachine().mirrorOf((short)10).type();
} else if (name.equals("java.lang.Float")) {
t = t.virtualMachine().mirrorOf(10f).type();
} else if (name.equals("java.lang.Double")) {
t = t.virtualMachine().mirrorOf(10.0).type();
String name = ((ClassType)t).name();

switch (name) {
case "java.lang.Boolean":
rval = t.virtualMachine().mirrorOf(true).type();
break;
case "java.lang.Byte":
rval = t.virtualMachine().mirrorOf((byte) 10).type();
break;
case "java.lang.Character":
rval = t.virtualMachine().mirrorOf('a').type();
break;
case "java.lang.Integer":
rval = t.virtualMachine().mirrorOf(10).type();
break;
case "java.lang.Long":
rval = t.virtualMachine().mirrorOf(10l).type();
break;
case "java.lang.Short":
rval = t.virtualMachine().mirrorOf((short)10).type();
break;
case "java.lang.Float":
rval = t.virtualMachine().mirrorOf(10f).type();
break;
case "java.lang.Double":
rval = t.virtualMachine().mirrorOf(10.0).type();
break;
default:
break;
}
}
return t;
return rval;
}

private static ObjectReference getDeclaredOrInheritedField(ClassType clazz, String name, VirtualMachine vm, ThreadReference thread) throws InternalExceptionWrapper, VMDisconnectedExceptionWrapper, ClassNotPreparedExceptionWrapper, ClassNotLoadedException, ObjectCollectedExceptionWrapper, IncompatibleThreadStateException, UnsupportedOperationExceptionWrapper, InvalidTypeException {
Expand Down Expand Up @@ -702,5 +694,4 @@ private static ObjectReference getDeclaredOrInheritedField(ClassType clazz, Stri
return null;
}
}

}

0 comments on commit de854f8

Please sign in to comment.