Skip to content

Instantly share code, notes, and snippets.

View odrotbohm's full-sized avatar
👨‍💻
@ home

Oliver Drotbohm odrotbohm

👨‍💻
@ home
View GitHub Profile
@odrotbohm
odrotbohm / gist:3141439
Created July 19, 2012 07:49
Proposal: Allow EntityManager(Factory) to be injected through a constructor.

When designing application components it's best practice to inject mandatory dependencies though the constructor and essentially make the application component immutable like this:

@Named
class MyComponent implements Component {

  private final Dependency dependency;

  @Inject
 public MyComponent(Dependency dependency) {
@odrotbohm
odrotbohm / gist:3169830
Created July 24, 2012 13:04
Java bean properties riddle
class Entity {
Number id;
Long getId() {
return // transform into Long;
}
public void setId(Object object) {
this.id = // transform into Number
@odrotbohm
odrotbohm / gist:3215345
Created July 31, 2012 09:08
Git aliases to ease cherry picking

When fixing bugs on a software project I repeatedly faced the situation where I'd like to apply the commit I just did to a branch to another. Think of fixing a bug in the master branch and immediately back-porting it to the maintenance branch. This usually needed the following steps:

  1. Looking up the SHA1 of the original commit
  2. Checking out the destination branch
  3. git cherry-pick $sha1

The very first step can be eased a bit by creating a git alias to lookup the SHA1 of the last commit of a given branch.

[alias]
@odrotbohm
odrotbohm / gist:3248304
Created August 3, 2012 14:48
m2eclipse Exception
java.lang.NullPointerException
at org.maven.ide.querydsl.AptConfigurator.configure(AptConfigurator.java:76)
at org.eclipse.m2e.core.project.configurator.AbstractLifecycleMapping.configure(AbstractLifecycleMapping.java:109)
at org.eclipse.m2e.core.internal.project.ProjectConfigurationManager.updateProjectConfiguration(ProjectConfigurationManager.java:414)
at org.eclipse.m2e.core.internal.project.ProjectConfigurationManager.configureNewMavenProject(ProjectConfigurationManager.java:240)
at org.eclipse.m2e.core.internal.project.ProjectConfigurationManager.importProjects(ProjectConfigurationManager.java:156)
at org.eclipse.m2e.core.ui.internal.wizards.MavenImportWizard$1.doCreateMavenProjects(MavenImportWizard.java:164)
at org.eclipse.m2e.core.ui.internal.wizards.AbstractCreateMavenProjectsOperation.run(AbstractCreateMavenProjectsOperation.java:73)
at org.eclipse.m2e.core.ui.internal.wizards.MavenImportWizard$3.runInWorkspace(MavenImportWizard.java:249)
at org.eclipse.core.internal.resources.InternalWorks
if [ "$TERM_PROGRAM" == "Apple_Terminal" ] && [ -z "$INSIDE_EMACS" ]; then
update_terminal_cwd() {
# Identify the directory using a "file:" scheme URL,
# including the host name to disambiguate local vs.
# remote connections. Percent-escape spaces.
local SEARCH=' '
local REPLACE='%20'
local PWD_URL="file://$HOSTNAME${PWD//$SEARCH/$REPLACE}"
printf '\e]7;%s\a' "$PWD_URL"
}
@odrotbohm
odrotbohm / gist:3497047
Created August 28, 2012 10:36
JPQL / SQL riddle
JPQL: select p from Person p
left outer join p.address address
order by address.city
SQL: select person0_ from Person person0_
left outer join Address address1_ on person0_.id=address1_.person_id
order by address1_.city
JPQL: select p from Person p
left outer join p.address address
order by p.address.city
@odrotbohm
odrotbohm / Test.java
Created September 10, 2012 16:06
Test case to show String.substring(int) performance issue for Java 7u06 and later.
/**
* Test class to show a huge performance dump in Java 7u06/u07 for the {@link String#substring(int)} compared to
* {@link String#substring(int, int)}. On versions before 7u06 I get numbers like 23ms for the first method call and
* 12ms for the second. On update 6 and 7 I get 10483ms for the first call, 16ms for the second.
* <p>
* The issue seems to be related to a fix in related to {@code Arrays.copyOnWrite} (this is the hotspot the profiler
* unveiled) introduced with update 6 (see links below).
* <p>
* Feel free to add your numbers in the comments below.
*
@odrotbohm
odrotbohm / gist:3706123
Created September 12, 2012 11:49
Java properties: shadowing vs. exposing
class BaseClass {
private final Dependency dependency;
public MyBaseClass(Dependency dependency) {
Assert.notNull(dependency);
this.dependency = dependency;
}
@odrotbohm
odrotbohm / MyTest.java
Created October 31, 2012 10:54
Using JavaConfig and XML configuration in Spring integration tests
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class MyTest {
@Configuration
@ImportResource("classpath:META-INF/spring/application-context.xml")
static class Config {
// JavaConfig goes here
@odrotbohm
odrotbohm / gist:4654469
Created January 28, 2013 10:30
Stacktrace for LinkageError in OpenJPA test case with Logback.
build 28-Jan-2013 02:16:38 -------------------------------------------------------
build 28-Jan-2013 02:16:38 T E S T S
build 28-Jan-2013 02:16:38 -------------------------------------------------------
build 28-Jan-2013 02:16:39 Running org.springframework.data.jpa.repository.OpenJpaUserRepositoryFinderTests
build 28-Jan-2013 02:16:40 Failed to instantiate [ch.qos.logback.classic.LoggerContext]
build 28-Jan-2013 02:16:40 Reported exception:
build 28-Jan-2013 02:16:40 java.lang.LinkageError: loader constraint violation: when resolving method "javax.xml.parsers.SAXParser.parse(Lorg/xml/sax/InputSource;Lorg/xml/sax/helpers/DefaultHandler;)V" the class loader (instance of org/apache/openjpa/lib/util/TemporaryClassLoader) of the current class, ch/qos/logback/core/joran/event/SaxEventRecorder, and the class loader (instance of <bootloader>) for resolved class, javax/xml/parsers/SAXParser, have different Class objects for the type org/xml/sax/InputSource used in the signature
build 28-Jan-2013 02:16:40 at ch.qos