forked from konsoletyper/teavm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f50895
commit bcf0929
Showing
9 changed files
with
560 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/target/ | ||
/.settings/ | ||
/.classpath | ||
/.project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
81 changes: 81 additions & 0 deletions
81
teavm-extras-slf4j/src/main/java/org/teavm/extras/slf4j/LoggerFactoryTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
teavm-extras-slf4j/src/main/java/org/teavm/extras/slf4j/Slf4jPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.