Skip to main content

ClassName

Struct ClassName 

Source
pub struct ClassName(/* private fields */);
Expand description

A JVM internal class name.

Internal names use / as the package separator, for example java/lang/String, rather than Java source names such as java.lang.String.

Implementations§

Source§

impl ClassName

Source

pub fn parse(value: impl Into<String>) -> Result<Self, NameError>

Parses and validates a JVM internal class name.

Empty names and descriptor-only punctuation are rejected. The value is otherwise preserved exactly as supplied.

Source

pub fn as_str(&self) -> &str

Returns the internal name as a string slice.

Examples found in repository?
examples/infer.rs (line 56)
5fn main() -> ExitCode {
6    let mut arguments = env::args_os().skip(1);
7    let Some(path) = arguments.next() else {
8        eprintln!("usage: cargo run --example infer -- <class-file> [--reference <jar>]...");
9        return ExitCode::FAILURE;
10    };
11
12    let mut hierarchy = ClassHierarchy::new();
13    while let Some(option) = arguments.next() {
14        if option != "--reference" {
15            eprintln!("unknown option: {}", option.to_string_lossy());
16            return ExitCode::FAILURE;
17        }
18        let Some(reference) = arguments.next() else {
19            eprintln!("--reference requires a JAR path");
20            return ExitCode::FAILURE;
21        };
22        if let Err(error) = hierarchy.insert_jar_file(&reference) {
23            eprintln!("failed to index {}: {error}", reference.to_string_lossy());
24            return ExitCode::FAILURE;
25        }
26    }
27    let mut config = InferenceConfig::default();
28    if !hierarchy.is_empty() {
29        config = config.with_shared_type_hierarchy(Arc::new(hierarchy));
30    }
31    let inferer = match Inferer::new(config) {
32        Ok(inferer) => inferer,
33        Err(error) => {
34            eprintln!("failed to configure inference: {error}");
35            return ExitCode::FAILURE;
36        }
37    };
38
39    let path = Path::new(&path);
40    let bytes = match fs::read(path) {
41        Ok(bytes) => bytes,
42        Err(error) => {
43            eprintln!("failed to read {}: {error}", path.display());
44            return ExitCode::FAILURE;
45        }
46    };
47
48    let inference = match inferer.infer_class(&bytes) {
49        Ok(inference) => inference,
50        Err(error) => {
51            eprintln!("failed to infer {}: {error}", path.display());
52            return ExitCode::FAILURE;
53        }
54    };
55
56    println!("class: {}", inference.class_name().as_str());
57    println!("analysis complete: {}", inference.analysis_complete());
58
59    for method in inference.methods() {
60        println!("\nmethod: {}", method.name());
61        println!("  descriptor: {:?}", method.descriptor());
62        println!("  declared return type: {:?}", method.return_type());
63        println!(
64            "  inferred return type: {:?}",
65            method.inferred_return_type()
66        );
67        println!(
68            "  returned parameter index: {:?}",
69            method.returned_parameter_index()
70        );
71        println!("  analysis complete: {}", method.analysis_complete());
72        println!("  local types:");
73
74        for (slot, inferred_type) in method.local_types().iter().enumerate() {
75            println!("    {slot}: {inferred_type:?}");
76        }
77
78        for instruction in method.instructions() {
79            if instruction.operand_expectations().is_empty() {
80                continue;
81            }
82            println!(
83                "  operand expectations at {}:",
84                instruction.bytecode_offset()
85            );
86            for expectation in instruction.operand_expectations() {
87                println!(
88                    "    stack[{}]: {:?}",
89                    expectation.stack_index(),
90                    expectation.constraint()
91                );
92            }
93        }
94    }
95
96    if inference.diagnostics().is_empty() {
97        println!("\ndiagnostics: none");
98    } else {
99        println!("\ndiagnostics:");
100        for diagnostic in inference.diagnostics() {
101            let location = diagnostic.location();
102            println!(
103                "  {:?} {:?}: {}",
104                diagnostic.severity(),
105                diagnostic.kind(),
106                diagnostic.message()
107            );
108
109            if let Some(method_name) = location.method_name() {
110                println!(
111                    "    method: {}{}",
112                    method_name,
113                    location.method_descriptor().unwrap_or_default()
114                );
115            }
116            if let Some(offset) = location.bytecode_offset() {
117                println!("    bytecode offset: {offset}");
118            }
119        }
120    }
121
122    ExitCode::SUCCESS
123}
Source

pub fn into_string(self) -> String

Consumes this name and returns its owned string.

Trait Implementations§

Source§

impl AsRef<str> for ClassName

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for ClassName

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ClassName

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for ClassName

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for ClassName

Source§

impl Hash for ClassName

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for ClassName

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for ClassName

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for ClassName

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for ClassName

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.