From 1b5e319a06db1766689053779e27a14f911088ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Fri, 23 Aug 2013 17:51:56 +0300 Subject: [PATCH] Add OperatorImpl cache population #481 --- .../com/mysema/query/types/OperationImpl.java | 18 +++++----- .../com/mysema/query/types/OperatorImpl.java | 35 +++++++++++++++---- .../mysema/query/types/OperatorImplTest.java | 19 ++++++++++ 3 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 querydsl-core/src/test/java/com/mysema/query/types/OperatorImplTest.java diff --git a/querydsl-core/src/main/java/com/mysema/query/types/OperationImpl.java b/querydsl-core/src/main/java/com/mysema/query/types/OperationImpl.java index 681156f800..b5ea624fbc 100644 --- a/querydsl-core/src/main/java/com/mysema/query/types/OperationImpl.java +++ b/querydsl-core/src/main/java/com/mysema/query/types/OperationImpl.java @@ -1,6 +1,6 @@ /* * Copyright 2011, Mysema Ltd - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -32,7 +32,7 @@ */ @Immutable public class OperationImpl extends ExpressionBase implements Operation { - + private static final long serialVersionUID = 4796432056083507588L; private final ImmutableList> args; @@ -42,15 +42,15 @@ public class OperationImpl extends ExpressionBase implements Operation public static Operation create(Class type, Operator operator, Expression one) { return new OperationImpl(type, operator, ImmutableList.>of(one)); } - + public static Operation create(Class type, Operator operator, Expression one, Expression two) { return new OperationImpl(type, operator, ImmutableList.of(one, two)); } - + protected OperationImpl(Class type, Operator operator, Expression... args) { this(type, operator, ImmutableList.copyOf(args)); } - + public OperationImpl(Class type, Operator operator, ImmutableList> args) { super(type); this.operator = operator; @@ -90,15 +90,15 @@ public final boolean equals(Object o) { public final R accept(Visitor v, C context) { return v.visit(this, context); } - + /** * Resets operator field to singleton version - * + * * @param ois * @throws ClassNotFoundException * @throws IOException */ - private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { + private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { try { ois.defaultReadObject(); Field field = OperationImpl.class.getDeclaredField("operator"); @@ -106,7 +106,7 @@ private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IO field.set(this, OperatorImpl.OPS.get(operator.getId())); } catch (Exception e) { throw new RuntimeException(e); - } + } } } diff --git a/querydsl-core/src/main/java/com/mysema/query/types/OperatorImpl.java b/querydsl-core/src/main/java/com/mysema/query/types/OperatorImpl.java index 007c06bcb2..4f0860c93c 100644 --- a/querydsl-core/src/main/java/com/mysema/query/types/OperatorImpl.java +++ b/querydsl-core/src/main/java/com/mysema/query/types/OperatorImpl.java @@ -1,6 +1,6 @@ /* * Copyright 2011, Mysema Ltd - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -13,7 +13,11 @@ */ package com.mysema.query.types; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import javax.annotation.concurrent.Immutable; @@ -25,13 +29,29 @@ public final class OperatorImpl implements Operator { static final Map> OPS = new HashMap>(150); - + + static { + try { + // initialize all fields of Ops + List fields = new ArrayList(); + fields.addAll(Arrays.asList(Ops.class.getFields())); + for (Class cl : Ops.class.getClasses()) { + fields.addAll(Arrays.asList(cl.getFields())); + } + for (Field field : fields) { + field.get(null); + } + } catch (Exception e) { + throw new RuntimeException(e.getMessage(), e); + } + } + private static final long serialVersionUID = -2435035383548549877L; - + private final String id; - + private final int hashCode; - + public OperatorImpl(String id) { this.id = id; this.hashCode = id.hashCode(); @@ -43,15 +63,16 @@ public OperatorImpl(String id) { * * @return */ + @Override public String getId() { return id; } - + @Override public String toString() { return id; } - + @Override public int hashCode() { return hashCode; diff --git a/querydsl-core/src/test/java/com/mysema/query/types/OperatorImplTest.java b/querydsl-core/src/test/java/com/mysema/query/types/OperatorImplTest.java new file mode 100644 index 0000000000..eb4b67e830 --- /dev/null +++ b/querydsl-core/src/test/java/com/mysema/query/types/OperatorImplTest.java @@ -0,0 +1,19 @@ +package com.mysema.query.types; + +import static org.junit.Assert.assertFalse; + +import java.lang.reflect.Field; +import java.util.Map; + +import org.junit.Test; + +public class OperatorImplTest { + + @Test + public void Cache_Isnt_Empty() throws Exception { + Field field = OperatorImpl.class.getDeclaredField("OPS"); + field.setAccessible(true); + Map map = (Map) field.get(null); + assertFalse(map.isEmpty()); + } +}