All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openrewrite.java.template.internal.ClasspathJarNameDetector Maven / Gradle / Ivy

There is a newer version: 1.17.1
Show newest version
/*
 * Copyright 2022 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 *

* https://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.openrewrite.java.template.internal; import com.sun.tools.javac.code.Symbol; import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree.JCFieldAccess; import com.sun.tools.javac.tree.TreeScanner; import javax.tools.JavaFileObject; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ClasspathJarNameDetector { /** * Locate types that are directly referred to by name in the * given tree and therefore need an import in the template. * * @return The list of imports to add. */ public static Set classpathFor(JCTree input, List imports) { Set jarNames = new LinkedHashSet() { @Override public boolean add(String s) { return s != null && super.add(s); } }; for (Symbol anImport : imports) { jarNames.add(jarNameFor(anImport)); } // Detect fully qualified classes new TreeScanner() { @Override public void scan(JCTree tree) { if (tree instanceof JCFieldAccess && ((JCFieldAccess) tree).sym instanceof Symbol.ClassSymbol && Character.isUpperCase(((JCFieldAccess) tree).getIdentifier().toString().charAt(0))) { jarNames.add(jarNameFor(((JCFieldAccess) tree).sym)); } super.scan(tree); } }.scan(input); return jarNames; } private static String jarNameFor(Symbol anImport) { Symbol.ClassSymbol enclClass = anImport instanceof Symbol.ClassSymbol ? (Symbol.ClassSymbol) anImport : anImport.enclClass(); while (enclClass.enclClass() != null && enclClass.enclClass() != enclClass) { enclClass = enclClass.enclClass(); } JavaFileObject classfile = enclClass.classfile; if (classfile != null) { String uriStr = classfile.toUri().toString(); Matcher matcher = Pattern.compile("([^/]*)?\\.jar!/").matcher(uriStr); if (matcher.find()) { String jarName = matcher.group(1); return jarName.replaceAll("-\\d.*$", ""); } } return null; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy