小编bl3*_*l3e的帖子

在编译时有效地获取java源代码中的行号

我有一个异常类,我想在其中传递当前行号

     line 70:
     line 71: throw new 
              LightException(FailureType.NOT_FOUND,this.getClass().getName(),linenum);
Run Code Online (Sandbox Code Playgroud)

有没有办法在没有硬编码的情况下将亚麻布作为72? eclipse是否提供了在编译时被替换为硬编码行号的任何东西.所以我不必把丑陋的硬编码行号

class LightException(FailureType type,String className,int lineNum)  extends RuntimeException 
{

LightException(FailureType type,String className,int lineNum){..
//
}

@Override
@Override public Throwable fillInStackTrace() { 
return this;
 }
}
Run Code Online (Sandbox Code Playgroud)

我不需要记录整个堆栈跟踪,并且不必为所有异常填充堆栈跟踪.我想添加引发异常的行号. 任何可以在编译时解析为常量的代码?

如果没有那么我可以写一个简单的预处理我的代码,它可以读取行并用行号替换一个特殊的常量_my_line_num,但应该存在一些东西.

我觉得像gradle这样的构建工具可以做到这一点.

java eclipse gradle

11
推荐指数
1
解决办法
787
查看次数

解释用Hibernate映射自动递增的复合id序列的行为

我有一张桌子

CREATE TABLE `SomeEntity` (
     `id` int(11) NOT NULL AUTO_INCREMENT,
     `subid` int(11) NOT NULL DEFAULT '0',
     PRIMARY KEY (`id`,`subid`),
Run Code Online (Sandbox Code Playgroud)

我有一个实体类,其中包含一个自动增量字段.我希望在持久化时读取分配给它的自动增量ID

getter上的注释如下

  private long id;
   private int subid;
  @Id
  @GeneratedValue **//How do i correct this to have multiple rows with same id and different subid**
  @Column(name = "id")
  public long getId() {
    return id;
  }

  @Id
  @Column(name = "subid")
  public int getSubid() {
    return subid;
  }
Run Code Online (Sandbox Code Playgroud)

我希望有实体

id 1 subid 0 
id 1 subid 1
id 1 subid 2
id 2 …
Run Code Online (Sandbox Code Playgroud)

java mysql spring hibernate jpa

11
推荐指数
1
解决办法
1655
查看次数

捕获未在本地抛出的异常?

我的问题与try catch块的语法行为有关

空的尝试块与捕获这样

void fun() {
    try {}
    catch(Exception e) {}
}
Run Code Online (Sandbox Code Playgroud)

要么

try {}
catch(ArrayIndexOutOfBoundsException e) {}
Run Code Online (Sandbox Code Playgroud)

编译好但编译器抱怨

try {}
catch(IOException e) {}
Run Code Online (Sandbox Code Playgroud)

为什么编译器允许捕获任何类型为Exception或RuntimeException的东西,而它却抱怨带有已检查异常的无法访问的代码?是因为JVM代码可以抛出这些类型吗?JVM怎么可能在空的try块中抛出ArrayIndexOutOfBoundsException?

java exception

8
推荐指数
1
解决办法
780
查看次数

Hadoop具有推测执行的多个输出

我有一个任务,它将avro输出写入由输入记录的几个字段组织的多个目录中.

For example : 
Process records of countries across years 
and write in a directory structure of country/year 
eg:
outputs/usa/2015/outputs_usa_2015.avro 
outputs/uk/2014/outputs_uk_2014.avro
AvroMultipleOutputs multipleOutputs=new AvroMultipleOutputs(context);
....
....
     multipleOutputs.write("output", avroKey, NullWritable.get(), 
            OUTPUT_DIR + "/" + record.getCountry() + "/" + record.getYear() + "/outputs_" +record.getCountry()+"_"+ record.getYear());
Run Code Online (Sandbox Code Playgroud)

下面的代码用什么输出提交器来编写输出.用于推测执行是不安全的?通过推测执行,这会导致(可能导致)org.apache.hadoop.hdfs.server.namenode.LeaseExpiredException

在这篇文章中 Hadoop Reducer:如何使用推测执行输出到多个目录? 建议使用自定义输出提交器

来自hadoop AvroMultipleOutputs的以下代码未说明推测执行的任何问题

 private synchronized RecordWriter getRecordWriter(TaskAttemptContext taskContext,
          String baseFileName) throws IOException, InterruptedException {

    writer =
                ((OutputFormat) ReflectionUtils.newInstance(taskContext.getOutputFormatClass(),
                    taskContext.getConfiguration())).getRecordWriter(taskContext);
...
}
Run Code Online (Sandbox Code Playgroud)

如果baseoutput路径在作业目录之外,write方法也不会记录任何问题

public void write(String namedOutput, Object key, Object value, String baseOutputPath)
Run Code Online (Sandbox Code Playgroud)

在作业目录外写作时,AvroMultipleOutputs(其他输出)是否存在推测执行的实际问题?如果,那么我如何覆盖AvroMultipleOutputs以拥有它自己的输出提交器.我在AvroMultipleOutputs中看不到任何输出格式,它使用的输出提交者

java hadoop speculative-execution hadoop-yarn multipleoutputs

7
推荐指数
1
解决办法
617
查看次数

Java泛型如何接受泛型参数中的任何派生类型

在以下2行代码中

HashMap<Integer, ?extends Collection<String>> map=
                                           new HashMap<Integer, TreeSet<String>>();

map.put(1,new TreeSet<String>());
Run Code Online (Sandbox Code Playgroud)

第2行:方法put(Integer,capture#1-of?extends Collection)在HashMap>类型中不适用于参数(int,TreeSet)

第1行:这没有错误.

为什么第1行允许使用相同的泛型类型(TreeSet <String>)但第2行不允许?

编辑:使用super而不是extends,为什么不允许以下内容.

HashMap<Integer, ?super Collection<String>> map=new HashMap(<Integer, TreeSet<String>>());
Run Code Online (Sandbox Code Playgroud)

HashMap<Integer, ?super Collection<String>> map=new HashMap();
map.put(1,new TreeSet<String>());
Run Code Online (Sandbox Code Playgroud)

被允许

java generics

6
推荐指数
1
解决办法
1231
查看次数

正确使用Entity AND DTO在Restful web service中提供Json

有很多文章指出 JPA/hibernate 不需要使用 DTO

使用视图模式中的开放会话或严格的组装阶段来避免未获取数据的问题。Hibernate 将开发人员从编写乏味的数据传输对象 (DTO) 中解放出来…… 以上行来自https://docs.jboss.org /hibernate/orm/3.5/reference/en/html/best-practices.html

另外在SO 成员 Bohzo的一篇文章中,我阅读了很少需要 DTO

即使在反对公开实体的文章中也指出,当实体没有任何行为(当它们是 POJO 时)时,不需要像在贫血域模型中那样的 DTO

假设有一个实体类

class Department{
    List<Employee> employees //lazily loaded collection 
Run Code Online (Sandbox Code Playgroud)

集合中的每个对象都包含另一个延迟加载的集合

 class Employee{
    List<Account> accounts
Run Code Online (Sandbox Code Playgroud)

有一个 getDepartment() 方法,它被一个宁静的服务用来提供部门的 Json 信息。

可能的解决方案是

解决方案 1)根据休眠文档打开和关闭每个请求的休眠会话(这是控制器中最上面的方法是事务性的?)或者更好地使用 Spring 的 OpenSessionInViewFilter作为这个SO 帖子

为什么 hibernate 不能重新打开会话并获取延迟加载的对象而不是抛出异常?有没有办法用 JPA/hibernate 配置它?

解决方案 2) 就像在 hibernate doc 中一样,另一种方法是进行组装阶段。这到底是什么意思? 将 getDepartment API 分解为 DAO 的不同 API 吗?

解决方案 3)使用 DTO即使有了 DTO,持久层如何才能知道视图是否需要满载的部门。这导致 API …

java spring design-patterns hibernate jpa

5
推荐指数
1
解决办法
2237
查看次数

在独立应用程序中使用Jersey的依赖注入

我这里有一个界面

interface Idemo{
  public int getDemo(int i);
}
Run Code Online (Sandbox Code Playgroud)

这是一个实现

class DemoImpl implements Idemo{
  @Override
  public int getDemo(int i){
    return i+10;
  }
}
Run Code Online (Sandbox Code Playgroud)

并且有一个类依赖于Idemo

class Sample{
  @Inject
  Idemo demo;

  public int getSample(int i){
    return demo.getDemo(i);
  }
}
Run Code Online (Sandbox Code Playgroud)

现在说我想测试Sample类

public class SampleTest extends JerseyTest {
  @Inject
  Sample s; 

  @Override
  protected Application configure() {
    AbstractBinder binder = new AbstractBinder() {
      @Override
      protected void configure() {
        bind(Demo.class).to(Idemo.class);
        bind(Sample.class).to(Sample.class); //**doesn't work**
      }
    };
    ResourceConfig config = new ResourceConfig(Sample.class);
    config.register(binder);
    return config;
  }
  @Test
  public void …
Run Code Online (Sandbox Code Playgroud)

java dependency-injection jersey jersey-2.0 hk2

3
推荐指数
1
解决办法
1790
查看次数

如何在java中解析对静态方法的调用?

如果在编译时解析静态方法,那么对象实例如何能够调用静态方法?

class StaticCall
{
    public static void main(String[] args)
    {
        String arr[]={"Class StaticCall","calls static method of class MyMainClass"};

        MyMainClass h=new MyMainClass();
        h.main(arr);         //How is an instance able to call a static method?
        System.out.println("this is StaticCall main");  
    }   

}


class MyMainClass 
{
    public static void main(String[] args){
        System.out.println(args[0]+" "+ args[1]);
    }
}
Run Code Online (Sandbox Code Playgroud)

运行StaticCall类后,输出为

StaticCall类调用MyMainClass类的静态方法

这是StaticCall的主要内容

由于静态字段和方法属于Class对象,实例如何能够调用静态方法?此外,何时创建了Class对象,它是否首次访问其中的任何字段或方法?

java static

2
推荐指数
1
解决办法
391
查看次数

Consistency of contains method on a HashSet and HashMap

Why does containsAll method on a HashSet does not remain consistent if remove is called on the Set whereas a containsValue method on a HashMap remains consistent after a value is removed After a value is removed from a HashSet containsAll returns false even if all values were present where as in case of HashMap the containsValue method returns correct value

public static void main(String[] args)
    {

    HashSet<String> lookup=new HashSet<String>();
    HashMap<Integer,String> findup=new HashMap<Integer,String>();

    String[] Alltokens={"This","is","a","programming","test","This","is","a","any","language"};
    for(String s:Alltokens)
        lookup.add(s);

    String[] tokens={"This","is","a"}; …
Run Code Online (Sandbox Code Playgroud)

java hashmap hashset

0
推荐指数
1
解决办法
289
查看次数