use of com.codename1.components.Switch in project CodenameOne by codenameone.
the class LiveDemo method start.
public void start() {
Form previewForm = new Form("Preview Theme");
Toolbar tb = new Toolbar();
previewForm.setToolbar(tb);
tb.setTitle("Preview Theme");
tb.addMaterialCommandToSideMenu("Side Command", FontImage.MATERIAL_HELP, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
ToastBar.showErrorMessage("Unmapped....");
}
});
tb.addMaterialCommandToOverflowMenu("Overflow Command", FontImage.MATERIAL_HELP, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
ToastBar.showErrorMessage("Unmapped....");
}
});
tb.addMaterialCommandToRightBar("", FontImage.MATERIAL_HELP, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
ToastBar.showErrorMessage("Unmapped....");
}
});
previewForm.setLayout(new BorderLayout());
Container first = new Container(new BoxLayout(BoxLayout.Y_AXIS));
first.addComponent(new Label("This is a Label"));
first.addComponent(new Button("This is a Button"));
MultiButton mb = new MultiButton("This is a MultiButton");
mb.setTextLine2("Second line of text");
FontImage.setMaterialIcon(mb, FontImage.MATERIAL_HELP);
first.addComponent(mb);
TextField txt = new TextField();
txt.setHint("This is a TextField");
first.addComponent(txt);
first.addComponent(new CheckBox("This is a CheckBox"));
first.addComponent(FlowLayout.encloseIn(new Label("This is a switch "), new Switch()));
RadioButton rb1 = new RadioButton("This is a Radio Button 1");
rb1.setGroup("group");
first.addComponent(rb1);
RadioButton rb2 = new RadioButton("This is a Radio Button 2");
rb2.setGroup("group");
first.addComponent(rb2);
final Slider s = new Slider();
s.setText("50%");
s.setProgress(50);
s.setEditable(true);
s.setRenderPercentageOnTop(true);
first.addComponent(s);
Button b1 = new Button("Show a Dialog");
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
Dialog.show("Dialog Title", "Dialog Body", "Ok", "Cancel");
}
});
first.addComponent(b1);
previewForm.add(BorderLayout.CENTER, first);
previewForm.show();
}
use of com.codename1.components.Switch in project CodenameOne by codenameone.
the class ResourceEditorApp method _main.
/**
* Main method launching the application.
*/
public static void _main(String[] args) throws Exception {
JavaSEPortWithSVGSupport.blockMonitors();
JavaSEPortWithSVGSupport.setDesignMode(true);
JavaSEPortWithSVGSupport.setShowEDTWarnings(false);
JavaSEPortWithSVGSupport.setShowEDTViolationStacks(false);
// creates a deadlock between FX, Swing and CN1. Horrible horrible deadlock...
JavaSEPortWithSVGSupport.blockNativeBrowser = true;
if (args.length > 0) {
if (args[0].equalsIgnoreCase("-buildVersion")) {
Properties p = new Properties();
try {
p.load(ResourceEditorApp.class.getResourceAsStream("/version.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println(p.getProperty("build", "1"));
System.exit(0);
return;
}
if (args[0].equalsIgnoreCase("-style")) {
java.awt.Container cnt = new java.awt.Container();
com.codename1.ui.Display.init(cnt);
final String uiid = args[2];
String themeName = args[3];
boolean isXMLEnabled = Preferences.userNodeForPackage(ResourceEditorView.class).getBoolean("XMLFileMode", true);
EditableResources.setXMLEnabled(isXMLEnabled);
EditableResources res = new EditableResources();
File resourceFile = new File(args[1]);
res.openFileWithXMLSupport(resourceFile);
Hashtable themeHash = res.getTheme(themeName);
final AddThemeEntry entry = new AddThemeEntry(false, res, null, new Hashtable(themeHash), "", themeName);
entry.setKeyValues(uiid, "");
entry.setPreferredSize(new Dimension(1000, 600));
JPanel wrapper = new JPanel(new BorderLayout());
wrapper.add(entry, BorderLayout.CENTER);
JPanel bottom = new JPanel();
ButtonGroup gr = new ButtonGroup();
JRadioButton unsel = new JRadioButton("Unselected", true);
gr.add(unsel);
unsel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
entry.setPrefix("");
entry.setKeyValues(uiid, "");
entry.revalidate();
}
});
bottom.add(unsel);
JRadioButton sel = new JRadioButton("Selected");
gr.add(sel);
sel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
entry.setPrefix("sel#");
entry.setKeyValues(uiid, "sel#");
entry.revalidate();
}
});
bottom.add(sel);
JRadioButton press = new JRadioButton("Pressed");
gr.add(press);
press.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
entry.setPrefix("press#");
entry.setKeyValues(uiid, "press#");
entry.revalidate();
}
});
bottom.add(press);
JRadioButton dis = new JRadioButton("Disabled");
gr.add(dis);
dis.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
entry.setPrefix("dis#");
entry.setKeyValues(uiid, "dis#");
entry.revalidate();
}
});
bottom.add(dis);
wrapper.add(bottom, BorderLayout.SOUTH);
if (ModifiableJOptionPane.showConfirmDialog(null, wrapper, "Edit") == JOptionPane.OK_OPTION) {
Hashtable tmp = new Hashtable(themeHash);
entry.updateThemeHashtable(tmp);
res.setTheme(themeName, tmp);
}
try (FileOutputStream fos = new FileOutputStream(resourceFile)) {
res.save(fos);
}
res.saveXML(resourceFile);
System.exit(0);
return;
}
if (args[0].equalsIgnoreCase("-img")) {
String imageName;
String fileName;
if (args.length == 3) {
imageName = args[2];
fileName = args[2];
} else {
if (args.length == 4) {
imageName = args[3];
fileName = args[2];
} else {
System.out.println("The img command works as: -img path_to_resourceFile.res pathToImageFile [image name]");
System.exit(1);
return;
}
}
File resourceFile = new File(args[1]);
File imageFile = new File(fileName);
if (!imageFile.exists()) {
System.out.println("File not found: " + imageFile.getAbsolutePath());
System.exit(1);
return;
}
java.awt.Container cnt = new java.awt.Container();
com.codename1.ui.Display.init(cnt);
CSSImageImporter cssImporter = new CSSImageImporter(resourceFile, imageFile, 0);
if (cssImporter.checkIfCSSProject()) {
// CSS projects can't just import images into the res file. They need to
// be added to the CSS file, and then have the resource file regenerated.
cssImporter.doImportAndExit();
System.exit(0);
return;
}
com.codename1.ui.Image img = ImageRGBEditor.createImageStatic(imageFile);
boolean isXMLEnabled = Preferences.userNodeForPackage(ResourceEditorView.class).getBoolean("XMLFileMode", true);
EditableResources.setXMLEnabled(isXMLEnabled);
EditableResources res = new EditableResources();
res.openFileWithXMLSupport(resourceFile);
res.setImage(imageName, img);
try (FileOutputStream fos = new FileOutputStream(resourceFile)) {
res.save(fos);
}
res.saveXML(resourceFile);
System.exit(0);
return;
}
if (args[0].equalsIgnoreCase("-mimg")) {
String fileName;
if (args.length == 4) {
fileName = args[3];
} else {
System.out.println("The mimg command works as: -img path_to_resourceFile.res dpi pathToImageFile");
System.out.println("dpi can be one of: high, veryhigh, hd, 560, 2hd, 4k");
System.exit(1);
return;
}
String dpi = args[2];
int dpiInt = -1;
int sourceDpi = 0;
switch(dpi.toLowerCase()) {
case "high":
dpiInt = 3;
sourceDpi = 240;
break;
case "veryhigh":
dpiInt = 4;
sourceDpi = 320;
break;
case "hd":
dpiInt = 5;
sourceDpi = 480;
break;
case "560":
dpiInt = 6;
sourceDpi = 480;
break;
case "2hd":
dpiInt = 7;
sourceDpi = 640;
break;
case "4k":
dpiInt = 8;
sourceDpi = 720;
break;
default:
System.out.println("dpi can be one of: high, veryhigh, hd, 560, 2hd, 4k");
System.exit(1);
return;
}
File imageFile = new File(fileName);
if (!imageFile.exists()) {
System.out.println("File not found: " + imageFile.getAbsolutePath());
System.exit(1);
return;
}
File resourceFile = new File(args[1]);
java.awt.Container cnt = new java.awt.Container();
com.codename1.ui.Display.init(cnt);
CSSImageImporter cssImporter = new CSSImageImporter(resourceFile, imageFile, sourceDpi);
if (cssImporter.checkIfCSSProject()) {
cssImporter.doImportAndExit();
System.exit(0);
return;
}
boolean isXMLEnabled = Preferences.userNodeForPackage(ResourceEditorView.class).getBoolean("XMLFileMode", true);
EditableResources.setXMLEnabled(isXMLEnabled);
EditableResources res = new EditableResources();
res.openFileWithXMLSupport(resourceFile);
AddAndScaleMultiImage.generateImpl(new File[] { imageFile }, res, dpiInt);
try (FileOutputStream fos = new FileOutputStream(resourceFile)) {
res.save(fos);
}
res.saveXML(resourceFile);
System.exit(0);
return;
}
if (args[0].equalsIgnoreCase("gen")) {
java.awt.Container cnt = new java.awt.Container();
com.codename1.ui.Display.init(cnt);
File output = new File(args[1]);
generateResourceFile(output, args[2], args[3]);
System.exit(0);
return;
}
if (args[0].equalsIgnoreCase("mig")) {
java.awt.Container cnt = new java.awt.Container();
com.codename1.ui.Display.init(cnt);
File projectDir = new File(args[1]);
EditableResources.setXMLEnabled(true);
EditableResources res = new EditableResources();
res.openFileWithXMLSupport(new File(args[2]));
migrateGuiBuilder(projectDir, res, args[3]);
System.exit(0);
return;
}
if (args[0].equalsIgnoreCase("-regen")) {
java.awt.Container cnt = new java.awt.Container();
com.codename1.ui.Display.init(cnt);
File output = new File(args[1]);
EditableResources.setXMLEnabled(true);
EditableResources res = new EditableResources();
res.openFileWithXMLSupport(output);
FileOutputStream fos = new FileOutputStream(output);
res.save(fos);
fos.close();
generate(res, output);
System.exit(0);
return;
}
if (args[0].equalsIgnoreCase("-sync-xml")) {
try {
java.awt.Container cnt = new java.awt.Container();
com.codename1.ui.Display.init(cnt);
File output = new File(args[1]);
EditableResources.setXMLEnabled(true);
EditableResources res = new EditableResources();
res.openFileWithXMLSupport(output);
FileOutputStream fos = new FileOutputStream(output);
res.save(fos);
fos.close();
} finally {
System.exit(0);
}
return;
}
if (args.length >= 2 && args[0].equalsIgnoreCase("-css")) {
List<String> cssArgs = new ArrayList<String>(Arrays.asList(args));
cssArgs.remove(0);
CN1CSSCLI.main(cssArgs.toArray(new String[cssArgs.size()]));
return;
}
}
JavaSEPortWithSVGSupport.setDefaultInitTarget(new JPanel());
Display.init(null);
launch(ResourceEditorApp.class, args);
}
use of com.codename1.components.Switch in project CodenameOne by codenameone.
the class RADStatusViewSample method start.
public void start() {
if (current != null) {
current.show();
return;
}
Form hi = new Form("Hi World", BoxLayout.y());
Tag TAG_ONLINE = new Tag("online");
class User extends Entity {
}
entityTypeBuilder(User.class).Boolean(TAG_ONLINE).string(Thing.name).factory(cls -> {
return new User();
}).build();
/**
* A view that displays the status (online/offline) of an entity using
* the TAG_ONLINE tag.
*/
class StatusView extends AbstractEntityView {
// ViewNode. Not used;
ViewNode node;
// Flag to indicate the current state online/offline of the view
private boolean online;
// Label used
private Label label = new Label();
/**
* Creates a new StatusView for user model.
*/
StatusView(User user) {
super(user);
this.node = new ViewNode(new Attribute[] {});
setLayout(new BorderLayout());
$(this).setMargin(0).setPadding(0);
this.add(CENTER, label);
update();
}
@Override
public void update() {
// Check to see if the model is online.
boolean modelOnline = !getEntity().isFalsey(TAG_ONLINE);
if (modelOnline != online) {
// Model state has changed since last update
online = modelOnline;
if (online) {
label.setText("Online");
label.setUIID("OnlineLabel");
FontImage.setMaterialIcon(label, FontImage.MATERIAL_CONNECTED_TV);
} else {
label.setText("Offline");
label.setUIID("OfflineLabel");
FontImage.setMaterialIcon(label, FontImage.MATERIAL_OFFLINE_BOLT);
}
Form f = getComponentForm();
if (f != null) {
// Changing the text in this case may change the layout size
// of the status view here, so it is best to just issue a revalidate
// for the whole form. If the status change didn't change
// the layout size, then we could have just skipped this step.
f.revalidateWithAnimationSafety();
}
}
}
@Override
public void commit() {
// Don't need to implement commit() because this view doesn't
// "update" the model - it only shows model stats.
}
@Override
public Node getViewNode() {
return node;
}
}
// Create a new user
User user = new User();
// Create a status view for the user
StatusView statusView = new StatusView(user);
// Add a UI switch to toggle user state between online and offline
Switch onlineSwitch = new Switch("Online");
onlineSwitch.addChangeListener(e -> {
// Set the user TAG_ONLINE status. This will trigger a property
// change in the model and update the view.
user.set(TAG_ONLINE, onlineSwitch.isOn());
});
hi.add(onlineSwitch);
hi.add(statusView);
hi.show();
}
use of com.codename1.components.Switch in project CodenameOne by codenameone.
the class InterFormContainerSample method start.
public void start() {
Button sharedButton = new Button("Shared Button");
InterFormContainer cnt = new InterFormContainer(sharedButton);
InterFormContainer cnt2 = new InterFormContainer(sharedButton);
Toolbar.setGlobalToolbar(true);
Form hi = new Form("Transitions", new BorderLayout());
Container c = new Container(BoxLayout.y());
hi.add(BorderLayout.CENTER, c);
hi.add(BorderLayout.SOUTH, cnt);
Style bg = hi.getContentPane().getUnselectedStyle();
bg.setBgTransparency(255);
bg.setBgColor(0xff0000);
Button showTransition = new Button("Show");
Picker pick = new Picker();
pick.setStrings("Slide", "SlideFade", "Cover", "Uncover", "Fade", "Flip");
pick.setSelectedString("Slide");
TextField duration = new TextField("10000", "Duration", 6, TextArea.NUMERIC);
CheckBox horizontal = CheckBox.createToggle("Horizontal");
pick.addActionListener((e) -> {
String s = pick.getSelectedString().toLowerCase();
horizontal.setEnabled(s.equals("slide") || s.indexOf("cover") > -1);
});
horizontal.setSelected(true);
c.add(showTransition).add(pick).add(duration).add(horizontal);
Form dest = new Form("Destination", new BorderLayout());
sharedButton.addActionListener(e -> {
if (sharedButton.getComponentForm() == hi) {
dest.show();
} else {
hi.showBack();
}
});
dest.add(BorderLayout.SOUTH, cnt2);
bg = dest.getContentPane().getUnselectedStyle();
bg.setBgTransparency(255);
bg.setBgColor(0xff);
dest.setBackCommand(dest.getToolbar().addCommandToLeftBar("Back", null, (e) -> hi.showBack()));
showTransition.addActionListener((e) -> {
int h = CommonTransitions.SLIDE_HORIZONTAL;
if (!horizontal.isSelected()) {
h = CommonTransitions.SLIDE_VERTICAL;
}
switch(pick.getSelectedString()) {
case "Slide":
hi.setTransitionOutAnimator(CommonTransitions.createSlide(h, true, duration.getAsInt(3000)));
dest.setTransitionOutAnimator(CommonTransitions.createSlide(h, true, duration.getAsInt(3000)));
break;
case "SlideFade":
hi.setTransitionOutAnimator(CommonTransitions.createSlideFadeTitle(true, duration.getAsInt(3000)));
dest.setTransitionOutAnimator(CommonTransitions.createSlideFadeTitle(true, duration.getAsInt(3000)));
break;
case "Cover":
hi.setTransitionOutAnimator(CommonTransitions.createCover(h, true, duration.getAsInt(3000)));
dest.setTransitionOutAnimator(CommonTransitions.createCover(h, true, duration.getAsInt(3000)));
break;
case "Uncover":
hi.setTransitionOutAnimator(CommonTransitions.createUncover(h, true, duration.getAsInt(3000)));
dest.setTransitionOutAnimator(CommonTransitions.createUncover(h, true, duration.getAsInt(3000)));
break;
case "Fade":
hi.setTransitionOutAnimator(CommonTransitions.createFade(duration.getAsInt(3000)));
dest.setTransitionOutAnimator(CommonTransitions.createFade(duration.getAsInt(3000)));
break;
case "Flip":
hi.setTransitionOutAnimator(new FlipTransition(-1, duration.getAsInt(3000)));
dest.setTransitionOutAnimator(new FlipTransition(-1, duration.getAsInt(3000)));
break;
}
dest.show();
});
hi.show();
}
use of com.codename1.components.Switch in project CodenameOne by codenameone.
the class BytecodeMethod method optimize.
boolean optimize() {
int instructionCount = instructions.size();
// optimize away a method that only contains the void return instruction e.g. blank constructors etc.
if (instructionCount < 6) {
int realCount = instructionCount;
Instruction actual = null;
for (int iter = 0; iter < instructionCount; iter++) {
Instruction current = instructions.get(iter);
if (current instanceof LabelInstruction) {
realCount--;
continue;
}
if (current instanceof LineNumber) {
realCount--;
continue;
}
actual = current;
}
if (realCount == 1 && actual != null && actual.getOpcode() == Opcodes.RETURN) {
return false;
}
}
boolean astoreCalls = false;
boolean hasInstructions = false;
boolean hasTryCatch = false;
for (int iter = 0; iter < instructionCount - 1; iter++) {
Instruction current = instructions.get(iter);
if (current instanceof TryCatch) {
hasTryCatch = true;
}
current.setMethod(this);
if (current.isOptimized()) {
continue;
}
int currentOpcode = current.getOpcode();
switch(currentOpcode) {
case Opcodes.CHECKCAST:
{
// Remove the check cast for now as it gets in the way of other optimizations
instructions.remove(iter);
iter--;
instructionCount--;
break;
}
}
}
for (int iter = 0; iter < instructionCount - 1; iter++) {
Instruction current = instructions.get(iter);
if (current.isOptimized()) {
// we should skip it and proceed to the next one
continue;
}
Instruction next = instructions.get(iter + 1);
int currentOpcode = current.getOpcode();
int nextOpcode = next.getOpcode();
if (ArithmeticExpression.isArithmeticOp(current)) {
int addedIndex = ArithmeticExpression.tryReduce(instructions, iter);
if (addedIndex >= 0) {
iter = addedIndex;
instructionCount = instructions.size();
continue;
}
}
if (current instanceof Field) {
int newIter = Field.tryReduce(instructions, iter);
if (newIter >= 0) {
iter = newIter;
instructionCount = instructions.size();
continue;
}
}
switch(currentOpcode) {
case Opcodes.ARRAYLENGTH:
{
if (!dependentClasses.contains("java_lang_NullPointerException")) {
dependentClasses.add("java_lang_NullPointerException");
}
int newIter = ArrayLengthExpression.tryReduce(instructions, iter);
if (newIter >= 0) {
instructionCount = instructions.size();
iter = newIter;
continue;
}
break;
}
case Opcodes.DUP:
{
int newIter = DupExpression.tryReduce(instructions, iter);
if (newIter >= 0) {
iter = newIter;
instructionCount = instructions.size();
continue;
}
break;
}
case Opcodes.POP:
{
if (iter > 0) {
Instruction prev = instructions.get(iter - 1);
if (prev instanceof CustomInvoke) {
CustomInvoke inv = (CustomInvoke) prev;
if (inv.methodHasReturnValue() && !inv.isNoReturn()) {
inv.setNoReturn(true);
instructions.remove(iter);
iter--;
instructionCount--;
continue;
}
}
}
break;
}
case Opcodes.ASTORE:
case Opcodes.ISTORE:
case Opcodes.DSTORE:
case Opcodes.LSTORE:
case Opcodes.FSTORE:
{
if (iter > 0 && current instanceof VarOp) {
VarOp currentVarOp = (VarOp) current;
Instruction prev = instructions.get(iter - 1);
if (prev instanceof AssignableExpression) {
AssignableExpression expr = (AssignableExpression) prev;
StringBuilder sb = new StringBuilder();
if (currentVarOp.assignFrom(expr, sb)) {
instructions.remove(iter - 1);
instructions.remove(iter - 1);
instructions.add(iter - 1, new CustomIntruction(sb.toString(), sb.toString(), dependentClasses));
iter = iter - 1;
instructionCount = instructions.size();
continue;
}
} else if (prev instanceof CustomInvoke) {
CustomInvoke inv = (CustomInvoke) prev;
StringBuilder sb = new StringBuilder();
if (currentVarOp.assignFrom(inv, sb)) {
instructions.remove(iter - 1);
instructions.remove(iter - 1);
instructions.add(iter - 1, new CustomIntruction(sb.toString(), sb.toString(), dependentClasses));
iter = iter - 1;
instructionCount = instructions.size();
continue;
}
}
}
break;
}
case Opcodes.IRETURN:
case Opcodes.FRETURN:
case Opcodes.ARETURN:
case Opcodes.LRETURN:
case Opcodes.DRETURN:
{
if (iter > 0 && current instanceof BasicInstruction) {
Instruction prev = instructions.get(iter - 1);
if (prev instanceof AssignableExpression) {
AssignableExpression expr = (AssignableExpression) prev;
StringBuilder sb = new StringBuilder();
if (expr.assignTo(null, sb)) {
instructions.remove(iter - 1);
instructions.remove(iter - 1);
String exprString = sb.toString().trim();
String retVal = exprString;
sb.setLength(0);
if (!prev.isConstant()) {
sb.append("\n{\n ");
switch(currentOpcode) {
case Opcodes.IRETURN:
sb.append("JAVA_INT");
break;
case Opcodes.FRETURN:
sb.append("JAVA_FLOAT");
break;
case Opcodes.ARETURN:
sb.append("JAVA_OBJECT");
break;
case Opcodes.LRETURN:
sb.append("JAVA_LONG");
break;
case Opcodes.DRETURN:
sb.append("JAVA_DOUBLE");
break;
}
sb.append(" ___returnValue=").append(exprString).append(";\n");
retVal = "___returnValue";
}
if (synchronizedMethod) {
if (staticMethod) {
sb.append(" monitorExitBlock(threadStateData, (JAVA_OBJECT)&class__");
sb.append(getClsName());
sb.append(");\n");
} else {
sb.append(" monitorExitBlock(threadStateData, __cn1ThisObject);\n");
}
}
if (hasTryCatch) {
sb.append(" releaseForReturnInException(threadStateData, cn1LocalsBeginInThread, methodBlockOffset); return ").append(retVal).append(";\n");
} else {
sb.append(" releaseForReturn(threadStateData, cn1LocalsBeginInThread); return ").append(retVal).append(";\n");
}
if (!prev.isConstant()) {
sb.append("}\n");
}
instructions.add(iter - 1, new CustomIntruction(sb.toString(), sb.toString(), dependentClasses));
iter--;
instructionCount = instructions.size();
continue;
}
} else if (prev instanceof CustomInvoke) {
CustomInvoke expr = (CustomInvoke) prev;
String returnType = expr.getReturnValue();
if (returnType != null && !"JAVA_OBJECT".equals(returnType)) {
// We can't safely return a JAVA_OBJECT directly because it needs to be added
// to the stack for the GC
StringBuilder sb = new StringBuilder();
if (expr.appendExpression(sb)) {
instructions.remove(iter - 1);
instructions.remove(iter - 1);
String exprString = sb.toString().trim();
String retVal = exprString;
sb.setLength(0);
if (!expr.isConstant()) {
sb.append("\n{\n ");
switch(currentOpcode) {
case Opcodes.IRETURN:
sb.append("JAVA_INT");
break;
case Opcodes.FRETURN:
sb.append("JAVA_FLOAT");
break;
case Opcodes.ARETURN:
sb.append("JAVA_OBJECT");
break;
case Opcodes.LRETURN:
sb.append("JAVA_LONG");
break;
case Opcodes.DRETURN:
sb.append("JAVA_DOUBLE");
break;
}
sb.append(" ___returnValue=").append(exprString).append(";\n");
retVal = "___returnValue";
}
if (synchronizedMethod) {
if (staticMethod) {
sb.append(" monitorExitBlock(threadStateData, (JAVA_OBJECT)&class__");
sb.append(getClsName());
sb.append(");\n");
} else {
sb.append(" monitorExitBlock(threadStateData, __cn1ThisObject);\n");
}
}
if (hasTryCatch) {
sb.append(" releaseForReturnInException(threadStateData, cn1LocalsBeginInThread, methodBlockOffset); return ").append(retVal).append(";\n");
} else {
sb.append(" releaseForReturn(threadStateData, cn1LocalsBeginInThread); return ").append(retVal).append(";\n");
}
if (!expr.isConstant()) {
sb.append("}\n");
}
instructions.add(iter - 1, new CustomIntruction(sb.toString(), sb.toString(), dependentClasses));
iter--;
instructionCount = instructions.size();
continue;
}
}
}
}
break;
}
case Opcodes.BASTORE:
case Opcodes.SASTORE:
case Opcodes.CASTORE:
case Opcodes.AASTORE:
case Opcodes.IASTORE:
case Opcodes.DASTORE:
case Opcodes.LASTORE:
case Opcodes.FASTORE:
{
if (iter > 2 && current instanceof BasicInstruction) {
StringBuilder devNull = new StringBuilder();
String arrayLiteral = null;
String indexLiteral = null;
String valueLiteral = null;
Instruction prev3 = instructions.get(iter - 3);
if (prev3 instanceof AssignableExpression) {
if (((AssignableExpression) prev3).assignTo(null, devNull)) {
arrayLiteral = devNull.toString().trim();
}
}
devNull.setLength(0);
Instruction prev2 = instructions.get(iter - 2);
if (prev2 instanceof AssignableExpression) {
if (((AssignableExpression) prev2).assignTo(null, devNull)) {
indexLiteral = devNull.toString().trim();
}
}
devNull.setLength(0);
Instruction prev1 = instructions.get(iter - 1);
if (prev1 instanceof AssignableExpression) {
if (((AssignableExpression) prev1).assignTo(null, devNull)) {
valueLiteral = devNull.toString().trim();
}
} else if (prev1 instanceof CustomInvoke) {
devNull.setLength(0);
if (((CustomInvoke) prev1).appendExpression(devNull)) {
valueLiteral = devNull.toString().trim();
}
}
if (arrayLiteral != null && indexLiteral != null && valueLiteral != null) {
String elementType = null;
switch(current.getOpcode()) {
case Opcodes.AASTORE:
elementType = "OBJECT";
break;
case Opcodes.IASTORE:
elementType = "INT";
break;
case Opcodes.DASTORE:
elementType = "DOUBLE";
break;
case Opcodes.LASTORE:
elementType = "LONG";
break;
case Opcodes.FASTORE:
elementType = "FLOAT";
break;
case Opcodes.CASTORE:
elementType = "CHAR";
break;
case Opcodes.BASTORE:
elementType = "BYTE";
break;
case Opcodes.SASTORE:
elementType = "SHORT";
break;
}
if (elementType == null) {
break;
}
instructions.remove(iter - 3);
instructions.remove(iter - 3);
instructions.remove(iter - 3);
instructions.remove(iter - 3);
String code = " CN1_SET_ARRAY_ELEMENT_" + elementType + "(" + arrayLiteral + ", " + indexLiteral + ", " + valueLiteral + ");\n";
instructions.add(iter - 3, new CustomIntruction(code, code, dependentClasses));
iter = iter - 3;
instructionCount = instructions.size();
continue;
}
}
break;
}
case Opcodes.FALOAD:
case Opcodes.BALOAD:
case Opcodes.IALOAD:
case Opcodes.LALOAD:
case Opcodes.DALOAD:
case Opcodes.AALOAD:
case Opcodes.SALOAD:
case Opcodes.CALOAD:
{
int newIter = ArrayLoadExpression.tryReduce(instructions, iter);
if (newIter >= 0) {
iter = newIter;
instructionCount = instructions.size();
continue;
}
break;
}
/* Try to optimize if statements that just use constants
and local variables so that they don't need the intermediate
push and pop from the stack.
*/
case Opcodes.IF_ACMPEQ:
case Opcodes.IF_ACMPNE:
case Opcodes.IF_ICMPLE:
case Opcodes.IF_ICMPLT:
case Opcodes.IF_ICMPNE:
case Opcodes.IF_ICMPGT:
case Opcodes.IF_ICMPEQ:
case Opcodes.IF_ICMPGE:
{
if (iter > 1) {
Instruction leftArg = instructions.get(iter - 2);
Instruction rightArg = instructions.get(iter - 1);
String leftLiteral = null;
String rightLiteral = null;
if (leftArg instanceof AssignableExpression) {
StringBuilder sb = new StringBuilder();
if (((AssignableExpression) leftArg).assignTo(null, sb)) {
leftLiteral = sb.toString().trim();
}
} else if (leftArg instanceof CustomInvoke) {
CustomInvoke inv = (CustomInvoke) leftArg;
StringBuilder sb = new StringBuilder();
if (!"JAVA_OBJECT".equals(inv.getReturnValue()) && inv.appendExpression(sb)) {
leftLiteral = sb.toString().trim();
}
}
if (rightArg instanceof AssignableExpression) {
StringBuilder sb = new StringBuilder();
if (((AssignableExpression) rightArg).assignTo(null, sb)) {
rightLiteral = sb.toString().trim();
}
} else if (rightArg instanceof CustomInvoke) {
CustomInvoke inv = (CustomInvoke) rightArg;
StringBuilder sb = new StringBuilder();
if (!"JAVA_OBJECT".equals(inv.getReturnValue()) && inv.appendExpression(sb)) {
rightLiteral = sb.toString().trim();
}
}
if (rightLiteral != null && leftLiteral != null) {
Jump jmp = (Jump) current;
instructions.remove(iter - 2);
instructions.remove(iter - 2);
instructions.remove(iter - 2);
// instructions.remove(iter-2);
iter -= 2;
// instructionCount -= 2;
StringBuilder sb = new StringBuilder();
String operator = null;
String opName = null;
switch(currentOpcode) {
case Opcodes.IF_ICMPLE:
operator = "<=";
opName = "IF_ICMPLE";
break;
case Opcodes.IF_ICMPLT:
operator = "<";
opName = "IF_IMPLT";
break;
case Opcodes.IF_ICMPNE:
operator = "!=";
opName = "IF_ICMPNE";
break;
case Opcodes.IF_ICMPGT:
operator = ">";
opName = "IF_ICMPGT";
break;
case Opcodes.IF_ICMPGE:
operator = ">=";
opName = "IF_ICMPGE";
break;
case Opcodes.IF_ICMPEQ:
operator = "==";
opName = "IF_ICMPEQ";
break;
case Opcodes.IF_ACMPEQ:
operator = "==";
opName = "IF_ACMPEQ";
break;
case Opcodes.IF_ACMPNE:
operator = "!=";
opName = "IF_ACMPNE";
break;
default:
throw new RuntimeException("Invalid operator during optimization of integer comparison");
}
sb.append("if (").append(leftLiteral).append(operator).append(rightLiteral).append(") /* ").append(opName).append(" CustomJump */ ");
CustomJump newJump = CustomJump.create(jmp, sb.toString());
// jmp.setCustomCompareCode(sb.toString());
newJump.setOptimized(true);
instructions.add(iter, newJump);
instructionCount = instructions.size();
}
}
break;
}
case Opcodes.IFNONNULL:
case Opcodes.IFNULL:
case Opcodes.IFLE:
case Opcodes.IFLT:
case Opcodes.IFNE:
case Opcodes.IFGT:
case Opcodes.IFEQ:
case Opcodes.IFGE:
{
String rightArg = "0";
if (currentOpcode == Opcodes.IFNONNULL || currentOpcode == Opcodes.IFNULL) {
rightArg = "JAVA_NULL";
}
if (iter > 0) {
Instruction leftArg = instructions.get(iter - 1);
String leftLiteral = null;
if (leftArg instanceof AssignableExpression) {
StringBuilder sb = new StringBuilder();
if (((AssignableExpression) leftArg).assignTo(null, sb)) {
leftLiteral = sb.toString().trim();
}
} else if (leftArg instanceof CustomInvoke) {
CustomInvoke inv = (CustomInvoke) leftArg;
StringBuilder sb = new StringBuilder();
if (inv.appendExpression(sb)) {
leftLiteral = sb.toString().trim();
}
}
if (leftLiteral != null) {
Jump jmp = (Jump) current;
instructions.remove(iter - 1);
instructions.remove(iter - 1);
// instructions.remove(iter-2);
iter -= 1;
// instructionCount -= 2;
StringBuilder sb = new StringBuilder();
String operator = null;
String opName = null;
switch(currentOpcode) {
case Opcodes.IFLE:
operator = "<=";
opName = "IFLE";
break;
case Opcodes.IFLT:
operator = "<";
opName = "IFLT";
break;
case Opcodes.IFNE:
operator = "!=";
opName = "IFNE";
break;
case Opcodes.IFGT:
operator = ">";
opName = "IFGT";
break;
case Opcodes.IFGE:
operator = ">=";
opName = "IFGE";
break;
case Opcodes.IFEQ:
operator = "==";
opName = "IFEQ";
break;
case Opcodes.IFNULL:
operator = "==";
opName = "IFNULL";
break;
case Opcodes.IFNONNULL:
operator = "!=";
opName = "IFNONNULL";
break;
default:
throw new RuntimeException("Invalid operator during optimization of integer comparison");
}
sb.append("if (").append(leftLiteral).append(operator).append(rightArg).append(") /* ").append(opName).append(" CustomJump */ ");
CustomJump newJump = CustomJump.create(jmp, sb.toString());
// jmp.setCustomCompareCode(sb.toString());
newJump.setOptimized(true);
instructions.add(iter, newJump);
instructionCount = instructions.size();
}
}
break;
}
case Opcodes.INVOKEVIRTUAL:
case Opcodes.INVOKESTATIC:
case Opcodes.INVOKESPECIAL:
case Opcodes.INVOKEINTERFACE:
{
if (current instanceof Invoke) {
Invoke inv = (Invoke) current;
List<ByteCodeMethodArg> invocationArgs = inv.getArgs();
int numArgs = invocationArgs.size();
// }
if (iter >= numArgs) {
String[] argLiterals = new String[numArgs];
StringBuilder devNull = new StringBuilder();
for (int i = 0; i < numArgs; i++) {
devNull.setLength(0);
Instruction instr = instructions.get(iter - numArgs + i);
if (instr instanceof AssignableExpression && ((AssignableExpression) instr).assignTo(null, devNull)) {
argLiterals[i] = devNull.toString().trim();
} else if (instr instanceof CustomInvoke) {
CustomInvoke cinv = (CustomInvoke) instr;
devNull.setLength(0);
if (!"JAVA_OBJECT".equals(cinv.getReturnValue()) && cinv.appendExpression(devNull)) {
// We can't add invocations that return objects directly
// because they need to be added to the stack for GC
argLiterals[i] = devNull.toString().trim();
}
} else if (instr instanceof ArithmeticExpression) {
argLiterals[i] = ((ArithmeticExpression) instr).getExpressionAsString().trim();
} else if (instr instanceof VarOp) {
VarOp var = (VarOp) instr;
switch(instr.getOpcode()) {
case Opcodes.ALOAD:
{
if (!isStatic() && var.getIndex() == 0) {
argLiterals[i] = "__cn1ThisObject";
} else {
argLiterals[i] = "locals[" + var.getIndex() + "].data.o";
}
break;
}
case Opcodes.ILOAD:
{
argLiterals[i] = "ilocals_" + var.getIndex() + "_";
break;
}
case Opcodes.ACONST_NULL:
{
argLiterals[i] = "JAVA_NULL";
break;
}
case Opcodes.DLOAD:
{
argLiterals[i] = "dlocals_" + var.getIndex() + "_";
break;
}
case Opcodes.FLOAD:
{
argLiterals[i] = "flocals_" + var.getIndex() + "_";
break;
}
case Opcodes.LLOAD:
{
argLiterals[i] = "llocals_" + var.getIndex() + "_";
break;
}
case Opcodes.ICONST_0:
{
argLiterals[i] = "0";
break;
}
case Opcodes.ICONST_1:
{
argLiterals[i] = "1";
break;
}
case Opcodes.ICONST_2:
{
argLiterals[i] = "2";
break;
}
case Opcodes.ICONST_3:
{
argLiterals[i] = "3";
break;
}
case Opcodes.ICONST_4:
{
argLiterals[i] = "4";
break;
}
case Opcodes.ICONST_5:
{
argLiterals[i] = "5";
break;
}
case Opcodes.ICONST_M1:
{
argLiterals[i] = "-1";
break;
}
case Opcodes.LCONST_0:
{
argLiterals[i] = "(JAVA_LONG)0";
break;
}
case Opcodes.LCONST_1:
{
argLiterals[i] = "(JAVA_LONG)1";
break;
}
case Opcodes.BIPUSH:
case Opcodes.SIPUSH:
{
argLiterals[i] = String.valueOf(var.getIndex());
break;
}
}
} else {
switch(instr.getOpcode()) {
case Opcodes.ACONST_NULL:
{
argLiterals[i] = "JAVA_NULL";
break;
}
case Opcodes.ICONST_0:
{
argLiterals[i] = "0";
break;
}
case Opcodes.ICONST_1:
{
argLiterals[i] = "1";
break;
}
case Opcodes.ICONST_2:
{
argLiterals[i] = "2";
break;
}
case Opcodes.ICONST_3:
{
argLiterals[i] = "3";
break;
}
case Opcodes.ICONST_4:
{
argLiterals[i] = "4";
break;
}
case Opcodes.ICONST_5:
{
argLiterals[i] = "5";
break;
}
case Opcodes.ICONST_M1:
{
argLiterals[i] = "-1";
break;
}
case Opcodes.LCONST_0:
{
argLiterals[i] = "(JAVA_LONG)0";
break;
}
case Opcodes.LCONST_1:
{
argLiterals[i] = "(JAVA_LONG)1";
break;
}
case Opcodes.BIPUSH:
{
if (instr instanceof BasicInstruction) {
argLiterals[i] = String.valueOf(((BasicInstruction) instr).getValue());
}
break;
}
case Opcodes.LDC:
{
if (instr instanceof Ldc) {
Ldc ldc = (Ldc) instr;
argLiterals[i] = ldc.getValueAsString();
}
break;
}
}
}
}
// Check to make sure that we have all the args as literals.
boolean missingLiteral = false;
for (String lit : argLiterals) {
if (lit == null) {
missingLiteral = true;
break;
}
}
// add them to our invoke instruction.
if (!missingLiteral) {
CustomInvoke newInvoke = CustomInvoke.create(inv);
instructions.remove(iter);
instructions.add(iter, newInvoke);
int newIter = iter;
for (int i = 0; i < numArgs; i++) {
instructions.remove(iter - numArgs);
newIter--;
newInvoke.setLiteralArg(i, argLiterals[i]);
}
if (inv.getOpcode() != Opcodes.INVOKESTATIC) {
Instruction ldTarget = instructions.get(iter - numArgs - 1);
if (ldTarget instanceof AssignableExpression) {
StringBuilder targetExprStr = new StringBuilder();
if (((AssignableExpression) ldTarget).assignTo(null, targetExprStr)) {
newInvoke.setTargetObjectLiteral(targetExprStr.toString().trim());
instructions.remove(iter - numArgs - 1);
newIter--;
}
} else if (ldTarget instanceof CustomInvoke) {
// WE Can't pass a custom invoke as the target directly
// because it the return value needs to be added to the
// stack for the GC
} else {
switch(ldTarget.getOpcode()) {
case Opcodes.ALOAD:
{
VarOp v = (VarOp) ldTarget;
if (isStatic() && v.getIndex() == 0) {
newInvoke.setTargetObjectLiteral("__cn1ThisObject");
} else {
newInvoke.setTargetObjectLiteral("locals[" + v.getIndex() + "].data.o");
}
instructions.remove(iter - numArgs - 1);
newIter--;
break;
}
}
}
}
newInvoke.setOptimized(true);
// iter = 0;
instructionCount = instructions.size();
iter = newIter;
}
}
}
break;
}
}
astoreCalls = astoreCalls || currentOpcode == Opcodes.ASTORE || currentOpcode == Opcodes.ISTORE || currentOpcode == Opcodes.LSTORE || currentOpcode == Opcodes.DSTORE || currentOpcode == Opcodes.FSTORE;
hasInstructions = hasInstructions | current.getOpcode() != -1;
}
return hasInstructions;
}
Aggregations