mirror of
https://git.huckle.dev/Huckles-Minecraft-Archive/jpexs-decompiler.git
synced 2026-09-25 06:31:17 +00:00
fixed some Netbeans code hints
This commit is contained in:
@@ -30,10 +30,7 @@ import com.jpexs.decompiler.flash.console.ContextMenuTools;
|
||||
import com.jpexs.decompiler.flash.gui.proxy.ProxyFrame;
|
||||
import com.jpexs.decompiler.flash.helpers.SWFDecompilerPlugin;
|
||||
import com.jpexs.decompiler.flash.tags.base.FontTag;
|
||||
import com.jpexs.decompiler.flash.timeline.Timeline;
|
||||
import com.jpexs.decompiler.flash.timeline.TweenDetector;
|
||||
import com.jpexs.decompiler.flash.treeitems.SWFList;
|
||||
import com.jpexs.decompiler.flash.types.MATRIX;
|
||||
import com.jpexs.helpers.Cache;
|
||||
import com.jpexs.helpers.CancellableWorker;
|
||||
import com.jpexs.helpers.Helper;
|
||||
@@ -53,7 +50,6 @@ import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
|
||||
@@ -178,7 +178,7 @@ public class ASMSourceEditorPane extends LineMarkedEditorPane implements CaretLi
|
||||
public void exec() {
|
||||
HashMap<Integer, Object> args = new HashMap<>();
|
||||
args.put(0, new Object()); //object "this"
|
||||
args.put(1, new Long(466561)); //param1
|
||||
args.put(1, 466561L); //param1
|
||||
Object o = abc.bodies.get(bodyIndex).getCode().execute(args, abc.constants);
|
||||
View.showMessageDialog(this, "Returned object:" + o.toString());
|
||||
}
|
||||
|
||||
@@ -1,128 +1,128 @@
|
||||
/*
|
||||
* Copyright (C) 2010-2014 JPEXS
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.jpexs.decompiler.flash.gui.abc;
|
||||
|
||||
import com.jpexs.decompiler.flash.gui.AppDialog;
|
||||
import com.jpexs.decompiler.flash.gui.View;
|
||||
import java.awt.Component;
|
||||
import java.awt.Container;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.FlowLayout;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Hashtable;
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JCheckBox;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JSlider;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author JPEXS
|
||||
*/
|
||||
public class DeobfuscationDialog extends AppDialog implements ActionListener {
|
||||
|
||||
static final String ACTION_OK = "OK";
|
||||
static final String ACTION_CANCEL = "CANCEL";
|
||||
|
||||
public JCheckBox processAllCheckbox = new JCheckBox(translate("processallclasses"));
|
||||
public JSlider codeProcessingLevel;
|
||||
public boolean ok = false;
|
||||
public static final int LEVEL_REMOVE_DEAD_CODE = 1;
|
||||
public static final int LEVEL_REMOVE_TRAPS = 2;
|
||||
public static final int LEVEL_RESTORE_CONTROL_FLOW = 3;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public DeobfuscationDialog() {
|
||||
setDefaultCloseOperation(HIDE_ON_CLOSE);
|
||||
setSize(new Dimension(330, 270));
|
||||
setTitle(translate("dialog.title"));
|
||||
Container cp = getContentPane();
|
||||
cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS));
|
||||
codeProcessingLevel = new JSlider(JSlider.VERTICAL, 1, 3, 3);
|
||||
codeProcessingLevel.setMajorTickSpacing(1);
|
||||
codeProcessingLevel.setPaintTicks(true);
|
||||
codeProcessingLevel.setMinorTickSpacing(1);
|
||||
codeProcessingLevel.setSnapToTicks(true);
|
||||
JLabel lab1 = new JLabel(translate("deobfuscation.level"));
|
||||
//lab1.setBounds(30, 0, getWidth() - 60, 25);
|
||||
lab1.setAlignmentX(0.5f);
|
||||
cp.add(lab1);
|
||||
Hashtable labelTable = new Hashtable();
|
||||
//labelTable.put(new Integer(LEVEL_NONE), new JLabel("None"));
|
||||
|
||||
labelTable.put(new Integer(LEVEL_REMOVE_DEAD_CODE), new JLabel(translate("deobfuscation.removedeadcode")));
|
||||
labelTable.put(new Integer(LEVEL_REMOVE_TRAPS), new JLabel(translate("deobfuscation.removetraps")));
|
||||
labelTable.put(new Integer(LEVEL_RESTORE_CONTROL_FLOW), new JLabel(translate("deobfuscation.restorecontrolflow")));
|
||||
codeProcessingLevel.setLabelTable(labelTable);
|
||||
|
||||
codeProcessingLevel.setPaintLabels(true);
|
||||
codeProcessingLevel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
//codeProcessingLevel.setSize(300, 200);
|
||||
|
||||
//codeProcessingLevel.setBounds(30, 25, getWidth() - 60, 125);
|
||||
codeProcessingLevel.setAlignmentX(0.5f);
|
||||
add(codeProcessingLevel);
|
||||
//processAllCheckbox.setBounds(50, 150, getWidth() - 100, 25);
|
||||
processAllCheckbox.setAlignmentX(0.5f);
|
||||
add(processAllCheckbox);
|
||||
|
||||
processAllCheckbox.setSelected(true);
|
||||
|
||||
JButton cancelButton = new JButton(translate("button.cancel"));
|
||||
cancelButton.addActionListener(this);
|
||||
cancelButton.setActionCommand(ACTION_CANCEL);
|
||||
JButton okButton = new JButton(translate("button.ok"));
|
||||
okButton.addActionListener(this);
|
||||
okButton.setActionCommand(ACTION_OK);
|
||||
|
||||
JPanel buttonsPanel = new JPanel(new FlowLayout());
|
||||
buttonsPanel.add(okButton);
|
||||
buttonsPanel.add(cancelButton);
|
||||
buttonsPanel.setAlignmentX(0.5f);
|
||||
cp.add(buttonsPanel);
|
||||
|
||||
setModal(true);
|
||||
View.centerScreen(this);
|
||||
//View.setWindowIcon(this);
|
||||
setIconImage(View.loadImage("deobfuscate16"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
switch (e.getActionCommand()) {
|
||||
case ACTION_OK:
|
||||
ok = true;
|
||||
setVisible(false);
|
||||
break;
|
||||
case ACTION_CANCEL:
|
||||
ok = false;
|
||||
setVisible(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVisible(boolean b) {
|
||||
if (b) {
|
||||
ok = false;
|
||||
}
|
||||
super.setVisible(b);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright (C) 2010-2014 JPEXS
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.jpexs.decompiler.flash.gui.abc;
|
||||
|
||||
import com.jpexs.decompiler.flash.gui.AppDialog;
|
||||
import com.jpexs.decompiler.flash.gui.View;
|
||||
import java.awt.Component;
|
||||
import java.awt.Container;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.FlowLayout;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Hashtable;
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JCheckBox;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JSlider;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author JPEXS
|
||||
*/
|
||||
public class DeobfuscationDialog extends AppDialog implements ActionListener {
|
||||
|
||||
static final String ACTION_OK = "OK";
|
||||
static final String ACTION_CANCEL = "CANCEL";
|
||||
|
||||
public JCheckBox processAllCheckbox = new JCheckBox(translate("processallclasses"));
|
||||
public JSlider codeProcessingLevel;
|
||||
public boolean ok = false;
|
||||
public static final int LEVEL_REMOVE_DEAD_CODE = 1;
|
||||
public static final int LEVEL_REMOVE_TRAPS = 2;
|
||||
public static final int LEVEL_RESTORE_CONTROL_FLOW = 3;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public DeobfuscationDialog() {
|
||||
setDefaultCloseOperation(HIDE_ON_CLOSE);
|
||||
setSize(new Dimension(330, 270));
|
||||
setTitle(translate("dialog.title"));
|
||||
Container cp = getContentPane();
|
||||
cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS));
|
||||
codeProcessingLevel = new JSlider(JSlider.VERTICAL, 1, 3, 3);
|
||||
codeProcessingLevel.setMajorTickSpacing(1);
|
||||
codeProcessingLevel.setPaintTicks(true);
|
||||
codeProcessingLevel.setMinorTickSpacing(1);
|
||||
codeProcessingLevel.setSnapToTicks(true);
|
||||
JLabel lab1 = new JLabel(translate("deobfuscation.level"));
|
||||
//lab1.setBounds(30, 0, getWidth() - 60, 25);
|
||||
lab1.setAlignmentX(0.5f);
|
||||
cp.add(lab1);
|
||||
Hashtable<Integer, JLabel> labelTable = new Hashtable<>();
|
||||
//labelTable.put(LEVEL_NONE, new JLabel("None"));
|
||||
|
||||
labelTable.put(LEVEL_REMOVE_DEAD_CODE, new JLabel(translate("deobfuscation.removedeadcode")));
|
||||
labelTable.put(LEVEL_REMOVE_TRAPS, new JLabel(translate("deobfuscation.removetraps")));
|
||||
labelTable.put(LEVEL_RESTORE_CONTROL_FLOW, new JLabel(translate("deobfuscation.restorecontrolflow")));
|
||||
codeProcessingLevel.setLabelTable(labelTable);
|
||||
|
||||
codeProcessingLevel.setPaintLabels(true);
|
||||
codeProcessingLevel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
//codeProcessingLevel.setSize(300, 200);
|
||||
|
||||
//codeProcessingLevel.setBounds(30, 25, getWidth() - 60, 125);
|
||||
codeProcessingLevel.setAlignmentX(0.5f);
|
||||
add(codeProcessingLevel);
|
||||
//processAllCheckbox.setBounds(50, 150, getWidth() - 100, 25);
|
||||
processAllCheckbox.setAlignmentX(0.5f);
|
||||
add(processAllCheckbox);
|
||||
|
||||
processAllCheckbox.setSelected(true);
|
||||
|
||||
JButton cancelButton = new JButton(translate("button.cancel"));
|
||||
cancelButton.addActionListener(this);
|
||||
cancelButton.setActionCommand(ACTION_CANCEL);
|
||||
JButton okButton = new JButton(translate("button.ok"));
|
||||
okButton.addActionListener(this);
|
||||
okButton.setActionCommand(ACTION_OK);
|
||||
|
||||
JPanel buttonsPanel = new JPanel(new FlowLayout());
|
||||
buttonsPanel.add(okButton);
|
||||
buttonsPanel.add(cancelButton);
|
||||
buttonsPanel.setAlignmentX(0.5f);
|
||||
cp.add(buttonsPanel);
|
||||
|
||||
setModal(true);
|
||||
View.centerScreen(this);
|
||||
//View.setWindowIcon(this);
|
||||
setIconImage(View.loadImage("deobfuscate16"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
switch (e.getActionCommand()) {
|
||||
case ACTION_OK:
|
||||
ok = true;
|
||||
setVisible(false);
|
||||
break;
|
||||
case ACTION_CANCEL:
|
||||
ok = false;
|
||||
setVisible(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVisible(boolean b) {
|
||||
if (b) {
|
||||
ok = false;
|
||||
}
|
||||
super.setVisible(b);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,303 +1,303 @@
|
||||
/*
|
||||
* Copyright (C) 2010-2014 JPEXS
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.jpexs.decompiler.flash.gui.generictageditors;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author JPEXS
|
||||
*/
|
||||
public class ReflectionTools {
|
||||
|
||||
public static Object getValue(Object obj, Field field, int index) throws IllegalArgumentException, IllegalAccessException {
|
||||
if (getFieldSubSize(obj, field) <= index) {
|
||||
return null;
|
||||
}
|
||||
Object value = field.get(obj);
|
||||
if (List.class.isAssignableFrom(field.getType())) {
|
||||
return ((List) value).get(index);
|
||||
}
|
||||
|
||||
if (field.getType().isArray()) {
|
||||
return Array.get(value, index);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static boolean needsIndex(Field field) {
|
||||
if (List.class.isAssignableFrom(field.getType())) {
|
||||
return true;
|
||||
} else if (field.getType().isArray()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static int getFieldSubSize(Object obj, Field field) {
|
||||
Object val;
|
||||
try {
|
||||
val = field.get(obj);
|
||||
} catch (IllegalArgumentException | IllegalAccessException ex) {
|
||||
return 0;
|
||||
}
|
||||
if (List.class.isAssignableFrom(field.getType())) {
|
||||
return ((List) val).size();
|
||||
} else if (field.getType().isArray()) {
|
||||
return Array.getLength(val);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void setValue(Object obj, Field field, int index, Object newValue) throws IllegalArgumentException, IllegalAccessException {
|
||||
Object value = field.get(obj);
|
||||
if (needsIndex(field) && index >= getFieldSubSize(obj, field)) { //outofbounds, ignore
|
||||
return;
|
||||
}
|
||||
if (List.class.isAssignableFrom(field.getType())) {
|
||||
((List) value).set(index, newValue);
|
||||
} else if (field.getType().isArray()) {
|
||||
Array.set(value, index, newValue);
|
||||
} else {
|
||||
field.set(obj, newValue);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean canInstantiate(Class cls) {
|
||||
if (cls.isInterface()) {
|
||||
return false;
|
||||
}
|
||||
if (Modifier.isAbstract(cls.getModifiers())) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean canAddToField(Object object, Field field) {
|
||||
if (List.class.isAssignableFrom(field.getType())) {
|
||||
|
||||
ParameterizedType listType = (ParameterizedType) field.getGenericType();
|
||||
Class<?> parameterClass = (Class<?>) listType.getActualTypeArguments()[0];
|
||||
return canInstantiate(parameterClass);
|
||||
}
|
||||
|
||||
if (field.getType().isArray()) {
|
||||
Object arrValue;
|
||||
try {
|
||||
arrValue = field.get(object);
|
||||
} catch (IllegalArgumentException | IllegalAccessException ex) {
|
||||
Logger.getLogger(ReflectionTools.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return false;
|
||||
}
|
||||
Class componentClass = arrValue.getClass().getComponentType();
|
||||
if (componentClass.isPrimitive()) {
|
||||
return true;
|
||||
}
|
||||
return canInstantiate(componentClass);
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
public static Object newInstanceOf(Class cls) throws InstantiationException, IllegalAccessException {
|
||||
if (cls == Integer.class || cls == int.class) {
|
||||
return new Integer(0);
|
||||
} else if (cls == Float.class || cls == float.class) {
|
||||
return new Float(0.0f);
|
||||
} else if (cls == Double.class || cls == double.class) {
|
||||
return new Double(0);
|
||||
} else if (cls == Long.class || cls == long.class) {
|
||||
return new Long(0L);
|
||||
}
|
||||
if (cls.isInterface()) {
|
||||
return null;
|
||||
}
|
||||
if (Modifier.isAbstract(cls.getModifiers())) {
|
||||
return null;
|
||||
}
|
||||
return cls.newInstance();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static boolean addToList(Object object, Field field, int index) {
|
||||
if (!List.class.isAssignableFrom(field.getType())) {
|
||||
return false;
|
||||
}
|
||||
List list;
|
||||
try {
|
||||
list = (List) field.get(object);
|
||||
} catch (IllegalArgumentException | IllegalAccessException ex) {
|
||||
Logger.getLogger(ReflectionTools.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return false;
|
||||
}
|
||||
ParameterizedType listType = (ParameterizedType) field.getGenericType();
|
||||
Class<?> parameterClass = (Class<?>) listType.getActualTypeArguments()[0];
|
||||
try {
|
||||
Object val = newInstanceOf(parameterClass);
|
||||
if (val == null) {
|
||||
return false;
|
||||
}
|
||||
list.add(index, val);
|
||||
} catch (InstantiationException | IllegalAccessException ex) {
|
||||
Logger.getLogger(ReflectionTools.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static Class<?> getFieldSubType(Object object, Field field) {
|
||||
if (field.getType().isArray()) {
|
||||
Object arrValue;
|
||||
try {
|
||||
arrValue = field.get(object);
|
||||
return arrValue.getClass().getComponentType();
|
||||
} catch (IllegalArgumentException | IllegalAccessException ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (List.class.isAssignableFrom(field.getType())) {
|
||||
ParameterizedType listType = (ParameterizedType) field.getGenericType();
|
||||
return (Class<?>) listType.getActualTypeArguments()[0];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean addToArray(Object object, Field field, int index, boolean notnull) {
|
||||
if (!field.getType().isArray()) {
|
||||
return false;
|
||||
}
|
||||
Object arrValue;
|
||||
try {
|
||||
arrValue = field.get(object);
|
||||
} catch (IllegalArgumentException | IllegalAccessException ex) {
|
||||
Logger.getLogger(ReflectionTools.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return false;
|
||||
}
|
||||
Class componentClass = arrValue.getClass().getComponentType();
|
||||
Object val = null;
|
||||
if (!componentClass.isPrimitive()) {
|
||||
try {
|
||||
val = newInstanceOf(componentClass);
|
||||
} catch (InstantiationException | IllegalAccessException ex) {
|
||||
Logger.getLogger(ReflectionTools.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return false;
|
||||
}
|
||||
if (val == null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int originalSize = Array.getLength(arrValue);
|
||||
Object copy = Array.newInstance(componentClass, originalSize + 1);
|
||||
|
||||
//Copy items before
|
||||
for (int i = 0; i < index; i++) {
|
||||
Array.set(copy, i, Array.get(arrValue, i));
|
||||
}
|
||||
if (val != null) {
|
||||
Array.set(copy, index, val);
|
||||
}
|
||||
//Copy items after
|
||||
for (int i = index; i < originalSize; i++) {
|
||||
Array.set(copy, i + 1, Array.get(arrValue, i));
|
||||
}
|
||||
try {
|
||||
field.set(object, copy);
|
||||
} catch (IllegalArgumentException | IllegalAccessException ex) {
|
||||
Logger.getLogger(ReflectionTools.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean addToField(Object object, Field field, int index, boolean notnull) {
|
||||
if (List.class.isAssignableFrom(field.getType())) {
|
||||
return addToList(object, field, index);
|
||||
}
|
||||
|
||||
if (field.getType().isArray()) {
|
||||
return addToArray(object, field, index, notnull);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean removeFromField(Object object, Field field, int index) {
|
||||
if (List.class.isAssignableFrom(field.getType())) {
|
||||
return removeFromList(object, field, index);
|
||||
}
|
||||
|
||||
if (field.getType().isArray()) {
|
||||
return removeFromArray(object, field, index);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean removeFromList(Object object, Field field, int index) {
|
||||
List list;
|
||||
try {
|
||||
list = (List) field.get(object);
|
||||
} catch (IllegalArgumentException | IllegalAccessException ex) {
|
||||
Logger.getLogger(ReflectionTools.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return false;
|
||||
}
|
||||
if (index < 0 || index >= list.size()) {
|
||||
return false;
|
||||
}
|
||||
list.remove(index);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean removeFromArray(Object object, Field field, int index) {
|
||||
Object arrValue;
|
||||
try {
|
||||
arrValue = field.get(object);
|
||||
} catch (IllegalArgumentException | IllegalAccessException ex) {
|
||||
Logger.getLogger(ReflectionTools.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return false;
|
||||
}
|
||||
Class componentClass = arrValue.getClass().getComponentType();
|
||||
int originalSize = Array.getLength(arrValue);
|
||||
Object copy = Array.newInstance(componentClass, originalSize - 1);
|
||||
int pos = 0;
|
||||
//copy all before index
|
||||
for (int i = 0; i < index; i++) {
|
||||
Array.set(copy, pos, Array.get(arrValue, i));
|
||||
pos++;
|
||||
}
|
||||
//copy all after index
|
||||
for (int i = index + 1; i < originalSize; i++) {
|
||||
Array.set(copy, pos, Array.get(arrValue, i));
|
||||
pos++;
|
||||
}
|
||||
try {
|
||||
field.set(object, copy);
|
||||
} catch (IllegalArgumentException | IllegalAccessException ex) {
|
||||
Logger.getLogger(ReflectionTools.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright (C) 2010-2014 JPEXS
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.jpexs.decompiler.flash.gui.generictageditors;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author JPEXS
|
||||
*/
|
||||
public class ReflectionTools {
|
||||
|
||||
public static Object getValue(Object obj, Field field, int index) throws IllegalArgumentException, IllegalAccessException {
|
||||
if (getFieldSubSize(obj, field) <= index) {
|
||||
return null;
|
||||
}
|
||||
Object value = field.get(obj);
|
||||
if (List.class.isAssignableFrom(field.getType())) {
|
||||
return ((List) value).get(index);
|
||||
}
|
||||
|
||||
if (field.getType().isArray()) {
|
||||
return Array.get(value, index);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static boolean needsIndex(Field field) {
|
||||
if (List.class.isAssignableFrom(field.getType())) {
|
||||
return true;
|
||||
} else if (field.getType().isArray()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static int getFieldSubSize(Object obj, Field field) {
|
||||
Object val;
|
||||
try {
|
||||
val = field.get(obj);
|
||||
} catch (IllegalArgumentException | IllegalAccessException ex) {
|
||||
return 0;
|
||||
}
|
||||
if (List.class.isAssignableFrom(field.getType())) {
|
||||
return ((List) val).size();
|
||||
} else if (field.getType().isArray()) {
|
||||
return Array.getLength(val);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void setValue(Object obj, Field field, int index, Object newValue) throws IllegalArgumentException, IllegalAccessException {
|
||||
Object value = field.get(obj);
|
||||
if (needsIndex(field) && index >= getFieldSubSize(obj, field)) { //outofbounds, ignore
|
||||
return;
|
||||
}
|
||||
if (List.class.isAssignableFrom(field.getType())) {
|
||||
((List) value).set(index, newValue);
|
||||
} else if (field.getType().isArray()) {
|
||||
Array.set(value, index, newValue);
|
||||
} else {
|
||||
field.set(obj, newValue);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean canInstantiate(Class cls) {
|
||||
if (cls.isInterface()) {
|
||||
return false;
|
||||
}
|
||||
if (Modifier.isAbstract(cls.getModifiers())) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean canAddToField(Object object, Field field) {
|
||||
if (List.class.isAssignableFrom(field.getType())) {
|
||||
|
||||
ParameterizedType listType = (ParameterizedType) field.getGenericType();
|
||||
Class<?> parameterClass = (Class<?>) listType.getActualTypeArguments()[0];
|
||||
return canInstantiate(parameterClass);
|
||||
}
|
||||
|
||||
if (field.getType().isArray()) {
|
||||
Object arrValue;
|
||||
try {
|
||||
arrValue = field.get(object);
|
||||
} catch (IllegalArgumentException | IllegalAccessException ex) {
|
||||
Logger.getLogger(ReflectionTools.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return false;
|
||||
}
|
||||
Class componentClass = arrValue.getClass().getComponentType();
|
||||
if (componentClass.isPrimitive()) {
|
||||
return true;
|
||||
}
|
||||
return canInstantiate(componentClass);
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
public static Object newInstanceOf(Class cls) throws InstantiationException, IllegalAccessException {
|
||||
if (cls == Integer.class || cls == int.class) {
|
||||
return 0;
|
||||
} else if (cls == Float.class || cls == float.class) {
|
||||
return 0.0f;
|
||||
} else if (cls == Double.class || cls == double.class) {
|
||||
return (double) 0;
|
||||
} else if (cls == Long.class || cls == long.class) {
|
||||
return 0L;
|
||||
}
|
||||
if (cls.isInterface()) {
|
||||
return null;
|
||||
}
|
||||
if (Modifier.isAbstract(cls.getModifiers())) {
|
||||
return null;
|
||||
}
|
||||
return cls.newInstance();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static boolean addToList(Object object, Field field, int index) {
|
||||
if (!List.class.isAssignableFrom(field.getType())) {
|
||||
return false;
|
||||
}
|
||||
List list;
|
||||
try {
|
||||
list = (List) field.get(object);
|
||||
} catch (IllegalArgumentException | IllegalAccessException ex) {
|
||||
Logger.getLogger(ReflectionTools.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return false;
|
||||
}
|
||||
ParameterizedType listType = (ParameterizedType) field.getGenericType();
|
||||
Class<?> parameterClass = (Class<?>) listType.getActualTypeArguments()[0];
|
||||
try {
|
||||
Object val = newInstanceOf(parameterClass);
|
||||
if (val == null) {
|
||||
return false;
|
||||
}
|
||||
list.add(index, val);
|
||||
} catch (InstantiationException | IllegalAccessException ex) {
|
||||
Logger.getLogger(ReflectionTools.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static Class<?> getFieldSubType(Object object, Field field) {
|
||||
if (field.getType().isArray()) {
|
||||
Object arrValue;
|
||||
try {
|
||||
arrValue = field.get(object);
|
||||
return arrValue.getClass().getComponentType();
|
||||
} catch (IllegalArgumentException | IllegalAccessException ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (List.class.isAssignableFrom(field.getType())) {
|
||||
ParameterizedType listType = (ParameterizedType) field.getGenericType();
|
||||
return (Class<?>) listType.getActualTypeArguments()[0];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean addToArray(Object object, Field field, int index, boolean notnull) {
|
||||
if (!field.getType().isArray()) {
|
||||
return false;
|
||||
}
|
||||
Object arrValue;
|
||||
try {
|
||||
arrValue = field.get(object);
|
||||
} catch (IllegalArgumentException | IllegalAccessException ex) {
|
||||
Logger.getLogger(ReflectionTools.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return false;
|
||||
}
|
||||
Class componentClass = arrValue.getClass().getComponentType();
|
||||
Object val = null;
|
||||
if (!componentClass.isPrimitive()) {
|
||||
try {
|
||||
val = newInstanceOf(componentClass);
|
||||
} catch (InstantiationException | IllegalAccessException ex) {
|
||||
Logger.getLogger(ReflectionTools.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return false;
|
||||
}
|
||||
if (val == null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int originalSize = Array.getLength(arrValue);
|
||||
Object copy = Array.newInstance(componentClass, originalSize + 1);
|
||||
|
||||
//Copy items before
|
||||
for (int i = 0; i < index; i++) {
|
||||
Array.set(copy, i, Array.get(arrValue, i));
|
||||
}
|
||||
if (val != null) {
|
||||
Array.set(copy, index, val);
|
||||
}
|
||||
//Copy items after
|
||||
for (int i = index; i < originalSize; i++) {
|
||||
Array.set(copy, i + 1, Array.get(arrValue, i));
|
||||
}
|
||||
try {
|
||||
field.set(object, copy);
|
||||
} catch (IllegalArgumentException | IllegalAccessException ex) {
|
||||
Logger.getLogger(ReflectionTools.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean addToField(Object object, Field field, int index, boolean notnull) {
|
||||
if (List.class.isAssignableFrom(field.getType())) {
|
||||
return addToList(object, field, index);
|
||||
}
|
||||
|
||||
if (field.getType().isArray()) {
|
||||
return addToArray(object, field, index, notnull);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean removeFromField(Object object, Field field, int index) {
|
||||
if (List.class.isAssignableFrom(field.getType())) {
|
||||
return removeFromList(object, field, index);
|
||||
}
|
||||
|
||||
if (field.getType().isArray()) {
|
||||
return removeFromArray(object, field, index);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean removeFromList(Object object, Field field, int index) {
|
||||
List list;
|
||||
try {
|
||||
list = (List) field.get(object);
|
||||
} catch (IllegalArgumentException | IllegalAccessException ex) {
|
||||
Logger.getLogger(ReflectionTools.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return false;
|
||||
}
|
||||
if (index < 0 || index >= list.size()) {
|
||||
return false;
|
||||
}
|
||||
list.remove(index);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean removeFromArray(Object object, Field field, int index) {
|
||||
Object arrValue;
|
||||
try {
|
||||
arrValue = field.get(object);
|
||||
} catch (IllegalArgumentException | IllegalAccessException ex) {
|
||||
Logger.getLogger(ReflectionTools.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return false;
|
||||
}
|
||||
Class componentClass = arrValue.getClass().getComponentType();
|
||||
int originalSize = Array.getLength(arrValue);
|
||||
Object copy = Array.newInstance(componentClass, originalSize - 1);
|
||||
int pos = 0;
|
||||
//copy all before index
|
||||
for (int i = 0; i < index; i++) {
|
||||
Array.set(copy, pos, Array.get(arrValue, i));
|
||||
pos++;
|
||||
}
|
||||
//copy all after index
|
||||
for (int i = index + 1; i < originalSize; i++) {
|
||||
Array.set(copy, pos, Array.get(arrValue, i));
|
||||
pos++;
|
||||
}
|
||||
try {
|
||||
field.set(object, copy);
|
||||
} catch (IllegalArgumentException | IllegalAccessException ex) {
|
||||
Logger.getLogger(ReflectionTools.class.getName()).log(Level.SEVERE, null, ex);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user