枚举枚举和修改“最终静态”字段的方法

在本新闻通讯中,该新闻通讯最初发表在Java专家的新闻通讯第161期中,我们研究了如何使用sun.reflect包中的反射类在Sun JDK中创建枚举实例。 显然,这仅适用于Sun的JDK。 如果需要在另一个JVM上执行此操作,则您可以自己完成。

这一切都始于爱丁堡的肯·多布森(Ken Dobson)的一封电子邮件,该电子邮件向我指出了sun.reflect.ConstructorAccessor的方向,他声称可以将其用于构造枚举实例。 我以前的方法(通讯#141)在Java 6中不起作用。

我很好奇为什么Ken要构造枚举。 这是他想使用它的方式:

public enum HumanState {HAPPY, SAD
}public class Human {public void sing(HumanState state) {switch (state) {case HAPPY:singHappySong();break;case SAD:singDirge();break;default:new IllegalStateException("Invalid State: " + state);}}private void singHappySong() {System.out.println("When you're happy and you know it ...");}private void singDirge() {System.out.println("Don't cry for me Argentina, ...");}
}

上面的代码需要进行单元测试。 你发现错误了吗? 如果没有,请使用细梳再次遍历代码以尝试找到它。 当我第一次看到这个时,我也没有发现错误。

当我们产生这样的错误时,我们应该做的第一件事就是进行显示该错误的单元测试。 但是,在这种情况下,我们无法使default情况发生,因为HumanState仅具有HAPPY和SAD枚举。

Ken的发现使我们可以使用sun.reflect包中的ConstructorAccessor类来创建枚举的实例。 它涉及到以下内容:

Constructor cstr = clazz.getDeclaredConstructor(String.class, int.class
);
ReflectionFactory reflection =ReflectionFactory.getReflectionFactory();
Enum e =reflection.newConstructorAccessor(cstr).newInstance("BLA",3);

但是,如果仅执行此操作,则最终会出现ArrayIndexOutOfBoundsException,当我们看到Java编译器如何将switch语句转换为字节代码时,这才有意义。 以上面的Human类为例,反编译后的代码如下所示(感谢Pavel Kouznetsov的JAD ):

public class Human {public void sing(HumanState state) {static class _cls1 {static final int $SwitchMap$HumanState[] =new int[HumanState.values().length];static {try {$SwitchMap$HumanState[HumanState.HAPPY.ordinal()] = 1;} catch(NoSuchFieldError ex) { }try {$SwitchMap$HumanState[HumanState.SAD.ordinal()] = 2;} catch(NoSuchFieldError ex) { }}}switch(_cls1.$SwitchMap$HumanState[state.ordinal()]) {case 1:singHappySong();break;case 2:singDirge();break;default:new IllegalStateException("Invalid State: " + state);break;}}private void singHappySong() {System.out.println("When you're happy and you know it ...");}private void singDirge() {System.out.println("Don't cry for me Argentina, ...");}
}

您可以立即看到为什么要得到ArrayIndexOutOfBoundsException,这要归功于内部类_cls1。

我第一次尝试解决此问题并没有得到一个不错的解决方案。 我试图在HumanState枚举中修改$ VALUES数组。 但是,我只是摆脱了Java的保护性代码。 您可以修改final字段 ,只要它们是非静态的即可。 对我来说,这种限制似乎是人为的,因此我着手寻找静态最终领域的圣杯。 再一次,它被藏在阳光反射的房间里。

设置“最终静态”字段

设置final static字段需要几件事。 首先,我们需要使用法线反射获取Field对象。 如果将其传递给FieldAccessor,我们将退出安全代码,因为我们正在处理静态的final字段。 其次,我们将Field对象实例中的修饰符字段值更改为非最终值。 第三,将经过修改的字段传递给sun.reflect包中的FieldAccessor并使用它进行设置。

这是我的ReflectionHelper类,可用于通过反射设置final static字段:

import sun.reflect.*;
import java.lang.reflect.*;public class ReflectionHelper {private static final String MODIFIERS_FIELD = "modifiers";private static final ReflectionFactory reflection =ReflectionFactory.getReflectionFactory();public static void setStaticFinalField(Field field, Object value)throws NoSuchFieldException, IllegalAccessException {// we mark the field to be publicfield.setAccessible(true);// next we change the modifier in the Field instance to// not be final anymore, thus tricking reflection into// letting us modify the static final fieldField modifiersField =Field.class.getDeclaredField(MODIFIERS_FIELD);modifiersField.setAccessible(true);int modifiers = modifiersField.getInt(field);// blank out the final bit in the modifiers intmodifiers &= ~Modifier.FINAL;modifiersField.setInt(field, modifiers);FieldAccessor fa = reflection.newFieldAccessor(field, false);fa.set(null, value);}
}

通过使用ReflectionHelper,我可以在枚举中设置$ VALUES数组以包含新的枚举。 这行得通,只是我必须在首次加载Human类之前执行此操作。 这会将竞争条件引入我们的测试用例中。 单独进行每个测试都可以,但是总的来说,它们可能会失败。 不好的情况!

重新连接枚举开关

下一个想法是重新连接实际的switch语句的$ SwitchMap $ HumanState字段。 在匿名内部类中找到该字段将相当容易。 您所需要的只是前缀$ SwitchMap $,后跟枚举类名称。 如果枚举在一个类中切换了几次,则内部类仅创建一次。

我昨天写的其他解决方案之一检查了我们的switch语句是否正在处理所有可能的情况。 将新类型引入系统后,这对于发现错误很有用。 我放弃了该特定解决方案,但是您应该能够基于稍后将向您展示的EnumBuster重新创建该解决方案。

纪念品设计模式

我最近重新编写了我的设计模式课程 (警告,该网站可能尚未建立最新的结构–请查询更多信息),以考虑Java的变化,丢弃一些过时的模式并介绍我以前排除的一些。 “新”模式之一是Memento,通常与撤消功能一起使用。 我认为这是一个很好的模式,可以用来在我们努力测试不可能的案例的努力中消除对枚举造成的损害。

出版专家通讯给我某些自由。 我不必解释我写的每一行。 因此,事不宜迟,这里是我的EnumBuster类,它使您可以创建枚举,将它们添加到现有的values []中,从数组中删除枚举,同时维护您指定的任何类的switch语句。

import sun.reflect.*;import java.lang.reflect.*;
import java.util.*;public class EnumBuster<E extends Enum<E>> {private static final Class[] EMPTY_CLASS_ARRAY =new Class[0];private static final Object[] EMPTY_OBJECT_ARRAY =new Object[0];private static final String VALUES_FIELD = "$VALUES";private static final String ORDINAL_FIELD = "ordinal";private final ReflectionFactory reflection =ReflectionFactory.getReflectionFactory();private final Class<E> clazz;private final Collection<Field> switchFields;private final Deque<Memento> undoStack =new LinkedList<Memento>();/*** Construct an EnumBuster for the given enum class and keep* the switch statements of the classes specified in* switchUsers in sync with the enum values.*/public EnumBuster(Class<E> clazz, Class... switchUsers) {try {this.clazz = clazz;switchFields = findRelatedSwitchFields(switchUsers);} catch (Exception e) {throw new IllegalArgumentException("Could not create the class", e);}}/*** Make a new enum instance, without adding it to the values* array and using the default ordinal of 0.*/public E make(String value) {return make(value, 0,EMPTY_CLASS_ARRAY, EMPTY_OBJECT_ARRAY);}/*** Make a new enum instance with the given ordinal.*/public E make(String value, int ordinal) {return make(value, ordinal,EMPTY_CLASS_ARRAY, EMPTY_OBJECT_ARRAY);}/*** Make a new enum instance with the given value, ordinal and* additional parameters.  The additionalTypes is used to match* the constructor accurately.*/public E make(String value, int ordinal,Class[] additionalTypes, Object[] additional) {try {undoStack.push(new Memento());ConstructorAccessor ca = findConstructorAccessor(additionalTypes, clazz);return constructEnum(clazz, ca, value,ordinal, additional);} catch (Exception e) {throw new IllegalArgumentException("Could not create enum", e);}}/*** This method adds the given enum into the array* inside the enum class.  If the enum already* contains that particular value, then the value* is overwritten with our enum.  Otherwise it is* added at the end of the array.** In addition, if there is a constant field in the* enum class pointing to an enum with our value,* then we replace that with our enum instance.** The ordinal is either set to the existing position* or to the last value.** Warning: This should probably never be called,* since it can cause permanent changes to the enum* values.  Use only in extreme conditions.** @param e the enum to add*/public void addByValue(E e) {try {undoStack.push(new Memento());Field valuesField = findValuesField();// we get the current Enum[]E[] values = values();for (int i = 0; i < values.length; i++) {E value = values[i];if (value.name().equals(e.name())) {setOrdinal(e, value.ordinal());values[i] = e;replaceConstant(e);return;}}// we did not find it in the existing array, thus// append it to the arrayE[] newValues =Arrays.copyOf(values, values.length + 1);newValues[newValues.length - 1] = e;ReflectionHelper.setStaticFinalField(valuesField, newValues);int ordinal = newValues.length - 1;setOrdinal(e, ordinal);addSwitchCase();} catch (Exception ex) {throw new IllegalArgumentException("Could not set the enum", ex);}}/*** We delete the enum from the values array and set the* constant pointer to null.** @param e the enum to delete from the type.* @return true if the enum was found and deleted;*         false otherwise*/public boolean deleteByValue(E e) {if (e == null) throw new NullPointerException();try {undoStack.push(new Memento());// we get the current E[]E[] values = values();for (int i = 0; i < values.length; i++) {E value = values[i];if (value.name().equals(e.name())) {E[] newValues =Arrays.copyOf(values, values.length - 1);System.arraycopy(values, i + 1, newValues, i,values.length - i - 1);for (int j = i; j < newValues.length; j++) {setOrdinal(newValues[j], j);}Field valuesField = findValuesField();ReflectionHelper.setStaticFinalField(valuesField, newValues);removeSwitchCase(i);blankOutConstant(e);return true;}}} catch (Exception ex) {throw new IllegalArgumentException("Could not set the enum", ex);}return false;}/*** Undo the state right back to the beginning when the* EnumBuster was created.*/public void restore() {while (undo()) {//}}/*** Undo the previous operation.*/public boolean undo() {try {Memento memento = undoStack.poll();if (memento == null) return false;memento.undo();return true;} catch (Exception e) {throw new IllegalStateException("Could not undo", e);}}private ConstructorAccessor findConstructorAccessor(Class[] additionalParameterTypes,Class<E> clazz) throws NoSuchMethodException {Class[] parameterTypes =new Class[additionalParameterTypes.length + 2];parameterTypes[0] = String.class;parameterTypes[1] = int.class;System.arraycopy(additionalParameterTypes, 0,parameterTypes, 2,additionalParameterTypes.length);Constructor<E> cstr = clazz.getDeclaredConstructor(parameterTypes);return reflection.newConstructorAccessor(cstr);}private E constructEnum(Class<E> clazz,ConstructorAccessor ca,String value, int ordinal,Object[] additional)throws Exception {Object[] parms = new Object[additional.length + 2];parms[0] = value;parms[1] = ordinal;System.arraycopy(additional, 0, parms, 2, additional.length);return clazz.cast(ca.newInstance(parms));}/*** The only time we ever add a new enum is at the end.* Thus all we need to do is expand the switch map arrays* by one empty slot.*/private void addSwitchCase() {try {for (Field switchField : switchFields) {int[] switches = (int[]) switchField.get(null);switches = Arrays.copyOf(switches, switches.length + 1);ReflectionHelper.setStaticFinalField(switchField, switches);}} catch (Exception e) {throw new IllegalArgumentException("Could not fix switch", e);}}private void replaceConstant(E e)throws IllegalAccessException, NoSuchFieldException {Field[] fields = clazz.getDeclaredFields();for (Field field : fields) {if (field.getName().equals(e.name())) {ReflectionHelper.setStaticFinalField(field, e);}}}private void blankOutConstant(E e)throws IllegalAccessException, NoSuchFieldException {Field[] fields = clazz.getDeclaredFields();for (Field field : fields) {if (field.getName().equals(e.name())) {ReflectionHelper.setStaticFinalField(field, null);}}}private void setOrdinal(E e, int ordinal)throws NoSuchFieldException, IllegalAccessException {Field ordinalField = Enum.class.getDeclaredField(ORDINAL_FIELD);ordinalField.setAccessible(true);ordinalField.set(e, ordinal);}/*** Method to find the values field, set it to be accessible,* and return it.** @return the values array field for the enum.* @throws NoSuchFieldException if the field could not be found*/private Field findValuesField()throws NoSuchFieldException {// first we find the static final array that holds// the values in the enum classField valuesField = clazz.getDeclaredField(VALUES_FIELD);// we mark it to be publicvaluesField.setAccessible(true);return valuesField;}private Collection<Field> findRelatedSwitchFields(Class[] switchUsers) {Collection<Field> result = new ArrayList<Field>();try {for (Class switchUser : switchUsers) {Class[] clazzes = switchUser.getDeclaredClasses();for (Class suspect : clazzes) {Field[] fields = suspect.getDeclaredFields();for (Field field : fields) {if (field.getName().startsWith("$SwitchMap$" +clazz.getSimpleName())) {field.setAccessible(true);result.add(field);}}}}} catch (Exception e) {throw new IllegalArgumentException("Could not fix switch", e);}return  result;}private void removeSwitchCase(int ordinal) {try {for (Field switchField : switchFields) {int[] switches = (int[]) switchField.get(null);int[] newSwitches = Arrays.copyOf(switches, switches.length - 1);System.arraycopy(switches, ordinal + 1, newSwitches,ordinal, switches.length - ordinal - 1);ReflectionHelper.setStaticFinalField(switchField, newSwitches);}} catch (Exception e) {throw new IllegalArgumentException("Could not fix switch", e);}}@SuppressWarnings("unchecked")private E[] values()throws NoSuchFieldException, IllegalAccessException {Field valuesField = findValuesField();return (E[]) valuesField.get(null);}private class Memento {private final E[] values;private final Map<Field, int[]> savedSwitchFieldValues =new HashMap<Field, int[]>();private Memento() throws IllegalAccessException {try {values = values().clone();for (Field switchField : switchFields) {int[] switchArray = (int[]) switchField.get(null);savedSwitchFieldValues.put(switchField,switchArray.clone());}} catch (Exception e) {throw new IllegalArgumentException("Could not create the class", e);}}private void undo() throwsNoSuchFieldException, IllegalAccessException {Field valuesField = findValuesField();ReflectionHelper.setStaticFinalField(valuesField, values);for (int i = 0; i < values.length; i++) {setOrdinal(values[i], i);}// reset all of the constants defined inside the enumMap<String, E> valuesMap =new HashMap<String, E>();for (E e : values) {valuesMap.put(e.name(), e);}Field[] constantEnumFields = clazz.getDeclaredFields();for (Field constantEnumField : constantEnumFields) {E en = valuesMap.get(constantEnumField.getName());if (en != null) {ReflectionHelper.setStaticFinalField(constantEnumField, en);}}for (Map.Entry<Field, int[]> entry :savedSwitchFieldValues.entrySet()) {Field field = entry.getKey();int[] mappings = entry.getValue();ReflectionHelper.setStaticFinalField(field, mappings);}}}
}

该类很长,可能仍然存在一些错误。 我是从旧金山到纽约的途中写的。 这是我们可以使用它来测试人类课程的方法:

import junit.framework.TestCase;public class HumanTest extends TestCase {public void testSingingAddingEnum() {EnumBuster<HumanState> buster =new EnumBuster<HumanState>(HumanState.class,Human.class);try {Human heinz = new Human();heinz.sing(HumanState.HAPPY);heinz.sing(HumanState.SAD);HumanState MELLOW = buster.make("MELLOW");buster.addByValue(MELLOW);System.out.println(Arrays.toString(HumanState.values()));try {heinz.sing(MELLOW);fail("Should have caused an IllegalStateException");}catch (IllegalStateException success) { }}finally {System.out.println("Restoring HumanState");buster.restore();System.out.println(Arrays.toString(HumanState.values()));}}
}

现在,此单元测试在前面显示的Human.java文件中显示了错误。 我们忘记添加throw关键字!

When you're happy and you know it ...
Don't cry for me Argentina, ...
[HAPPY, SAD, MELLOW]
Restoring HumanState
[HAPPY, SAD]AssertionFailedError: Should have caused an IllegalStateExceptionat HumanTest.testSingingAddingEnum(HumanTest.java:23)

EnumBuster类可以做的更多。 我们可以使用它来删除不需要的枚举。 如果我们指定switch语句是哪个类,则将同时维护这些类。 另外,我们可以还原到初始状态。 很多功能!

我注销之前的最后一个测试用例,我们将测试类添加到switch类中以进行维护。

import junit.framework.TestCase;public class EnumSwitchTest extends TestCase {public void testSingingDeletingEnum() {EnumBuster<HumanState> buster =new EnumBuster<HumanState>(HumanState.class,EnumSwitchTest.class);try {for (HumanState state : HumanState.values()) {switch (state) {case HAPPY:case SAD:break;default:fail("Unknown state");}}buster.deleteByValue(HumanState.HAPPY);for (HumanState state : HumanState.values()) {switch (state) {case SAD:break;case HAPPY:default:fail("Unknown state");}}buster.undo();buster.deleteByValue(HumanState.SAD);for (HumanState state : HumanState.values()) {switch (state) {case HAPPY:break;case SAD:default:fail("Unknown state");}}buster.deleteByValue(HumanState.HAPPY);for (HumanState state : HumanState.values()) {switch (state) {case HAPPY:case SAD:default:fail("Unknown state");}}} finally {buster.restore();}}
}

EnumBuster甚至保留常量,因此,如果从values()中删除一个枚举,它将清空最终的静态字段。 如果重新添加,它将设置为新值。

肯·多布森(Ken Dobson)的想法以一种我不知道有可能的方式进行反思,真是令人愉悦。 (任何Sun工程师都读过这篇文章,请不要在Java的未来版本中插入这些漏洞!)

亲切的问候

亨氏

JavaSpecialists在您公司内提供所有课程。 更多信息 …
请务必阅读我们有关Java并发性的新课程。 请与我联系以获取更多信息。

关于Heinz M.Kabutz博士

自2000年以来,我一直在为Java专家社区写作。这很有趣。 当您与可能会喜欢的人分享这本书时,这会更加有趣。 如果他们前往www.javaspecialists.eu并将自己添加到列表中,则他们可以每个月获得新鲜的东西。

中继:这篇文章是Java Advent Calendar的一部分,并根据Creative Commons 3.0 Attribution许可获得许可。 如果您喜欢它,请通过共享,发推,FB,G +等来传播信息! 想为博客写文章吗? 我们正在寻找能够填补所有24个职位的贡献者,并希望能为您贡献力量! 联系Attila Balazs贡献力量!

参考资料:来自Java日历日历博客的JCG合作伙伴 Attila-Mihaly Balazs的“枚举枚举和修改”最终静态”字段 。

翻译自: https://www.javacodegeeks.com/2012/12/of-hacking-enums-and-modifying-final-static-fields.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/369718.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java编译找不到符号_关于久违的Javac,编译出现“找不到符号”

参考文档&#xff1a;http://blog.csdn.net/qq369201191/article/details/49946609工作以来习惯了maven编译&#xff0c;已经忘记了javac这个东东&#xff0c;以至于遇到javac问题时困惑了&#xff0c;下面总结一下&#xff0c;以便后者参考。一、使用javac进行项目java文件编译…

CSS--居中方式总结

一、水平居中方法 1.行内元素、字体的水平居中 1.对于行内元素&#xff08;display值为inline或inline-block都可以&#xff09;或者字体&#xff1a;父元素添加css规则&#xff1a;text-align&#xff1a;center; <style> p{/*关键*/text-align:center; }ul{/*关键*/…

009-MailUtils工具类模板

版本一&#xff1a;JavaMail的一个工具类 package ${enclosing_package};import java.security.GeneralSecurityException; import java.util.Properties;import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.ma…

HTML5 Video标签

1.代码格式 <video width"320" height"240" controls><source src"movie.mp4" type"video/mp4"><source src"movie.ogg" type"video/ogg">您的浏览器不支持Video标签。</video>视频格式及…

某些小时后MySql连接自动掉线

MySql配置为删除任何闲置超过8小时的连接。 这意味着什么&#xff1f; 在8个小时的间隔后返回到已部署的应用程序之后&#xff08;如果未更改默认SQL参数&#xff09;&#xff0c;将会遇到异常情况。 如何解决这个问题&#xff1f; 增加wait_time参数-不是一个好主意&#xff…

AutoMapper的使用

本来是想洋洋洒洒写点儿关于这个神奇的具体使用方法&#xff0c;但是发现园子里已经有了&#xff0c;URL奉上&#xff1a;http://www.cnblogs.com/CreateMyself/p/7635429.html&#xff0c;直接打开撸就行。转载于:https://www.cnblogs.com/pangjianxin/p/8367589.html

shopxx 阿里云OSS设置

shopxx 使用文档没有啊&#xff0c;只能自己看了 数据中心 字段其实是 EndPoint字段 URL前缀 是 图片服务器的主机地址。这个在阿里云回传的时候是不带的。 对应 阿里OSS 外网域名 转载于:https://www.cnblogs.com/nanahome/p/7346641.html

bio java 例子_JAVA BIO 服务器与客户端实现示例

代码只兼容JAVA 7及以上版本。服务器端代码&#xff1a;package com.stevex.app.bio;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;import j…

我的HTML总结之常用基础便签

HTML&#xff1a;是Hyper Text Markup Language&#xff08;超级文本标记语言&#xff09;的缩写&#xff0c;HTML不是一种程序&#xff0c;只是一种控制网页中数据显示的标识语言。 HTML由一组标签组成。 HTML的基本结构 <html> <head> <title>第一个HTML示…

您是否应该信任JVM中的默认设置?

如今&#xff0c;JVM被认为是智能的。 预期不会进行太多配置–只需设置要在启动脚本中使用的最大堆&#xff0c;您就可以进行了。 所有其他默认设置都很好。 大概我们当中有些人误以为。 实际上&#xff0c;在运行时期间发生了很多事情&#xff0c;无法自动调整性能&#xff0c…

【翻译】A Next-Generation Smart Contract and Decentralized Application Platform

原文链接&#xff1a;https://github.com/ethereum/wiki/wiki/White-Paper 当中本聪在2009年1月启动比特币区块链时&#xff0c;他同时向世界引入了两种未经测试的革命性的新概念。第一种就是比特币&#xff08;bitcoin&#xff09;&#xff0c;一种去中心化的点对点的网上货币…

SAS Fuctions

1. monotonic(), 单调递增函数。返回一列变量的序列等&#xff0c;类似于_N_ 。 2. index v indexw: INDEX Function Searches a character expression for a string of characters, and returns the position of the string’s first character for the first occurrence of t…

循环内的局部变量和性能

总览 有时会出现一个问题&#xff0c;即分配一个新的局部变量需要花费多少工作。 我的感觉一直是&#xff0c;代码已优化到成本为静态的程度&#xff0c;即一次执行&#xff0c;而不是每次运行时都执行。 最近&#xff0c; Ishwor Gurung建议考虑将一些局部变量移出循环。 我怀…

CSS3伪元素、伪类选择器

伪元素选择器&#xff1a; ::first-letter:为某个元素中的文字的首字母或第一个字使用样式。 ::first-line:为某个元素的第一行文字使用样式。 ::before:在某个元素之前插入一些内容。 ::after:在某个元素之后插入一些内容 ::selection:匹配元素中被用户选中或处于高亮状态的部…

bzoj1212: [HNOI2004]L语言

这又是什么神题啊。 这题一眼AC机。然后呢企鹅也是这么想的。 写完发现企鹅看错题了。然后其实建字典树就行了。 弄个v数组表示能否匹配到第i个位置。然后因为字典里的串很短&#xff0c;就判一下前面L&#xff08;表示字典里最长那个串的长度&#xff09;个位置能否匹配&#…

css小随笔(二)与通用样式

51先在学校HTML5已经有半个多月了&#xff0c;然后这个星期做了一个京东的手机网站&#xff0c;接触到了通用样式&#xff0c;下面以京东的手机站为例 这两个就是京东手机站了的不同的两个板块&#xff0c;因为HTML5仅仅只是学完了基本标签跟css的标签&#xff0c;所以在没有接…

增加堆大小–谨防眼镜蛇效应

“眼镜蛇效应”一词源于英国殖民印度统治英国时所产生的轶事。 英国政府担心毒蛇眼镜蛇的数量。 因此&#xff0c;政府对每条死蛇给予悬赏。 最初&#xff0c;这是一个成功的策略&#xff0c;因为大量蛇被杀死以获取奖励。 最终&#xff0c;印度人开始养殖眼镜蛇以赚取收入。 …

label 标签里面元素点击事件

想做一个单击显示&#xff0c;单击消失的效果&#xff0c;两个元素都在label标签里面&#xff0c;通过打log发现&#xff0c;当点击消失的时候&#xff0c;先执行了消失的单击事件&#xff0c;有执行了出现的单击事件&#xff0c;所以元素并没有消失&#xff0c;这个出现的原因…

java contenttype_POST不同提交方式对应的Content-Type,及java服务器接收参数方式

简介:Content-Type(MediaType)&#xff0c;即是Internet Media Type&#xff0c;互联网媒体类型&#xff1b;也叫做MIME类型&#xff0c;在Http协议消息头中&#xff0c;使用Content-Type来表示具体请求中的媒体类型信息.参考response.Header里常见Content-Type一般有以下四种&…

Java 进程占用 VIRT 虚拟内存超高的问题研究

一、现象说明 最近发现线上机器java 7(openjdk)进程的 VIRT 虚拟内存使用达到了 50G&#xff0c;如下所示&#xff1a; PID    USER  PR NI VIRT  RES SHR S   %CPU   %MEM   TIME   COMMAND 3130   tomcat  20 0 9128m  1.4g 6544 S…