Astra: a Java tool for analysing and refactoring Java source code

Overview

Astra logo

License Build Status

What is Astra?

Astra is a Java tool for analysing and refactoring Java source code.

For example:

  • "References to type A should instead reference type B"
  • "Callers of method A should add an additional argument B"
  • "Find classes which are annotated with A"

Astra has been developed and tested at Alfa to improve the velocity at which large scale refactors may be performed.

How do I use Astra?

  • First, please see the Wiki!
  • Astra can be run as part of a Java application, using astra-core as a dependency and using the refactors it provides. For an illustration of how to do this, please see the README in astra-core. The code can be found in astra-example.
  • For cases needing a more bespoke approach, astra-core also provides an SPI for writing your own custom ASTOperations. See the astra-core README for further details.
  • For very simple cases, there is also a command line interface which exposes a small subset of Astra's refactoring operations. Please see astra-cli for more information.

Why would I use Astra?

A simple and common use case is renaming a method, and updating all the callers of that method so that they use the new name. Your IDE is often the best tool for the job, but sometimes this isn't possible. There may be so many modules that manually selecting and opening them is a real pain, or the overall size of the modules may mean that your IDE struggles to open them all at once. This means that sometimes it's easier to just add a new method, deprecate the old one, and leave all the existing callers. The same issues apply to many other refactors, such as renaming a type.

Astra can be used to make changes like these easily, and on a massive scale.

How does Astra work?

Please see How does Astra work? in the Wiki.

Technologies

  • Java 8
  • Eclipse JDT
  • JUnit
  • log4j

License

Astra is released under the Apache 2.0 License. Contributions are also made under this license, as per the GitHub terms of service.

