Skip to content

Commit

Permalink
validate package names
Browse files Browse the repository at this point in the history
  • Loading branch information
siordache committed Aug 20, 2018
1 parent 0791c04 commit b002674
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/main/groovy/org/beryx/jlink/impl/JlinkTaskImpl.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,8 @@ class JlinkTaskImpl {
File genDummyModuleInfo(File jarFile, File targetDir) {
def packages = new TreeSet<String>()
new ZipFile(jarFile).entries().each { entry ->
if(!entry.name.contains('META-INF') && entry.name.endsWith('.class')) {
int pos = entry.name.lastIndexOf('/')
if(pos > 0) {
packages << entry.name.substring(0, pos).replace('/', '.')
}
}
def pkgName = getPackage(entry.name)
if(pkgName) packages << pkgName
}
def moduleName = getModuleName(jarFile)
def modinfoDir = new File(targetDir, moduleName)
Expand All @@ -164,6 +160,25 @@ class JlinkTaskImpl {
modinfoDir
}

static String getPackage(String entryName) {
if(!entryName.endsWith('.class')) return null;
int pos = entryName.lastIndexOf('/')
if(pos <= 0) return null
def pkgName = entryName.substring(0, pos).replace('/', '.')
boolean valid = pkgName.split('\\.').every {isValidIdentifier(it)}
return valid ? pkgName : null
}

static boolean isValidIdentifier(String name) {
if (!name) return false
if (!Character.isJavaIdentifierStart(name.charAt(0))) return false
for (int i = 1; i < name.length(); i++) {
if (!Character.isJavaIdentifierPart(name.charAt(i))) return false
}
true
}


def copyRuntimeJars(DependencyManager depMgr) {
project.delete(jlinkJarsDirPath, nonModularJarsDirPath)
project.logger.info("Copying modular jars required by non-modular jars to ${jlinkJarsDirPath}...")
Expand Down
37 changes: 37 additions & 0 deletions src/test/groovy/org/beryx/jlink/JlinkTaskImplSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2018 the original author or authors.
*
* 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.beryx.jlink

import org.beryx.jlink.impl.JlinkTaskImpl
import spock.lang.Specification
import spock.lang.Unroll

class JlinkTaskImplSpec extends Specification {
@Unroll
def "should get package #pkgName from jar entry #entry"() {
expect:
JlinkTaskImpl.getPackage(entry) == pkgName

where:
entry | pkgName
'App.class' | null
'org/App.class' | 'org'
'org/example/App.class' | 'org.example'
'org/example-bad/App.class' | null
'org/example/info.txt' | null
'META-INF/org/example/App.class' | null
}
}

0 comments on commit b002674

Please sign in to comment.