我很想知道您希望如何为Java Web应用程序自动化Javascript缩小.以下是我特别感兴趣的几个方面:
这将主要作为我未来项目的参考(希望其他SOer也会发现它的信息),所以各种工具都很有趣.
(请注意,这不是关于哪个minifier最好的问题.我们已经有很多这样的.)
考虑以下一般形式的功能:
Foo findFoo(Collection<Foo> foos, otherarguments)
throws ObjectNotFoundException {
for(Foo foo : foos){
if(/* foo meets some condition*/){
return foo;
}
}
throw new ObjectNotFoundException();
}
Run Code Online (Sandbox Code Playgroud)
例如,一个具体的案例是:
User findUserByName(Collection<User> users, String name)
throws ObjectNotFoundException {
for(User user : users){
if(user.getName().equals(name)){
return user;
}
}
throw new ObjectNotFoundException();
}
Run Code Online (Sandbox Code Playgroud)
如果找不到对象,这些函数会抛出异常.我可以为此目的创建一个自定义异常类(在示例中ObjectNotFoundException),但我更喜欢使用现有的类.但是,我在标准java库中找不到具有此含义的任何异常类.您知道是否有可以在这里使用的标准例外吗?
我正在做一些Scala体操,Seq[T]在那里我尝试找到"最小"的元素.这就是我现在所做的:
val leastOrNone = seq.reduceOption { (best, current) =>
if (current.something < best.something) current
else best
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我不太满意 - 对于这么简单的事情来说有点长,而且我并不关心"如果".使用minBy会更优雅:
val least = seq.minBy(_.something)
Run Code Online (Sandbox Code Playgroud)
......但min与minBy抛出异常时序列为空.是否有一种惯用的,更优雅的方式来查找可能为空的列表中的最小元素Option?
码:
Multimap<String, String> myMultimap = ArrayListMultimap.create();
myMultimap.put("12345", "qwer");
myMultimap.put("12345", "abcd");
myMultimap.put("12345", "qwer");
System.out.println(myMultimap);
Run Code Online (Sandbox Code Playgroud)
结果:
{12345=[qwer, abcd, qwer]}
Run Code Online (Sandbox Code Playgroud)
是否有可能消除重复的"qwer"?谢谢.
是否有任何方法可以为本地范围的变量和本地成员变量/方法显示自动建议下拉列表而无需按ctrl + space?像Visual Studio一样吗?在输入"对象"之后,它会自动显示为对象的成员.
我发现自己输入了几个字母并直接点击了ctrl +空格,如果我能看到列表知道是否输入更多字母,或点击向下键是最快的话,对我来说会更有效率. ..
我在设置中找不到这个选项,所以想知道是否有其他方法可以实现我所追求的目标.
谢谢,山姆.
(后期编辑:这个问题将有望被淘汰时的Java 7自带因为的,"最终重投"功能,这似乎将被添加.)
很多时候,我发现自己处于这样的情况:
do some initialization
try {
do some work
} catch any exception {
undo initialization
rethrow exception
}
在C#中你可以这样做:
InitializeStuff();
try
{
DoSomeWork();
}
catch
{
UndoInitialize();
throw;
}
Run Code Online (Sandbox Code Playgroud)
对于Java来说,没有很好的替代,并且由于改进的异常处理的提议是从Java 7中删除的,所以看起来好几年才能得到类似的东西.因此,我决定自己动手:
(编辑: 半年后,最后的重新抛出回来了,或者看起来如此.)
public final class Rethrow {
private Rethrow() { throw new AssertionError("uninstantiable"); }
/** Rethrows t if it is an unchecked exception. */
public static void unchecked(Throwable t) {
if (t instanceof Error)
throw …Run Code Online (Sandbox Code Playgroud) 我有一个Swing应用程序,一个大面板,包裹在一个JScrollPane.用户通常通过Tab键在面板的子组件之间移动,因此当他们选择显示视图时,我希望滚动窗格自动滚动,以便具有输入焦点的组件始终可见.
我试过KeyboardFocusManager用来监听输入焦点的变化,然后调用scrollRectToVisible.
这是一个显示我当前策略的SSCCE(只需复制/粘贴并运行!):
import java.awt.KeyboardFocusManager;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
public class FollowFocus {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final int ROWS = 100;
final JPanel content = new JPanel();
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
content.add(new JLabel(
"Thanks for helping out. Use tab to move around."));
for (int i = 0; i < ROWS; i++) {
JTextField field = new JTextField("" + i);
field.setName("field#" + …Run Code Online (Sandbox Code Playgroud) Eclipse的新类型的默认模板(Window> Preferences> Code Style> Code Templates> New Java Files)如下所示:
${filecomment}
${package_declaration}
${typecomment}
${type_declaration}
创建一个新类,它看起来像这样:
package pkg;
import blah.blah;
public class FileName {
// Class is accessible to everyone, and can be inherited
}
Run Code Online (Sandbox Code Playgroud)
现在,我热切地认为访问应该尽可能地受到限制,除非明确允许,否则应该禁止继承,所以我想改变${type_declaration}声明所有类final而不是public:
package pkg;
import blah.blah;
final class FileName {
// Class is only accessible in package, and can't be inherited
}
Run Code Online (Sandbox Code Playgroud)
说起来容易做起来难.我发现谷歌搜索的唯一问题是关于Eclipse邮件列表的2004年问题,该问题未得到答复.
那么,简而言之:如何在Eclipse中更改默认的类/类型修饰符?
如果重要的话,我正在使用Eclipse Galileo(3.5).
我有一些非常大的XML文件,我用它读取System.Xml.Serialization.XmlSerializer.它非常快(嗯,足够快),但我希望它能够汇集字符串,因为一些长字符串会出现很多次.
XML看起来有点像这样:
<Report>
<Row>
<Column name="A long column name!">hey</Column>
<Column name="Another long column name!!">7</Column>
<Column name="A third freaking long column name!!!">hax</Column>
<Column name="Holy cow, can column names really be this long!?">true</Column>
</Row>
<Row>
<Column name="A long column name!">yo</Column>
<Column name="Another long column name!!">53</Column>
<Column name="A third freaking long column name!!!">omg</Column>
<Column name="Holy cow, can column names really be this long!?">true</Column>
</Row>
<!-- ... ~200k more rows go here... -->
</Report>
Run Code Online (Sandbox Code Playgroud)
XML被反序列化的类看起来像这样:
class Report
{
public Row[] Rows { …Run Code Online (Sandbox Code Playgroud) 我有一个页面,其中www.example.com/index.html上的脚本在弹出窗口中打开home.example.com/foo.html.当用户关闭弹出窗口时,我想通过调用它上面的Javascript函数来通知开启者页面(这对DOM做了一些事情).我用unbeforeunload这样的:
// In index.html on www.example.com:
window.fn = function () { /* Perform stuff after foo.html has closed */ }
// In foo.html on home.example.com:
window.onbeforeunload = function () {
if (window.opener && window.opener.fn)
window.opener.fn();
};
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为网页位于不同的域上.我可以设置document.domain属性来克服这个问题:
document.domain = "example.com";
Run Code Online (Sandbox Code Playgroud)
不幸的是,这与我在foo.html端(Apache Wicket)上使用的Web应用程序框架不兼容,因为它包含一个执行如下操作的脚本:
var src = (window.location.protocol == 'https:') ? something : other;
Run Code Online (Sandbox Code Playgroud)
显然,在IE6 *中,当您设置文档域时,该location对象变为只写,因此尝试读取window.location.protocol会抛出"拒绝访问".
所以,我的问题是:如何在允许我的脚本读取location对象内容的同时允许跨域Javascript函数调用?
window.location.protocol在设置之前无法读取属性document.domain,然后在条件赋值中使用该值; 这样做需要我重建Web框架库 - 而不是我想做的事情. …java ×7
eclipse ×2
exception ×2
javascript ×2
.net ×1
automation ×1
autosuggest ×1
c# ×1
coding-style ×1
collections ×1
cross-domain ×1
guava ×1
intellisense ×1
jscrollpane ×1
minify ×1
multimap ×1
scala ×1
swing ×1
templates ×1