Comments
  • Add tests for MethodMatcher matching on return type

    Add tests for MethodMatcher matching on return type

    The MethodMatcher provides the option to match by return type. This isn't currently unit tested - coverage for this feature should be added to TestMethodMatcher.

    good first issue 
    opened by RadikalJin 8
  • Add test coverage for FindMethodInvocationsAnalysis and FindMethodDeclarationsAnalysis

    Add test coverage for FindMethodInvocationsAnalysis and FindMethodDeclarationsAnalysis

    Test coverage should be added for these analysis operations using the AbstractAnalysisTest framework. The existing TestAssignmentAnalysis can be used as a model.

    opened by RadikalJin 6
  • Add tests for AstraUtils#getPackageName(TypeDeclaration)

    Add tests for AstraUtils#getPackageName(TypeDeclaration)

    AstraUtils provides a utility for getting the package name for a type declaration. This isn't currently unit tested - coverage for this feature should be added to TestAstraUtils.

    good first issue 
    opened by RadikalJin 6
  • Reduce cognitive complexity of ChainedMethodInvocationRefactor#handleMethodInvocation and use only one break or continue statement

    Reduce cognitive complexity of ChainedMethodInvocationRefactor#handleMethodInvocation and use only one break or continue statement

    See https://sonarcloud.io/project/issues?resolved=false&types=CODE_SMELL&id=alfasoftware_astra&open=AYDiEpr_kKZeZDp5nkOu and https://sonarcloud.io/project/issues?resolved=false&types=CODE_SMELL&id=alfasoftware_astra&open=AYDiEpr_kKZeZDp5nkOt

    good first issue 
    opened by RadikalJin 4
  • Add test for adding marker annotation in TestAnnotationsRefactor

    Add test for adding marker annotation in TestAnnotationsRefactor

    TestAnnotationsRefactor already includes a test for adding a single member annotation. This should also include a test for adding a marker annotation. This should be similar to the existing testAddAnnotation, but without adding the member.

    good first issue 
    opened by RadikalJin 4
  • Fix/issue 104 reduce cognitive complexity

    Fix/issue 104 reduce cognitive complexity

    Fixes #104

    Change proposed in this PR: • have extracted processing of methodinvocation node to new method • refactored new method to return value or null instead of continue or break.

    opened by mohannaidu 3
  • Reduce cognitive complexity of AstraUtils#getFullyQualifiedName #88

    Reduce cognitive complexity of AstraUtils#getFullyQualifiedName #88

    What I have updated in the PR

    1. Refactored the code by creating a different method named as #mapOnDemandMatches.
    2. Updated the native for-loops with the lambda expression and stream API.
    3. Added the comment with the issue No for future reference.

    Mandatory Check I have done before creating PR

    1. No compilation issue
    2. No sonar issue in the code added by me
    3. Full maven build is successful
    4. No test cases failure

    Let me know if any changes required in the code.

    opened by kapilgahlot1998 3
  • Test failure due to variable name mismatch in InvocationChanedWrapped test case of MethodInvocationRefactor

    Test failure due to variable name mismatch in InvocationChanedWrapped test case of MethodInvocationRefactor

    Describe the bug In InvocationChainedWrappedExample, variable name for type of A is wrappedA. In the InvocationChangedWrappedExampleAfter, variable name is changed to wrappedB. MethodInvocationRefactor currently changes the method-type, but doesn't rename the variable. As a result, testInvocationChainedWrapped() test fails.

    The issue is related to TypeReferanceRefactor. In updateSimpleName(CompilationUnit compilationUnit, SimpleName name, ASTRewrite rewriter) method, in the else if condition we have a condition ((IVariableBinding) binding).getType().getQualifiedName().contains(getFromType()) which returns false if the variable name has Optional with it. So, Optional is needed to be removed from the qualifiedName().

    opened by monjurmorshed793 3
  • ChainedMethodInvocationRefactor should be able to handle chains of arbitrary length

    ChainedMethodInvocationRefactor should be able to handle chains of arbitrary length

    Describe the bug The ChainedMethodInvocationRefactorwill currently match on two chained method invocations, and swap them to a different method.

    For example, getCurrentFoo().doFooThing() can be swapped to doBarThing()

    Expected behavior This refactor should be updated so that:

    • Chains of arbitrary length can be matched
    • Those chains can themselves be replaced by other chains of arbitrary length, rather than just one method invocation.

    For example, getCurrentFoo() or getCurrentFoo().doFooThing().doOtherThing() should be valid matchable, and replaced with getCurrentBar().doBarThing() or any other chain of method invocations.

    Additional context See Javadoc on ChainedMethodInvocationRefactor and associated tests for further example usage.

    opened by RadikalJin 3
  • Cannot specify multiple items on the classpath when using CLI

    Cannot specify multiple items on the classpath when using CLI

    Describe the bug Cannot specify multiple items on the classpath when using CLI.

    To Reproduce Steps to reproduce the behavior:

    1. Follow the steps from the CLI README to use the CLI
    2. Attempt to specify more than one classpath parameter item

    Expected behavior Both classpath parameter items are accepted

    Actual behaviour Only one can be used.

    Additional context Attempted to use delimiters including semi-colon, comma and blanks, and wrapping with quotes, but none successful. It may be that the parameter in the refactor requires a property for the delimiter.

    good first issue 
    opened by RadikalJin 3
  • 90 Add coverage information with sonar

    90 Add coverage information with sonar

    Issue#90: Add coverage information with sonar

    Ciao! regarding this issue - apparently goal for code coverage report was missing in pom.xml. After adding the goal was able to capture the code coverage on sonarqube running locally. If it sounds useful then definitely check it out.

    :crossed_fingers: Hope this helps

    astra1

    astra2

    opened by ade3p 2
