Search in sources :

Example 1 with ProjectDependency

use of meghanada.project.ProjectDependency in project meghanada-server by mopemope.

the class AndroidSupport method parseAndroidProject.

void parseAndroidProject(AndroidProject androidProject) throws IOException {
    JavaCompileOptions javaCompileOptions = androidProject.getJavaCompileOptions();
    this.project.setCompileSource(javaCompileOptions.getSourceCompatibility());
    this.project.setCompileTarget(javaCompileOptions.getTargetCompatibility());
    ProductFlavorContainer defaultConfig = androidProject.getDefaultConfig();
    if (isNull(this.project.getOutput())) {
        File buildDir = androidProject.getBuildFolder().getCanonicalFile();
        String build = Joiner.on(File.separator).join(buildDir, INTERMEDIATE_DIR, CLASSES_DIR, DEBUG_DIR);
        this.project.setOutput(project.normalize(build));
    }
    if (isNull(this.project.getTestOutput())) {
        File buildDir = androidProject.getBuildFolder().getCanonicalFile();
        String build = Joiner.on(File.separator).join(buildDir, INTERMEDIATE_DIR, CLASSES_DIR, TEST_DIR, DEBUG_DIR);
        this.project.setTestOutput(this.project.normalize(build));
    }
    Map<String, Set<File>> androidSources = this.getAndroidSources(defaultConfig);
    Set<ProjectDependency> dependencies = this.getAndroidDependencies(androidProject);
    this.project.getSources().addAll(androidSources.getOrDefault(SOURCES_KEY, new HashSet<>()));
    this.project.getResources().addAll(androidSources.getOrDefault(RESOURCES_KEY, new HashSet<>()));
    this.project.getTestSources().addAll(androidSources.getOrDefault(TEST_SOURCES_KEY, new HashSet<>()));
    this.project.getTestResources().addAll(androidSources.getOrDefault(AndroidSupport.TEST_RESOURCES_KEY, new HashSet<>()));
    this.project.getDependencies().addAll(dependencies);
    // merge other project
    if (this.project.getSources().isEmpty()) {
        File file = new File(Joiner.on(File.separator).join(SRC_DIR, MAIN_DIR, JAVA_DIR)).getCanonicalFile();
        this.project.getSources().add(file);
    }
    if (this.project.getTestSources().isEmpty()) {
        File file = new File(Joiner.on(File.separator).join(SRC_DIR, TEST_DIR, JAVA_DIR)).getCanonicalFile();
        this.project.getTestSources().add(file);
    }
    if (isNull(this.project.getOutput())) {
        String buildDir = new File(this.project.getProjectRoot(), BUILD_DIR).getCanonicalPath();
        String build = Joiner.on(File.separator).join(buildDir, INTERMEDIATE_DIR, CLASSES_DIR, DEBUG_DIR);
        this.project.setOutput(this.project.normalize(build));
    }
    if (isNull(this.project.getTestOutput())) {
        String buildDir = new File(this.project.getProjectRoot(), BUILD_DIR).getCanonicalPath();
        String build = Joiner.on(File.separator).join(buildDir, INTERMEDIATE_DIR, CLASSES_DIR, TEST_DIR, DEBUG_DIR);
        this.project.setTestOutput(this.project.normalize(build));
    }
    // load exists aar
    String aar = Joiner.on(File.separator).join(this.project.getProjectRoot(), BUILD_DIR, INTERMEDIATE_DIR, EXPLODED_DIR);
    FileUtils.collectFiles(new File(aar), EXT_JAR).forEach(wrapIOConsumer(this::addAAR));
    log.debug("sources {}", this.project.getSources());
    log.debug("resources {}", this.project.getResources());
    log.debug("output {}", this.project.getOutput());
    log.debug("test sources {}", this.project.getTestSources());
    log.debug("test resources {}", this.project.getTestResources());
    log.debug("test output {}", this.project.getTestOutput());
    for (ProjectDependency projectDependency : this.project.getDependencies()) {
        log.debug("dependency {}", projectDependency);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) JavaCompileOptions(com.android.builder.model.JavaCompileOptions) ProductFlavorContainer(com.android.builder.model.ProductFlavorContainer) ProjectDependency(meghanada.project.ProjectDependency) File(java.io.File) HashSet(java.util.HashSet)

Example 2 with ProjectDependency

use of meghanada.project.ProjectDependency in project meghanada-server by mopemope.

the class AndroidSupport method getAndroidDependencies.

private Set<ProjectDependency> getAndroidDependencies(AndroidProject androidProject) {
    Set<ProjectDependency> dependencies = new HashSet<>(16);
    Collection<String> bootClasspath = androidProject.getBootClasspath();
    for (String cp : bootClasspath) {
        File file = new File(cp);
        addDependencies(dependencies, file);
    }
    Collection<Variant> variants = androidProject.getVariants();
    for (Variant variant : variants) {
        String buildType = variant.getBuildType();
        boolean debugBuild = buildType.equals(DEBUG_BUILD);
        AndroidArtifact mainArtifact = variant.getMainArtifact();
        if (debugBuild) {
            Collection<File> generatedSourceFolders = mainArtifact.getGeneratedSourceFolders();
            for (File src : generatedSourceFolders) {
                this.project.getSources().add(src);
            }
            Collection<File> generatedResourceFolders = mainArtifact.getGeneratedResourceFolders();
            for (File src : generatedResourceFolders) {
                this.project.getResources().add(src);
            }
        }
        Dependencies compileDependencies = mainArtifact.getDependencies();
        Collection<AndroidLibrary> libraries = compileDependencies.getLibraries();
        for (AndroidLibrary androidLibrary : libraries) {
            String project = androidLibrary.getProject();
            if (nonNull(project) && !androidLibrary.isProvided()) {
                if (project.startsWith(":")) {
                    project = project.substring(1);
                }
                if (this.project.allModules.containsKey(project)) {
                    File root = this.project.allModules.get(project);
                    final ProjectDependency projectDependency = new ProjectDependency(project, "COMPILE", "1.0.0", root, ProjectDependency.Type.PROJECT);
                    dependencies.add(projectDependency);
                }
            }
            Collection<File> localJars = androidLibrary.getLocalJars();
            for (File jar : localJars) {
                addDependencies(dependencies, jar);
            }
        }
        Collection<JavaLibrary> javaLibraries = compileDependencies.getJavaLibraries();
        for (JavaLibrary javaLibrary : javaLibraries) {
            File file = javaLibrary.getJarFile();
            addDependencies(dependencies, file);
        }
        parseExtraAndroidArtifacts(dependencies, variant);
        parseExtraJavaArtifacts(dependencies, variant);
    }
    return dependencies;
}
Also used : ProjectDependency(meghanada.project.ProjectDependency) AndroidArtifact(com.android.builder.model.AndroidArtifact) Variant(com.android.builder.model.Variant) JavaLibrary(com.android.builder.model.JavaLibrary) AndroidLibrary(com.android.builder.model.AndroidLibrary) Dependencies(com.android.builder.model.Dependencies) File(java.io.File) HashSet(java.util.HashSet)

Example 3 with ProjectDependency

use of meghanada.project.ProjectDependency in project meghanada-server by mopemope.

the class GradleProject method parseIdeaModule.

private void parseIdeaModule(final IdeaModule ideaModule) throws IOException {
    final org.gradle.tooling.model.GradleProject gradleProject = ideaModule.getGradleProject();
    String name = convertName(gradleProject.getPath());
    if (nonNull(name) && !name.isEmpty()) {
        this.name = name;
    }
    final AndroidProject androidProject = AndroidSupport.getAndroidProject(this.rootProject, gradleProject);
    if (nonNull(androidProject)) {
        Set<ProjectDependency> projectDependencies = analyzeDependencies(ideaModule);
        this.dependencies.addAll(projectDependencies);
        // parse android
        this.isAndroidProject = true;
        this.androidApiVersion = androidProject.getApiVersion();
        this.androidModelVersion = androidProject.getModelVersion();
        final AndroidSupport androidSupport = new AndroidSupport(this);
        androidSupport.parseAndroidProject(androidProject);
    } else {
        // normal
        this.parseIdeaModule(gradleProject, ideaModule);
    }
}
Also used : AndroidProject(com.android.builder.model.AndroidProject) ProjectDependency(meghanada.project.ProjectDependency)

Example 4 with ProjectDependency

use of meghanada.project.ProjectDependency in project meghanada-server by mopemope.

the class GradleProject method parseIdeaModule.

private void parseIdeaModule(final org.gradle.tooling.model.GradleProject gradleProject, final IdeaModule ideaModule) throws IOException {
    if (isNull(this.output)) {
        final String buildDir = gradleProject.getBuildDirectory().getCanonicalPath();
        String build = Joiner.on(File.separator).join(buildDir, "classes", "main");
        if (nonNull(gradleVersion) && gradleVersion.compareTo(new ComparableVersion("4.0")) >= 0) {
            build = Joiner.on(File.separator).join(buildDir, "classes", "java", "main");
        }
        this.output = this.normalize(build);
    }
    if (isNull(this.testOutput)) {
        final String buildDir = gradleProject.getBuildDirectory().getCanonicalPath();
        String build = Joiner.on(File.separator).join(buildDir, "classes", "test");
        if (nonNull(gradleVersion) && gradleVersion.compareTo(new ComparableVersion("4.0")) >= 0) {
            build = Joiner.on(File.separator).join(buildDir, "classes", "java", "test");
        }
        this.testOutput = this.normalize(build);
    }
    final Set<ProjectDependency> dependencies = this.analyzeDependencies(ideaModule);
    final Map<String, Set<File>> sources = this.searchProjectSources(ideaModule);
    this.sources.addAll(sources.get("sources"));
    this.resources.addAll(sources.get("resources"));
    this.testSources.addAll(sources.get("testSources"));
    this.testResources.addAll(sources.get("testResources"));
    this.dependencies.addAll(dependencies);
    // merge other project
    if (this.sources.isEmpty()) {
        final File file = new File(Joiner.on(File.separator).join("src", "main", "java")).getCanonicalFile();
        this.sources.add(file);
    }
    if (this.testSources.isEmpty()) {
        final File file = new File(Joiner.on(File.separator).join("src", "test", "java")).getCanonicalFile();
        this.testSources.add(file);
    }
    if (isNull(this.output)) {
        final String buildDir = new File(this.getProjectRoot(), "build").getCanonicalPath();
        String build = Joiner.on(File.separator).join(buildDir, "classes", "main");
        if (nonNull(gradleVersion) && gradleVersion.compareTo(new ComparableVersion("4.0")) >= 0) {
            build = Joiner.on(File.separator).join(buildDir, "classes", "java", "main");
        }
        this.output = this.normalize(build);
    }
    if (isNull(this.testOutput)) {
        final String buildDir = new File(this.getProjectRoot(), "build").getCanonicalPath();
        String build = Joiner.on(File.separator).join(buildDir, "classes", "test");
        if (nonNull(gradleVersion) && gradleVersion.compareTo(new ComparableVersion("4.0")) >= 0) {
            build = Joiner.on(File.separator).join(buildDir, "classes", "java", "test");
        }
        this.testOutput = this.normalize(build);
    }
    log.debug("sources {}", this.sources);
    log.debug("resources {}", this.resources);
    log.debug("output {}", this.output);
    log.debug("test sources {}", this.testSources);
    log.debug("test resources {}", this.testResources);
    log.debug("test output {}", this.testOutput);
    for (final ProjectDependency projectDependency : this.getDependencies()) {
        log.debug("{} {}", projectDependency.getScope(), projectDependency.getId());
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) DomainObjectSet(org.gradle.tooling.model.DomainObjectSet) ComparableVersion(org.apache.maven.artifact.versioning.ComparableVersion) ProjectDependency(meghanada.project.ProjectDependency) File(java.io.File)

Example 5 with ProjectDependency

use of meghanada.project.ProjectDependency in project meghanada-server by mopemope.

the class LocationSearcher method searchFromDependency.

private Location searchFromDependency(final SearchContext context) throws IOException {
    final String searchFQCN = context.searchFQCN;
    final CachedASMReflector reflector = CachedASMReflector.getInstance();
    final File classFile = reflector.getClassFile(searchFQCN);
    final String tempDir = System.getProperty("java.io.tmpdir");
    if (classFile != null && classFile.exists() && classFile.getName().endsWith(FileUtils.JAR_EXT)) {
        final String androidHome = System.getenv("ANDROID_HOME");
        if (androidHome != null) {
            final Optional<ProjectDependency> dependencyOptional = this.project.getDependencies().stream().filter(dependency -> dependency.getFile().equals(classFile)).findFirst();
            if (dependencyOptional.isPresent()) {
                final ProjectDependency dependency = dependencyOptional.get();
                final String sourceJar = ClassNameUtils.getSimpleName(dependency.getId()) + '-' + dependency.getVersion() + "-sources.jar";
                final File root = new File(androidHome, "extras");
                if (root.exists()) {
                    return getLocationFromSrcOrDecompile(context, classFile, root, sourceJar);
                }
            }
        }
        final File depParent = classFile.getParentFile();
        final File dependencyDir = depParent.getParentFile();
        final String srcJarName = ClassNameUtils.replace(classFile.getName(), FileUtils.JAR_EXT, "-sources.jar");
        final String disable = System.getProperty("disable-source-jar");
        if (disable != null && disable.equals("true")) {
            return searchLocationFromDecompileFile(context, searchFQCN, classFile, tempDir);
        }
        return getLocationFromSrcOrDecompile(context, classFile, dependencyDir, srcJarName);
    }
    return null;
}
Also used : DecompilationListener(org.jboss.windup.decompiler.api.DecompilationListener) FunctionUtils.wrapIOConsumer(meghanada.utils.FunctionUtils.wrapIOConsumer) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Map(java.util.Map) ZipFile(java.util.zip.ZipFile) MethodCall(meghanada.analyze.MethodCall) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) FunctionUtils.wrapIO(meghanada.utils.FunctionUtils.wrapIO) GlobalCache(meghanada.cache.GlobalCache) SimpleName(com.github.javaparser.ast.expr.SimpleName) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) StandardOpenOption(java.nio.file.StandardOpenOption) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) MethodScope(meghanada.analyze.MethodScope) ClassNameUtils(meghanada.utils.ClassNameUtils) Optional(java.util.Optional) Project(meghanada.project.Project) Pattern(java.util.regex.Pattern) ProjectDependency(meghanada.project.ProjectDependency) Config(meghanada.config.Config) Parameter(com.github.javaparser.ast.body.Parameter) Position(com.github.javaparser.Position) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Variable(meghanada.analyze.Variable) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ClassScope(meghanada.analyze.ClassScope) OutputStream(java.io.OutputStream) Filter(org.jboss.windup.decompiler.util.Filter) DecompilationResult(org.jboss.windup.decompiler.api.DecompilationResult) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) FileOutputStream(java.io.FileOutputStream) FileUtils.existsFQCN(meghanada.utils.FileUtils.existsFQCN) IOException(java.io.IOException) CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) FileUtils(meghanada.utils.FileUtils) EntryMessage(org.apache.logging.log4j.message.EntryMessage) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Paths(java.nio.file.Paths) Source(meghanada.analyze.Source) FernflowerDecompiler(org.jboss.windup.decompiler.fernflower.FernflowerDecompiler) TypeScope(meghanada.analyze.TypeScope) LogManager(org.apache.logging.log4j.LogManager) JavaParser(com.github.javaparser.JavaParser) InputStream(java.io.InputStream) CachedASMReflector(meghanada.reflect.asm.CachedASMReflector) ProjectDependency(meghanada.project.ProjectDependency) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Aggregations

ProjectDependency (meghanada.project.ProjectDependency)9 File (java.io.File)6 HashSet (java.util.HashSet)4 IOException (java.io.IOException)3 Set (java.util.Set)3 AndroidProject (com.android.builder.model.AndroidProject)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 ProjectParseException (meghanada.project.ProjectParseException)2 AndroidArtifact (com.android.builder.model.AndroidArtifact)1 AndroidLibrary (com.android.builder.model.AndroidLibrary)1 Dependencies (com.android.builder.model.Dependencies)1 JavaCompileOptions (com.android.builder.model.JavaCompileOptions)1 JavaLibrary (com.android.builder.model.JavaLibrary)1 ProductFlavorContainer (com.android.builder.model.ProductFlavorContainer)1 Variant (com.android.builder.model.Variant)1 JavaParser (com.github.javaparser.JavaParser)1