Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2020 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.tree;
import com.fasterxml.jackson.annotation.JsonCreator;
import lombok.*;
import lombok.experimental.FieldDefaults;
import lombok.experimental.NonFinal;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.LoathingOfOthers;
import org.openrewrite.internal.SelfLoathing;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.java.JavaPrinter;
import org.openrewrite.java.JavaTypeVisitor;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.JavadocVisitor;
import org.openrewrite.java.internal.TypesInUse;
import org.openrewrite.java.search.FindTypes;
import org.openrewrite.marker.Markers;
import java.beans.Transient;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
@SuppressWarnings("unused")
public interface J extends Tree {
@SuppressWarnings("unchecked")
@Override
default R accept(TreeVisitor v, P p) {
return (R) acceptJava(v.adapt(JavaVisitor.class), p);
}
@Override
default
v, P p) {
return v.defaultValue(this, p);
}
J2 withPrefix(Space space);
Space getPrefix();
default List getComments() {
return getPrefix().getComments();
}
default J2 withComments(List comments) {
return withPrefix(getPrefix().withComments(comments));
}
/**
* @return This tree, printed.
* @deprecated This method doesn't print in a way that is
* specialized for each language extension of the base Java model. Use {@link #print(Cursor)} instead.
*/
@Deprecated
default String print() {
PrintOutputCapture outputCapture = new PrintOutputCapture<>(0);
new JavaPrinter().visit(this, outputCapture);
return outputCapture.getOut();
}
/**
* @return This tree, printed.
* @deprecated This method doesn't print in a way that is
* specialized for each language extension of the base Java model. Use {@link #print(Cursor)} instead.
*/
@Deprecated
default String printTrimmed() {
return StringUtils.trimIndent(print());
}
@SuppressWarnings("unchecked")
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
@Data
final class AnnotatedType implements J, Expression, TypeTree {
@With
@EqualsAndHashCode.Include
UUID id;
@With
Space prefix;
@With
Markers markers;
@With
List annotations;
@With
TypeTree typeExpression;
@Override
public JavaType getType() {
return typeExpression.getType();
}
@Override
public AnnotatedType withType(@Nullable JavaType type) {
return withTypeExpression(typeExpression.withType(type));
}
/**
* @deprecated Use {@link org.openrewrite.java.service.AnnotationService#getAllAnnotations(J)} instead.
*/
@Deprecated
public List getAllAnnotations() {
List allAnnotations = annotations;
List moreAnnotations;
if (typeExpression instanceof FieldAccess &&
!(moreAnnotations = ((FieldAccess) typeExpression).getName().getAnnotations()).isEmpty()) {
if (allAnnotations.isEmpty()) {
allAnnotations = moreAnnotations;
} else {
allAnnotations = new ArrayList<>(annotations);
allAnnotations.addAll(moreAnnotations);
}
}
return allAnnotations;
}
@Override
public
J acceptJava(JavaVisitor
v, P p) {
return v.visitAnnotatedType(this, p);
}
@Override
public String toString() {
return withPrefix(Space.EMPTY).printTrimmed(new JavaPrinter<>());
}
@Override
@Transient
public CoordinateBuilder.Expression getCoordinates() {
return new CoordinateBuilder.Expression(this);
}
}
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
@RequiredArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
final class Annotation implements J, Expression {
@Nullable
@NonFinal
transient WeakReference padding;
@With
@EqualsAndHashCode.Include
@Getter
UUID id;
@With
@Getter
Space prefix;
@With
@Getter
Markers markers;
@With
@Getter
NameTree annotationType;
public String getSimpleName() {
if (annotationType instanceof Identifier) {
return ((Identifier) annotationType).getSimpleName();
} else if (annotationType instanceof J.FieldAccess) {
return ((J.FieldAccess) annotationType).getSimpleName();
} else {
// allow for extending languages like Kotlin to supply a different representation
return annotationType.printTrimmed();
}
}
@Nullable
JContainer arguments;
public @Nullable List getArguments() {
return arguments == null ? null : arguments.getElements();
}
public Annotation withArguments(@Nullable List arguments) {
return getPadding().withArguments(JContainer.withElementsNullable(this.arguments, arguments));
}
@Override
public JavaType getType() {
return annotationType.getType();
}
@SuppressWarnings("unchecked")
@Override
public Annotation withType(@Nullable JavaType type) {
return withAnnotationType(annotationType.withType(type));
}
@Override
public
J acceptJava(JavaVisitor
v, P p) {
return v.visitAnnotation(this, p);
}
@Override
@Transient
public CoordinateBuilder.Annotation getCoordinates() {
return new CoordinateBuilder.Annotation(this);
}
public Padding getPadding() {
Padding p;
if (this.padding == null) {
p = new Padding(this);
this.padding = new WeakReference<>(p);
} else {
p = this.padding.get();
if (p == null || p.t != this) {
p = new Padding(this);
this.padding = new WeakReference<>(p);
}
}
return p;
}
@Override
public String toString() {
return withPrefix(Space.EMPTY).printTrimmed(new JavaPrinter<>());
}
@RequiredArgsConstructor
public static class Padding {
private final Annotation t;
public @Nullable JContainer getArguments() {
return t.arguments;
}
public Annotation withArguments(@Nullable JContainer arguments) {
return t.arguments == arguments ? t : new Annotation(t.id, t.prefix, t.markers, t.annotationType, arguments);
}
}
}
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
@Data
final class ArrayAccess implements J, Expression, TypedTree {
@With
@EqualsAndHashCode.Include
UUID id;
@With
Space prefix;
@With
Markers markers;
@With
Expression indexed;
@With
ArrayDimension dimension;
@With
@Nullable
JavaType type;
@Override
public
J acceptJava(JavaVisitor
v, P p) {
return v.visitArrayAccess(this, p);
}
@Override
@Transient
public CoordinateBuilder.Expression getCoordinates() {
return new CoordinateBuilder.Expression(this);
}
@Override
public String toString() {
return withPrefix(Space.EMPTY).printTrimmed(new JavaPrinter<>());
}
}
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
@RequiredArgsConstructor
@With
@Data
final class ArrayType implements J, TypeTree, Expression {
@EqualsAndHashCode.Include
UUID id;
Space prefix;
Markers markers;
TypeTree elementType;
@Nullable
List annotations;
@Nullable // nullable for backwards compatibility only
JLeftPadded dimension;
JavaType type;
/**
* For backwards compatibility with older LSTs.
* Do not remove until we're confident older LSTs are no longer in use.
*/
@Deprecated
@JsonCreator
static ArrayType create(
UUID id,
Space prefix,
Markers markers,
TypeTree elementType,
@Nullable List> dimensions, // Do not remove or rename, required for backwards compatibility.
@Nullable List annotations,
@Nullable JLeftPadded dimension,
@Nullable JavaType type) {
if (dimensions != null) {
// To create a consistent JavaType$Array from old Groovy and Java LSTs, we need to map the element type.
// The JavaType from GroovyTypeMapping was a JavaType$Array, while the JavaType from JavaTypeMapping was a JavaType$Class.
JavaType updated = elementType.getType();
while (updated instanceof JavaType.Array) {
updated = ((JavaType.Array) updated).getElemType();
}
elementType = elementType.withType(updated);
if (dimensions.isEmpty()) {
// varargs in Javadoc
type = new JavaType.Array(null, elementType.getType(), null);
} else {
int dimensionCount = dimensions.size();
elementType = mapOldFormat(elementType, dimensions.subList(0, dimensionCount - 1));
type = new JavaType.Array(null, elementType.getType(), null);
dimension = JLeftPadded.build(dimensions.get(dimensionCount - 1).getAfter()).withBefore(dimensions.get(dimensionCount - 1).getElement());
}
}
return new ArrayType(id, prefix, markers, elementType, annotations, dimension, type == null ? JavaType.Unknown.getInstance() : type);
}
private static TypeTree mapOldFormat(TypeTree elementType, List> dimensions) {
int count = dimensions.size();
if (count == 0) {
return elementType;
}
return mapOldFormat(
new ArrayType(
Tree.randomId(),
Space.EMPTY,
Markers.EMPTY,
elementType,
null,
JLeftPadded.build(dimensions.get(0).getAfter()).withBefore(dimensions.get(0).getElement()),
new JavaType.Array(null, elementType.getType(), null)
),
dimensions.subList(1, count)
);
}
@Override
public
J acceptJava(JavaVisitor
v, P p) {
return v.visitArrayType(this, p);
}
@Override
public String toString() {
return withPrefix(Space.EMPTY).printTrimmed(new JavaPrinter<>());
}
@Override
@Transient
public CoordinateBuilder.Expression getCoordinates() {
return new CoordinateBuilder.Expression(this);
}
}
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
@Data
final class Assert implements J, Statement {
@With
@EqualsAndHashCode.Include
UUID id;
@With
Space prefix;
@With
Markers markers;
@With
Expression condition;
@Nullable
@With
JLeftPadded detail;
@Override
public
J acceptJava(JavaVisitor
v, P p) {
return v.visitAssert(this, p);
}
@Override
@Transient
public CoordinateBuilder.Statement getCoordinates() {
return new CoordinateBuilder.Statement(this);
}
@Override
public String toString() {
return withPrefix(Space.EMPTY).printTrimmed(new JavaPrinter<>());
}
}
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
@RequiredArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
final class Assignment implements J, Statement, Expression, TypedTree {
@Nullable
@NonFinal
transient WeakReference padding;
@With
@EqualsAndHashCode.Include
@Getter
UUID id;
@With
@Getter
Space prefix;
@With
@Getter
Markers markers;
@With
@Getter
Expression variable;
JLeftPadded assignment;
public Expression getAssignment() {
return assignment.getElement();
}
public Assignment withAssignment(Expression assignment) {
return getPadding().withAssignment(this.assignment.withElement(assignment));
}
@Override
@Transient
public CoordinateBuilder.Statement getCoordinates() {
return new CoordinateBuilder.Statement(this);
}
@With
@Nullable
@Getter
JavaType type;
@Override
public
J acceptJava(JavaVisitor
v, P p) {
return v.visitAssignment(this, p);
}
@Override
@Transient
public List getSideEffects() {
return singletonList(this);
}
public Padding getPadding() {
Padding p;
if (this.padding == null) {
p = new Padding(this);
this.padding = new WeakReference<>(p);
} else {
p = this.padding.get();
if (p == null || p.t != this) {
p = new Padding(this);
this.padding = new WeakReference<>(p);
}
}
return p;
}
@Override
public String toString() {
return withPrefix(Space.EMPTY).printTrimmed(new JavaPrinter<>());
}
@RequiredArgsConstructor
public static class Padding {
private final Assignment t;
public JLeftPadded getAssignment() {
return t.assignment;
}
public Assignment withAssignment(JLeftPadded assignment) {
return t.assignment == assignment ? t : new Assignment(t.id, t.prefix, t.markers, t.variable, assignment, t.type);
}
}
}
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
@RequiredArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
final class AssignmentOperation implements J, Statement, Expression, TypedTree {
@Nullable
@NonFinal
transient WeakReference padding;
@With
@EqualsAndHashCode.Include
@Getter
UUID id;
@With
@Getter
Space prefix;
@With
@Getter
Markers markers;
@With
@Getter
Expression variable;
JLeftPadded operator;
public Type getOperator() {
return operator.getElement();
}
public AssignmentOperation withOperator(Type operator) {
return getPadding().withOperator(this.operator.withElement(operator));
}
@With
@Getter
Expression assignment;
@With
@Nullable
@Getter
JavaType type;
@Override
public
J acceptJava(JavaVisitor
v, P p) {
return v.visitAssignmentOperation(this, p);
}
@Override
@Transient
public CoordinateBuilder.Statement getCoordinates() {
return new CoordinateBuilder.Statement(this);
}
@Override
@Transient
public List getSideEffects() {
return singletonList(this);
}
public enum Type {
Addition,
BitAnd,
BitOr,
BitXor,
Division,
/**
* Raises the left operand to the power of the right operand.
* Unused in Java, used in Python
*/
Exponentiation,
/**
* Division of the left operand by the right operand, rounding down to the nearest integer.
* Unused in Java, used in Python.
*/
FloorDivision,
LeftShift,
/**
* Matrix multiplication of the left operand by the right operand.
* Unused in Java, used in Python
*/
MatrixMultiplication,
Modulo,
Multiplication,
RightShift,
Subtraction,
UnsignedRightShift
}
public Padding getPadding() {
Padding p;
if (this.padding == null) {
p = new Padding(this);
this.padding = new WeakReference<>(p);
} else {
p = this.padding.get();
if (p == null || p.t != this) {
p = new Padding(this);
this.padding = new WeakReference<>(p);
}
}
return p;
}
@Override
public String toString() {
return withPrefix(Space.EMPTY).printTrimmed(new JavaPrinter<>());
}
@RequiredArgsConstructor
public static class Padding {
private final AssignmentOperation t;
public JLeftPadded getOperator() {
return t.operator;
}
public AssignmentOperation withOperator(JLeftPadded operator) {
return t.operator == operator ? t : new AssignmentOperation(t.id, t.prefix, t.markers, t.variable, operator, t.assignment, t.type);
}
}
}
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
@RequiredArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Data
final class Binary implements J, Expression, TypedTree {
@Nullable
@NonFinal
transient WeakReference padding;
@With
@EqualsAndHashCode.Include
UUID id;
@With
Space prefix;
@With
Markers markers;
@With
Expression left;
JLeftPadded operator;
public Type getOperator() {
return operator.getElement();
}
public Binary withOperator(Type operator) {
return getPadding().withOperator(this.operator.withElement(operator));
}
@With
Expression right;
@With
@Nullable
JavaType type;
@Override
public
J acceptJava(JavaVisitor
v, P p) {
return v.visitBinary(this, p);
}
@Override
@Transient
public CoordinateBuilder.Expression getCoordinates() {
return new CoordinateBuilder.Expression(this);
}
@Transient
@Override
public List getSideEffects() {
List sideEffects = new ArrayList<>(2);
sideEffects.addAll(left.getSideEffects());
sideEffects.addAll(right.getSideEffects());
return sideEffects;
}
public enum Type {
Addition,
Subtraction,
Multiplication,
Division,
Modulo,
LessThan,
GreaterThan,
LessThanOrEqual,
GreaterThanOrEqual,
Equal,
NotEqual,
BitAnd,
BitOr,
BitXor,
LeftShift,
RightShift,
UnsignedRightShift,
Or,
And
}
public Padding getPadding() {
Padding p;
if (this.padding == null) {
p = new Padding(this);
this.padding = new WeakReference<>(p);
} else {
p = this.padding.get();
if (p == null || p.t != this) {
p = new Padding(this);
this.padding = new WeakReference<>(p);
}
}
return p;
}
@Override
public String toString() {
return withPrefix(Space.EMPTY).printTrimmed(new JavaPrinter<>());
}
@RequiredArgsConstructor
public static class Padding {
private final Binary t;
public JLeftPadded getOperator() {
return t.operator;
}
public Binary withOperator(JLeftPadded operator) {
return t.operator == operator ? t : new Binary(t.id, t.prefix, t.markers, t.left, operator, t.right, t.type);
}
}
}
/**
* A block of statements, enclosed in curly braces.
*
* To create an empty block, use {@link #createEmptyBlock()}.
*/
@ToString
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
@RequiredArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
final class Block implements J, Statement {
@Nullable
@NonFinal
transient WeakReference padding;
@With
@Getter
@EqualsAndHashCode.Include
UUID id;
@Getter
@With
Space prefix;
@Getter
@With
Markers markers;
JRightPadded statik;
public boolean isStatic() {
return statik.getElement();
}
public Block withStatic(boolean statik) {
return getPadding().withStatic(this.statik.withElement(statik));
}
List> statements;
public List getStatements() {
return JRightPadded.getElements(statements);
}
public Block withStatements(List statements) {
return getPadding().withStatements(JRightPadded.withElements(this.statements, statements));
}
@Getter
@With
Space end;
@Override
public
J acceptJava(JavaVisitor
v, P p) {
return v.visitBlock(this, p);
}
@Override
@Transient
public CoordinateBuilder.Block getCoordinates() {
return new CoordinateBuilder.Block(this);
}
public Padding getPadding() {
Padding p;
if (this.padding == null) {
p = new Padding(this);
this.padding = new WeakReference<>(p);
} else {
p = this.padding.get();
if (p == null || p.t != this) {
p = new Padding(this);
this.padding = new WeakReference<>(p);
}
}
return p;
}
@RequiredArgsConstructor
public static class Padding {
private final Block t;
public JRightPadded getStatic() {
return t.statik;
}
public Block withStatic(JRightPadded statik) {
return t.statik == statik ? t : new Block(t.id, t.prefix, t.markers, statik, t.statements, t.end);
}
public List> getStatements() {
return t.statements;
}
public Block withStatements(List> statements) {
return t.statements == statements ? t : new Block(t.id, t.prefix, t.markers, t.statik, statements, t.end);
}
}
public static J.Block createEmptyBlock() {
return new J.Block(
Tree.randomId(),
Space.EMPTY,
Markers.EMPTY,
JRightPadded.build(false),
Collections.emptyList(),
Space.EMPTY
);
}
@SelfLoathing(name = "Jonathan Leitschuh")
@LoathingOfOthers("Who didn't encode this in the model?!")
@Incubating(since = "7.25.0")
public static boolean isInitBlock(Cursor cursor) {
if (!(cursor.getValue() instanceof J.Block)) {
throw new IllegalArgumentException("Cursor must point to a J.Block!");
}
J.Block block = cursor.getValue();
if (block.isStatic()) {
return false;
}
J.Block parentBlock = null;
Iterator