Releases(astra-2.1.2)
  • astra-2.1.2(Oct 31, 2022)

    What's Changed

    • 90 Add coverage information with sonar by @ade3p in https://github.com/alfasoftware/astra/pull/110
    • 62 add test coverage for MethodAnalysis by @ade3p in https://github.com/alfasoftware/astra/pull/111
    • 112 get fully qualified name utilities handle type declarations such … by @RadikalJin in https://github.com/alfasoftware/astra/pull/113

    New Contributors

    • @ade3p made their first contribution in https://github.com/alfasoftware/astra/pull/110

    Full Changelog: https://github.com/alfasoftware/astra/compare/astra-2.1.1...astra-2.1.2

    Source code(tar.gz)
    Source code(zip)
  • astra-2.1.1(Jul 15, 2022)

    [2.1.1] - 2022-07-15

    Changed

    • Addressing sonar issues following AnnotationChangeRefactor updates by @RadikalJin in https://github.com/alfasoftware/astra/pull/99
    • Adding .mvn to .gitignore by @RadikalJin in https://github.com/alfasoftware/astra/pull/100
    • 101 TypeMatcher matches on parameterized interface types by @RadikalJin in https://github.com/alfasoftware/astra/pull/102
    • Issue #36 resolved. by @monjurmorshed793 in https://github.com/alfasoftware/astra/pull/85
    • Fix/issue 104 reduce cognitive complexity by @mohannaidu in https://github.com/alfasoftware/astra/pull/105
    • Fix/issue 103 reduce cognitive complexity of AnnotationChangeRefactor#removeMembersFromAnnotation by @Dafodils in https://github.com/alfasoftware/astra/pull/106
    • #87 InlineInterfaceRefactor method refactoring by @Vichukano in https://github.com/alfasoftware/astra/pull/107
    • 108 Broaden applicability of AstraUtils functions by @RadikalJin in https://github.com/alfasoftware/astra/pull/109
    Source code(tar.gz)
    Source code(zip)
  • astra-2.1.0(May 19, 2022)

    What's Changed

    Added

    • Feature/96 annotation change member value by @owoller and @RadikalJin in https://github.com/alfasoftware/astra/pull/97

    Full Changelog: https://github.com/alfasoftware/astra/compare/astra-2.0.6...astra-2.1.0

    Source code(tar.gz)
    Source code(zip)
  • astra-2.0.6(May 10, 2022)

    What's Changed

    • Fixes Issue #86 : TypeMatcher: Replace type specification in construct… by @shyamrox in https://github.com/alfasoftware/astra/pull/91
    • (#89) Reduce cognitive complexity of AstraUtils#isStaticallyImportedMethod by @cmuagab in https://github.com/alfasoftware/astra/pull/92
    • Reduce cognitive complexity of AstraUtils#getFullyQualifiedName #88 by @kapilgahlot1998 in https://github.com/alfasoftware/astra/pull/93
    • 94 Make AddAnnotationRefactor's member optional by @RadikalJin in https://github.com/alfasoftware/astra/pull/95

    New Contributors

    • @shyamrox made their first contribution in https://github.com/alfasoftware/astra/pull/91
    • @cmuagab made their first contribution in https://github.com/alfasoftware/astra/pull/92
    • @kapilgahlot1998 made their first contribution in https://github.com/alfasoftware/astra/pull/93

    Full Changelog: https://github.com/alfasoftware/astra/compare/astra-2.0.4...astra-2.0.6

    Source code(tar.gz)
    Source code(zip)
  • astra-2.0.4(Mar 9, 2022)

    [2.0.4] - 2022-03-09

    Added

    • #83 Support text file "before" examples for testing

    Fixed

    • #81 Type reference refactor improvements to handling imports for annotations which are inner types
    Source code(tar.gz)
    Source code(zip)
  • astra-2.0.3(Feb 4, 2022)

    [2.0.3] - 2022-02-04

    Added

    • #59 Added tests for AstraUtils#getPackageName(TypeDeclaration)
    • #68 Tests added for AnnotationChangeRefactor
    • #72 Added test example for refactoring chains of method invocations
    • #76 Added MarkerAnnotation unit test

    Changed

    • #71 Removed type reference variable rename
    • #75 Changed description of AstraChangeType cli command for readability

    What's Changed

    • Tweak to static method type resolution attempting to find single appl… by @RadikalJin in https://github.com/alfasoftware/astra/pull/48
    • Allow multiple files in --cp by @Arraying in https://github.com/alfasoftware/astra/pull/49
    • Extra null safety when checking method names, and static on-demand im… by @RadikalJin in https://github.com/alfasoftware/astra/pull/51
    • Sonar tweaks following import handling improvements by @RadikalJin in https://github.com/alfasoftware/astra/pull/52
    • Standardising name of parameter types for methods, between when a Met… by @RadikalJin in https://github.com/alfasoftware/astra/pull/53
    • Add mention of versioning approach to CONTRIBUTING.md by @RadikalJin in https://github.com/alfasoftware/astra/pull/50
    • Method parameter type name standardisation by @RadikalJin in https://github.com/alfasoftware/astra/pull/54
    • Added unit test for return type matcher by @10wonga in https://github.com/alfasoftware/astra/pull/55
    • Don't remove import when changing a static method by name only, as we… by @owoller in https://github.com/alfasoftware/astra/pull/56
    • Compile with Java 11, and adding function to get name for anonymous class declaration by @RadikalJin in https://github.com/alfasoftware/astra/pull/58
    • Added compatibility for fully qualified names in Javadoc in TypeReferenceRefactor. by @10wonga in https://github.com/alfasoftware/astra/pull/57
    • Removing redundant code and excluding code fragments from SONAR warni… by @RadikalJin in https://github.com/alfasoftware/astra/pull/63
    • Adding support for dealing with static imports of methods named solel… by @RadikalJin in https://github.com/alfasoftware/astra/pull/64
    • Incrementing version of javadoc plugin with fix affecting later versions (9+) of java by @RadikalJin in https://github.com/alfasoftware/astra/pull/65
    • Getting simple names for types now aligned with the JLS spec by @RadikalJin in https://github.com/alfasoftware/astra/pull/66
    • When finding method invocations, method candidates that are not found are now also included in the list of results by @RadikalJin in https://github.com/alfasoftware/astra/pull/67
    • Tests added for AnnotationChangeRefactor by @monjurmorshed793 in https://github.com/alfasoftware/astra/pull/68
    • Removing type reference variable rename feature by @RadikalJin in https://github.com/alfasoftware/astra/pull/71
    • Adding test example for refactoring chains of method invocations by @RadikalJin in https://github.com/alfasoftware/astra/pull/72
    • Small tweaks following sonar review by @RadikalJin in https://github.com/alfasoftware/astra/pull/73
    • Minor whitespace tweaks by @RadikalJin in https://github.com/alfasoftware/astra/pull/74
    • changed description of command for readability by @kellyxavier in https://github.com/alfasoftware/astra/pull/75
    • Added Marker Annotation Unittest by @Hunter-1 in https://github.com/alfasoftware/astra/pull/76
    • Add test astra utils get package name by @RadikalJin in https://github.com/alfasoftware/astra/pull/78
    • Add test for AstraUtils#getPackageName(TypeDeclaration) by @oussamabadr in https://github.com/alfasoftware/astra/pull/77
    • #79 The TypeMatcher now matches with multilevel inheritance by @RadikalJin in https://github.com/alfasoftware/astra/pull/80

    New Contributors

    • @Arraying made their first contribution in https://github.com/alfasoftware/astra/pull/49
    • @10wonga made their first contribution in https://github.com/alfasoftware/astra/pull/55
    • @monjurmorshed793 made their first contribution in https://github.com/alfasoftware/astra/pull/68
    • @kellyxavier made their first contribution in https://github.com/alfasoftware/astra/pull/75
    • @Hunter-1 made their first contribution in https://github.com/alfasoftware/astra/pull/76
    • @oussamabadr made their first contribution in https://github.com/alfasoftware/astra/pull/77

    Full Changelog: https://github.com/alfasoftware/astra/compare/astra-2.0.1...astra-2.0.3

    Source code(tar.gz)
    Source code(zip)
  • astra-2.0.1(Aug 4, 2021)

    [2.0.1] - 2021-08-04

    Added

    Changed

    • #63 Removing redundant code and excluding code fragments from SONAR warnings about commented out code
    • #64 Adding support for dealing with static imports of methods named solely $, as with JUnit's JunitParamsRunner.$
    • #66 Getting simple names for types now aligned with the JLS spec
    • #67 When finding method invocations, method candidates that are not found are now also included in the list of results

    Fixed

    • #57 Added compatibility for fully qualified names in Javadoc in TypeReferenceRefactor (fixes #3)
    • #65 Incrementing version of javadoc plugin with fix affecting later versions (9+) of java
    Source code(tar.gz)
    Source code(zip)
  • astra-2.0.0(Jul 12, 2021)

    [2.0.0] - 2021-07-12

    Added

    • #55 Added unit test for matching methods by return type with MethodMatcher
    • #58 Added function to get name for anonymous class declaration

    Changed

    • #58 Compile with Java 11
    • #56 Don't remove import when changing a static method by name only, as we may not be changing all invocations of this method. Instead, leave the import and let the UnusedImportRefactor clean it up if necessary.

    Fixed

    Source code(tar.gz)
    Source code(zip)
  • astra-1.4.0(Jun 30, 2021)

    [1.4.0] - 2021-06-30

    Added

    Changed

    • #54 Cached sources and classpath so that they are only retrieved once, and shared between validation and use

    Fixed

    • #54 Method parameter type name standardisation
    Source code(tar.gz)
    Source code(zip)
  • astra-1.3.9(Jun 30, 2021)

    [1.3.9] - 2021-06-30

    Added

    Changed

    • #53 Standardising name of parameter types for methods, between when a MethodMatcher is constructed and evaluated

    Fixed

    Source code(tar.gz)
    Source code(zip)
  • astra-1.3.8(Jun 29, 2021)

    [1.3.8] - 2021-06-29

    Added

    Changed

    • #51 Extra null safety when checking method names, and static on-demand imports are checked when finding declaring methods

    Fixed

    • #49 Allow multiple files in --cp
    Source code(tar.gz)
    Source code(zip)
  • astra-1.3.7(Jun 23, 2021)

    [1.3.7] - 2021-06-23

    Added

    Changed

    • Tweak to static method type resolution attempting to find single applicable type name from imports, if possible, before falling back to resolving

    Fixed

    Source code(tar.gz)
    Source code(zip)
  • 1.3.6(Jun 16, 2021)

    [1.3.6] - 2021-06-16

    Added

    Changed

    Fixed

    • Fixes for static imports, particularly better detection of when imports are really unused.
    • MethodMatcher now works better when matching parameters that are arrays and type variables.
    Source code(tar.gz)
    Source code(zip)
  • astra-1.3.5(Jun 10, 2021)

  • 1.3.3(Jun 9, 2021)

    [1.3.3] - 2021-06-09

    Added

    Changed

    Fixed

    • Fixed case where adding a static import would remove an existing static import of the same name, even if this was valid.
    Source code(tar.gz)
    Source code(zip)
  • 1.3.1(Jun 4, 2021)

  • 1.2.2(May 24, 2021)

  • 1.2.1(May 19, 2021)

    #22, #25 Type matcher now matches on parameterized supertypes #27 Method matcher now allows custom predicates to be used for matching any AST node types handled by MethodMatcher

    Issues fixed: #16 Fix unused imports refactor so it doesn't lose inner types. #20 Log the total number of files after filtering with the predicate

    Source code(tar.gz)
    Source code(zip)
Owner
Alfa
Alfa is the premier supplier of software and consultancy services to the asset finance industry.
Alfa
Inria 1.4k Dec 29, 2022
OpenGrok is a fast and usable source code search and cross reference engine, written in Java

Copyright (c) 2006, 2020 Oracle and/or its affiliates. All rights reserved. OpenGrok - a wicked fast source browser OpenGrok - a wicked fast source br

Oracle 3.8k Jan 8, 2023
A tool to help eliminate NullPointerExceptions (NPEs) in your Java code with low build-time overhead

NullAway: Fast Annotation-Based Null Checking for Java NullAway is a tool to help eliminate NullPointerExceptions (NPEs) in your Java code. To use Nul

Uber Open Source 3.2k Dec 29, 2022
SpotBugs is FindBugs' successor. A tool for static analysis to look for bugs in Java code.

SpotBugs is the spiritual successor of FindBugs, carrying on from the point where it left off with support of its community. SpotBugs is licensed unde

null 2.9k Jan 4, 2023
Sourcetrail - free and open-source interactive source explorer

Sourcetrail Sourcetrail is a free and open-source cross-platform source explorer that helps you get productive on unfamiliar source code. Windows: Lin

Coati Software 13.2k Jan 5, 2023
Reformats Java source code to comply with Google Java Style.

google-java-format google-java-format is a program that reformats Java source code to comply with Google Java Style. Using the formatter from the comm

Google 4.8k Dec 31, 2022
:coffee: SonarSource Static Analyzer for Java Code Quality and Security

Code Quality and Security for Java This SonarSource project is a code analyzer for Java projects. Information about the analysis of Java features is a

SonarSource 976 Jan 5, 2023
Java library for parsing report files from static code analysis.

Violations Lib This is a Java library for parsing report files like static code analysis. Example of supported reports are available here. A number of

Tomas Bjerre 127 Nov 23, 2022
An extensible multilanguage static code analyzer.

PMD About PMD is a source code analyzer. It finds common programming flaws like unused variables, empty catch blocks, unnecessary object creation, and

PMD 4.1k Jan 2, 2023
A static analyzer for Java, C, C++, and Objective-C

Infer Infer is a static analysis tool for Java, C++, Objective-C, and C. Infer is written in OCaml. Installation Read our Getting Started page for det

Facebook 13.7k Dec 28, 2022
Catch common Java mistakes as compile-time errors

Error Prone Error Prone is a static analysis tool for Java that catches common programming mistakes at compile-time. public class ShortSet { public

Google 6.3k Dec 31, 2022
A free injection hacked client for Minecraft using Java-agents

Swift Swift is a free and open-source injection hacked client base for Minecraft using Java-agents. Issues If you notice any bugs, you can let us know

static final 36 Oct 8, 2022
Java bytecode static analyzer

This project is abandoned and unlikely will be supported in future HuntBugs 0.0.11 New Java bytecode static analyzer tool based on Procyon Compiler To

Tagir Valeev 302 Aug 13, 2022
SBSRE is an eclipse plugin for extract method refactoring based on the single responsibility principle(SRP)

SBSRE is a slice-based single responsibility extraction approach supported by an eclipse plugin for identifying Single responsibility violations in the methods.

null 4 Jul 8, 2022
mobsfscan is a static analysis tool that can find insecure code patterns in your Android and iOS source code.

mobsfscan is a static analysis tool that can find insecure code patterns in your Android and iOS source code. Supports Java, Kotlin, Swift, and Objective C Code. mobsfscan uses MobSF static analysis rules and is powered by semgrep and libsast pattern matcher.

Mobile Security Framework 347 Dec 29, 2022
Source code of APK-Explorer-Editor (AEE), an open-source tool to explore the contents of an installed APK!

APK Explorer & Editor (AEE) APK Explorer & Editor, an open-source tool to explore the contents of an installed APK, is strictly made with an aim to in

APK Explorer & Editor 271 Jan 8, 2023
Inria 1.4k Dec 29, 2022