Skip to content

Commit

Permalink
Add slf4j support
Browse files Browse the repository at this point in the history
  • Loading branch information
konsoletyper committed Mar 8, 2015
1 parent 4f50895 commit bcf0929
Show file tree
Hide file tree
Showing 9 changed files with 560 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<module>teavm-cli</module>
<module>teavm-chrome-rdp</module>
<module>teavm-tests</module>
<module>teavm-extras-slf4j</module>
</modules>

<dependencyManagement>
Expand Down
4 changes: 4 additions & 0 deletions teavm-extras-slf4j/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/target/
/.settings/
/.classpath
/.project
68 changes: 68 additions & 0 deletions teavm-extras-slf4j/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<!--
Copyright 2015 Alexey Andreev.
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
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.teavm</groupId>
<artifactId>teavm</artifactId>
<version>0.3.0-SNAPSHOT</version>
</parent>
<artifactId>teavm-extras-slf4j</artifactId>

<name>TeaVM slf4j</name>
<description>TeaVM backend for slf4j</description>

<dependencies>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-core</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-jso</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>../checkstyle.xml</configLocation>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2015 Alexey Andreev.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.extras.slf4j;

import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;
import org.teavm.diagnostics.Diagnostics;
import org.teavm.model.*;
import org.teavm.model.instructions.*;

/**
*
* @author Alexey Andreev
*/
public class LoggerFactoryTransformer implements ClassHolderTransformer {
@Override
public void transformClass(ClassHolder cls, ClassReaderSource innerSource, Diagnostics diagnostics) {
if (!cls.getName().equals(LoggerFactory.class.getName())) {
return;
}
addCacheField(cls);
modifyClinit(cls);
replaceGetFactory(cls);
}

private void addCacheField(ClassHolder cls) {
FieldHolder cacheField = new FieldHolder("loggerFactoryCache");
cacheField.setLevel(AccessLevel.PRIVATE);
cacheField.getModifiers().add(ElementModifier.STATIC);
cacheField.setType(ValueType.object(TeaVMLoggerFactory.class.getName()));
cls.addField(cacheField);
}

private void modifyClinit(ClassHolder cls) {
MethodHolder clinit = cls.getMethod(new MethodDescriptor("<clinit>", void.class));
BasicBlock clinitBlock = clinit.getProgram().basicBlockAt(0);
Variable factoryVar = clinit.getProgram().createVariable();
ConstructInstruction construct = new ConstructInstruction();
construct.setType(TeaVMLoggerFactory.class.getName());
construct.setReceiver(factoryVar);
clinitBlock.getInstructions().add(0, construct);
InvokeInstruction init = new InvokeInstruction();
init.setInstance(factoryVar);
init.setMethod(new MethodReference(TeaVMLoggerFactory.class, "<init>", void.class));
init.setType(InvocationType.SPECIAL);
clinitBlock.getInstructions().add(1, init);
PutFieldInstruction put = new PutFieldInstruction();
put.setValue(factoryVar);
put.setField(new FieldReference(LoggerFactory.class.getName(), "loggerFactoryCache"));
clinitBlock.getInstructions().add(2, put);
}

private void replaceGetFactory(ClassHolder cls) {
MethodHolder method = cls.getMethod(new MethodDescriptor("getILoggerFactory", ILoggerFactory.class));
Program program = new Program();
BasicBlock block = program.createBasicBlock();
Variable cacheVar = program.createVariable();
GetFieldInstruction get = new GetFieldInstruction();
get.setField(new FieldReference(LoggerFactory.class.getName(), "loggerFactoryCache"));
get.setFieldType(ValueType.object(ILoggerFactory.class.getName()));
get.setReceiver(cacheVar);
block.getInstructions().add(get);
ExitInstruction exit = new ExitInstruction();
exit.setValueToReturn(cacheVar);
block.getInstructions().add(exit);
method.setProgram(program);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2015 Alexey Andreev.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.extras.slf4j;

import org.teavm.vm.spi.TeaVMHost;
import org.teavm.vm.spi.TeaVMPlugin;

/**
*
* @author Alexey Andreev
*/
public class Slf4jPlugin implements TeaVMPlugin {
@Override
public void install(TeaVMHost host) {
host.add(new LoggerFactoryTransformer());
}
}
Loading

0 comments on commit bcf0929

Please sign in to comment.